示例#1
0
 def test_07_getNodeSet(self):
     num_partitions = 5
     num_replicas = 2
     pt = PartitionTable(num_partitions, num_replicas)
     # add two kind of node, usable and unusable
     uuid1 = self.getStorageUUID()
     server1 = ("127.0.0.1", 19001)
     sn1 = self.createStorage(server1, uuid1)
     pt._setCell(0, sn1, CellStates.UP_TO_DATE)
     uuid2 = self.getStorageUUID()
     server2 = ("127.0.0.2", 19001)
     sn2 = self.createStorage(server2, uuid2)
     pt._setCell(0, sn2, CellStates.OUT_OF_DATE)
     uuid3 = self.getStorageUUID()
     server3 = ("127.0.0.3", 19001)
     sn3 = self.createStorage(server3, uuid3)
     pt._setCell(0, sn3, CellStates.FEEDING)
     uuid4 = self.getStorageUUID()
     server4 = ("127.0.0.4", 19001)
     sn4 = self.createStorage(server4, uuid4)
     pt._setCell(0, sn4, CellStates.DISCARDED)  # won't be added
     # must get only two node as feeding and discarded not taken
     # into account
     self.assertEqual(pt.getNodeSet(True), {sn1, sn3})
     self.assertEqual(len(pt.getNodeSet()), 3)
示例#2
0
 def test_14_notifyPartitionChanges2(self):
     # cases :
     uuid1, uuid2, uuid3 = [self.getStorageUUID() for i in range(3)]
     cells = (
         (0, uuid1, CellStates.UP_TO_DATE),
         (1, uuid2, CellStates.DISCARDED),
         (2, uuid3, CellStates.OUT_OF_DATE),
     )
     # context
     conn = self.getMasterConnection()
     app = self.app
     # register nodes
     app.nm.createStorage(uuid=uuid1)
     app.nm.createStorage(uuid=uuid2)
     app.nm.createStorage(uuid=uuid3)
     ptid1, ptid2 = (1, 2)
     self.assertNotEqual(ptid1, ptid2)
     app.pt = PartitionTable(3, 1)
     app.dm = Mock({})
     app.replicator = Mock({})
     self.operation.notifyPartitionChanges(conn, ptid2, cells)
     # ptid set
     self.assertEqual(app.pt.getID(), ptid2)
     # dm call
     calls = self.app.dm.mockGetNamedCalls('changePartitionTable')
     self.assertEqual(len(calls), 1)
     calls[0].checkArgs(ptid2, cells)
示例#3
0
 def test_06_clear(self):
     # add some nodes
     num_partitions = 5
     num_replicas = 2
     pt = PartitionTable(num_partitions, num_replicas)
     # add two kind of node, usable and unusable
     uuid1 = self.getStorageUUID()
     server1 = ("127.0.0.1", 19001)
     sn1 = self.createStorage(server1, uuid1)
     pt._setCell(0, sn1, CellStates.UP_TO_DATE)
     uuid2 = self.getStorageUUID()
     server2 = ("127.0.0.2", 19001)
     sn2 = self.createStorage(server2, uuid2)
     pt._setCell(1, sn2, CellStates.OUT_OF_DATE)
     uuid3 = self.getStorageUUID()
     server3 = ("127.0.0.3", 19001)
     sn3 = self.createStorage(server3, uuid3)
     pt._setCell(2, sn3, CellStates.FEEDING)
     # now checks result
     self.assertEqual(len(pt.partition_list[0]), 1)
     self.assertEqual(len(pt.partition_list[1]), 1)
     self.assertEqual(len(pt.partition_list[2]), 1)
     pt.clear()
     partition_list = pt.partition_list
     self.assertEqual(len(partition_list), num_partitions)
     for x in xrange(num_partitions):
         part = partition_list[x]
         self.assertTrue(isinstance(part, list))
         self.assertEqual(len(part), 0)
     self.assertEqual(len(pt.count_dict), 0)
