-
Notifications
You must be signed in to change notification settings - Fork 2
/
requeueFailedCRRCronJob.js
294 lines (278 loc) · 9.4 KB
/
requeueFailedCRRCronJob.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
const {
waterfall, doWhilst, eachLimit, retry,
} = require('async');
const http = require('http');
const { http: httpArsn } = require('httpagent');
const { Producer } = require('node-rdkafka');
const { scheduleJob } = require('node-schedule');
const { errors } = require('arsenal');
const VID_SEP = require('arsenal').versioning.VersioningConstants
.VersionId.Separator;
const { Logger } = require('werelogs');
const BackbeatClient = require('./BackbeatClient');
const log = new Logger('s3utils::requeueFailedCRRCronJob');
const {
CLOUDSERVER_ENDPOINT,
BACKBEAT_API_ENDPOINT,
ACCESS_KEY,
SECRET_KEY,
SITE_NAME,
KAFKA_HOSTS,
KAFKA_TOPIC,
} = process.env;
const CRON_RULE = process.env.CRON_RULE || '0 */6 * * *';
if (!CLOUDSERVER_ENDPOINT) {
throw new Error('CLOUDSERVER_ENDPOINT not defined');
}
if (!BACKBEAT_API_ENDPOINT) {
throw new Error('BACKBEAT_API_ENDPOINT not defined');
}
if (!ACCESS_KEY) {
throw new Error('ACCESS_KEY not defined');
}
if (!SECRET_KEY) {
throw new Error('SECRET_KEY not defined');
}
if (!SITE_NAME) {
throw new Error('missing SITE_NAME environment variable, must be set to'
+ ' the value of "site" property in the CRR configuration');
}
if (!KAFKA_HOSTS) {
throw new Error('KAFKA_HOSTS not defined');
}
if (!KAFKA_TOPIC) {
throw new Error('KAFKA_TOPIC not defined');
}
const PRODUCER_MESSAGE_MAX_BYTES = 5000020;
const PRODUCER_RETRY_DELAY_MS = 5000;
const PRODUCER_MAX_RETRIES = 60;
const PRODUCER_POLL_INTERVAL_MS = 2000;
const bbOptions = {
accessKeyId: ACCESS_KEY,
secretAccessKey: SECRET_KEY,
endpoint: CLOUDSERVER_ENDPOINT,
region: 'us-east-1',
sslEnabled: false,
s3ForcePathStyle: true,
apiVersions: { s3: '2006-03-01' },
signatureVersion: 'v4',
signatureCache: false,
httpOptions: {
timeout: 0,
agent: new httpArsn.Agent({ keepAlive: true }),
},
};
const bb = new BackbeatClient(bbOptions);
const producer = new Producer({
'metadata.broker.list': KAFKA_HOSTS,
'message.max.bytes': PRODUCER_MESSAGE_MAX_BYTES,
}, {
});
let requeueInProgress = false;
let stopRequest = false;
function _requeueObject(bucket, key, versionId, counters, cb) {
/* eslint-disable no-param-reassign */
if (stopRequest) {
return process.nextTick(cb);
}
return waterfall([
// get object blob
next => bb.getMetadata({
Bucket: bucket,
Key: key,
VersionId: versionId,
}, (err, mdRes) => {
if (err) {
log.error('error getting metadata of object', {
bucket,
key,
error: err.message,
});
++counters.errors;
}
next(err, mdRes);
}),
(mdRes, next) => {
const objMD = JSON.parse(mdRes.Body);
if (!objMD.replicationInfo
|| objMD.replicationInfo.status !== 'FAILED') {
log.info('skipping object: not FAILED', {
bucket,
key,
versionId,
status: objMD.replicationInfo
? objMD.replicationInfo.status : 'NEW',
});
++counters.skipped;
return next();
}
const objSize = objMD['content-length'];
// reset to PENDING
objMD.replicationInfo.status = 'PENDING';
objMD.replicationInfo.backends[0].status = 'PENDING';
const mdBlob = JSON.stringify(objMD);
// create an entry as if coming from the raft log
const entry = JSON.stringify({
type: 'put',
bucket,
key: `${key}${VID_SEP}${objMD.versionId}`,
value: mdBlob,
});
return retry({
times: PRODUCER_MAX_RETRIES,
interval: PRODUCER_RETRY_DELAY_MS,
}, attemptDone => {
try {
producer.produce(
KAFKA_TOPIC,
null, // partition
Buffer.from(entry), // value
`${bucket}/${key}`, // key (for keyed partitioning)
Date.now(), // timestamp
null,
);
log.info('requeued object version for replication', {
bucket,
key,
versionId,
});
++counters.requeued;
counters.requeuedBytes += objSize;
return attemptDone();
} catch (err) {
log.error('error producing entry to kafka, retrying', {
bucket,
key,
error: err.message,
});
return attemptDone(err);
}
}, err => {
if (err) {
log.error(
'give up producing entry to kafka after retries',
{ bucket, key, error: err.message },
);
++counters.errors;
}
next(err);
});
},
], err => {
if (err) {
log.error(
'error in _requeueObject waterfall',
{ error: err.message },
);
}
return cb();
});
/* eslint-enable no-param-reassign */
}
function _requeueAll() {
log.info('starting requeuing task');
const counters = {
requeued: 0,
requeuedBytes: 0,
skipped: 0,
errors: 0,
};
let marker;
requeueInProgress = true;
doWhilst(
batchDone => {
log.info('requeuing task progress', counters);
let url = `${BACKBEAT_API_ENDPOINT}/_/crr/failed?sitename=${SITE_NAME}`;
if (marker !== undefined) {
url += `&marker=${marker}`;
}
const failedReq = http.request(url, res => {
const bodyChunks = [];
res.on('data', chunk => bodyChunks.push(chunk));
res.on('error', err => {
log.error('error receiving response body for the list of failed CRR', {
error: err.message,
});
return batchDone(err);
});
res.on('end', () => {
const body = Buffer.concat(bodyChunks).toString();
if (res.statusCode !== 200) {
log.error('request to retrieve list of failed CRR returned HTTP error status', {
errorCode: res.statusCode,
error: body,
});
return batchDone(errors.InternalError);
}
let result;
try {
result = JSON.parse(body);
} catch (err) {
log.error('invalid response: not JSON');
return batchDone(errors.InternalError);
}
marker = result.NextMarker;
return eachLimit(
result.Versions,
10,
(version, objDone) => _requeueObject(version.Bucket, version.Key, version.VersionId, counters, objDone),
() => batchDone(null, result.IsTruncated),
);
});
});
failedReq.on('error', err => {
log.error('error sending request to retrieve list of failed CRR', {
error: err.message,
});
return batchDone(err);
});
failedReq.end();
},
isTruncated => isTruncated && !stopRequest,
() => {
requeueInProgress = false;
if (stopRequest) {
log.info('aborted requeuing task', counters);
producer.disconnect();
} else {
log.info('completed requeuing task', counters);
}
},
);
}
let cronJob = null;
producer.connect();
producer.on('ready', () => {
producer.setPollInterval(PRODUCER_POLL_INTERVAL_MS);
log.info('process is ready', { cronRule: CRON_RULE });
cronJob = scheduleJob(CRON_RULE, _requeueAll);
});
producer.on('event.error', error => {
// This is a bit hacky: the "broker transport failure"
// error occurs when the kafka broker reaps the idle
// connections every few minutes, and librdkafka handles
// reconnection automatically anyway, so we ignore those
// harmless errors (moreover with the current
// implementation there's no way to access the original
// error code, so we match the message instead).
if (!['broker transport failure',
'all broker connections are down']
.includes(error.message)) {
log.error('error with producer', {
error: error.message,
});
}
});
function stop(signal) {
log.info('received signal, exiting', { signal });
if (cronJob) {
cronJob.cancel();
}
if (requeueInProgress) {
stopRequest = true;
} else {
producer.disconnect();
}
}
process.on('SIGTERM', () => stop('SIGTERM'));
process.on('SIGINT', () => stop('SIGINT'));