-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathConsumer.js
302 lines (277 loc) · 9.92 KB
/
Consumer.js
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
const stream = require('stream');
const fs = require('fs');
const path = require('path');
const { assert, ensureDirectory } = require('./util');
const Storage = require('./Storage/ReadableStorage');
const MAX_CATCHUP_BATCH = 10;
/**
* Safely unlink a file and ignore if it doesn't exist.
* @param {string} filename
*/
const safeUnlink = (filename) => {
/* istanbul ignore next */
try {
fs.unlinkSync(filename);
} catch (e) {
if (e.code !== "ENOENT") {
throw e;
}
}
};
/**
* Implements an event-driven durable Consumer that provides at-least-once delivery semantics or exactly-once processing semantics if only using setState().
*/
class Consumer extends stream.Readable {
/**
* @param {Storage} storage The storage to create the consumer for.
* @param {string} indexName The name of the index to consume.
* @param {string} identifier The unique name to identify this consumer.
* @param {object} [initialState] The initial state of the consumer.
* @param {number} [startFrom] The revision to start from within the index to consume.
*/
constructor(storage, indexName, identifier, initialState = {}, startFrom = 0) {
super({ objectMode: true });
assert(storage instanceof Storage, 'Must provide a storage for the consumer.');
assert(typeof indexName === 'string' && indexName !== '', 'Must specify an index name for the consumer.');
assert(typeof identifier === 'string' && identifier !== '', 'Must specify an identifier name for the consumer.');
this.initializeStorage(storage, indexName, identifier);
this.restoreState(initialState, startFrom);
this.handler = this.handleNewDocument.bind(this);
this.on('error', () => (this.handleDocument = false));
}
/**
* @private
* @param {Storage} storage The storage to create the consumer for.
* @param {string} indexName The name of the index to consume.
* @param {string} identifier The unique name to identify this consumer.
*/
initializeStorage(storage, indexName, identifier) {
this.storage = storage;
this.index = this.storage.openIndex(indexName);
this.indexName = indexName;
const consumerDirectory = path.join(this.storage.indexDirectory, 'consumers');
this.fileName = path.join(consumerDirectory, this.storage.storageFile + '.' + indexName + '.' + identifier);
if (ensureDirectory(consumerDirectory)) {
this.cleanUpFailedWrites();
}
}
/**
* Iterate over all files in the directory of this consumer and unlink any file that starts with the filename followed by a dot.
* @private
*/
cleanUpFailedWrites() {
const consumerNamePrefix = path.basename(this.fileName) + '.';
const consumerDirectory = path.dirname(this.fileName);
const files = fs.readdirSync(consumerDirectory);
for (let file of files) {
if (file.startsWith(consumerNamePrefix)) {
safeUnlink(path.join(consumerDirectory, file));
}
}
}
/**
* @private
* @param {object} initialState The initial state if no persisted state exists.
* @param {number} startFrom The revision to start from within the index to consume.
*/
restoreState(initialState, startFrom) {
/* istanbul ignore if */
if (!this.fileName) {
return;
}
if (typeof initialState === 'number') {
startFrom = initialState;
initialState = {};
}
try {
const consumerData = fs.readFileSync(this.fileName);
this.position = consumerData.readInt32LE(0);
this.state = JSON.parse(consumerData.toString('utf8', 4));
} catch (e) {
this.position = startFrom;
this.state = initialState;
}
Object.freeze(this.state);
this.persisting = null;
this.consuming = false;
}
/**
* Update the state of this consumer transactionally with the position.
* May only be called from within the document handling callback.
*
* @param {object|function(object):object} newState
* @param {boolean} [persist] Set to false if this state update should not be persisted yet
* @api
*/
setState(newState, persist = true) {
assert(this.handleDocument, 'Called setState outside of document handler!');
if (typeof newState === 'function') {
newState = newState(this.state);
}
this.state = Object.freeze(newState);
this.doPersist = persist;
}
/**
* Handler method that is supposed to be triggered for each new document in the storage.
*
* @private
* @param {string} name The name of the index the document was added for.
* @param {number} position The 1-based position inside the index that the document was added to.
* @param {object} document The document that was added.
*/
handleNewDocument(name, position, document) {
if (name !== this.indexName) {
return;
}
/* istanbul ignore if */
if (this.position !== position - 1) {
return;
}
this.handleDocument = true;
this.once('data', () => (this.handleDocument = false));
if (!this.push(document)) {
this.stop();
}
this.position = position;
if (this.doPersist) {
this.persist();
}
}
/**
* Persist current state of this consumer.
* This will write the current position and state to the consumer storage file.
*
* @private
*/
persist() {
if (this.persisting) {
return;
}
this.persisting = setImmediate(() => {
const consumerState = JSON.stringify(this.state);
const consumerData = Buffer.allocUnsafe(4 + consumerState.length);
consumerData.writeInt32LE(this.position, 0);
consumerData.write(consumerState, 4, consumerState.length, 'utf-8');
const tmpFile = this.fileName + '.' + this.position;
this.persisting = null;
/* istanbul ignore if */
if (fs.existsSync(tmpFile)) {
throw new Error(`Trying to update consumer ${this.name} concurrently. Keep each single consumer within a single process.`);
}
try {
fs.writeFileSync(tmpFile, consumerData);
// If the write fails (half-way), the consumer state file will not be corrupted
fs.renameSync(tmpFile, this.fileName);
this.emit('persisted', consumerState);
} catch (e) {
/* istanbul ignore next */
safeUnlink(tmpFile);
}
});
}
/**
* Check if this consumer has caught up. If so, register a handler for the stream and emit a 'caught-up' event.
*
* @private
* @returns {boolean} True if this consumer has caught up and can
*/
checkCaughtUp() {
if (this.index.length <= this.position) {
this.handleDocument = false;
this.storage.on('index-add', this.handler);
this.emit('caught-up');
return true;
}
return (this.consuming === false);
}
/**
* Consume (push) a number of documents and update the position record.
*
* @private
* @param {Array|Generator} documents The list or a stream of documents to consume
*/
consumeDocuments(documents) {
for (let document of documents) {
if (!this.push(document)) {
this.stop();
break;
}
++this.position;
}
}
/**
* Start consuming documents.
*
* This will also catch up from the last position in case new documents were added.
* @api
*/
start() {
if (this.isPaused()) {
this.resume();
}
if (this.consuming) {
return;
}
this.consuming = true;
this.handleDocument = true;
// Catch up to current index position
const catchUpBatch = () => {
setImmediate(() => {
if (this.checkCaughtUp()) {
return;
}
const maxBatchPosition = Math.min(this.position + MAX_CATCHUP_BATCH + 1, this.index.length);
const documents = this.storage.readRange(this.position + 1, maxBatchPosition, this.index);
this.consumeDocuments(documents);
this.once('persisted', () => catchUpBatch());
this.persist();
});
};
catchUpBatch();
}
/**
* Stop consuming new documents. Consuming can be started again at any time.
* @api
*/
stop() {
if (this.consuming) {
this.pause();
}
this.storage.removeListener('index-add', this.handler);
this.consuming = false;
this.handleDocument = false;
}
/**
* Reset this projection to restart processing all documents again.
* NOTE: This will overwrite the current state of the projection and hence be destructive.
* @param {object} [initialState] The initial state of the consumer.
* @param {number} [startFrom] The revision to start from within the index to consume.
* @api
*/
reset(initialState = {}, startFrom = 0) {
if (typeof initialState === 'number') {
startFrom = initialState;
initialState = {};
}
const restart = this.consuming;
this.stop();
this.state = Object.freeze(initialState);
this.position = startFrom;
this.persist();
if (restart) {
this.start();
}
}
// noinspection JSUnusedGlobalSymbols
/**
* Readable stream implementation.
* @private
*/
_read() {
if (this.isPaused()) {
return;
}
this.start();
}
}
module.exports = Consumer;