示例#4
0
文件: app.py 项目: pyzh/neoppod
    def connectToPrimary(self):
        """Find a primary master node, and connect to it.

        If a primary master node is not elected or ready, repeat
        the attempt of a connection periodically.

        Note that I do not accept any connection from non-master nodes
        at this stage.
        """
        self.cluster_state = None
        # search, find, connect and identify to the primary master
        bootstrap = BootstrapManager(self, NodeTypes.ADMIN, self.server)
        self.master_node, self.master_conn, num_partitions, num_replicas = \
            bootstrap.getPrimaryConnection()

        if self.pt is None:
            self.pt = PartitionTable(num_partitions, num_replicas)
        elif self.pt.getPartitions() != num_partitions:
            # XXX: shouldn't we recover instead of raising ?
            raise RuntimeError('the number of partitions is inconsistent')
        elif self.pt.getReplicas() != num_replicas:
            # XXX: shouldn't we recover instead of raising ?
            raise RuntimeError('the number of replicas is inconsistent')

        # passive handler
        self.master_conn.setHandler(self.master_event_handler)
        self.master_conn.ask(Packets.AskClusterState())
        self.master_conn.ask(Packets.AskPartitionTable())
 def test_09_answerPartitionTable(self):
     # send a table
     conn = self.getClientConnection()
     self.app.pt = PartitionTable(3, 2)
     node_1 = self.getStorageUUID()
     node_2 = self.getStorageUUID()
     node_3 = self.getStorageUUID()
     self.app.uuid = node_1
     # SN already know all nodes
     self.app.nm.createStorage(uuid=node_1)
     self.app.nm.createStorage(uuid=node_2)
     self.app.nm.createStorage(uuid=node_3)
     self.assertFalse(list(self.app.dm.getPartitionTable()))
     row_list = [(0, ((node_1, CellStates.UP_TO_DATE),
                      (node_2, CellStates.UP_TO_DATE))),
                 (1, ((node_3, CellStates.UP_TO_DATE),
                      (node_1, CellStates.UP_TO_DATE))),
                 (2, ((node_2, CellStates.UP_TO_DATE),
                      (node_3, CellStates.UP_TO_DATE)))]
     self.assertFalse(self.app.pt.filled())
     # send a complete new table and ack
     self.verification.sendPartitionTable(conn, 2, row_list)
     self.assertTrue(self.app.pt.filled())
     self.assertEqual(self.app.pt.getID(), 2)
     self.assertTrue(list(self.app.dm.getPartitionTable()))
示例#6
0
    def test_10_operational(self):
        num_partitions = 5
        num_replicas = 2
        pt = PartitionTable(num_partitions, num_replicas)
        self.assertFalse(pt.filled())
        self.assertFalse(pt.operational())
        # adding a node in all partition
        uuid1 = self.getStorageUUID()
        server1 = ("127.0.0.1", 19001)
        sn1 = StorageNode(Mock(), server1, uuid1)
        for x in xrange(num_partitions):
            pt.setCell(x, sn1, CellStates.UP_TO_DATE)
        self.assertTrue(pt.filled())
        # it's up to date and running, so operational
        sn1.setState(NodeStates.RUNNING)
        self.assertTrue(pt.operational())
        # same with feeding state
        pt.clear()
        self.assertFalse(pt.filled())
        self.assertFalse(pt.operational())
        # adding a node in all partition
        uuid1 = self.getStorageUUID()
        server1 = ("127.0.0.1", 19001)
        sn1 = StorageNode(Mock(), server1, uuid1)
        for x in xrange(num_partitions):
            pt.setCell(x, sn1, CellStates.FEEDING)
        self.assertTrue(pt.filled())
        # it's feeding and running, so operational
        sn1.setState(NodeStates.RUNNING)
        self.assertTrue(pt.operational())

        # same with feeding state but non running node
        pt.clear()
        self.assertFalse(pt.filled())
        self.assertFalse(pt.operational())
        # adding a node in all partition
        uuid1 = self.getStorageUUID()
        server1 = ("127.0.0.1", 19001)
        sn1 = StorageNode(Mock(), server1, uuid1)
        sn1.setState(NodeStates.TEMPORARILY_DOWN)
        for x in xrange(num_partitions):
            pt.setCell(x, sn1, CellStates.FEEDING)
        self.assertTrue(pt.filled())
        # it's up to date and not running, so not operational
        self.assertFalse(pt.operational())

        # same with out of date state and running
        pt.clear()
        self.assertFalse(pt.filled())
        self.assertFalse(pt.operational())
        # adding a node in all partition
        uuid1 = self.getStorageUUID()
        server1 = ("127.0.0.1", 19001)
        sn1 = StorageNode(Mock(), server1, uuid1)
        for x in xrange(num_partitions):
            pt.setCell(x, sn1, CellStates.OUT_OF_DATE)
        self.assertTrue(pt.filled())
        # it's not up to date and running, so not operational
        self.assertFalse(pt.operational())
