-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_db.py
executable file
·409 lines (346 loc) · 10.5 KB
/
node_db.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/env python3
# node_db.py - An SQLite3 database for node information
from enum import Enum
import sqlite3
# What the database is used for
DATABASE_CREATOR = "com/wirepas/otap-tool"
# Version of supported database schema
# NOTE: Version 0 didn't have a metadata table
SUPPORTED_SCHEMA_VERSION = 1
# Metadata table definition
CREATE_METADATA_TABLE_QUERY = """
CREATE TABLE metadata(
key TEXT PRIMARY KEY,
value TEXT
) WITHOUT ROWID;
"""
# Node information table definition
CREATE_NODES_TABLE_QUERY = """
CREATE TABLE nodes(
node_addr INT PRIMARY KEY,
last_seen INT,
phase INT,
node_role INT,
lock_status INT,
last_req INT,
last_resp INT,
last_info INT,
st_len INT,
st_crc INT,
st_seq INT,
st_type INT,
st_status INT,
fw_len INT,
fw_crc INT,
fw_seq INT,
fw_id INT,
fw_ver TEXT,
app_len INT,
app_crc INT,
app_seq INT,
app_id INT,
app_ver TEXT
);
"""
# Read metadata value
METADATA_READ_QUERY = """
SELECT * FROM metadata WHERE key = :key
"""
# Set metadata value
METADATA_WRITE_QUERY = """
INSERT INTO metadata VALUES (:key, :value)
"""
# Insert a new node, but keep or update old data if node already exists
INSERT_OR_UPDATE_QUERY = """
INSERT INTO nodes (
node_addr,
last_seen,
phase,
node_role,
lock_status,
last_req,
last_resp,
last_info,
st_len,
st_crc,
st_seq,
st_type,
st_status,
fw_len,
fw_crc,
fw_seq,
fw_id,
fw_ver,
app_len,
app_crc,
app_seq,
app_id,
app_ver
) VALUES (
:node_addr,
:last_seen,
:phase,
:node_role,
:lock_status,
:last_req,
:last_resp,
:last_info,
:st_len,
:st_crc,
:st_seq,
:st_type,
:st_status,
:fw_len,
:fw_crc,
:fw_seq,
:fw_id,
:fw_ver,
:app_len,
:app_crc,
:app_seq,
:app_id,
:app_ver
)
ON CONFLICT(node_addr) DO UPDATE SET
node_addr = coalesce(:node_addr, node_addr),
last_seen = coalesce(:last_seen, last_seen),
phase = coalesce(:phase, phase),
node_role = coalesce(:node_role, node_role),
lock_status = coalesce(:lock_status, lock_status),
last_req = coalesce(:last_req, last_req),
last_resp = coalesce(:last_resp, last_resp),
last_info = coalesce(:last_info, last_info),
st_len = coalesce(:st_len, st_len),
st_crc = coalesce(:st_crc, st_crc),
st_seq = coalesce(:st_seq, st_seq),
st_type = coalesce(:st_type, st_type),
st_status = coalesce(:st_status, st_status),
fw_len = coalesce(:fw_len, fw_len),
fw_crc = coalesce(:fw_crc, fw_crc),
fw_seq = coalesce(:fw_seq, fw_seq),
fw_id = coalesce(:fw_id, fw_id),
fw_ver = coalesce(:fw_ver, fw_ver),
app_len = coalesce(:app_len, app_len),
app_crc = coalesce(:app_crc, app_crc),
app_seq = coalesce(:app_seq, app_seq),
app_id = coalesce(:app_id, app_id),
app_ver = coalesce(:app_ver, app_ver)
"""
# Delete node from node information table
DELETE_QUERY = """
DELETE FROM nodes WHERE node_addr = :node_addr
"""
# Find a specific node in node information table
FIND_QUERY = """
SELECT * FROM nodes WHERE node_addr = :node_addr
"""
# Find a node with the oldest request time
FIND_OLDEST_REQ_QUERY = """
SELECT node_addr FROM nodes WHERE (
last_req is NULL OR
last_info is NULL OR
(:timeout != 0 AND :now - last_req >= :timeout)
)
ORDER BY last_req LIMIT 1
"""
# Iterate over all nodes in node information table
ITER_QUERY = """
SELECT * FROM nodes ORDER BY node_addr
"""
# Count number of nodes in node information table
COUNT_QUERY = """
SELECT COUNT(*) AS count FROM nodes
"""
# Statistics about sequence numbers
SEQ_INFO_QUERY = """
SELECT st_seq, COUNT(*) AS count FROM nodes GROUP BY st_seq
"""
class Phase(Enum):
"""Node phase values"""
INIT = 0
INFO_REQ = 1
LOCK_UNLOCK_REQ = 2
DONE = 3
class OtapLockStatus(Enum):
"""Node lock_status values"""
UNLOCKED = 0
UNLOCKED_KEY_SET = 1
UNLOCKED_BITS_SET = 2
LOCKED = 3
class _NodeIterator:
"""Iterator for node information"""
def __init__(self, conn):
self.conn = conn
def __iter__(self):
self.cursor = self.conn.execute(ITER_QUERY)
return self
def __next__(self):
row = self.res.fetchone()
if not row:
raise StopIteration
return row
class NodeDb:
"""SQLite3 node information database"""
def __init__(self, filename):
# Open an SQLite3 database
self.conn = sqlite3.connect(filename)
self.conn.row_factory = sqlite3.Row
self.transaction_cursor = None # No transaction open, yet
# Create tables, if the database is empty
self._create_tables()
# Check that the database is something this module can use
self._check_database_format()
def _create_tables(self):
# Get a list of tables in the database
cursor = self.conn.execute("SELECT * FROM sqlite_master WHERE type='table'")
tables = [r["name"] for r in cursor]
cursor.close()
if len(tables) > 0:
# Database not empty, do not create tables
return
# Create "metadata" and "nodes" tables
self.conn.execute(CREATE_METADATA_TABLE_QUERY).close()
self.conn.execute(CREATE_NODES_TABLE_QUERY).close()
# Populate "metadata" table
transaction_cursor = self.conn.execute("BEGIN TRANSACTION")
row = {"key": "creator", "value": DATABASE_CREATOR}
transaction_cursor.execute(METADATA_WRITE_QUERY, row)
row = {"key": "schema_version", "value": SUPPORTED_SCHEMA_VERSION}
transaction_cursor.execute(METADATA_WRITE_QUERY, row)
transaction_cursor.execute("COMMIT TRANSACTION")
def _check_database_format(self):
try:
# Get database creator
cursor = self.conn.execute(METADATA_READ_QUERY, {"key": "creator"})
db_creator = cursor.fetchone()["value"]
cursor.close()
if db_creator != DATABASE_CREATOR:
# Not the expected creator
raise ValueError
# Get the database schema version
cursor = self.conn.execute(METADATA_READ_QUERY, {"key": "schema_version"})
schema_version = int(cursor.fetchone()["value"])
cursor.close()
except (ValueError, TypeError, sqlite3.OperationalError):
raise ValueError("unsupported database schema") from None
if schema_version != SUPPORTED_SCHEMA_VERSION:
raise ValueError(
f"unsupported database schema version {schema_version}"
f", only version {SUPPORTED_SCHEMA_VERSION} supported"
)
def get_number_of_nodes(self):
cursor = self.conn.execute(COUNT_QUERY)
return (cursor.fetchone() or {"count": 0})[
"count"
] # Just in case None is returned
def find_node(self, node_addr):
cursor = self.conn.execute(FIND_QUERY, {"node_addr": node_addr})
row = cursor.fetchone()
if row is None:
return None
node_info = dict(row)
# Convert value to Enum
if node_info["phase"] is not None:
node_info["phase"] = Phase(node_info["phase"])
if node_info["lock_status"] is not None:
node_info["lock_status"] = OtapLockStatus(node_info["lock_status"])
return node_info
def find_node_oldest_req(self, now=0, timeout=0):
# Timeout may be zero or omitted, in which case only
# nodes with no info are considered
cursor = self.conn.execute(
FIND_OLDEST_REQ_QUERY, {"now": now, "timeout": timeout}
)
row = cursor.fetchone()
if row is None:
return None
return row["node_addr"]
def seq_info(self):
cursor = self.conn.execute(SEQ_INFO_QUERY)
seqs = {}
for row in cursor:
seqs[row["st_seq"]] = row["count"]
return seqs
def iterator(self):
# return _NodeIterator(self.conn)
return self.conn.execute(ITER_QUERY)
def open_transaction(self):
if self.transaction_cursor:
raise ValueError("transaction already open")
self.transaction_cursor = self.conn.execute("BEGIN TRANSACTION")
def add_or_update_node(
self,
node_addr,
last_seen=None,
phase=None,
node_role=None,
lock_status=None,
last_req=None,
last_resp=None,
last_info=None,
st_len=None,
st_crc=None,
st_seq=None,
st_type=None,
st_status=None,
fw_len=None,
fw_crc=None,
fw_seq=None,
fw_id=None,
fw_ver=None,
app_len=None,
app_crc=None,
app_seq=None,
app_id=None,
app_ver=None,
):
if not self.transaction_cursor:
raise ValueError("no transaction open")
# Convert Enum to value
if phase is not None:
phase = phase.value
if lock_status is not None:
lock_status = lock_status.value
values = {
"node_addr": node_addr,
"last_seen": last_seen,
"phase": phase,
"node_role": node_role,
"lock_status": lock_status,
"last_req": last_req,
"last_resp": last_resp,
"last_info": last_info,
"st_len": st_len,
"st_crc": st_crc,
"st_seq": st_seq,
"st_type": st_type,
"st_status": st_status,
"fw_len": fw_len,
"fw_crc": fw_crc,
"fw_seq": fw_seq,
"fw_id": fw_id,
"fw_ver": fw_ver,
"app_len": app_len,
"app_crc": app_crc,
"app_seq": app_seq,
"app_id": app_id,
"app_ver": app_ver,
}
# Create a new row or update existing row with any given non-NULL values
self.transaction_cursor.execute(INSERT_OR_UPDATE_QUERY, values)
def delete_node(self, node_addr):
if not self.transaction_cursor:
raise ValueError("no transaction open")
self.transaction_cursor.execute(DELETE_QUERY, {"node_addr": node_addr})
def commit(self):
if not self.transaction_cursor:
return
# Commit
self.transaction_cursor.execute("COMMIT TRANSACTION")
self.transaction_cursor = None # Transaction committed
def cancel(self):
if not self.transaction_cursor:
return
# Cancel
self.transaction_cursor.execute("CANCEL TRANSACTION")
self.transaction_cursor = None # Transaction cancelled