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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
Wire proto
----------
# all in network byte-order
PktHeader
.id u32 // autoincrement on ask (?)
.code u16 // defined by seqno if register(...) call in Packets
.length u32 // whole pkt length
# response_code = 0x8000 | code
List
.len u32
[len]items
Dict
.len u32
[len] key, value
Enum
.value s32 // None -> -1
String
.len u32
[len] strdata
Address
.host String
.port u16 // present only if .host != ''
Bool
.value u8
Number
.value u32
Index
.value u64
PTID
.value u64 // 0 <-> None
Protocol
Number // encode <- version, decode -> check version
Checksum
.checksum [20]u8
UUID
.uuid s32 // 0 <-> None
TID
.tid [8]byte // None <-> \xff*8
POID = PTID
Float
.value float64 // '\xff' * 8 if None
~~~~~~~~
NodeList []
type NodeType
address Address
uuid UUID
state NodeState
id_timestamp Float
CellList []
uuid UUID
state CellState
RowList []
offset Number // u32
cell_list CellList
HistoryList []
serial TID
size Number // u32
UUIDList []
uuid UUID
TidList []
tid TID
OidList []
oid OID
~~~~~~~~
Notify
.message String
Error
.code Number
.message String
Ping
ø
_answer = PFEmpty
CloseClient
ø
RequestIdentification
.protocol_version PProtocol
.node_type NodeType
.uuid UUID
.address Address
.name String
.id_timestamp Float
TODO
Storage related messages
------------------------
NotifyReady S -> M
Recovery PM -> S S -> PM
LastIDs PM -> S S -> PM
PartitionTable PM -> S S -> PM
NotifyPartitionTable PM -> S, C
PartitionChanges PM -> S, C // subset of NotifyPartitionTable (?)
StartOperation PM -> S
StopOperation PM -> S
UnfinishedTransactions S -> PM PM -> S
LockedTransactions PM -> S S -> PM
FinalTID * -> S , C -> PM (?)
ValidateTransaction PM -> S
NotifyTransactionFinished M -> S
LockInformation PM -> S S -> PM
UnlockInformation PM -> S
StoreObject C -> S
.oid OID
.serial TID // original serial
.data String
.data_serial TID
.tid tid // current txn id
.unlock bool
AnswerStoreObject S -> C
.conflicting bool
.oid OID
.serial TID
GetObject C -> S
.oid
.serial
.tid
AnswerGetObject S -> C
.oid OID
.serial_start TID
.serial_end TID
.data String
.data_serial TID
AbortTransaction C -> S, PM
.tid
StoreTransaction C -> S S -> C
.tid TID
user, description, ext String
oidv []OID
VoteTransaction C -> S S -> C
.tid
TIDList C -> S
first, last Index // u64 [first, last)
partition Number // u32
AnswerTIDList S -> C
tidv []TID
TIDListFrom C -> S
min_tid, max_tid TID
length Number
partition Number
AnswerTIDListFrom S -> C
tidv []TID
TransactionInformation * -> S
tid TID
AnswerTransactionInformation S -> *
tid TID
user, description, extension String
packed bool
oidv []OID
ObjectHistory C -> S
oid OID
first, last Index // [first, last]
AnswerObjectHistory
oid OID
historyv []strict{serial TID; size Number}
ObjectUndoSerial C -> S
tid TID
ltid TID // ?
undone_tid TID // ?
oidv []OID
AnswerObjectUndoSerial S -> C
{} oid ->
.current_serial TID
.undo_serial TID
.is_current bool
HasLock C -> S
tid TID
oid OID
AnswerHasLock S -> C
oid OID
lock_state LockState // not_locket, granted, granted_to_other:w
CheckCurrentSerial C -> S AnswerCheckCurrentSerial S -> C
tid TID conflicting bool
serial TID oid OID
oid OID serial TID
Pack C -> M -> S
tid TID
CheckPartition M -> S
partition Number
upstream_name String
address Address
min_tid, max_tid TID
CheckTIDRange S -> S AnswerCheckTIDRange S -> S
partition Number count Number
length Number checksum Checksum
min_tid, max_tid TID max_tid TID
CheckSerialRange S -> S
ParitionCorrupted S -> M
partition Number
uuidv []UUID
~~~~~~~~
(?)
NotifyNodeIntormation
NodeInformation
TODO
Storage communications
----------------------
* bootstrap (connect to primary master):
>M RequestIdentification
<M AcceptIdentification (AnswerRequestIdentification)
* init (under master command go through recovery / verification)
(BaseMasterHandler)
<M AskRecovery
>M AnswerRecovery (ptid, backup_tid, truncate_tid)
<M AskPartitionTable
>M AnswerPartitionTable (ptid, []PtRow)
# neoctl start
<M NotifyNodeInformation (S1.state=RUNNING)
# S: "I was told I'm RUNNING"
<M NotifyClusterInformation (state=VERIFYING)
<M NotifyNodeInformation (S1.state=RUNNING) # again, why?
# S: "I was told I'm RUNNING"
<M NotifyPartitionTable (ptid=1, `node 0: S1, R`)
# S saves PT info locally
# M asks about unfinished transactions
<M AskLockedTransactions
>M AnswerLockedTransactions {} ttid -> tid # in example we have empty
<M AskLastIDs
>M AnswerLastIDs (last_oid, last_tid)
<M NotifyClusterInformation (state=RUNNING)
<M StartOperation
>M NotifyReady
--- also in base/init handlers:
<M StopOperation
<M AskFinalTID
# >M answerUnfinishedTransactions (only upon S request)
# <M ReelectPrimary ?
<M Truncate
<M Validatetransactions
* operation (serve clients):
Tables
------
- config
.name str
.value str
(name, nid, partitions, ptid, replicas, version, zodb=pickle...)
- pt
.rid int // = row id = part of oid space
.nid int
.state tinyint // = cell state
pkey (rid, nid)
# committed txns
- trans
.partition smallint
.tid bigint
.packed bool
.oids mediumblob // []oid
.user blob
.description blob
.ext blob
.ttid bigint // XXX ?
pkey (partition, tid)
# committed object metadata
- obj
.partition smallint
.oid bigint
.tid bigint
.data_id bigint | NULL // -> data.id
.value_tid bigint | NULL // XXX ? (у нас NULL)
pkey (partition, tid, oid)
key (partition, oid, tid)
key data_id
# object data
- data
.id bigint
.hash sha1 // UNIQUE (hash, compression)
.compression tinyint
.value mediumblob
key id
key (hash, compression) // <- from UNIQUE ^^^
- (bigdata)
# uncommitted transactions
# (= trans)
- ttrans
.partition smallint
.tid bigint
.packed bool
.oids mediumblob // []oid
.user blob
.description blob
.ext blob
ttid bigint // XXX ?
# uncommitted object metadata
# (= obj)
- tobj
Partition Table (general & current-py)
--------------------------------------
#Np (how-many partitions) #R (replication factor)
Cell
.node (-> .uuid, .addr)
.state
.backup_tid # last tid this cell has all data for
.replicating # currently replicating up to this (.replicating) tid
PT
.id↑
.partition_list [#Np] of []Cell
.count_dict {} node -> #node_used_in_pt
Pt
+-+
| |
+-+ +----------+ +------------+ +-----+
| | |node,state| |node2,state2| |cell3| ...
+-+ +----------+ +------------+ +-----+
Np | |
+-+
| |
+-+ oid -> PTentry (as PT[oid % Np]
| | tid
+-+
----------------------------------------
BaseApplication
.em EventManager
.nm NodeManager
(master) Application < BaseApplication
.tm (master) TransactionManager
.pt (master) PartitionTable
.listening_conn ListeningConnection
.primary bool # "am I primary"
.primary_master_node ?
.cluster_state
.current_manager None | RecoveryManager | VerificationManager
.backup_app BackupApplication
.backup_tid
.truncate_tid
.admin_handler AdministrationHandler
.secondary_master_handler SecondaryMasterHandler
.client_service_handler ClientServiceHandler
.storage_service_handler StorageServiceHandler
(storage) Application < BaseApplication
.tm (storage) TransactionManager
.dm DatabaseManager (Importer | MySQL | SQLite)
.pt ? PartitionTable
.checker Checker # checks sha1([]tid -- XXX only tid list, not other metadata & data)
.replicator Replicator
.listening_conn ListeningConnection
.master_conn
.master_node
# (queued events are executed at some txn boundaries & locks)
.event_queue deque (key, callable, msg_id, conn, args)
.event_queue_dict {} key -> count(.event_queue, key)
.task_queue deque
.operational bool
.ready bool # when .operational=T and "got all informations"
Handlers
--------
AdministrationHandler < MasterHandler
.connectionLost(...)
.setClusterSate(...)
.setNodeState(...)
.addPendingNodes(...)
.tweakPartitionTable(...)
.truncate(conn, tid)
.checkReplicas(...)
SecondaryMasterHandler < MasterHandler
.connectionLost(...)
.announcePrimary(...)
.reelectPrimary(...)
ClientServiceHandler < MasterHandler
.connectionLost(...)
.askNodeInformation(conn) # send informations about master and storages only
.askBeginTransaction(conn, tid)
.askNewOIDs(conn, num_oids)
.askFinalTID(conn, ttid)
.askPack(conn, tid)
.abortTransaction(conn, tid)
StorageServiceHandler < BaseServiceHandler
.connectionCompleted(conn, new)
.connectionLost(conn, new_state)
.askUnfinishedTransactions(conn)
.answerInformationLocked(conn, ttid)
.notifyPartitionCorrupted(conn, partition, cell_list)
.notifyReplicationDone(conn, offset, tid)
.answerPack(conn, status)
MasterHandler < EventHandler
.connectionCompleted(...)
.requestIdentification(...)
.askClusterState(...)
.askRecovery(...)
.askLastIDs(...)
.askLastTransaction(...)
.askNodeInformation(...)
.askPartitionTable(...)
BaseServiceHandler < MasterHandler
.connectionCompleted(...)
.connectionLost(...)
.notifyReady(conn)
EventHandler
.packetReceived(conn, pkt, **kw)
-> .dispatch(conn, pkt, **kw)
-> conn.setPeerId(pkt.getId())
-> getattr(self, pkt.handler_method_name) (conn, *pkt.decode(), **kw)
except ...:
reply error
IdentificationHandler < MasterHandler
Connections
-----------
ListeningConnection < BaseConnection
.__init__(self, app, handler, addr):
connector = self.ConnectorClass(addr)
BaseConnection.__init__(self, app, connector, addr)
connector.makeListeningConnection() # ~= self.socket.bind(addr) + listen()
self.em.register(self)
.readable(self):
connector, addr = self.connector.accept()
conn = ServerConnection(self.em, self._handlers.getHandler(), connector, addr)
conn._connected()
BaseConnection
.ConnectorClass = SocketConnector # by default
.em EventManager
.connector self.ConnectorClass() # by default ?
.addr
._handlers HandlerSwitcher
HandlerSwitcher
# pending handlers and related requests
._pending [] of [{}(msg_id -> answer_class, timeout, on_timeout, kw), handler]
.emit(request, timeout, on_timeout, kw)
.handle(connection, pkt)
-> ._handle(connection, pkt)
._handle(connection, pkt):
msg_id = pkt.getId()
request_dict, handler = ._pending[0]
if !pkt.isResponse():
handler.packetReceived(connection, pkt)
klass, _, _, kw = request_pop(msg_id)
if klass && isinstance(pkt, klass) || pkt.isError():
handler.packetReceived(connection, packet, kw)
else:
# unexpected answer
# skip all intermediate handlers and apply to "latest" (fff6cf45)
while len(._pending) > 1 && !._pending[0].handler:
del ._pending[0]
ServerConnection <Connection
KEEP_ALICE = Connection.KEEP_ALIVE + 5
server = True
# main worker class for handling data exchanging
Connection <BaseConnection
.read_buf ReadBuffer ([][]byte = iovec)
._queue []
._parser_state ParserState
.writable()
-> .connector.send()
.readable()
-> .connector.receive(.read_buf)
while .read_buf:
pkt = Packets.parse(.read_buf, ._parser_state)
._queue.append(pkt)
return bool(._queue)
# called from under EventManager.poll() # incoming messages
.process()
-> ._handlers.handle(self, ._queue.pop(0))
._addPacket(pkt):
.connector.queue(pkt.encode())
.em.addWriter(self)
.notify(pkt):
pkt.setId(next-msg-id)
._addPacket(pkt)
.ask(pkt, timeout=30, on_timeout=None, **kw):
.notify(pkt)
._handlers.emit(pkt, timeout, on_timeout, kw)
.answer(pkt, msg_id=None):
pkt.setId(msgid ?: self.getPeerId())
._addPacket(pkt)
------------
EventManager
.connection_dict {} fd -> Connection
.reader_set set
.writer_set set
.epoll
._pending_processing []Connection # fetched from .epoll
.register(conn)
.unregister(conn, close=False)
.poll(blocking=1):
._poll(blocking)
to_process = ._pending_processing.pop(0)
to_process.process()
._poll(blocking):
event_list = .epoll.poll(blocking)
for fd, event in event_list:
conn = .connection_dict[fd]
if event & EPOLLIN:
if conn.readable():
._addPendingConnection(conn) # queues to ._pending_processing
# EPOLLOUT -> wlist -> conn.writable()
# EPOLLERR -> elist -> conn.readable() -> ._addPendingConnection(conn)
.addReader(conn)
.removeReader(conn)
.addWriter(conn)
.removeWriter(conn)
--------
# M waits for S* to connect so PT is filled
RecoveryManager < MasterHandler
...
# Finish all transactions for which we know that tpc_finish was called but not fully processed
#
# ( abort(?) / commit half-way finished transactions (commit if we know tpc_finish was started)
# XXX no actual abort happens since .verifyData():
# Transaction not locked. No need to tell nodes to delete it,
# since they drop any unfinished data just before being
# operational. )
VerificationManager < BaseServiceHandler
...