示例#7
0
 def setUp(self):
     NeoUnitTestBase.setUp(self)
     config = self.getStorageConfiguration(master_number=1)
     self.app = Application(config)
     self.app.name = 'NEO'
     self.app.ready = True
     self.app.pt = PartitionTable(4, 1)
     self.identification = IdentificationHandler(self.app)
示例#8
0
    def test_10_operational(self):
        def createStorage():
            uuid = self.getStorageUUID()
            return self.createStorage(("127.0.0.1", uuid), uuid)

        num_partitions = 5
        num_replicas = 2
        pt = PartitionTable(num_partitions, num_replicas)
        self.assertFalse(pt.filled())
        self.assertFalse(pt.operational())
        # adding a node in all partition
        sn1 = createStorage()
        for x in xrange(num_partitions):
            pt._setCell(x, sn1, CellStates.UP_TO_DATE)
        self.assertTrue(pt.filled())
        # it's up to date and running, so operational
        sn1.setState(NodeStates.RUNNING)
        self.assertTrue(pt.operational())
        # same with feeding state
        pt.clear()
        self.assertFalse(pt.filled())
        self.assertFalse(pt.operational())
        # adding a node in all partition
        sn1 = createStorage()
        for x in xrange(num_partitions):
            pt._setCell(x, sn1, CellStates.FEEDING)
        self.assertTrue(pt.filled())
        # it's feeding and running, so operational
        sn1.setState(NodeStates.RUNNING)
        self.assertTrue(pt.operational())

        # same with feeding state but non running node
        pt.clear()
        self.assertFalse(pt.filled())
        self.assertFalse(pt.operational())
        # adding a node in all partition
        sn1 = createStorage()
        sn1.setState(NodeStates.DOWN)
        for x in xrange(num_partitions):
            pt._setCell(x, sn1, CellStates.FEEDING)
        self.assertTrue(pt.filled())
        # it's up to date and not running, so not operational
        self.assertFalse(pt.operational())

        # same with out of date state and running
        pt.clear()
        self.assertFalse(pt.filled())
        self.assertFalse(pt.operational())
        # adding a node in all partition
        sn1 = createStorage()
        for x in xrange(num_partitions):
            pt._setCell(x, sn1, CellStates.OUT_OF_DATE)
        self.assertTrue(pt.filled())
        # it's not up to date and running, so not operational
        self.assertFalse(pt.operational())
示例#9
0
 def test_09_hasOffset(self):
     num_partitions = 5
     num_replicas = 2
     pt = PartitionTable(num_partitions, num_replicas)
     # add two kind of node, usable and unusable
     uuid1 = self.getStorageUUID()
     server1 = ("127.0.0.1", 19001)
     sn1 = self.createStorage(server1, uuid1)
     pt._setCell(0, sn1, CellStates.UP_TO_DATE)
     # now test
     self.assertTrue(pt.hasOffset(0))
     self.assertFalse(pt.hasOffset(1))
     # unknown partition
     self.assertFalse(pt.hasOffset(50))
