forked from vanthome/winston-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulk_writer.js
305 lines (286 loc) · 8.62 KB
/
bulk_writer.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
303
304
305
/* eslint no-underscore-dangle: ['error', { 'allow': ['_index', '_type'] }] */
const fs = require('fs');
const path = require('path');
const Promise = require('promise');
const debug = require('debug')('winston:elasticsearch');
const retry = require('retry');
const BulkWriter = function BulkWriter(transport, client, options) {
this.transport = transport;
this.client = client;
this.options = options;
this.interval = options.interval || 5000;
this.healthCheckTimeout = options.healthCheckTimeout || '30s';
this.healthCheckWaitForStatus = options.healthCheckWaitForStatus || 'yellow';
this.healthCheckWaitForNodes = options.healthCheckWaitForNodes || '>=1';
this.waitForActiveShards = options.waitForActiveShards || '1';
this.pipeline = options.pipeline;
this.retryLimit = options.retryLimit || 400;
this.bulk = []; // bulk to be flushed
this.running = false;
this.timer = false;
debug('created', this);
};
BulkWriter.prototype.start = function start() {
this.checkEsConnection(this.retryLimit);
debug('started');
};
BulkWriter.prototype.stop = function stop() {
this.running = false;
if (!this.timer) {
return;
}
clearTimeout(this.timer);
this.timer = null;
debug('stopped');
};
BulkWriter.prototype.schedule = function schedule() {
const thiz = this;
this.timer = setTimeout(() => {
thiz.tick();
}, this.interval);
};
BulkWriter.prototype.tick = function tick() {
debug('tick');
const thiz = this;
if (!this.running) {
return;
}
this.flush()
.then(() => {
// Emulate finally with last .then()
})
.then(() => {
// finally()
thiz.schedule();
});
};
BulkWriter.prototype.flush = function flush() {
// write bulk to elasticsearch
if (this.bulk.length === 0) {
debug('nothing to flush');
return new Promise((resolve) => {
// pause the buffering process when there's no more bulk to flush
// thus allowing the process to be terminated
this.running = false;
return resolve();
});
}
const bulk = this.bulk.concat();
this.bulk = [];
const body = [];
// eslint-disable-next-line object-curly-newline
bulk.forEach(({ index, doc, attempts }) => {
body.push({
[this.options.dataStream ? 'create' : 'index']: {
_index: index,
pipeline: this.pipeline
},
attempts
},
doc);
});
debug('bulk writer is going to write', body);
return this.write(body);
};
BulkWriter.prototype.append = function append(index, doc) {
if (this.options.buffering === true) {
if (
typeof this.options.bufferLimit === 'number'
&& this.bulk.length >= this.options.bufferLimit
) {
debug('message discarded because buffer limit exceeded');
// @todo: i guess we can use callback to notify caller
return;
}
this.bulk.unshift({
index,
doc,
attempts: 0
});
// resume the buffering process
if (!this.running) {
this.running = true;
this.tick();
}
} else {
this.write([
{ [this.options.dataStream ? 'create' : 'index']: { _index: index, pipeline: this.pipeline } },
doc
]);
}
};
BulkWriter.prototype.write = function write(body) {
const thiz = this;
const operation = [thiz.options.dataStream ? 'create' : 'index'];
debug('writing to ES');
return this.client
.bulk({
body,
waitForActiveShards: this.waitForActiveShards,
timeout: this.interval + 'ms',
})
.then((response) => {
const res = response.body;
if (res && res.errors && res.items) {
const err = new Error('Elasticsearch error');
res.items.forEach((item, itemIndex) => {
const bodyData = body[itemIndex * 2 + 1];
const opKey = Object.keys(item)[0];
if (item[opKey] && item[opKey].error) {
debug('elasticsearch indexing error', item[opKey].error);
thiz.options.internalLogger('elasticsearch indexing error', item[opKey].error, bodyData);
err.indexError = item[opKey].error;
err.causedBy = bodyData;
}
});
throw err;
}
})
.catch((e) => {
// rollback this.bulk array
const newBody = [];
body.forEach((chunk, index, chunks) => {
const { attempts, created } = chunk;
if (!created && attempts < thiz.retryLimit) {
newBody.push({
index: chunk[operation]._index,
doc: chunks[index + 1],
attempts: attempts + 1,
});
} else {
debug('retry attempts exceeded');
}
});
const lenSum = thiz.bulk.length + newBody.length;
if (thiz.options.bufferLimit && lenSum >= thiz.options.bufferLimit) {
thiz.bulk = newBody.concat(
thiz.bulk.slice(0, thiz.options.bufferLimit - newBody.length)
);
} else {
thiz.bulk = newBody.concat(thiz.bulk);
}
debug('error occurred during writing', e);
this.stop();
this.checkEsConnection(thiz.retryLimit)
.catch((err) => thiz.transport.emit('error', err));
thiz.transport.emit('warning', e);
thiz.bulk.forEach((bulk) => {
if (bulk.attempts === thiz.retryLimit) {
this.transport.emit('error', e);
}
});
});
};
BulkWriter.prototype.checkEsConnection = function checkEsConnection(retryLimit) {
const thiz = this;
thiz.esConnection = false;
const operation = retry.operation({
forever: false,
retries: retryLimit,
factor: 1,
minTimeout: 1000,
maxTimeout: 10 * 1000,
randomize: false
});
return new Promise((fulfill, reject) => {
operation.attempt((currentAttempt) => {
debug('checking for ES connection');
thiz.client.cluster.health({
timeout: thiz.healthCheckTimeout,
wait_for_nodes: thiz.healthCheckWaitForNodes,
wait_for_status: thiz.healthCheckWaitForStatus
})
.then(
(res) => {
thiz.esConnection = true;
const start = () => {
if (thiz.options.buffering === true) {
debug('starting bulk writer');
thiz.running = true;
thiz.tick();
}
};
// Ensure mapping template is existing if desired
if (thiz.options.ensureIndexTemplate) {
thiz.ensureIndexTemplate((res1) => {
fulfill(res1);
start();
}, reject);
} else {
fulfill(true);
start();
}
},
(err) => {
debug('re-checking for connection to ES');
if (operation.retry(err)) {
return;
}
thiz.esConnection = false;
debug('cannot connect to ES');
reject(new Error('Cannot connect to ES'));
}
);
});
});
};
BulkWriter.prototype.ensureIndexTemplate = function ensureIndexTemplate(
fulfill,
reject
) {
const thiz = this;
const indexPrefix = typeof thiz.options.indexPrefix === 'function'
? thiz.options.indexPrefix()
: thiz.options.indexPrefix;
// eslint-disable-next-line prefer-destructuring
let indexTemplate = thiz.options.indexTemplate;
if (indexTemplate === null || typeof indexTemplate === 'undefined') {
const rawdata = fs.readFileSync(
path.join(__dirname, 'index-template-mapping.json')
);
indexTemplate = JSON.parse(rawdata);
}
let templateName = indexPrefix;
if (thiz.options.dataStream) {
if (!thiz.options.index) {
// hm, has this to be a console error or better a throw? is it needed at all?
thiz.options.internalLogger('Error while deriving templateName with options', thiz.options);
} else {
templateName = thiz.options.index;
}
}
const tmplCheckMessage = {
name: 'template_' + templateName
};
debug('Checking tpl name', tmplCheckMessage);
thiz.client.indices.existsIndexTemplate(tmplCheckMessage).then(
(res) => {
if (res.statusCode && res.statusCode === 404) {
const tmplMessage = {
name: 'template_' + templateName,
create: true,
body: indexTemplate
};
thiz.client.indices.putIndexTemplate(tmplMessage).then(
(res1) => {
debug('Index template created successfully');
fulfill(res1.body);
},
(err1) => {
debug('Failed to create index template');
thiz.transport.emit('warning', err1);
reject(err1);
}
);
} else {
fulfill(res.body);
}
},
(res) => {
debug('Failed to check for index template');
thiz.transport.emit('warning', res);
reject(res);
}
);
};
module.exports = BulkWriter;