def _upload_to_server(self, configs, matrix): # print('start uploading conn_matrix to other nodes') for ip, port in configs['nodes']: # print('Addr to connect: ' + ip + ":" + port) with grpc.insecure_channel(ip + ':' + port) as channel: stub = chaosmonkey_pb2_grpc.ChaosMonkeyStub(channel) response = stub.UploadMatrix(matrix)
def editMatrix(config_path, row, col, val): configs = load_config(config_path) for ip, port in configs['nodes']: print('Addr to connect: ' + ip + ":" + port) with grpc.insecure_channel(ip + ':' + port) as channel: stub = chaosmonkey_pb2_grpc.ChaosMonkeyStub(channel) response = stub.UpdateValue( chaosmonkey_pb2.MatValue(row=int(row), col=int(col), val=float(val))) print('Response from port' + str(port) + ":" + str(response.ret))
def update_server(address, server_index, crash): value = 0.0 if crash: value = 1.0 with grpc.insecure_channel(address) as channel: cmstub = chaosmonkey_pb2_grpc.ChaosMonkeyStub(channel) for row in range(len(address)): cmstub.UpdateValue( chaosmonkey_pb2.MatValue(row=row, col=server_index, val=value)) for col in range(len(address)): cmstub.UpdateValue( chaosmonkey_pb2.MatValue(row=server_index, col=col, val=value))
def run(): #One server test cases with grpc.insecure_channel('localhost:50050') as channel: stub = chaosmonkey_pb2_grpc.ChaosMonkeyStub(channel) op = 1 if op == 0: l0 = chaosmonkey_pb2.ConnMatrix.MatRow(vals=[0.0, 0.1, 0.2]) l1 = chaosmonkey_pb2.ConnMatrix.MatRow(vals=[0.3, 0.4, 0.5]) l2 = chaosmonkey_pb2.ConnMatrix.MatRow(vals=[0.6, 0.7, 0.8]) matrix = chaosmonkey_pb2.ConnMatrix(rows=[l0, l1, l2]) stub.UploadMatrix(matrix) else: mv = chaosmonkey_pb2.MatValue(row=1, col=2, val=0.0) stub.UpdateValue(mv)
def get_current_connMatrix(self, node_index): # return if_succeed, matrix # if_succeed: 0 for succeeded, 1 for failed ip, port = self.configs['nodes'][node_index] with grpc.insecure_channel(ip + ':' + port) as channel: stub = chaosmonkey_pb2_grpc.ChaosMonkeyStub(channel) try: response = stub.GetMatrix(chaosmonkey_pb2.Empty()) matrix = list() for row in response.rows: to_row = list() for e in row.vals: to_row.append(e) matrix.append(to_row) return 0, matrix except Exception as e: return 1, list()
def upload(address, server_num): with grpc.insecure_channel(address) as channel: # ChaosMoney matrix creation # cmMat = chaosmonkey_pb2.ConnMatrix() # for i in range(server_num): # mat_row = cmMat.rows.add() # for j in range(server_num): # mat_row.vals.append(float(0.0)) # mat_row.vals.add(float(0.25)) cmstub = chaosmonkey_pb2_grpc.ChaosMonkeyStub(channel) # Add more addresses # response = cmstub.UploadMatrix(cmMat) # print("Client Upload ChaosMonkey Matrix received: " + str(response.ret) + ", @: "+address) response = cmstub.UpdateValue( chaosmonkey_pb2.MatValue(row=1, col=2, val=0.99)) print("Client Update ChaosMonkey Matrix Value received: " + str(response.ret) + "@: " + address)
def kill_a_node(self, node_id): # return if_succeed # 0 for succeeded, 1 for failed if node_id < 0 or node_id >= len(self.configs['nodes']): print('Invalid node_id.') return 1 # for id in range(len(self.configs['nodes'])): # self.editMatrix(id, node_id, 1.0) # self.editMatrix(node_id, id, 1.0) all_correct = True for ip, port in self.configs['nodes']: with grpc.insecure_channel(ip + ':' + port) as channel: stub = chaosmonkey_pb2_grpc.ChaosMonkeyStub(channel) response = stub.KillANode(chaosmonkey_pb2.KillANodeRequest(node_index=node_id)) if response.ret == 1: all_correct = False if not all_correct: print('Kill node failed!') return 0
def editMatrix(self, row, col, val): for ip, port in self.configs['nodes']: # print('Addr to connect: ' + ip + ":" + port) with grpc.insecure_channel(ip + ':' + port) as channel: stub = chaosmonkey_pb2_grpc.ChaosMonkeyStub(channel) response = stub.UpdateValue(chaosmonkey_pb2.MatValue(row=int(row), col=int(col), val=float(val)))