示例#10
0
    def _acceptIdentification(self, node, uuid, num_partitions, num_replicas,
                              your_uuid, primary, known_master_list):
        app = self.app

        # Register new master nodes.
        found = False
        conn_address = node.getAddress()
        for node_address, node_uuid in known_master_list:
            if node_address == conn_address:
                assert uuid == node_uuid, (dump(uuid), dump(node_uuid))
                found = True
            n = app.nm.getByAddress(node_address)
            if n is None:
                n = app.nm.createMaster(address=node_address)
            if node_uuid is not None and n.getUUID() != node_uuid:
                n.setUUID(node_uuid)
        assert found, (node, dump(uuid), known_master_list)

        conn = node.getConnection()
        if primary is not None:
            primary_node = app.nm.getByAddress(primary)
            if primary_node is None:
                # I don't know such a node. Probably this information
                # is old. So ignore it.
                logging.warning('Unknown primary master: %s. Ignoring.',
                                primary)
                return
            else:
                if app.trying_master_node is not primary_node:
                    app.trying_master_node = None
                    conn.close()
                app.primary_master_node = primary_node
        else:
            if app.primary_master_node is not None:
                # The primary master node is not a primary master node
                # any longer.
                app.primary_master_node = None

            app.trying_master_node = None
            conn.close()
            return

        # the master must give an UUID
        if your_uuid is None:
            raise ProtocolError('No UUID supplied')
        app.uuid = your_uuid
        logging.info('Got an UUID: %s', dump(app.uuid))

        # Always create partition table
        app.pt = PartitionTable(num_partitions, num_replicas)
示例#11
0
 def test_08_filled(self):
     num_partitions = 5
     num_replicas = 2
     pt = PartitionTable(num_partitions, num_replicas)
     self.assertEqual(pt.np, num_partitions)
     self.assertEqual(pt.num_filled_rows, 0)
     self.assertFalse(pt.filled())
     # adding a node in all partition
     uuid1 = self.getStorageUUID()
     server1 = ("127.0.0.1", 19001)
     sn1 = self.createStorage(server1, uuid1)
     for x in xrange(num_partitions):
         pt._setCell(x, sn1, CellStates.UP_TO_DATE)
     self.assertEqual(pt.num_filled_rows, num_partitions)
     self.assertTrue(pt.filled())
 def setUp(self):
     NeoUnitTestBase.setUp(self)
     self.prepareDatabase(number=1)
     # create an application object
     config = self.getStorageConfiguration(master_number=1)
     self.app = Application(config)
     self.verification = InitializationHandler(self.app)
     # define some variable to simulate client and storage node
     self.master_port = 10010
     self.storage_port = 10020
     self.client_port = 11011
     self.num_partitions = 1009
     self.num_replicas = 2
     self.app.operational = False
     self.app.load_lock_dict = {}
     self.app.pt = PartitionTable(self.num_partitions, self.num_replicas)
示例#13
0
 def loadPartitionTable(self):
     """Load a partition table from the database."""
     ptid = self.dm.getPTID()
     if ptid is None:
         self.pt = PartitionTable(0, 0)
         return
     row_list = []
     for offset, uuid, state in self.dm.getPartitionTable():
         while len(row_list) <= offset:
             row_list.append([])
         # register unknown nodes
         if self.nm.getByUUID(uuid) is None:
             self.nm.createStorage(uuid=uuid)
         row_list[offset].append((uuid, CellStates[state]))
     self.pt = object.__new__(PartitionTable)
     self.pt.load(ptid, self.dm.getNumReplicas(), row_list, self.nm)
示例#14
0
 def test_05_getCellList(self):
     num_partitions = 5
     num_replicas = 2
     pt = PartitionTable(num_partitions, num_replicas)
     # add two kind of node, usable and unusable
     uuid1 = self.getStorageUUID()
     server1 = ("127.0.0.1", 19001)
     sn1 = self.createStorage(server1, uuid1)
     pt._setCell(0, sn1, CellStates.UP_TO_DATE)
     uuid2 = self.getStorageUUID()
     server2 = ("127.0.0.2", 19001)
     sn2 = self.createStorage(server2, uuid2)
     pt._setCell(0, sn2, CellStates.OUT_OF_DATE)
     uuid3 = self.getStorageUUID()
     server3 = ("127.0.0.3", 19001)
     sn3 = self.createStorage(server3, uuid3)
     pt._setCell(0, sn3, CellStates.FEEDING)
     uuid4 = self.getStorageUUID()
     server4 = ("127.0.0.4", 19001)
     sn4 = self.createStorage(server4, uuid4)
     pt._setCell(0, sn4, CellStates.DISCARDED)  # won't be added
     # now checks result
     self.assertEqual(len(pt.partition_list[0]), 3)
     for x in xrange(num_partitions):
         if x == 0:
             # all nodes
             all_cell = pt.getCellList(0)
             all_nodes = [x.getNode() for x in all_cell]
             self.assertEqual(len(all_cell), 3)
             self.assertTrue(sn1 in all_nodes)
             self.assertTrue(sn2 in all_nodes)
             self.assertTrue(sn3 in all_nodes)
             self.assertTrue(sn4 not in all_nodes)
             # readable nodes
             all_cell = pt.getCellList(0, readable=True)
             all_nodes = [x.getNode() for x in all_cell]
             self.assertEqual(len(all_cell), 2)
             self.assertTrue(sn1 in all_nodes)
             self.assertTrue(sn2 not in all_nodes)
             self.assertTrue(sn3 in all_nodes)
             self.assertTrue(sn4 not in all_nodes)
         else:
             self.assertEqual(len(pt.getCellList(1, False)), 0)
             self.assertEqual(len(pt.getCellList(1, True)), 0)
