Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- import redis
- import sys
- CLUSTER_HASH_SLOTS = 16384
- MIGRATE_DEFAULT_TIMEOUT = 600000
- MIGRATE_DEFAULT_PIPELINE = 10
- REBALANCE_DEFAULT_THRESHOLD = 2
- def print_verbose(*args, **kwargs):
- if print_verbose.verbose:
- print(' '.join(msg), **kwargs)
- class ClusterNode:
- def __init__(self, addr):
- s = addr.split('@')[0].split(':')
- if len(s) < 2:
- print('Invalid IP or Port (given as {addr})'\
- ' - use IP:Port format'.format(addr))
- port = s[-1]
- ip = s[:-1].join(':') # if s.length > 1 here, it's IPv6, so restore address
- self._r =
- self._info = {
- 'host': ip,
- 'port': port,
- 'slots': {},
- 'migrating': {},
- 'importing': {},
- 'replicate': False,
- }
- self._dirty = False # True if we need to flush slots info into node.
- self._friends = []
- @property
- def friends(self):
- return self._frineds
- @property
- def slots(self):
- return self._info['slots']
- def has_flag(self, flag):
- flags = self._info.get('flags')
- return True if flags and flags.find(flag) else False
- def __str__(self):
- return '{host}:{port}'.format(info['host'], info['port'])
- def connect(self, o={}):
- if self._r:
- return self._r
- if _global_verbose:
- print_verbose('Connecting to node {self}: '.format(self), end='')
- try:
- self._r = redis.StrictRedis(host=info['host'],
- port=info['port'],
- timeout=60)
- self._r.ping()
- except Exception:
- print('''[ERR] Sorry, can't connect to node {self}'''.format(self))
- if o['abort']:
- sys.exit(1)
- self._r = None
- print_verbose("OK")
- def assert_cluster(self):
- info = self._info
- if (not info.get('cluster_enabled') or
- or int(info['cluster_enabled']) == 0):
- print('[ERR] Node {self} is not configured'\
- ' as a cluster node.'.format(self))
- sys.exit(1)
- def assert_empty(self):
- if (not self._r.cluster('info').get('cluster_known_nodes') == 1
- or self._r.info.get('db0')):
- print('[ERR] Node {self} is not empty. '\
- 'Either the node already knows other nodes '\
- '(check with CLUSTER NODES) '\
- 'or contains some key in database 0.'.format(self))
- sys.exit(1)
- def load_info(self, o={}):
- self.connect()
- nodes = self._r.cluster('nodes')
- for n in nodes:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement