Пример #1
0
    def _enqueue_msg(self, msg, sendor, sendee):
        msg_type = ffi.getctype(ffi.typeof(msg))

        msg_size = ffi.sizeof(msg[0])
        new_msg = ffi.cast(ffi.typeof(msg), lib.malloc(msg_size))
        ffi.memmove(new_msg, msg, msg_size)

        if msg_type == 'msg_appendentries_t *':
            size_of_entries = ffi.sizeof(ffi.getctype('msg_entry_t')) * new_msg.n_entries
            new_msg.entries = ffi.cast('msg_entry_t*', lib.malloc(size_of_entries))
            ffi.memmove(new_msg.entries, msg.entries, size_of_entries)

        self.messages.append(Message(new_msg, sendor, sendee))
Пример #2
0
    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
Пример #3
0
    def _enqueue_msg(self, msg, sendor, sendee):
        msg_type = ffi.getctype(ffi.typeof(msg))

        msg_size = ffi.sizeof(msg[0])
        new_msg = ffi.cast(ffi.typeof(msg), lib.malloc(msg_size))
        ffi.memmove(new_msg, msg, msg_size)

        if msg_type == 'msg_appendentries_t *':
            new_msg.entries = lib.raft_entry_array_deepcopy(
                msg.entries, msg.n_entries)

        self.messages.append(Message(new_msg, sendor, sendee))
Пример #4
0
 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
Пример #5
0
    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)
Пример #6
0
    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
Пример #7
0
    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
Пример #8
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
Пример #9
0
    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))