示例#15
0
 def test_12_getRow(self):
     num_partitions = 5
     num_replicas = 2
     pt = PartitionTable(num_partitions, num_replicas)
     # add nodes
     uuid1 = self.getStorageUUID()
     server1 = ("127.0.0.1", 19001)
     sn1 = self.createStorage(server1, uuid1)
     pt._setCell(0, sn1, CellStates.UP_TO_DATE)
     pt._setCell(1, sn1, CellStates.UP_TO_DATE)
     pt._setCell(2, sn1, CellStates.UP_TO_DATE)
     uuid2 = self.getStorageUUID()
     server2 = ("127.0.0.2", 19001)
     sn2 = self.createStorage(server2, uuid2)
     pt._setCell(0, sn2, CellStates.UP_TO_DATE)
     pt._setCell(1, sn2, CellStates.UP_TO_DATE)
     uuid3 = self.getStorageUUID()
     server3 = ("127.0.0.3", 19001)
     sn3 = self.createStorage(server3, uuid3)
     pt._setCell(0, sn3, CellStates.UP_TO_DATE)
     # test
     row_0 = pt.getRow(0)
     self.assertEqual(len(row_0), 3)
     for uuid, state in row_0:
         self.assertTrue(uuid in (sn1.getUUID(), sn2.getUUID(),
                                  sn3.getUUID()))
         self.assertEqual(state, CellStates.UP_TO_DATE)
     row_1 = pt.getRow(1)
     self.assertEqual(len(row_1), 2)
     for uuid, state in row_1:
         self.assertTrue(uuid in (sn1.getUUID(), sn2.getUUID()))
         self.assertEqual(state, CellStates.UP_TO_DATE)
     row_2 = pt.getRow(2)
     self.assertEqual(len(row_2), 1)
     for uuid, state in row_2:
         self.assertEqual(uuid, sn1.getUUID())
         self.assertEqual(state, CellStates.UP_TO_DATE)
     row_3 = pt.getRow(3)
     self.assertEqual(len(row_3), 0)
     row_4 = pt.getRow(4)
     self.assertEqual(len(row_4), 0)
     # unknown row
     self.assertRaises(IndexError, pt.getRow, 5)
示例#16
0
 def test_04_removeCell(self):
     num_partitions = 5
     num_replicas = 2
     pt = PartitionTable(num_partitions, num_replicas)
     uuid1 = self.getStorageUUID()
     server1 = ("127.0.0.1", 19001)
     sn1 = self.createStorage(server1, uuid1)
     for x in xrange(num_partitions):
         self.assertEqual(len(pt.partition_list[x]), 0)
     # add a cell to an empty row
     self.assertFalse(pt.count_dict.has_key(sn1))
     pt._setCell(0, sn1, CellStates.UP_TO_DATE)
     self.assertTrue(pt.count_dict.has_key(sn1))
     self.assertEqual(pt.count_dict[sn1], 1)
     for x in xrange(num_partitions):
         if x == 0:
             self.assertEqual(len(pt.partition_list[x]), 1)
         else:
             self.assertEqual(len(pt.partition_list[x]), 0)
     # remove it
     pt.removeCell(0, sn1)
     self.assertEqual(pt.count_dict[sn1], 0)
     for x in xrange(num_partitions):
         self.assertEqual(len(pt.partition_list[x]), 0)
     # add a feeding cell
     pt._setCell(0, sn1, CellStates.FEEDING)
     self.assertTrue(pt.count_dict.has_key(sn1))
     self.assertEqual(pt.count_dict[sn1], 0)
     for x in xrange(num_partitions):
         if x == 0:
             self.assertEqual(len(pt.partition_list[x]), 1)
         else:
             self.assertEqual(len(pt.partition_list[x]), 0)
     # remove it
     pt.removeCell(0, sn1)
     self.assertEqual(pt.count_dict[sn1], 0)
     for x in xrange(num_partitions):
         self.assertEqual(len(pt.partition_list[x]), 0)
