-
Notifications
You must be signed in to change notification settings - Fork 73
/
stm.ts
532 lines (473 loc) · 14.9 KB
/
stm.ts
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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import * as grpc from '@grpc/grpc-js';
import BigNumber from 'bignumber.js';
import * as Builder from './builder';
import { ClientRuntimeError, STMConflictError } from './errors';
import { Range } from './range';
import * as RPC from './rpc';
import { NSApplicator, toBuffer } from './util';
/**
* Isolation level which can be passed into the ISTMOptions.
*/
export const enum Isolation {
/**
* SerializableSnapshot provides serializable isolation and
* also checks for write conflicts.
*/
SerializableSnapshot,
/**
* Serializable reads within the same transaction attempt return data
* from the at the revision of the first read.
*/
Serializable,
/**
* RepeatableReads reads within the same transaction attempt always
* return the same data.
*/
RepeatableReads,
/**
* ReadCommitted reads keys from any committed revision.
*/
ReadCommitted,
}
/**
* ISTMOptions are optionally passed to `etcd3.stm(options)`.
*/
export interface ISTMOptions {
/**
* Number of times we'll retry the transaction if we get a conflict.
* Defaults to 3.
*/
retries: number;
/**
* WithPrefetch is a hint to prefetch a list of keys before trying to apply.
* If an STM transaction will unconditionally fetch a set of keys, prefetching
* those keys will save the round-trip cost from requesting
* each key one by one with `.get()`.
*/
prefetch: string[];
/**
* Isolation level for the transaction. Defaults to SerializableSnapshot.
*/
isolation: Isolation;
/**
* Options to pass into the STM transaction's commit.
*/
callOptions?: grpc.CallOptions;
}
/**
* Converts the key/value pair to a partial response that contains it. The
* response *will not* contain header or revision information.
*/
function keyValueToResponse(key: string | Buffer, value?: Buffer): RPC.IRangeResponse {
key = toBuffer(key);
if (!value) {
return { kvs: [], more: false, count: '0' } as any;
}
return {
kvs: [
{
key: Buffer.from(key),
value,
},
],
more: false,
count: '1',
} as any;
}
interface CompletedReads {
key: Buffer;
res: RPC.IRangeResponse;
}
/**
* ReadSet records a set of reads in a SoftwareTransaction.
*/
class ReadSet {
private readonly reads: Record<string, Promise<RPC.IRangeResponse> | undefined> =
Object.create(null);
private readonly completedReads: CompletedReads[] = [];
private earliestMod = new BigNumber(Infinity);
/**
* Returns the earliest modified revision of any key in this change set.
*/
public earliestModRevision(): BigNumber {
return this.earliestMod;
}
/**
* Add checks to the comparator to make sure that the mod revision of all
* keys read during the transaction are the same.
*/
public addCurrentChecks(cmp: Builder.ComparatorBuilder) {
this.completedReads.forEach(({ key, res }) => {
if (res.kvs.length) {
cmp.and(key, 'Mod', '==', res.kvs[0].mod_revision);
} else {
cmp.and(key, 'Mod', '==', 0);
}
});
}
/**
* runRequest sets read options and executes the outgoing request.
*/
public runRequest(kv: RPC.KVClient, req: RPC.IRangeRequest): Promise<RPC.IRangeResponse> {
const key = req.key!.toString();
const previous = this.reads[key];
if (previous) {
return previous;
}
const promise = kv.range(req).then(res => {
this.completedReads.push({ key: req.key!, res });
if (res.kvs.length > 0) {
this.earliestMod = BigNumber.min(new BigNumber(res.kvs[0].mod_revision), this.earliestMod);
}
return res;
});
this.reads[key] = promise;
return promise;
}
}
const enum WriteKind {
Write,
DeleteKey,
DeleteRange,
}
type WriteOp =
| { op: WriteKind.Write; req: RPC.IPutRequest }
| { op: WriteKind.DeleteKey; key: Buffer; req: RPC.IDeleteRangeRequest }
| { op: WriteKind.DeleteRange; range: Range; req: RPC.IDeleteRangeRequest };
/**
* WriteSet records a set of writes in a SoftwareTransaction.
*/
class WriteSet {
private readonly ops: WriteOp[] = [];
/**
* Add checks to make sure that none of the write tagets have changed since
* the given revision.
*/
public addNotChangedChecks(cmp: Builder.ComparatorBuilder, sinceBeforeMod: string) {
if (sinceBeforeMod === 'Infinity') {
return; // no reads were made
}
this.ops.forEach(op => {
switch (op.op) {
case WriteKind.Write:
cmp.and(op.req.key!, 'Mod', '<', sinceBeforeMod);
break;
case WriteKind.DeleteKey:
cmp.and(op.key, 'Mod', '<', sinceBeforeMod);
break;
case WriteKind.DeleteRange:
// error, no way to check that every single key in that range is the same
throw new Error(`You cannot delete ranges in the SerializableSnapshot isolation level`);
default:
throw new ClientRuntimeError(`Unexpected write op ${JSON.stringify(op)}`);
}
});
}
/**
* Adds the changed keys as consequents of the builder.
*/
public addChanges(cmp: Builder.ComparatorBuilder) {
const clauses: RPC.IRequestOp[] = [];
this.ops.forEach(op => {
switch (op.op) {
case WriteKind.Write:
clauses.push({ request_put: op.req });
break;
case WriteKind.DeleteKey:
clauses.push({ request_delete_range: op.req });
break;
case WriteKind.DeleteRange:
clauses.push({ request_delete_range: op.req });
break;
default:
throw new ClientRuntimeError(`Unexpected write op ${JSON.stringify(op)}`);
}
});
cmp.then(...clauses);
}
/**
* findExistingWrite returns an existing write (put or delete) against the key.
* Returns null if no operations against it were recorded.
*/
public findExistingWrite(key: Buffer): RPC.IRangeResponse | null {
for (let i = this.ops.length - 1; i >= 0; i--) {
const op = this.ops[i];
switch (op.op) {
case WriteKind.Write:
if (op.req.key!.equals(key)) {
return keyValueToResponse(key, op.req.value);
}
break;
case WriteKind.DeleteKey:
if (op.key.equals(key)) {
return keyValueToResponse(key);
}
break;
case WriteKind.DeleteRange:
if (op.range.includes(key)) {
return keyValueToResponse(key);
}
break;
default:
throw new ClientRuntimeError(`Unexpected write op ${JSON.stringify(op)}`);
}
}
return null;
}
/**
* Inserts a put operation into the set.
*/
public addPut(put: RPC.IPutRequest) {
this.purgeExistingOperationOn(put.key!);
this.ops.push({ op: WriteKind.Write, req: put });
}
/**
* Inserts a delete operation.
*/
public addDeletion(req: RPC.IDeleteRangeRequest) {
if (req.range_end) {
this.ops.push({ req, op: WriteKind.DeleteRange, range: new Range(req.key!, req.range_end) });
} else {
this.purgeExistingOperationOn(req.key!);
this.ops.push({ req, op: WriteKind.DeleteKey, key: req.key! });
}
}
private purgeExistingOperationOn(key: Buffer) {
for (let i = 0; i < this.ops.length; i++) {
const { op, req } = this.ops[i];
if (op === WriteKind.Write || op === WriteKind.DeleteKey) {
if (req.key!.equals(key)) {
this.ops.splice(i, 1);
break;
}
}
}
}
}
/**
* BasicTransaction is the base wrapper class for a transaction. It implements
* the necessary mechanics for Repeatablereads
* and ReadCommitted isolation levels.
*/
class BasicTransaction {
public readonly writeSet = new WriteSet();
public readonly readSet = new ReadSet();
constructor(protected readonly options: ISTMOptions) {}
/**
* Gets the range in a transaction-y way!
*/
public range(kv: RPC.KVClient, req: RPC.IRangeRequest): Promise<RPC.IRangeResponse> {
this.assertReadInvariants(req);
const existingWrite = this.writeSet.findExistingWrite(req.key!);
if (existingWrite !== null) {
return Promise.resolve(existingWrite);
}
req.serializable = true;
return this.readSet.runRequest(kv, req);
}
/**
* Schedules the put request in the writeSet.
*/
public put(req: RPC.IPutRequest): Promise<RPC.IPutResponse> {
this.assertNoOption('put', req, ['lease', 'prev_kv']);
this.writeSet.addPut(req);
return Promise.resolve({} as any);
}
/**
* Schedules the put request in the writeSet.
*/
public deleteRange(req: RPC.IDeleteRangeRequest): Promise<RPC.IDeleteRangeResponse> {
this.assertNoOption('delete', req, ['prev_kv']);
this.writeSet.addDeletion(req);
return Promise.resolve({
header: undefined as any,
deleted: '1',
prev_kvs: [],
});
}
protected assertReadInvariants(range: RPC.IRangeRequest) {
this.assertNoOption('read', range, [
'revision',
'range_end',
'min_mod_revision',
'max_mod_revision',
'min_create_revision',
'max_create_revision',
]);
}
protected assertNoOption<T>(req: string, obj: T, keys: (keyof T)[]) {
keys.forEach(key => {
if (obj[key] !== undefined) {
throw new Error(
`"${String(key)}" is not supported in ${req} requests within STM transactions`,
);
}
});
}
}
/**
* BasicTransaction is the class for serializable transactions. It implements
* the necessary mechanics for SerializableSnapshot
* and Serializable isolation levels.
*/
class SerializableTransaction extends BasicTransaction {
private firstRead: Promise<RPC.IRangeResponse> | null;
constructor(options: ISTMOptions, kv: RPC.KVClient) {
super(options);
options.prefetch.forEach(key => {
this.range(kv, { key: toBuffer(key) }).catch(() => undefined);
});
}
/**
* @override
*/
public range(kv: RPC.KVClient, req: RPC.IRangeRequest): Promise<RPC.IRangeResponse> {
this.assertReadInvariants(req);
const existingWrite = this.writeSet.findExistingWrite(req.key!);
if (existingWrite !== null) {
return Promise.resolve(existingWrite);
}
if (!this.firstRead) {
return (this.firstRead = this.readSet.runRequest(kv, req));
}
return this.firstRead.then(res => {
req.serializable = true;
req.revision = res.header.revision;
return this.readSet.runRequest(kv, req);
});
}
}
/**
* SoftwareTransaction is an implementation of software transaction memory,
* described in greater detail [here](https://coreos.com/blog/transactional-memory-with-etcd3.html).
* The idea is that you can have a block that contains multiple reads
* and writes, then we'll commit all those changes as a single transaction,
* automatically retrying as necessary.
*
* Inside the `.transact()` block, all reads and writes **must** go through
* the transaction, not the client, or they won't be tracked.
*
* For example, this is a safe way to move 'money' between `bank/account1` and
* `bank/account2`:
*
* ```js
* const amount = 42;
*
* etcd3.stm().transact(tx => {
* return Promise.all([
* tx.get('bank/account1').number(),
* tx.get('bank/account2').number(),
* ]).then(([balance1, balance2]) => {
* if (balance1 < amount) {
* throw new Error('You do not have enough money to transfer!');
* }
*
* return Promise.all([
* tx.put('bank/account1').value(balance1 - amount),
* tx.put('bank/account2').value(balance2 + amount),
* ]);
* });
* });
* ```
*
* (Note: the author does not condone using etcd for your banking infrastructure)
*/
export class SoftwareTransaction {
private readonly kv: RPC.KVClient;
private tx: BasicTransaction;
constructor(
private readonly options: ISTMOptions,
private readonly namespace: NSApplicator,
private readonly rawKV: RPC.KVClient,
) {
this.kv = new Proxy(rawKV, {
get: (target, key) => {
switch (key) {
case 'range':
return (req: RPC.IRangeRequest) => this.tx.range(target, req);
case 'put':
return (req: RPC.IPutRequest) => this.tx.put(req);
case 'deleteRange':
return (req: RPC.IDeleteRangeRequest) => this.tx.deleteRange(req);
default:
throw new ClientRuntimeError(`Unexpected kv operation in STM: ${key.toString()}`);
}
},
});
}
/**
* transact runs the function with the current configuration. It will be
* retried until the transaction succeeds, or until the maximum number of
* retries has been exceeded.
*/
public transact<T>(fn: (tx: this) => T | PromiseLike<T>): Promise<T> {
return this.transactInner(this.options.retries, fn);
}
/**
* `.get()` starts a query to look up a single key from etcd.
*/
public get(key: string): Builder.SingleRangeBuilder {
return new Builder.SingleRangeBuilder(this.kv, this.namespace, key);
}
/**
* `.put()` starts making a put request against etcd.
*/
public put(key: string | Buffer): Builder.PutBuilder {
return new Builder.PutBuilder(this.kv, this.namespace, key);
}
/**
* `.delete()` starts making a delete request against etcd.
*/
public delete(): Builder.DeleteBuilder {
return new Builder.DeleteBuilder(this.kv, this.namespace);
}
private transactInner<T>(retries: number, fn: (tx: this) => T | PromiseLike<T>): Promise<T> {
this.tx =
this.options.isolation === Isolation.Serializable ||
this.options.isolation === Isolation.SerializableSnapshot
? new SerializableTransaction(this.options, this.rawKV)
: new BasicTransaction(this.options);
return Promise.resolve(fn(this)).then(value => {
return this.commit()
.then(() => value)
.catch(err => {
if (retries === 0 || !(err instanceof STMConflictError)) {
throw err;
}
return this.transactInner(retries - 1, fn);
});
});
}
private commit(): Promise<void> {
const cmp = new Builder.ComparatorBuilder(this.rawKV, NSApplicator.default);
switch (this.options.isolation) {
case Isolation.SerializableSnapshot:
const earliestMod = this.tx.readSet.earliestModRevision().plus(1).toString();
this.tx.writeSet.addNotChangedChecks(cmp, earliestMod);
this.tx.readSet.addCurrentChecks(cmp);
break;
case Isolation.Serializable:
this.tx.readSet.addCurrentChecks(cmp);
break;
case Isolation.RepeatableReads:
this.tx.readSet.addCurrentChecks(cmp);
break;
case Isolation.ReadCommitted:
break; // none
default:
throw new Error(`Unknown isolation level "${this.options.isolation}"`);
}
this.tx.writeSet.addChanges(cmp);
return cmp
.options(this.options.callOptions)
.commit()
.then(result => {
if (!result.succeeded) {
throw new STMConflictError();
}
});
}
}