-
Notifications
You must be signed in to change notification settings - Fork 124
/
satellite.proto
624 lines (523 loc) · 19.4 KB
/
satellite.proto
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
/*
This file defines protobuf protocol for Satellite <> Electric replication
In this document there is a notation of the Client/Server and
Producer/Consumer which are used to annotate messages.
This protocol uses a custom RPC implementation that allows bidirectional RPC calls (usually the client
calls the server, but the server makes some RPC calls to the client too) and non-RPC messages.
Any RPC call should be done as an `SatRpcRequest` message, with `message` field being a method-appropriate
encoded message from this protocol. The answering side should then respond with `SatRpcResponse` with the same
method and request id. If RPC call fully failed, the `error` field will be filled. Otherwise, the message field
will be field, which may or may not have its own internal error fields.
Any message not wrapped in rpc request or response should not warrant a direct response from the other side.
*/
syntax = "proto3";
package Electric.Satellite;
/* RPC request transport message, must be used to implement service RPC calls in the protocol */
message SatRpcRequest {
string method = 1;
uint32 request_id = 2;
bytes message = 3;
}
/* RPC response transport message, must be used to implement service RPC calls in the protocol */
message SatRpcResponse {
string method = 1;
uint32 request_id = 2;
oneof result {
bytes message = 3;
SatErrorResp error = 4;
}
}
/* Main RPC service that the Electric server fulfills */
service Root {
rpc authenticate (SatAuthReq) returns (SatAuthResp);
rpc startReplication (SatInStartReplicationReq) returns (SatInStartReplicationResp);
rpc stopReplication (SatInStopReplicationReq) returns (SatInStopReplicationResp);
rpc subscribe (SatSubsReq) returns (SatSubsResp);
rpc unsubscribe (SatUnsubsReq) returns (SatUnsubsResp);
}
/* RPC calls that the server makes to the client */
service ClientRoot {
rpc startReplication (SatInStartReplicationReq) returns (SatInStartReplicationResp);
rpc stopReplication (SatInStopReplicationReq) returns (SatInStopReplicationResp);
}
enum SatAuthHeader {
reserved 1;
// Required by the Protobuf spec.
UNSPECIFIED = 0;
}
message SatAuthHeaderPair {
SatAuthHeader key = 1;
string value = 2;
}
// (Client) Auth request
//
// Client request is the first request that the client should send before
// executing any other request.
message SatAuthReq {
// Identity of the Satellite application. Is expected to be something like
// UUID. Required field
string id = 1;
// Authentication token, auth method specific, required
string token = 2;
// Headers, required
repeated SatAuthHeaderPair headers = 3;
}
// (Server) Auth response
message SatAuthResp {
// Identity of the Server
string id = 1;
// Headers optional
repeated SatAuthHeaderPair headers = 3;
}
// General purpose error message, that could be sent to any request from any
// side. FIXME: We might want to separate that into Client/Server parts.
message SatErrorResp {
enum ErrorCode {
INTERNAL = 0;
AUTH_REQUIRED = 1;
AUTH_FAILED = 2;
REPLICATION_FAILED = 3;
INVALID_REQUEST = 4;
PROTO_VSN_MISMATCH = 5;
SCHEMA_VSN_MISMATCH = 6;
}
ErrorCode error_type = 1;
// lsn of the txn that caused the problem, if available
optional bytes lsn = 2;
// human readable explanation of what went wrong
optional string message = 3;
}
// (Consumer) Starts replication stream from producer to consumer
message SatInStartReplicationReq {
enum Option {
reserved 1, 2, 3, 4;
// Required by the Protobuf spec.
NONE = 0;
}
reserved 3;
// LSN position of the log on the producer side
bytes lsn = 1;
repeated Option options = 2;
// the subscriptions identifiers the client wants to resume subscription
repeated string subscription_ids = 4;
// The version of the most recent migration seen by the client.
optional string schema_version = 5;
/**
* List of transaction IDs for which the client
* observed additional data before disconnect
*/
repeated uint64 observed_transaction_data = 6;
// Note:
// - a client might resume replication only for a subset of previous subscriptions
// in case the application cancelled some subscriptions while disconnected from the
// server. The server is responsible to keep/gc the remaining subscriptions.
// - the server must ensure that if the client skips a portion of the replication
// stream, the client is not able to read data it no longer has access to.
}
// (Producer) The result of the start replication requests
message SatInStartReplicationResp {
// returned in case replication fails to start
optional ReplicationError err = 1;
/** How many unacked transactions the producer is willing to send */
optional uint32 unacked_window_size = 2;
// Error returned by the Producer when replication fails to start
message ReplicationError {
// error code enum
enum Code {
// Required by the Protobuf spec.
CODE_UNSPECIFIED = 0;
// requested LSN is behind the current replication window
BEHIND_WINDOW = 1;
// e.g. jumping ahead of the subscriptions cursor
INVALID_POSITION = 2;
// requested subscription not found
SUBSCRIPTION_NOT_FOUND = 3;
// the replication request has malformed LSN
MALFORMED_LSN = 4;
// consumer requested replication at schema version that is
// not known to the producer
UNKNOWN_SCHEMA_VSN = 5;
}
// error code
Code code = 1;
// a human-readable description of the error
string message = 2;
}
}
// (Consumer) Request to stop replication
message SatInStopReplicationReq {
}
// (Producer) Acknowledgement that replication has been stopped
message SatInStopReplicationResp {
}
message SatRelationColumn {
string name = 1;
string type = 2;
bool primaryKey = 3;
bool is_nullable = 4;
}
message SatRelation {
enum RelationType {
TABLE = 0;
INDEX = 1;
VIEW = 2;
TRIGGER = 3;
}
string schema_name = 1;
RelationType table_type = 2;
string table_name = 3;
// Volatile identity defined at the start of the replication protocol may or
// may not be persisted is used in SatTransOp operations, to indicate
// relation the operation is working on.
uint32 relation_id = 4;
repeated SatRelationColumn columns = 5;
}
// Time granularity.
// -----------------
// commit timestamps have microseconds granularity
// (Producer) Type defines replication messages, that flow from Producer once
// the replication is established. Message contains operations log. Operations
// should go in the LSN order. Begin and Commit operations corresponds to
// transaction boundaries.
// Transactions are guranteed not to be mixed, and will follow one by one.
message SatOpLog {
repeated SatTransOp ops = 1;
}
/**
* Acknowledgement message that the transaction with given LSN has been incorporated by the client.
* Sent by the consumer and used by the producer to regulate garbage collection & backpressure.
* Clients that don't send it after a certain number of transactions will be considered non-responsive
* and the producer may choose to pause sending further information to such a client.
*
* It's also important the the producer may deny connection requests from clients who try to connect with
* LSN number less than the most recently acknowledged one, as the acknowledgement may have caused a
* cleanup of information for this client before this point in time.
*/
message SatOpLogAck {
/** Timestamp on the sending side */
uint64 ack_timestamp = 1;
/** LSN of the most recent incorporated transaction */
bytes lsn = 2;
/** Transaction ID of the most recent incorporated transaction */
uint64 transaction_id = 3;
/** Subscription IDs for data that was received immediately after this transaction */
repeated string subscription_ids = 4;
/** Transaction IDs for which additional data was received immediately after this transaction */
repeated uint64 additional_data_source_ids = 5;
}
// (Producer) Single operation, should be only send as part of the SatOplog
// message
message SatTransOp {
oneof op {
SatOpBegin begin = 1;
SatOpCommit commit = 2;
SatOpUpdate update = 3;
SatOpInsert insert = 4;
SatOpDelete delete = 5;
SatOpMigrate migrate = 6;
SatOpCompensation compensation = 7;
SatOpGone gone = 8;
SatOpAdditionalBegin additional_begin = 9;
SatOpAdditionalCommit additional_commit = 10;
}
}
// (Producer) Replication message that indicates transaction boundaries
// should be only send as payload in the SatTransOp message
message SatOpBegin {
reserved 2;
uint64 commit_timestamp = 1;
// Lsn position that points to first data segment of transaction in the
// WAL
bytes lsn = 3;
// Globally unique id of the source that transaction originated from. For
// data coming from Satellite this field is ignored. For data coming from
// Electric this field can be used to deduce if the incoming transaction
// originated on this Satellite instance or not.
optional string origin = 4;
// does this transaction contain ddl statements?
bool is_migration = 5;
/**
* If not 0, a transient reference for additional data pseudo-transaction
* that will be sent at a later point in the stream. It may be shared by multiple transactions
* sent by the server at the same time, because this additional data will be queried at the same
* time. Duplicated on SatOpCommit.
*/
uint64 additional_data_ref = 6;
/** Unique transaction ID, sent only by the server. No guarantees of monotonicity. */
optional uint64 transaction_id = 7;
}
// (Producer) Replication message that indicates a transaction boundary for additional data that existed on the server
// but the client can now see
message SatOpAdditionalBegin {
uint64 ref = 1;
}
// (Producer) Replication message that indicates transaction boundaries
// should be only send as payload in the SatTransOp message
message SatOpCommit {
reserved 2;
uint64 commit_timestamp = 1;
bytes lsn = 3;
/** If not 0, a transient reference for additional data pseudo-transaction
that will be sent at a later point in the stream. It may be shared by multiple transactions
sent by the server at the same time, because this additional data will be queried at the same
time. Duplicated on SatOpBegin.
*/
uint64 additional_data_ref = 4;
/** Unique transaction ID, sent only by the server. No guarantees of monotonicity. */
optional uint64 transaction_id = 5;
}
message SatOpAdditionalCommit {
uint64 ref = 1;
}
// (Producer) Data manipulation message, that only should be part of the
// SatTransOp message
message SatOpInsert {
uint32 relation_id = 1;
SatOpRow row_data = 2;
// dependency information
repeated string tags = 3;
}
// (Producer) Data manipulation message, that only should be part of the
// SatTransOp message
message SatOpUpdate {
uint32 relation_id = 1;
SatOpRow row_data = 2;
SatOpRow old_row_data = 3;
// dependency information
repeated string tags = 4;
}
// (Producer) Data manipulation message, that only should be part of the
// SatTransOp message
message SatOpDelete {
uint32 relation_id = 1;
SatOpRow old_row_data = 2;
// dependency information
repeated string tags = 3;
}
message SatOpCompensation {
uint32 relation_id = 1;
SatOpRow pk_data = 2;
// dependency information
repeated string tags = 4;
}
message SatOpGone {
uint32 relation_id = 1;
SatOpRow pk_data = 2;
}
// Dependency information for row data.
// ------------------------------------
//
// Tags in a row is a set of strings, where each tag is textual representation
// of conacatenation of `origin` and `commit_timestamp`, that can be obtained
// from SatOpBegin message.
// Format: origin@commit_timestamp
// Example: fcb57d30-95ce-4d75-b295-c08c7a1e75d8@1674210220785123
// Message that corresponds to the single row.
message SatOpRow {
bytes nulls_bitmask = 1;
// values may contain binaries with size 0 for NULLs and empty values
// check nulls_bitmask to differentiate between the two
repeated bytes values = 2;
}
// A migration message, originating in Postgres, captured via event triggers,
// propagated to electric, converted from postgres to the equivalent sqlite
// statement and inserted into the replication stream
//
// Each migration message includes the sql strings to execute on the satellite
// client as well as metadata information about the resulting structure of the
// changed tables.
message SatOpMigrate {
enum Type {
CREATE_TABLE = 0;
CREATE_INDEX = 1;
ALTER_ADD_COLUMN = 6;
}
message Stmt {
Type type = 1;
string sql = 2;
}
message PgColumnType {
// the pg type name, e.g. int4, char
string name = 1;
// array dimensions, or [] for scalar types
// e.g. for a column declared as int4[][3], size = [-1, 3]
repeated int32 array = 2;
// any size information, e.g. for varchar(SIZE) or [] for no size
repeated int32 size = 3;
}
message Column {
string name = 1;
string sqlite_type = 2;
PgColumnType pg_type = 3;
}
message ForeignKey {
// the columns in the child table that point to the parent
repeated string fk_cols = 1;
// the parent table
string pk_table = 2;
// the cols in the parent table
repeated string pk_cols = 3;
}
message Table {
string name = 1;
repeated Column columns = 2;
repeated ForeignKey fks = 3;
repeated string pks = 4;
}
// the migration version as specified by the developer and put into
// the postgresql migration as an electric function call
string version = 1;
// a list of sql ddl statements to apply, converted from the pg originals
// The migration machinery converts an `ALTER TABLE action1, action2, action3;`
// query into a set of 3: `ALTER TABLE action1; ALTER TABLE action2,` etc
// so we need to support 1+ statements for every migration event.
repeated Stmt stmts = 2;
// The resulting table definition after applying these migrations
// (a DDL statement can only affect one table at a time).
optional Table table = 3;
}
// (Consumer) Request for new subscriptions
message SatSubsReq {
// a client-generated identifier to track the subscription
string subscription_id = 1;
// Shape requests
repeated SatShapeReq shape_requests = 2;
}
// (Producer) Response for a subscription request
message SatSubsResp {
// identifier of the subscription this response refers to
string subscription_id = 1;
// the error details if the request failed
optional SatSubsError err = 2;
// Error message returned by the Producer when it encounters
// an error handling subscription request
message SatSubsError {
// error code enum
enum Code {
// Required by the Protobuf spec.
CODE_UNSPECIFIED = 0;
// DUPLICATE IDENTIFIER
SUBSCRIPTION_ID_ALREADY_EXISTS = 1;
// Error requesting shape
SHAPE_REQUEST_ERROR = 2;
}
// error code
Code code = 2;
// A human-readable description of the error
string message = 3;
// Details of the shape request error
repeated ShapeReqError shape_request_error = 4;
// Shape request error
message ShapeReqError {
// error code enum
enum Code {
// Required by the Protobuf spec.
CODE_UNSPECIFIED = 0;
// Table does not exist in current schema version
TABLE_NOT_FOUND = 1;
// Requested shape does not maintain referential integirty
REFERENTIAL_INTEGRITY_VIOLATION = 2;
// The shape request contains an empty shape definition
EMPTY_SHAPE_DEFINITION = 3;
// Attempt to request the same table more than once in one shape
DUPLICATE_TABLE_IN_SHAPE_DEFINITION = 4;
// Malformed WHERE clause on a table
INVALID_WHERE_CLAUSE = 5;
// Specified include tree does not match known schema
INVALID_INCLUDE_TREE = 6;
}
// error code
Code code = 1;
// a human-readable description of the error
string message = 2;
// the shape request identifier that this error refers to
string request_id = 3;
}
}
}
// (Consumer) Request to cancel subscriptions
message SatUnsubsReq {
// Identifiers of the subscriptions
repeated string subscription_ids = 1;
}
// (Producer) Acknowledgment that the subscriptions were cancelled
message SatUnsubsResp {
}
// Shape request
message SatShapeReq {
// Identifier of the request
string request_id = 1;
// The shape definition
SatShapeDef shape_definition = 2;
}
// Top-level structure of a shape definition
message SatShapeDef {
message Relation {
repeated string foreign_key = 1;
Select select = 2;
}
// Select structure
message Select {
// table name for this select
string tablename = 1;
string where = 2;
repeated Relation include = 3;
}
// Selects for the Shape definition
repeated Select selects = 1;
}
// Error message returned by the Producer when it encounters
// an error handling subscription data
message SatSubsDataError {
// error code enum
enum Code {
// Required by the Protobuf spec.
CODE_UNSPECIFIED = 0;
// Error delivering shape
SHAPE_DELIVERY_ERROR = 1;
}
// error code
Code code = 1;
// A human-readable description of the error
string message = 2;
// Subscription identifier this error refers to
string subscription_id = 3;
// Details of the shape request error
repeated ShapeReqError shape_request_error = 4;
// Shape request error
message ShapeReqError {
// error code enum
enum Code {
// Required by the Protobuf spec.
CODE_UNSPECIFIED = 0;
// Requested shape exceed the maximum allowed shape size
SHAPE_SIZE_LIMIT_EXCEEDED = 1;
}
// error code
Code code = 1;
// a human-readable description of the error
string message = 2;
// the shape request identifier that this error refers to
string request_id = 3;
}
}
// Begin delimiter for the incoming subscription data
message SatSubsDataBegin {
// Identifier of the subscription
string subscription_id = 1;
// LSN at which this data is being sent. May be a duplicate of a transaction that was sent immediately before.
bytes lsn = 2;
}
// End delimiter for the incoming subscription data
message SatSubsDataEnd {
}
// Begin delimiter for the initial shape data
message SatShapeDataBegin {
// Identifier of the request
string request_id = 1;
// The UUID of the shape on the Producer
string uuid = 2;
}
// End delimiter for the initial shape data
message SatShapeDataEnd {
}