-
Notifications
You must be signed in to change notification settings - Fork 390
/
disc_cassdb.py
390 lines (352 loc) · 15.4 KB
/
disc_cassdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#
import re
from cfgm_common import jsonutils as json
import time
import gevent
import disc_consts
import disc_exceptions
from datetime import datetime
from gevent.coros import BoundedSemaphore
import pycassa
import pycassa.util
from pycassa.system_manager import *
from pycassa.util import *
from pycassa.types import *
from cfgm_common.vnc_cassandra import VncCassandraClient
from sandesh_common.vns.constants import DISCOVERY_SERVER_KEYSPACE_NAME, \
CASSANDRA_DEFAULT_GC_GRACE_SECONDS
class DiscoveryCassandraClient(VncCassandraClient):
_DISCOVERY_KEYSPACE_NAME = DISCOVERY_SERVER_KEYSPACE_NAME
_DISCOVERY_CF_NAME = 'discovery'
@classmethod
def get_db_info(cls):
db_info = [(cls._DISCOVERY_KEYSPACE_NAME, [cls._DISCOVERY_CF_NAME])]
return db_info
# end get_db_info
def __init__(self, module, cass_srv_list, config_log, reset_config=False,
db_prefix=None, cass_credential=None):
self._debug = {
'db_upd_oper_state': 0,
'db_upd_admin_state': 0,
}
keyspaces = {
self._DISCOVERY_KEYSPACE_NAME: {
self._DISCOVERY_CF_NAME: {
'create_cf_args': {
'comparator_type': CompositeType(
AsciiType(), UTF8Type(), UTF8Type())
}
}
}
}
super(DiscoveryCassandraClient, self).__init__(
cass_srv_list, db_prefix, keyspaces, None,
config_log, reset_config=reset_config, credential=cass_credential)
DiscoveryCassandraClient._disco_cf = self._cf_dict[self._DISCOVERY_CF_NAME]
#end __init__
def get_debug_stats(self):
return self._debug
# end
"""
various column names
('client', client_id, 'client-entry')
('subscriber', service_id, client_id)
('service', service_id, 'service-entry')
"""
# decorator to catch connectivity error
def cass_error_handler(func):
def error_handler(*args, **kwargs):
try:
return func(*args,**kwargs)
except (pycassa.pool.AllServersUnavailable,
pycassa.pool.MaximumRetryException):
raise disc_exceptions.ServiceUnavailable()
except Exception as e:
raise
return error_handler
# return all clients
def subscriber_entries(self):
col_name = ('client',)
data = self._disco_cf.get_range(column_start = col_name, column_finish = col_name)
for service_type, clients in data:
for col_name in clients:
(_, client_id, service_id) = col_name
# skip pure client entry
if service_id == disc_consts.CLIENT_TAG:
continue
yield((client_id, service_type))
# end
# return all subscriptions
@cass_error_handler
def get_all_clients(self, service_type=None, service_id=None, unique_clients=False):
r = []
entry_format_subscriber = False
if service_type and service_id:
# ('subscriber', service_id, client_id)
col_name = ('subscriber', service_id,)
try:
clients = self._disco_cf.get(service_type, column_start = col_name,
column_finish = col_name, column_count = disc_consts.MAX_COL)
except pycassa.NotFoundException:
return None
data = [(service_type, dict(clients))]
entry_format_subscriber = True
elif service_type:
col_name = ('client', )
try:
clients = self._disco_cf.get(service_type, column_start = col_name,
column_finish = col_name, column_count = disc_consts.MAX_COL)
except pycassa.NotFoundException:
return None
data = [(service_type, dict(clients))]
else:
col_name = ('client', )
try:
data = self._disco_cf.get_range(column_start=col_name,
column_finish = col_name, column_count = disc_consts.MAX_COL)
except pycassa.NotFoundException:
return None
for service_type, clients in data:
rr = []
for col_name in clients:
if entry_format_subscriber:
(_, service_id, client_id) = col_name
else:
(_, client_id, service_id) = col_name
entry_str = clients[col_name]
entry = json.loads(entry_str)
# skip pure client entry unless unique clients wanted
if unique_clients and service_id == disc_consts.CLIENT_TAG:
rr.append((service_type, client_id, entry))
elif not unique_clients and service_id != disc_consts.CLIENT_TAG:
rr.append((service_type, client_id, service_id,
entry['mtime'], entry['ttl']))
# sort by modification time
# rr = sorted(rr, key=lambda entry: entry[3])
r.extend(rr)
return r
# end get_all_clients
# update publisher entry
@cass_error_handler
def update_service(self, service_type, service_id, entry):
self.insert_service(service_type, service_id, entry)
# end
@cass_error_handler
def insert_service(self, service_type, service_id, entry):
col_name = ('service', service_id, 'service-entry')
self._disco_cf.insert(service_type, {col_name : json.dumps(entry)})
# end insert_service
@cass_error_handler
# admin state has been split from rest of published information
def set_admin_state(self, service_type, service_id, admin_state):
col_name = ('service', service_id, disc_consts.ADMIN_STATE_TAG)
self._disco_cf.insert(service_type, {col_name : admin_state})
# end insert_service
# forget service and subscribers
@cass_error_handler
def delete_service(self, entry):
self._disco_cf.remove(entry['service_type'],
columns = [('service', entry['service_id'], 'service-entry')])
#end delete_service
# return service entry
@cass_error_handler
def lookup_service(self, service_type=None, service_id=None, include_count=False):
try:
col_name = ('service', service_id,) if service_id else ('service',)
if service_type:
services = self._disco_cf.get(service_type, column_start = col_name,
column_finish = col_name, column_count = disc_consts.MAX_COL)
data = [(service_type, services)]
else:
data = self._disco_cf.get_range(column_start = col_name, column_finish = col_name)
# admin state is maintained seperately from rest of publisher info
# thus need a seperate pass to merge them together
data_dict = {}; admin_state = {}
for serv_type, services in data:
for col_name, col_val in services.items():
(_, serv_id, tag) = col_name
if tag == disc_consts.SERVICE_TAG:
entry = json.loads(col_val)
entry['in_use'] = 0
data_dict[(serv_type,serv_id)] = entry
elif tag == disc_consts.ADMIN_STATE_TAG:
admin_state[(serv_type,serv_id)] = col_val
if not include_count:
continue
# get in-use count for each publisher.
col_name = ('subscriber',)
try:
# read all subs for a service type to minimize read requests
subscribers = self._disco_cf.get(serv_type, column_start = col_name,
column_finish = col_name, column_count = disc_consts.MAX_COL)
for col, val in subscribers.items():
_, serv_id, client_id = col
if (serv_type,serv_id) in data_dict:
data_dict[(serv_type,serv_id)]['in_use'] += 1
except pycassa.NotFoundException:
pass
if len(data_dict) == 0:
return None if service_id else []
for key, entry in data_dict.items():
entry['admin_state'] = admin_state.get(key, "up")
data = [data_dict[key] for key in sorted(data_dict.iterkeys())]
return data[0] if service_id else data
except pycassa.NotFoundException:
return None
# end lookup_service
@cass_error_handler
def query_service(self, service_type):
return self.lookup_service(service_type, service_id = None)
# end
# this is actually client create :-(
@cass_error_handler
def insert_client_data(self, service_type, client_id, blob):
col_name = ('client', client_id, disc_consts.CLIENT_TAG)
self._disco_cf.insert(service_type, {col_name : json.dumps(blob)})
# end insert_client_data
# insert a subscription (blob/ttl per service_type)
@cass_error_handler
def insert_client(self, service_type, service_id, client_id, blob, ttl):
col_val = json.dumps({'ttl': ttl, 'blob': blob, 'mtime': int(time.time())})
col_name = ('subscriber', service_id, client_id)
self._disco_cf.insert(service_type, {col_name : col_val},
ttl = ttl + disc_consts.TTL_EXPIRY_DELTA)
col_name = ('client', client_id, service_id)
self._disco_cf.insert(service_type, {col_name : col_val},
ttl = ttl + disc_consts.TTL_EXPIRY_DELTA)
# end insert_client
# return client (subscriber) entry
@cass_error_handler
def lookup_client(self, service_type, client_id, subs = False):
r = []
col_name = ('client', client_id, )
try:
subs = self._disco_cf.get(service_type, column_start = col_name,
column_finish = col_name, include_timestamp = True,
column_count = disc_consts.MAX_COL)
# sort columns by timestamp (subs is array of (col_name, (value, timestamp)))
subs = sorted(subs.items(), key=lambda entry: entry[1][1])
# col_name = (client, cliend_id, service_id)
# col_val = (real-value, timestamp)
data = None
for col_name, col_val in subs:
_, client_id, service_id = col_name
if service_id == disc_consts.CLIENT_TAG:
data = json.loads(col_val[0])
continue
entry = json.loads(col_val[0])
r.append((col_name[2], entry.get('expired', False)))
return (data, r)
except pycassa.NotFoundException:
return (None, [])
# end lookup_client
# return all subscriptions for a given client
@cass_error_handler
def lookup_subscription(self, service_type, client_id):
r = []
col_name = ('client', client_id, )
try:
subs = self._disco_cf.get(service_type, column_start = col_name,
column_finish = col_name, column_count = disc_consts.MAX_COL)
# col_name = subscription, cliend_id, service_id
for col_name, col_val in subs.items():
_, client_id, bar = col_name
if bar == disc_consts.CLIENT_TAG:
continue
entry = json.loads(col_val)
r.append((col_name[2], entry['blob']))
return r
except pycassa.NotFoundException:
return None
# end lookup_subscription
# delete client subscription.
@cass_error_handler
def delete_subscription(self, service_type, client_id, service_id):
self._disco_cf.remove(service_type,
columns = [('client', client_id, service_id)])
self._disco_cf.remove(service_type,
columns = [('subscriber', service_id, client_id)])
# end
# mark client subscription for deletion in the future. If client never came
# back, entry would still get deleted due to TTL
@cass_error_handler
def mark_delete_subscription(self, service_type, client_id, service_id):
col_name = ('client', client_id, service_id)
x = self._disco_cf.get(service_type, columns = [col_name])
data = [json.loads(val) for col,val in x.items()]
entry = data[0]
entry['expired'] = True
self._disco_cf.insert(service_type, {col_name : json.dumps(entry)})
col_name = ('subscriber', service_id, client_id)
x = self._disco_cf.get(service_type, columns = [col_name])
data = [json.loads(val) for col,val in x.items()]
entry = data[0]
entry['expired'] = True
self._disco_cf.insert(service_type, {col_name : json.dumps(entry)})
# end
@cass_error_handler
def db_update_service_entry_oper_state(self):
col_name = ('service',)
data = self._disco_cf.get_range(column_start = col_name, column_finish = col_name)
for service_type, services in data:
for col_name in services:
(_, _, tag) = col_name
if tag != 'service-entry':
continue
col_value = services[col_name]
entry = json.loads(col_value)
if 'oper_state' not in entry:
entry['oper_state'] = 'up'
entry['oper_state_msg'] = ''
self._disco_cf.insert(service_type, {col_name : json.dumps(entry)})
self._debug['db_upd_oper_state'] += 1
@cass_error_handler
def db_update_service_entry_admin_state(self):
col_name = ('service',)
data = self._disco_cf.get_range(column_start = col_name, column_finish = col_name)
for service_type, services in data:
for col_name in services:
(_, service_id, tag) = col_name
if tag != 'service-entry':
continue
col_value = services[col_name]
entry = json.loads(col_value)
admin_state = entry.get('admin_state', None)
if admin_state and admin_state == "down":
self.set_admin_state(service_type, service_id, admin_state)
self._debug['db_upd_admin_state'] += 1
"""
read DSA rules
'dsa_rule_entry': {
u'subscriber': [{
u'service_type': u'',
u'service_id': u'',
u'service_prefix': {u'ip_prefix': u'2.2.2.2', u'ip_prefix_len': 32},
u'service_version': u''}],
u'publisher': {
u'service_type': u'',
u'service_id': u'',
u'service_prefix': {u'ip_prefix': u'1.1.1.1', u'ip_prefix_len': 32},
u'service_version': u''}
}
"""
def read_dsa_config(self):
try:
obj_type = 'discovery_service_assignment'
dsa_uuid = self.fq_name_to_uuid(
obj_type, ['default-discovery-service-assignment'])
(ok, dsa_obj) = self.object_read(obj_type, [dsa_uuid])
except Exception as e:
return None
dsa_rules = dsa_obj[0].get('dsa_rules', None)
if dsa_rules is None:
return None
result = []
for rule in dsa_rules:
(ok, rule_obj) = self.object_read('dsa_rule', [rule['uuid']])
entry = rule_obj[0]['dsa_rule_entry']
if entry:
result.append(entry)
return result