def __init__(self, network): self.connection_status = NODE_DISCONNECTED self.raft = lib.raft_new() self.udata = ffi.new_handle(self) network.add_server(self) self.load_callbacks() cbs = ffi.new('raft_cbs_t*') cbs.send_requestvote = self.raft_send_requestvote cbs.send_appendentries = self.raft_send_appendentries cbs.send_snapshot = self.raft_send_snapshot cbs.applylog = self.raft_applylog cbs.persist_vote = self.raft_persist_vote cbs.persist_term = self.raft_persist_term cbs.log_offer = self.raft_logentry_offer cbs.log_poll = self.raft_logentry_poll cbs.log_pop = self.raft_logentry_pop cbs.log_get_node_id = self.raft_logentry_get_node_id cbs.node_has_sufficient_logs = self.raft_node_has_sufficient_logs cbs.notify_membership_event = self.raft_notify_membership_event cbs.log = self.raft_log lib.raft_set_callbacks(self.raft, cbs, self.udata) lib.raft_set_election_timeout(self.raft, 500) self.fsm_dict = {} self.fsm_log = []
def prep_dynamic_configuration(self): """ Add configuration change for leader's node """ server = self.active_servers[0] self.leader = server server.set_connection_status(NODE_CONNECTED) lib.raft_add_non_voting_node(server.raft, server.udata, server.id, 1) lib.raft_become_leader(server.raft) # Configuration change entry to bootstrap other nodes ety = ffi.new('msg_entry_t*') ety.term = 0 ety.type = lib.RAFT_LOGTYPE_ADD_NODE ety.id = self.new_entry_id() change = ffi.new_handle(ChangeRaftEntry(server.id)) ety.data.buf = change ety.data.len = ffi.sizeof(ety.data.buf) self.entries.append((ety, change)) e = server.recv_entry(ety) assert e == 0 lib.raft_set_commit_idx(server.raft, 1) e = lib.raft_apply_all(server.raft) assert e == 0
def add_entry(self, etype, data): data_handle = ffi.new_handle(data) ety = lib.raft_entry_newdata(data_handle) ety.term = 0 ety.id = self.new_entry_id() ety.type = etype self.entries.append((ety, data_handle, data)) return ety
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 node_has_sufficient_entries(self, node): assert(not lib.raft_node_is_voting(node)) ety = ffi.new('msg_entry_t*') ety.term = 0 ety.id = self.network.new_entry_id() ety.type = lib.RAFT_LOGTYPE_ADD_NODE change = ffi.new_handle(ChangeRaftEntry(lib.raft_node_get_id(node))) ety.data.buf = change ety.data.len = ffi.sizeof(ety.data.buf) self.network.entries.append((ety, change)) assert(lib.raft_entry_is_cfg_change(ety)) # FIXME: leak e = self.recv_entry(ety) # print(err2str(e)) assert e == 0 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 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))