예제 #1
0
 def get(self, path, watch=None):
     if path == '/service/test/no_node':
         raise NoNodeError
     elif path == '/service/test/other_exception':
         raise Exception()
     elif '/members/' in path:
         return (
             'postgres://*****:*****@localhost:5434/postgres?application_name=http://127.0.0.1:8009/patroni',
             ZnodeStat(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
     elif path.endswith('/optime/leader'):
         return '1'
     elif path.endswith('/leader'):
         if self.leader:
             return ('foo', ZnodeStat(0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0))
         return ('foo', ZnodeStat(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
예제 #2
0
    def test_save_watcher_to_dict_with_disabled_cache(self, mocked_save_dict):
        # If caching is disabled, it should return quickly
        fake_node_data = {
            'children': {},
            'data':
            None,
            'path':
            '/services/ssh',
            'stat':
            ZnodeStat(czxid=505,
                      mzxid=505,
                      ctime=1355719651687,
                      mtime=1355719651687,
                      version=0,
                      cversion=1,
                      aversion=0,
                      ephemeralOwner=0,
                      dataLength=0,
                      numChildren=0,
                      pzxid=506)
        }

        self.ndsr._cachefile = False
        self.ndsr._save_watcher_to_dict(fake_node_data)
        self.assertEqual(mocked_save_dict.mock_calls, [])
예제 #3
0
    def test_save_watcher_to_dict_with_empty_data(self, mocked_save_dict):
        # A data node though that has NO DATA is assumed to be a path used
        # only for storing ephemeral node lists (server registrations).
        # In this case, if the children-list AND data-list are empty, we
        # do not cache this.
        fake_node_data = {
            'children': {},
            'data':
            None,
            'path':
            '/services/ssh',
            'stat':
            ZnodeStat(czxid=505,
                      mzxid=505,
                      ctime=1355719651687,
                      mtime=1355719651687,
                      version=0,
                      cversion=1,
                      aversion=0,
                      ephemeralOwner=0,
                      dataLength=0,
                      numChildren=0,
                      pzxid=506)
        }

        self.ndsr._cachefile = True
        self.ndsr._save_watcher_to_dict(fake_node_data)
        self.assertEqual(mocked_save_dict.mock_calls, [])
예제 #4
0
    def test_save_watcher_to_dict(self, mocked_save_dict):
        # A normal data node storing some real data should be cachable
        fake_node_data = {
            'children': {},
            'data': {
                'some': 'data'
            },
            'path':
            '/mydata',
            'stat':
            ZnodeStat(czxid=505,
                      mzxid=505,
                      ctime=1355719651687,
                      mtime=1355719651687,
                      version=0,
                      cversion=1,
                      aversion=0,
                      ephemeralOwner=0,
                      dataLength=0,
                      numChildren=0,
                      pzxid=506)
        }

        self.ndsr._cachefile = True
        self.ndsr._save_watcher_to_dict(fake_node_data)
        mocked_save_dict.assert_called_with({'/mydata': fake_node_data}, True)
예제 #5
0
 def get(self, path, watch=None):
     if not isinstance(path, six.string_types):
         raise TypeError("Invalid type for 'path' (string expected)")
     if path == '/no_node':
         raise NoNodeError
     elif '/members/' in path:
         return (
             b'postgres://*****:*****@localhost:5434/postgres?application_name=http://127.0.0.1:8009/patroni',
             ZnodeStat(0, 0, 0, 0, 0, 0, 0, 0 if self.exists else -1, 0, 0,
                       0))
     elif path.endswith('/leader'):
         if self.leader:
             return (b'foo', ZnodeStat(0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0))
         return (b'foo', ZnodeStat(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
     elif path.endswith('/initialize'):
         return (b'foo', ZnodeStat(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
     return (b'', ZnodeStat(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
예제 #6
0
 def stat(self):
     return ZnodeStat(czxid=0,
                      mzxid=0,
                      ctime=time.time(),
                      mtime=time.time(),
                      version=self.version,
                      cversion=0,
                      aversion=0,
                      ephemeralOwner=0,
                      dataLength=len(self._value),
                      numChildren=len(self._children),
                      pzxid=0)
    def getStats(self, paths, options):
        """
        Returns Stat[]
        Parameters:
            paths: List<String>options: int
        @Override


        """
        if paths == None or paths.__len__() == 0:
            self.LOG.error("paths is null or empty")
            #            return new Stat[0]
            return []
        # Stat[]
        stats = [ZnodeStat() for path in paths]
        # long
        startT = time.time()
        try:
            # ExistsCallbackHandler[]
            cbList = [ExistsCallbackHandler() for path in paths.__len__]
            for i in range(len(paths)):  # String
                path = paths[i]
                cbList[i] = ExistsCallbackHandler()
                self._zkClient.asyncExists(path, cbList[i])

            for i in range(len(cbList)):  # ExistsCallbackHandler
                cb = cbList[i]
                cb.waitForSuccess()
                stats[i] = cb._stat

            return stats
        finally:
            # long
            endT = time.time()
            #                if self.LOG.isDebugEnabled():
            self.LOG.debug("exists_async, size: " + str(paths.__len__()) +
                           ", paths: " + str(paths.__getitem__(0)) +
                           ",... time: " + str((endT - startT)) + " ns")
예제 #8
0
def _parse_znode_stat(data):
    return ZnodeStat(*map(_b_to_uint, (data[8 * i:8 * (i + 1)]
                                       for i in range(11))))