def push_set_entry(self, k, v): for sv in self.active_servers: if lib.raft_is_leader(sv.raft): ety = self.add_entry(lib.RAFT_LOGTYPE_NORMAL, SetRaftEntry(k, v)) e = sv.recv_entry(ety) assert e == 0 break
def _check_election_safety(self): """ FIXME: this is O(n^2) Election Safety At most one leader can be elected in a given term. """ for i, sv1 in enumerate(net.active_servers): if not lib.raft_is_leader(sv1.raft): continue for sv2 in net.active_servers[i + 1:]: term1 = lib.raft_get_current_term(sv1.raft) term2 = lib.raft_get_current_term(sv2.raft) if lib.raft_is_leader(sv2.raft) and term1 == term2: logging.error("election safety invalidated") print(sv1, sv2, term1) print('partitions:', self.partitions) sys.exit(1)
def push_set_entry(self, k, v): for sv in self.active_servers: if lib.raft_is_leader(sv.raft): ety = ffi.new('msg_entry_t*') ety.term = 0 ety.id = self.new_entry_id() ety.type = lib.RAFT_LOGTYPE_NORMAL change = ffi.new_handle(SetRaftEntry(k, v)) ety.data.buf = change ety.data.len = ffi.sizeof(ety.data.buf) self.entries.append((ety, change)) e = sv.recv_entry(ety) assert e == 0 break
def remove_member(self): if not self.leader: logging.error('no leader') return leader = self.leader server = self.random.choice(self.active_servers) if not lib.raft_is_leader(leader.raft): return if lib.raft_voting_change_is_in_progress(leader.raft): # logging.error('{} voting change in progress'.format(server)) return if leader == server: # logging.error('can not remove leader') return if server.connection_status in [NODE_CONNECTING, NODE_DISCONNECTING]: # logging.error('can not remove server that is changing connection status') return if NODE_DISCONNECTED == server.connection_status: self.remove_server(server) return # Create a new configuration entry to be processed by the leader ety = ffi.new('msg_entry_t*') ety.term = 0 ety.id = self.new_entry_id() assert server.connection_status == NODE_CONNECTED ety.type = lib.RAFT_LOGTYPE_DEMOTE_NODE change = ffi.new_handle(ChangeRaftEntry(server.id)) ety.data.buf = change ety.data.len = ffi.sizeof(ety.data.buf) assert (lib.raft_entry_is_cfg_change(ety)) self.entries.append((ety, change)) e = leader.recv_entry(ety) if 0 != e: logging.error(err2str(e)) return else: self.num_membership_changes += 1 # Wake up new node assert NODE_CONNECTED == server.connection_status server.set_connection_status(NODE_DISCONNECTING)
def entry_apply(self, ety, idx): # collect stats if self.network.latest_applied_log_idx < idx: self.network.latest_applied_log_idx = idx self.network.latest_applied_log_iteration = self.network.iteration e = self._check_log_matching(ety, idx) if e is not None: return e change = ffi.from_handle(ety.data.buf) if ety.type == lib.RAFT_LOGTYPE_NORMAL: self.fsm_dict[change.key] = change.val elif ety.type == lib.RAFT_LOGTYPE_DEMOTE_NODE: if change.node_id == lib.raft_get_nodeid(self.raft): # logging.warning("{} shutting down because of demotion".format(self)) return lib.RAFT_ERR_SHUTDOWN # Follow up by removing the node by receiving new entry elif lib.raft_is_leader(self.raft): new_ety = ffi.new('msg_entry_t*') new_ety.term = 0 new_ety.id = self.network.new_entry_id() new_ety.type = lib.RAFT_LOGTYPE_REMOVE_NODE new_ety.data.buf = ety.data.buf new_ety.data.len = ffi.sizeof(ety.data.buf) assert(lib.raft_entry_is_cfg_change(new_ety)) e = self.recv_entry(new_ety) assert e == 0 elif ety.type == lib.RAFT_LOGTYPE_REMOVE_NODE: if change.node_id == lib.raft_get_nodeid(self.raft): # logging.warning("{} shutting down because of removal".format(self)) return lib.RAFT_ERR_SHUTDOWN elif ety.type == lib.RAFT_LOGTYPE_ADD_NODE: if change.node_id == self.id: self.set_connection_status(NODE_CONNECTED) elif ety.type == lib.RAFT_LOGTYPE_ADD_NONVOTING_NODE: pass return 0
def add_member(self): if net.num_of_servers <= len(self.active_servers): return if not self.leader: logging.error('no leader') return leader = self.leader if not lib.raft_is_leader(leader.raft): return if lib.raft_voting_change_is_in_progress(leader.raft): # logging.error('{} voting change in progress'.format(server)) return server = RaftServer(self) # Create a new configuration entry to be processed by the leader ety = ffi.new('msg_entry_t*') ety.term = 0 ety.id = self.new_entry_id() ety.type = lib.RAFT_LOGTYPE_ADD_NONVOTING_NODE change = ffi.new_handle(ChangeRaftEntry(server.id)) ety.data.buf = change ety.data.len = ffi.sizeof(ety.data.buf) assert (lib.raft_entry_is_cfg_change(ety)) self.entries.append((ety, change)) e = leader.recv_entry(ety) if 0 != e: logging.error(err2str(e)) return else: self.num_membership_changes += 1 # Wake up new node server.set_connection_status(NODE_CONNECTING) assert server.udata added_node = lib.raft_add_non_voting_node(server.raft, server.udata, server.id, 1) assert added_node
def remove_member(self): if not self.leader: logger.error('no leader') return leader = self.leader server = self.random.choice(self.active_servers) if not lib.raft_is_leader(leader.raft): return if lib.raft_voting_change_is_in_progress(leader.raft): # logger.error('{} voting change in progress'.format(server)) return if leader == server: # logger.error('can not remove leader') return if server.connection_status in [NODE_CONNECTING, NODE_DISCONNECTING]: # logger.error('can not remove server that is changing connection status') return if NODE_DISCONNECTED == server.connection_status: self.remove_server(server) return # Create a new configuration entry to be processed by the leader ety = self.add_entry(lib.RAFT_LOGTYPE_DEMOTE_NODE, ChangeRaftEntry(server.id)) assert server.connection_status == NODE_CONNECTED assert (lib.raft_entry_is_cfg_change(ety)) e = leader.recv_entry(ety) if 0 != e: logger.error(err2str(e)) return else: self.num_membership_changes += 1 # Wake up new node assert NODE_CONNECTED == server.connection_status server.set_connection_status(NODE_DISCONNECTING)
def add_member(self): if net.num_of_servers <= len(self.active_servers): return if not self.leader: logger.error('no leader') return leader = self.leader if not lib.raft_is_leader(leader.raft): return if lib.raft_voting_change_is_in_progress(leader.raft): # logger.error('{} voting change in progress'.format(server)) return server = RaftServer(self) # Create a new configuration entry to be processed by the leader ety = self.add_entry(lib.RAFT_LOGTYPE_ADD_NONVOTING_NODE, ChangeRaftEntry(server.id)) assert (lib.raft_entry_is_cfg_change(ety)) e = leader.recv_entry(ety) if 0 != e: logger.error(err2str(e)) return else: self.num_membership_changes += 1 # Wake up new node server.set_connection_status(NODE_CONNECTING) assert server.udata added_node = lib.raft_add_non_voting_node(server.raft, server.udata, server.id, 1) assert added_node
def toggle_membership(self): if not self.leader: logging.error('no leader') return leader = self.leader server = self.random.choice(self.active_servers) if not lib.raft_is_leader(leader.raft): return if lib.raft_voting_change_is_in_progress(leader.raft): # logging.error('{} voting change in progress'.format(server)) return if leader == server: # logging.error('can not remove leader') return if server.connection_status in [NODE_CONNECTING, NODE_DISCONNECTING]: # logging.error('can not remove server that is changing connection status') return if NODE_DISCONNECTED == server.connection_status: self.remove_server(server) server = RaftServer(self) # Create a new configuration entry to be processed by the leader ety = ffi.new('msg_entry_t*') ety.term = 0 ety.id = self.new_entry_id() if server.connection_status == NODE_CONNECTED: ety.type = lib.RAFT_LOGTYPE_DEMOTE_NODE else: ety.type = lib.RAFT_LOGTYPE_ADD_NONVOTING_NODE change = ffi.new_handle(ChangeRaftEntry(server.id)) ety.data.buf = change ety.data.len = ffi.sizeof(ety.data.buf) assert(lib.raft_entry_is_cfg_change(ety)) self.entries.append((ety, change)) # logging.warning("{}> {} Membership change {}: ety.id:{} id:{} {}".format( # self.iteration, # server, # "ADD" if ety.type == lib.RAFT_LOGTYPE_ADD_NONVOTING_NODE else "DEMOTE", # ety.id, # lib.raft_get_nodeid(leader.raft), # server.id, # )) e = leader.recv_entry(ety) if 0 != e: logging.error(err2str(e)) return else: self.num_membership_changes += 1 # Wake up new node if NODE_DISCONNECTED == server.connection_status: server.set_connection_status(NODE_CONNECTING) assert server.udata added_node = lib.raft_add_non_voting_node(server.raft, server.udata, server.id, 1) assert added_node elif NODE_CONNECTED == server.connection_status: server.set_connection_status(NODE_DISCONNECTING) else: logging.warning('Unknown status set {}'.format(server.connection_status))