def dispatch_join(source, req_code, req_type, data): #if this node is prev ( this.succ== is_responsible ) update its next to source put its ip to prev datum and forward request to next #if this node is_resp === is next update its prev to source put its ip to succ datum and get same_keys , new_keys and respond to source #if nothing of the above just forward to next node if (is_responsible(data['key'])): data['succ'] = node.ip_port same_new = node.get_same_new_keys(data['key']) data['same_keys'] = same_new[0] data['new_keys'] = same_new[1] if (node.is_alone()): # special case --> here we are previous also so we must fix those values too node.succ_ip_port = source node.prev_ip_port = source data['prev'] = node.ip_port #respond to source node.prev_ip_port = source resp = make_resp(source, req_type, data, req_code) post_resp_thread(source, resp) elif (succ_is_responsible(data['key'])): temp = node.succ_ip_port node.succ_ip_port = source data['prev'] = node.ip_port req = make_same_req(source, req_type, data, req_code) post_req_thread(temp, req) else: req = make_same_req(source, req_type, data, req_code) post_req_thread(node.succ_ip_port, req) return
def dispatch_join(source, req_code, req_type, data): #if this node is prev ( this.succ== is_responsible ) update its next to source put its ip to prev datum and forward request to next #if this node is_resp === is next update its prev to source put its ip to succ datum and get same_keys , new_keys and respond to source #if nothing of the above just forward to next node #### THIS IS ONLY FOR BOOTSTRAP NODE --> IT SENDS THE NODE THAT JOINS REPN AND REP_TYPE INFORMATION#### resp = make_resp(source, 'join_vars', { 'repn': node.get_replicas(), 'rep_type': node.get_rep_type() }, req_code) post_resp_to(source, resp) ####################################################################################################### if (is_responsible(data['key'])): ######only for bootstrap cause he is the first to receive join req ######if he is responsible its the only case when we dont pass from the prev ######so we must set the data['prev']=node.prev_ip_port ###### and also send request to prev to update its succ to the source data['prev'] = node.prev_ip_port post_resp_to(node.prev_ip_port, { 'type': 'set_neighboors', 'prev': "None", 'succ': source }) ###### data['succ'] = node.ip_port same_new = node.get_same_new_keys(data['key']) data['same_keys'] = same_new[0] data['new_keys'] = same_new[1] if (node.is_alone()): # special case --> here we are previous also so we must fix those values too node.succ_ip_port = source node.prev_ip_port = source data['prev'] = node.ip_port #respond to source node.prev_ip_port = source resp = make_resp(source, req_type, data, req_code) post_resp_thread(source, resp) elif (succ_is_responsible(data['key'])): temp = node.succ_ip_port node.succ_ip_port = source data['prev'] = node.ip_port req = make_same_req(source, req_type, data, req_code) post_req_thread(temp, req) else: req = make_same_req(source, req_type, data, req_code) post_req_thread(node.succ_ip_port, req) return
def is_responsible(key): curr = node.id prev = hash(node.prev_ip_port) if node.is_alone(): # ara uparxei mono enas komvos sto chord return True elif prev > curr: if (key > prev or key <= curr): return True else: return False else: if (key > prev and key <= curr): return True else: return False
def depart(): if request.method == 'POST': if not node.get_isInChord(): return 'This node does not participate in Chord!\n' else: # Στις επόμενες 4 γραμμές, μαζεύω τα data που θα στείλω στον successor global seqn seqn = seqn + 1 req_code = str(seqn) new_keys = node.keys_vals[0] last_keys = node.keys_vals[node.get_replicas() - 1] #send message to boot to find out K_nodes value boot_resp = requests.post("http://" + node.boot_ip_port + "/ntwresp", json={'type': 'K_nodes'}) K_nodes = json.loads(boot_resp.text)['K_nodes'] data = { 'index': 0, 'new_keys': new_keys, 'last_keys': last_keys, 'K_nodes': K_nodes } if node.is_alone(): node.init_state() return f"Node {node.ip_port} departed from the Chord!\n" else: #make depart request and post to next node--> it will update all the rm chain req = make_req('depart', data, req_code) post_req_thread(node.succ_ip_port, req) while responses_dict.get(req_code, "None") == "None": {} responses_dict.pop(req_code) # then take care of the neighbour pointers # data = {'prev': node.prev_ip_port, 'succ': node.succ_ip_port, 'type':'set_neighboors'} prev = node.prev_ip_port succ = node.succ_ip_port if node.is_duo(): # an exoume mono 2 nodes tote prepei na steiloume na enimerwsei kai ton next kai ton prev o enas mas geitonas #to parakatw tha perimenei mexri na lavoume apantisi post_resp_to(node.prev_ip_port, { 'prev': prev, 'succ': succ, 'type': 'set_neighboors' }) else: #an uparxoun toul 3 komvoi stelnoume kai ston prev kai ston next na enimerwsoun mono ton enan geitona tous ekastos post_resp_to(node.prev_ip_port, { 'prev': "None", 'succ': succ, 'type': 'set_neighboors' }) post_resp_to(node.succ_ip_port, { 'prev': prev, 'succ': "None", 'type': 'set_neighboors' }) # Then return the node to dafault state node.init_state() post_resp_to(node.boot_ip_port, {'type': 'dec_number'}) return f"Node {node.ip_port} successfully departed from Chord!\n"