def test_cluster_with_multiple_nodes(self): app = handlers.base.app with app.test_client() as client: r = client.post('/nodes/add', data={ 'host': '127.0.0.1', 'port': '7100', 'mem': '1048576', }) self.assertEqual(200, r.status_code) r = client.post('/nodes/add', data={ 'host': '127.0.0.1', 'port': '7101', 'mem': '1048576', }) self.assertEqual(200, r.status_code) r = client.post('/cluster/add', data={ 'descr': 'the-quick-brown-fox', }) self.assertEqual(200, r.status_code) cluster_id = r.data r = client.post('/cluster/launch', data={ 'cluster_id': cluster_id, 'host': '127.0.0.1', 'port': 7100, }) self.assertEqual(200, r.status_code) r = client.post('/cluster/join', data={ 'cluster_id': cluster_id, 'host': '127.0.0.1', 'port': 7101, }) self.assertEqual(200, r.status_code) nodes, node_7100 = comm.list_nodes('127.0.0.1', 7100) self.assertEqual(2, len(nodes)) self.assertEqual(range(8192, 16384), node_7100.assigned_slots) r = client.post('/cluster/migrate_slots', data={ 'src_host': '127.0.0.1', 'src_port': 7100, 'dst_host': '127.0.0.1', 'dst_port': 7101, 'slots': '8192,8193,8194,8195', }) self.assertEqual(200, r.status_code) nodes, node_7100 = comm.list_nodes('127.0.0.1', 7100) self.assertEqual(2, len(nodes)) self.assertEqual(range(8196, 16384), node_7100.assigned_slots) r = client.post('/cluster/quit', data={ 'cluster_id': cluster_id, 'host': '127.0.0.1', 'port': 7100, }) comm.shutdown_cluster('127.0.0.1', 7101)
def test_execution(self): with self.app.test_client() as client: r = client.post('/nodes/add', data={ 'host': '127.0.0.1', 'port': '7100', 'mem': '1048576', }) self.assertReqStatus(200, r) r = client.post('/cluster/add', data={ 'descr': 'lazy dog', }) self.assertReqStatus(200, r) cluster_id = r.data r = client.post('/cluster/launch', data={ 'cluster_id': cluster_id, 'host': '127.0.0.1', 'port': 7100, }) self.assertReqStatus(200, r) task = ClusterTask(cluster_id=int(cluster_id), task_type=0) task.add_step('join', cluster_id=cluster_id, cluster_host='127.0.0.1', cluster_port=7100, newin_host='127.0.0.1', newin_port=7101) task.add_step('migrate', src_host='127.0.0.1', src_port=7100, dst_host='127.0.0.1', dst_port=7101, slots=[0, 1]) self.db.session.add(task) self.db.session.commit() self.exec_all_tasks() nodes, node_7100 = comm.list_nodes('127.0.0.1', 7100) self.assertEqual(2, len(nodes)) self.assertEqual(range(2, 16384), sorted(node_7100.assigned_slots)) tasks = list(self.db.session.query(ClusterTask).all()) self.assertEqual(1, len(tasks)) t = tasks[0] self.assertIsNotNone(t.completion) self.assertIsNone(t.exec_error) self.assertIsNone(t.acquired_lock()) comm.quit_cluster('127.0.0.1', 7101) comm.shutdown_cluster('127.0.0.1', 7100)
def test_execution(self): with self.app.test_client() as client: r = client.post('/nodes/add', data={ 'host': '127.0.0.1', 'port': '7100', 'mem': '1048576', }) self.assertReqStatus(200, r) r = client.post('/cluster/add', data={ 'descr': 'lazy dog', }) self.assertReqStatus(200, r) cluster_id = r.data r = client.post('/cluster/launch', data={ 'cluster_id': cluster_id, 'host': '127.0.0.1', 'port': 7100, }) self.assertReqStatus(200, r) task = ClusterTask(cluster_id=int(cluster_id), task_type=0) task.add_step( 'join', cluster_id=cluster_id, cluster_host='127.0.0.1', cluster_port=7100, newin_host='127.0.0.1', newin_port=7101) task.add_step( 'migrate', src_host='127.0.0.1', src_port=7100, dst_host='127.0.0.1', dst_port=7101, slots=[0, 1]) self.db.session.add(task) self.db.session.commit() self.exec_all_tasks() nodes, node_7100 = comm.list_nodes('127.0.0.1', 7100) self.assertEqual(2, len(nodes)) self.assertEqual(range(2, 16384), sorted(node_7100.assigned_slots)) tasks = list(self.db.session.query(ClusterTask).all()) self.assertEqual(1, len(tasks)) t = tasks[0] self.assertIsNotNone(t.completion) self.assertIsNone(t.exec_error) self.assertIsNone(t.acquired_lock()) comm.quit_cluster('127.0.0.1', 7101) comm.shutdown_cluster('127.0.0.1', 7100)
def test_execution_failed(self): with self.app.test_client() as client: r = client.post('/redis/add', data={ 'host': '127.0.0.1', 'port': '7100', }) self.assertReqStatus(200, r) r = client.post('/cluster/add', data={ 'descr': 'lazy dog', }) self.assertReqStatus(200, r) cluster_id = r.data r = client.post('/task/launch', data=json.dumps({ 'cluster': cluster_id, 'nodes': [{ 'host': '127.0.0.1', 'port': 7100, }], })) self.assertReqStatus(200, r) self.exec_all_tasks() task = ClusterTask(cluster_id=int(cluster_id), task_type=0) task.add_step('join', cluster_id=cluster_id, cluster_host='127.0.0.1', cluster_port=7100, newin_host='127.0.0.1', newin_port=7101) task.add_step('migrate', src_host='127.0.0.1', src_port=7100, dst_host='127.0.0.1', dst_port=7101, slots=[0, 1]) task.add_step('migrate', src_host='127.0.0.1', src_port=7100, dst_host='127.0.0.1', dst_port=7101, slots=[0, 1]) task.add_step('migrate', src_host='127.0.0.1', src_port=7100, dst_host='127.0.0.1', dst_port=7101, slots=[2, 3]) self.db.session.add(task) self.db.session.commit() self.exec_all_tasks() nodes, node_7100 = comm.list_nodes('127.0.0.1', 7100) self.assertEqual(2, len(nodes)) self.assertEqual(range(2, 16384), sorted(node_7100.assigned_slots)) tasks = list( self.db.session.query(ClusterTask).order_by( ClusterTask.id.asc()).all()) self.assertEqual(2, len(tasks)) t = tasks[1] self.assertIsNotNone(t.completion) self.assertIsNotNone(t.exec_error) self.assertIsNone(t.acquired_lock()) steps = t.all_steps self.assertEqual(4, len(steps)) step = steps[0] self.assertTrue(step.completed) self.assertIsNone(step.exec_error) step = steps[1] self.assertTrue(step.completed) self.assertIsNone(step.exec_error) step = steps[2] self.assertTrue(step.completed) self.assertIsNotNone(step.exec_error) step = steps[3] self.assertFalse(step.started) self.assertFalse(step.completed) self.assertIsNone(step.exec_error) comm.quit_cluster('127.0.0.1', 7101) comm.shutdown_cluster('127.0.0.1', 7100)
def list_nodes(host, port): return {(node.host, node.port): node for node in comm.list_nodes(host, port, '127.0.0.1')[0]}
def test_cluster_with_multiple_nodes(self): with self.app.test_client() as client: r = client.post('/nodes/add', data={ 'host': '127.0.0.1', 'port': '7100', 'mem': '1048576', }) self.assertReqStatus(200, r) r = client.post('/nodes/add', data={ 'host': '127.0.0.1', 'port': '7101', 'mem': '1048576', }) self.assertReqStatus(200, r) r = client.post('/cluster/add', data={ 'descr': 'the-quick-brown-fox', }) self.assertReqStatus(200, r) cluster_id = r.data r = client.post('/cluster/launch', data={ 'cluster_id': cluster_id, 'host': '127.0.0.1', 'port': 7100, }) self.assertReqStatus(200, r) r = client.post('/cluster/join', data={ 'cluster_id': cluster_id, 'host': '127.0.0.1', 'port': 7101, }) self.assertReqStatus(200, r) nodes, node_7100 = comm.list_nodes('127.0.0.1', 7100) self.assertEqual(1, len(nodes)) tasks = list(models.task.undone_tasks()) self.assertEqual(1, len(tasks)) self.exec_all_tasks() nodes, node_7100 = comm.list_nodes('127.0.0.1', 7100) self.assertEqual(2, len(nodes)) self.assertEqual(16384, len(node_7100.assigned_slots)) r = client.post('/cluster/migrate_slots', data={ 'src_host': '127.0.0.1', 'src_port': 7100, 'dst_host': '127.0.0.1', 'dst_port': 7101, 'slots': '8192,8193,8194,8195', }) self.assertReqStatus(200, r) nodes, node_7100 = comm.list_nodes('127.0.0.1', 7100) self.assertEqual(2, len(nodes)) self.assertEqual(16384, len(node_7100.assigned_slots)) tasks = list(models.task.undone_tasks()) self.assertEqual(1, len(tasks)) self.exec_all_tasks() nodes, node_7100 = comm.list_nodes('127.0.0.1', 7100) self.assertEqual(2, len(nodes)) self.assertEqual(16380, len(node_7100.assigned_slots)) r = client.post('/cluster/quit', data=json.dumps({ 'host': '127.0.0.1', 'port': 7101, 'migratings': [{ 'host': '127.0.0.1', 'port': 7100, 'slots': [8192, 8193, 8194, 8195], }], })) self.assertReqStatus(200, r) nodes, node_7100 = comm.list_nodes('127.0.0.1', 7100) self.assertEqual(2, len(nodes)) tasks = list(models.task.undone_tasks()) self.assertEqual(1, len(tasks)) self.exec_all_tasks() nodes, node_7100 = comm.list_nodes('127.0.0.1', 7100) self.assertEqual(1, len(nodes)) comm.shutdown_cluster('127.0.0.1', 7100)