mock_etcd.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import threading
  2. try:
  3. import queue
  4. except ImportError:
  5. import Queue as queue
  6. class MockEtcd(object):
  7. class KV(object):
  8. def __init__(self, key, value):
  9. self._key = key
  10. self._value = value
  11. @property
  12. def key(self):
  13. if isinstance(self._key, str):
  14. return self._key.encode()
  15. return self._key
  16. @property
  17. def value(self):
  18. if isinstance(self._value, str):
  19. return self._value.encode()
  20. return self._value
  21. class EventNotifier(object):
  22. def __init__(self, clnt):
  23. self._queue = queue.Queue()
  24. self._clnt = clnt
  25. def get_client_belongto(self):
  26. return self._clnt
  27. def notify(self, key, value):
  28. self._queue.put(MockEtcd.KV(key, value))
  29. def wait_for_event(self):
  30. while True:
  31. event = self._queue.get()
  32. if event is None:
  33. return
  34. yield event
  35. def cancel(self):
  36. self._queue.put(None)
  37. def __init__(self):
  38. self._lock = threading.Lock()
  39. self._data = {}
  40. self._event_notifier = {}
  41. def get(self, key):
  42. with self._lock:
  43. if key in self._data:
  44. if isinstance(self._data[key], str):
  45. return self._data[key].encode(), None
  46. return self._data[key], None
  47. return None, None
  48. def put(self, key, value):
  49. with self._lock:
  50. self._data[key] = value
  51. self._notify_if_need(key)
  52. def delete(self, key):
  53. with self._lock:
  54. self._data.pop(key, None)
  55. self._notify_if_need(key)
  56. def delete_prefix(self, prefix):
  57. with self._lock:
  58. deleted = []
  59. for key in self._data:
  60. if key.startswith(prefix):
  61. deleted.append(key)
  62. for key in deleted:
  63. self._data.pop(key, None)
  64. self._notify_if_need(key)
  65. def put_if_not_exists(self, key, value):
  66. with self._lock:
  67. if key in self._data:
  68. return False
  69. self._data[key] = value
  70. self._notify_if_need(key)
  71. return True
  72. def replace(self, key, old_value, new_value):
  73. with self._lock:
  74. stored = None
  75. if key in self._data:
  76. stored = self._data[key]
  77. if stored != old_value:
  78. return False
  79. self._data[key] = new_value
  80. self._notify_if_need(key)
  81. return True
  82. def watch(self, key, clnt):
  83. with self._lock:
  84. en = MockEtcd.EventNotifier(clnt)
  85. if key not in self._event_notifier:
  86. self._event_notifier[key] = [en]
  87. else:
  88. self._event_notifier[key].append(en)
  89. return en.wait_for_event(), en.cancel
  90. def close(self, clnt):
  91. with self._lock:
  92. for key in self._event_notifier:
  93. self._event_notifier[key] = [
  94. en for en in self._event_notifier[key] if
  95. en.get_client_belongto() == clnt
  96. ]
  97. def get_prefix(self, prefix, sort_order='ascend'):
  98. kvs = []
  99. with self._lock:
  100. for key, value in self._data.items():
  101. if key.startswith(prefix):
  102. kvs.append((value.encode(), MockEtcd.KV(key, None)))
  103. if sort_order == 'descend':
  104. kvs = sorted(kvs, key=lambda kv: kv[1].key, reverse=True)
  105. elif sort_order == 'ascend':
  106. kvs = sorted(kvs, key=lambda kv: kv[1].key, reverse=False)
  107. return kvs
  108. def _notify_if_need(self, key):
  109. if key in self._event_notifier:
  110. value = None
  111. if key in self._data:
  112. value = self._data[key]
  113. for en in self._event_notifier[key]:
  114. en.notify(key, value)
  115. class MockEtcdClient(object):
  116. POOL_LOCK = threading.Lock()
  117. MOCK_ETCD_POOL = {}
  118. def __init__(self, host, port):
  119. key = '{}:{}'.format(host, port)
  120. with self.POOL_LOCK:
  121. if key not in self.MOCK_ETCD_POOL:
  122. self.MOCK_ETCD_POOL[key] = MockEtcd()
  123. self._mock_etcd = self.MOCK_ETCD_POOL[key]
  124. def __getattr__(self, attr):
  125. return getattr(self._mock_etcd, attr)
  126. def watch(self, key):
  127. return self._mock_etcd.watch(key, self)
  128. def close(self):
  129. self._mock_etcd.close(self)