示例#17
0
    def test_01_loadPartitionTable(self):
        self.app.dm = Mock({
            'getPartitionTable': [],
        })
        self.assertEqual(self.app.pt, None)
        num_partitions = 3
        num_replicas = 2
        self.app.pt = PartitionTable(num_partitions, num_replicas)
        self.assertFalse(self.app.pt.getNodeSet())
        self.assertFalse(self.app.pt.filled())
        for x in xrange(num_partitions):
            self.assertFalse(self.app.pt.hasOffset(x))

        # load an empty table
        self.app.loadPartitionTable()
        self.assertFalse(self.app.pt.getNodeSet())
        self.assertFalse(self.app.pt.filled())
        for x in xrange(num_partitions):
            self.assertFalse(self.app.pt.hasOffset(x))

        # add some node, will be remove when loading table
        master_uuid = self.getMasterUUID()
        master = self.app.nm.createMaster(uuid=master_uuid)
        storage_uuid = self.getStorageUUID()
        storage = self.app.nm.createStorage(uuid=storage_uuid)
        client_uuid = self.getClientUUID()

        self.app.pt._setCell(0, master, CellStates.UP_TO_DATE)
        self.app.pt._setCell(0, storage, CellStates.UP_TO_DATE)
        self.assertEqual(len(self.app.pt.getNodeSet()), 2)
        self.assertFalse(self.app.pt.filled())
        for x in xrange(num_partitions):
            if x == 0:
                self.assertTrue(self.app.pt.hasOffset(x))
            else:
                self.assertFalse(self.app.pt.hasOffset(x))
        # load an empty table, everything removed
        self.app.loadPartitionTable()
        self.assertFalse(self.app.pt.getNodeSet())
        self.assertFalse(self.app.pt.filled())
        for x in xrange(num_partitions):
            self.assertFalse(self.app.pt.hasOffset(x))

        # add some node
        self.app.pt._setCell(0, master, CellStates.UP_TO_DATE)
        self.app.pt._setCell(0, storage, CellStates.UP_TO_DATE)
        self.assertEqual(len(self.app.pt.getNodeSet()), 2)
        self.assertFalse(self.app.pt.filled())
        for x in xrange(num_partitions):
            if x == 0:
                self.assertTrue(self.app.pt.hasOffset(x))
            else:
                self.assertFalse(self.app.pt.hasOffset(x))
        # fill partition table
        self.app.dm = Mock({
            'getPartitionTable': [
                (0, client_uuid, CellStates.UP_TO_DATE),
                (1, client_uuid, CellStates.UP_TO_DATE),
                (1, storage_uuid, CellStates.UP_TO_DATE),
                (2, storage_uuid, CellStates.UP_TO_DATE),
                (2, master_uuid, CellStates.UP_TO_DATE),
            ],
            'getPTID':
            1,
        })
        self.app.pt.clear()
        self.app.loadPartitionTable()
        self.assertTrue(self.app.pt.filled())
        for x in xrange(num_partitions):
            self.assertTrue(self.app.pt.hasOffset(x))
        # check each row
        cell_list = self.app.pt.getCellList(0)
        self.assertEqual(len(cell_list), 1)
        self.assertEqual(cell_list[0].getUUID(), client_uuid)
        cell_list = self.app.pt.getCellList(1)
        self.assertEqual(len(cell_list), 2)
        self.assertTrue(cell_list[0].getUUID() in (client_uuid, storage_uuid))
        self.assertTrue(cell_list[1].getUUID() in (client_uuid, storage_uuid))
        cell_list = self.app.pt.getCellList(2)
        self.assertEqual(len(cell_list), 2)
        self.assertTrue(cell_list[0].getUUID() in (master_uuid, storage_uuid))
        self.assertTrue(cell_list[1].getUUID() in (master_uuid, storage_uuid))
示例#18
0
    def test_03_setCell(self):
        num_partitions = 5
        num_replicas = 2
        pt = PartitionTable(num_partitions, num_replicas)
        uuid1 = self.getStorageUUID()
        server1 = ("127.0.0.1", 19001)
        sn1 = self.createStorage(server1, uuid1)
        for x in xrange(num_partitions):
            self.assertEqual(len(pt.partition_list[x]), 0)
        # add a cell to an empty row
        self.assertFalse(pt.count_dict.has_key(sn1))
        pt._setCell(0, sn1, CellStates.UP_TO_DATE)
        self.assertTrue(pt.count_dict.has_key(sn1))
        self.assertEqual(pt.count_dict[sn1], 1)
        for x in xrange(num_partitions):
            if x == 0:
                self.assertEqual(len(pt.partition_list[x]), 1)
                cell = pt.partition_list[x][0]
                self.assertEqual(cell.getState(), CellStates.UP_TO_DATE)
            else:
                self.assertEqual(len(pt.partition_list[x]), 0)
        # try to add to a nonexistent partition
        self.assertRaises(IndexError, pt._setCell, 10, sn1,
                          CellStates.UP_TO_DATE)
        # if we add in discards state, must be removed
        pt._setCell(0, sn1, CellStates.DISCARDED)
        for x in xrange(num_partitions):
            self.assertEqual(len(pt.partition_list[x]), 0)
        self.assertEqual(pt.count_dict[sn1], 0)
        # add a feeding node into empty row
        pt._setCell(0, sn1, CellStates.FEEDING)
        self.assertTrue(pt.count_dict.has_key(sn1))
        self.assertEqual(pt.count_dict[sn1], 0)
        for x in xrange(num_partitions):
            if x == 0:
                self.assertEqual(len(pt.partition_list[x]), 1)
                cell = pt.partition_list[x][0]
                self.assertEqual(cell.getState(), CellStates.FEEDING)
            else:
                self.assertEqual(len(pt.partition_list[x]), 0)
        # re-add it as feeding, nothing change
        pt._setCell(0, sn1, CellStates.FEEDING)
        self.assertTrue(pt.count_dict.has_key(sn1))
        self.assertEqual(pt.count_dict[sn1], 0)
        for x in xrange(num_partitions):
            if x == 0:
                self.assertEqual(len(pt.partition_list[x]), 1)
                cell = pt.partition_list[x][0]
                self.assertEqual(cell.getState(), CellStates.FEEDING)
            else:
                self.assertEqual(len(pt.partition_list[x]), 0)
        # now add it as up to date
        pt._setCell(0, sn1, CellStates.UP_TO_DATE)
        self.assertTrue(pt.count_dict.has_key(sn1))
        self.assertEqual(pt.count_dict[sn1], 1)
        for x in xrange(num_partitions):
            if x == 0:
                self.assertEqual(len(pt.partition_list[x]), 1)
                cell = pt.partition_list[x][0]
                self.assertEqual(cell.getState(), CellStates.UP_TO_DATE)
            else:
                self.assertEqual(len(pt.partition_list[x]), 0)

        # now add down state, must not be taken into account
        pt._setCell(0, sn1, CellStates.DISCARDED)
        for x in xrange(num_partitions):
            self.assertEqual(len(pt.partition_list[x]), 0)
        self.assertEqual(pt.count_dict[sn1], 0)
        sn1.setState(NodeStates.UNKNOWN)
        self.assertRaises(PartitionTableException, pt._setCell, 0, sn1,
                          CellStates.UP_TO_DATE)
        for x in xrange(num_partitions):
            self.assertEqual(len(pt.partition_list[x]), 0)
        self.assertEqual(pt.count_dict[sn1], 0)
示例#19
0
 def _acceptIdentification(self, node, num_partitions, num_replicas):
     self.app.pt = PartitionTable(num_partitions, num_replicas)