Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. from __future__ import print_function
  2.  
  3. import redis
  4. import sys
  5.  
  6.  
  7. CLUSTER_HASH_SLOTS = 16384
  8. MIGRATE_DEFAULT_TIMEOUT = 600000
  9. MIGRATE_DEFAULT_PIPELINE = 10
  10. REBALANCE_DEFAULT_THRESHOLD = 2
  11.  
  12.  
  13. def print_verbose(*args, **kwargs):
  14. if print_verbose.verbose:
  15. print(' '.join(msg), **kwargs)
  16.  
  17.  
  18. class ClusterNode:
  19. def __init__(self, addr):
  20. s = addr.split('@')[0].split(':')
  21. if len(s) < 2:
  22. print('Invalid IP or Port (given as {addr})'\
  23. ' - use IP:Port format'.format(addr))
  24.  
  25. port = s[-1]
  26. ip = s[:-1].join(':') # if s.length > 1 here, it's IPv6, so restore address
  27. self._r =
  28. self._info = {
  29. 'host': ip,
  30. 'port': port,
  31. 'slots': {},
  32. 'migrating': {},
  33. 'importing': {},
  34. 'replicate': False,
  35. }
  36. self._dirty = False # True if we need to flush slots info into node.
  37. self._friends = []
  38.  
  39.  
  40. @property
  41. def friends(self):
  42. return self._frineds
  43.  
  44. @property
  45. def slots(self):
  46. return self._info['slots']
  47.  
  48.  
  49. def has_flag(self, flag):
  50. flags = self._info.get('flags')
  51. return True if flags and flags.find(flag) else False
  52.  
  53.  
  54. def __str__(self):
  55. return '{host}:{port}'.format(info['host'], info['port'])
  56.  
  57.  
  58. def connect(self, o={}):
  59. if self._r:
  60. return self._r
  61. if _global_verbose:
  62. print_verbose('Connecting to node {self}: '.format(self), end='')
  63.  
  64. try:
  65. self._r = redis.StrictRedis(host=info['host'],
  66. port=info['port'],
  67. timeout=60)
  68. self._r.ping()
  69. except Exception:
  70. print('''[ERR] Sorry, can't connect to node {self}'''.format(self))
  71. if o['abort']:
  72. sys.exit(1)
  73. self._r = None
  74.  
  75. print_verbose("OK")
  76.  
  77.  
  78. def assert_cluster(self):
  79. info = self._info
  80. if (not info.get('cluster_enabled') or
  81. or int(info['cluster_enabled']) == 0):
  82. print('[ERR] Node {self} is not configured'\
  83. ' as a cluster node.'.format(self))
  84. sys.exit(1)
  85.  
  86.  
  87. def assert_empty(self):
  88. if (not self._r.cluster('info').get('cluster_known_nodes') == 1
  89. or self._r.info.get('db0')):
  90. print('[ERR] Node {self} is not empty. '\
  91. 'Either the node already knows other nodes '\
  92. '(check with CLUSTER NODES) '\
  93. 'or contains some key in database 0.'.format(self))
  94. sys.exit(1)
  95.  
  96.  
  97. def load_info(self, o={}):
  98. self.connect()
  99. nodes = self._r.cluster('nodes')
  100. for n in nodes:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement