-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathReplicationStatusUpdater.js
More file actions
431 lines (411 loc) · 17.5 KB
/
ReplicationStatusUpdater.js
File metadata and controls
431 lines (411 loc) · 17.5 KB
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
const {
doWhilst, eachSeries, eachLimit, waterfall, series,
} = require('async');
const werelogs = require('werelogs');
const { ObjectMD } = require('arsenal').models;
const { setupClients } = require('./clients');
const LOG_PROGRESS_INTERVAL_MS = 10000;
class ReplicationStatusUpdater {
/**
* @param {Object} params - An object containing the configuration parameters for the instance.
* @param {Array<string>} params.buckets - An array of bucket names to process.
* @param {Array<string>} params.replicationStatusToProcess - Replication status to be processed.
* @param {number} params.workers - Number of worker threads for processing.
* @param {string} params.accessKey - Access key for AWS SDK authentication.
* @param {string} params.secretKey - Secret key for AWS SDK authentication.
* @param {string} params.endpoint - Endpoint URL for the S3 service.
* @param {Object} log - The logging object used for logging purposes within the instance.
*
* @param {string} [params.siteName] - (Optional) Name of the destination site.
* @param {string} [params.storageType] - (Optional) Type of the destination site (aws_s3, azure...).
* @param {string} [params.targetPrefix] - (Optional) Prefix to target for replication.
* @param {number} [params.listingLimit] - (Optional) Limit for listing objects.
* @param {number} [params.maxUpdates] - (Optional) Maximum number of updates to perform.
* @param {number} [params.maxScanned] - (Optional) Maximum number of items to scan.
* @param {string} [params.keyMarker] - (Optional) Key marker for resuming object listing.
* @param {string} [params.versionIdMarker] - (Optional) Version ID marker for resuming object listing.
*/
constructor(params, log) {
const {
buckets,
replicationStatusToProcess,
workers,
accessKey,
secretKey,
endpoint,
siteName,
storageType,
targetPrefix,
listingLimit,
maxUpdates,
maxScanned,
keyMarker,
versionIdMarker,
} = params;
// inputs
this.buckets = buckets;
this.replicationStatusToProcess = replicationStatusToProcess;
this.workers = workers;
this.accessKey = accessKey;
this.secretKey = secretKey;
this.endpoint = endpoint;
this.siteName = siteName;
this.storageType = storageType;
this.targetPrefix = targetPrefix;
this.listingLimit = listingLimit;
this.maxUpdates = maxUpdates;
this.maxScanned = maxScanned;
this.inputKeyMarker = keyMarker;
this.inputVersionIdMarker = versionIdMarker;
this.log = log;
this._setupClients();
this.logProgressInterval = setInterval(this._logProgress.bind(this), LOG_PROGRESS_INTERVAL_MS);
// intenal state
this._nProcessed = 0;
this._nSkipped = 0;
this._nUpdated = 0;
this._nErrors = 0;
this._bucketInProgress = null;
this._VersionIdMarker = null;
this._KeyMarker = null;
}
/**
* Sets up and initializes the S3 and Backbeat client instances.
*
* @returns {void} This method does not return a value; instead, it sets the S3 and Backbeat clients.
*/
_setupClients() {
const { s3, bb } = setupClients({
accessKey: this.accessKey,
secretKey: this.secretKey,
endpoint: this.endpoint,
}, this.log);
this.s3 = s3;
this.bb = bb;
}
/**
* Logs the progress of the CRR process at regular intervals.
* @private
* @returns {void}
*/
_logProgress() {
this.log.info('progress update', {
updated: this._nUpdated,
skipped: this._nSkipped,
errors: this._nErrors,
bucket: this._bucketInProgress || null,
keyMarker: this._KeyMarker || null,
versionIdMarker: this._VersionIdMarker || null,
});
}
/**
* Determines if an object should be updated based on its replication metadata properties.
* @private
* @param {ObjectMD} objMD - The metadata of the object.
* @param {string} site - The destination site name.
* @returns {boolean} True if the object should be updated.
*/
_objectShouldBeUpdated(objMD, site) {
return this.replicationStatusToProcess.some(filter => {
if (filter === 'NEW') {
// Either site specific replication info is missing
// or are initialized with empty fields.
return (!objMD.getReplicationInfo()
|| !objMD.getReplicationSiteStatus(site));
}
return (objMD.getReplicationInfo()
&& objMD.getReplicationSiteStatus(site) === filter);
});
}
/**
* Marks an object as pending for replication.
* @private
* @param {string} bucket - The bucket name.
* @param {string} key - The object key.
* @param {string} versionId - The object version ID.
* @param {string} storageClass - The storage class for replication.
* @param {Object} repConfig - The replication configuration.
* @param {Function} cb - Callback function.
* @returns {void}
*/
_markObjectPending(
bucket,
key,
versionId,
storageClass,
repConfig,
cb,
) {
let objMD;
let skip = false;
return waterfall([
// get object blob
next => this.bb.getMetadata({
Bucket: bucket,
Key: key,
VersionId: versionId,
}, next),
(mdRes, next) => {
// NOTE: The Arsenal Object Metadata schema version 8.1 is being used for both Ring S3C and Artesca,
// it is acceptable because the 8.1 schema only adds extra properties to the 7.10 schema.
// This is beneficial because:
// - Forward compatibility: Having the 8.1 properties in place now ensures that
// S3C is compatible with the 8.1 schema, which could be useful if we plan to upgrade
// from 7.10 to 8.1 in the future.
// - No impact on current functionality: The extra properties from the 8.1
// schema do not interfere with the current functionalities of the 7.10 environment,
// so there is no harm in keeping them. S3C should ignore them without causing any issues.
// - Simple codebase: Not having to remove these properties simplifies the codebase of s3utils.
// Less complexity and potential errors linked with conditionally removing metadata properties
// based on the version.
// - Single schema approach: Keeping a single, unified schema approach in s3utils can make the
// codebase easier to maintain and upgrade, as opposed to having multiple branches or versions of
// the code for different schema versions.
objMD = new ObjectMD(JSON.parse(mdRes.Body));
if (!this._objectShouldBeUpdated(objMD, storageClass)) {
skip = true;
return process.nextTick(next);
}
// Initialize replication info, if missing
// This is particularly important if the object was created before
// enabling replication on the bucket.
let replicationInfo = objMD.getReplicationInfo();
if (!replicationInfo || !replicationInfo.status) {
const { Rules, Role } = repConfig;
const destination = Rules[0].Destination.Bucket;
// set replication properties
const ops = objMD.getContentLength() === 0 ? ['METADATA']
: ['METADATA', 'DATA'];
replicationInfo = {
status: 'PENDING',
content: ops,
backends: [],
destination,
storageClass: '',
role: Role,
storageType: '',
};
objMD.setReplicationInfo(replicationInfo);
}
// Update replication info with site specific info
if (!objMD.getReplicationSiteStatus(storageClass)) {
// When replicating to multiple destinations,
// the storageClass and storageType properties
// become comma-separated lists of the storage
// classes and types of the replication destinations.
const storageClasses = objMD.getReplicationStorageClass()
? `${objMD.getReplicationStorageClass()},${storageClass}` : storageClass;
objMD.setReplicationStorageClass(storageClasses);
if (this.storageType) {
const storageTypes = objMD.getReplicationStorageType()
? `${objMD.getReplicationStorageType()},${this.storageType}` : this.storageType;
objMD.setReplicationStorageType(storageTypes);
}
// Add site to the list of replication backends
const backends = objMD.getReplicationBackends();
backends.push({
site: storageClass,
status: 'PENDING',
dataStoreVersionId: '',
});
objMD.setReplicationBackends(backends);
}
objMD.setReplicationSiteStatus(storageClass, 'PENDING');
objMD.setReplicationStatus('PENDING');
objMD.updateMicroVersionId();
const md = objMD.getSerialized();
return this.bb.putMetadata({
Bucket: bucket,
Key: key,
VersionId: versionId,
ContentLength: Buffer.byteLength(md),
Body: md,
}, next);
},
], err => {
++this._nProcessed;
if (err) {
++this._nErrors;
this.log.error('error updating object', {
bucket, key, versionId, error: err.message,
});
cb();
return;
}
if (skip) {
++this._nSkipped;
} else {
++this._nUpdated;
}
cb();
});
}
/**
* Lists object versions for a bucket.
* @private
* @param {string} bucket - The bucket name.
* @param {string} VersionIdMarker - The version ID marker for pagination.
* @param {string} KeyMarker - The key marker for pagination.
* @param {Function} cb - Callback function.
* @returns {void}
*/
_listObjectVersions(bucket, VersionIdMarker, KeyMarker, cb) {
return this.s3.listObjectVersions({
Bucket: bucket,
MaxKeys: this.listingLimit,
Prefix: this.targetPrefix,
VersionIdMarker,
KeyMarker,
}, cb);
}
/**
* Marks pending replication for listed object versions.
* @private
* @param {string} bucket - The bucket name.
* @param {Array} versions - Array of object versions.
* @param {Function} cb - Callback function.
* @returns {void}
*/
_markPending(bucket, versions, cb) {
const options = { Bucket: bucket };
waterfall([
next => this.s3.getBucketReplication(options, (err, res) => {
if (err) {
this.log.error('error getting bucket replication', { error: err });
return next(err);
}
return next(null, res.ReplicationConfiguration);
}),
(repConfig, next) => {
const { Rules } = repConfig;
const storageClass = this.siteName || Rules[0].Destination.StorageClass;
if (!storageClass) {
const errMsg = 'missing SITE_NAME environment variable, must be set to'
+ ' the value of "site" property in the CRR configuration';
this.log.error(errMsg);
return next(new Error(errMsg));
}
if (!this.siteName) {
this.log.warn(`missing SITE_NAME environment variable, triggering replication to the ${storageClass} storage class`);
}
return eachLimit(versions, this.workers, (i, apply) => {
const { Key, VersionId } = i;
this._markObjectPending(bucket, Key, VersionId, storageClass, repConfig, apply);
}, next);
},
], cb);
}
/**
* Triggers CRR process on a specific bucket.
* @private
* @param {string} bucketName - The name of the bucket.
* @param {Function} cb - Callback function.
* @returns {void}
*/
_triggerCRROnBucket(bucketName, cb) {
const bucket = bucketName.trim();
this._bucketInProgress = bucket;
this.log.info(`starting task for bucket: ${bucket}`);
if (this.inputKeyMarker || this.inputVersionIdMarker) {
// resume from where we left off in previous script launch
this._KeyMarker = this.inputKeyMarker;
this._VersionIdMarker = this.inputVersionIdMarker;
this.inputKeyMarker = undefined;
this.inputVersionIdMarker = undefined;
this.log.info(`resuming bucket: ${bucket} at: KeyMarker=${this._KeyMarker} `
+ `VersionIdMarker=${this._VersionIdMarker}`);
}
return doWhilst(
done => this._listObjectVersions(
bucket,
this._VersionIdMarker,
this._KeyMarker,
(err, data) => {
if (err) {
this.log.error('error listing object versions', { error: err });
return done(err);
}
return this._markPending(bucket, data.Versions.concat(data.DeleteMarkers), err => {
if (err) {
return done(err);
}
this._VersionIdMarker = data.NextVersionIdMarker;
this._KeyMarker = data.NextKeyMarker;
return done();
});
},
),
async () => {
if (this._nUpdated >= this.maxUpdates || this._nProcessed >= this.maxScanned) {
this._logProgress();
let remainingBuckets;
if (this._VersionIdMarker || this._KeyMarker) {
// next bucket to process is still the current one
remainingBuckets = this.buckets.slice(
this.buckets.findIndex(bucket => bucket === bucketName),
);
} else {
// next bucket to process is the next in bucket list
remainingBuckets = this.buckets.slice(
this.buckets.findIndex(bucket => bucket === bucketName) + 1,
);
}
let message = 'reached '
+ `${this._nUpdated >= this.maxUpdates ? 'update' : 'scanned'} `
+ 'count limit, resuming from this '
+ 'point can be achieved by re-running the script with '
+ `the bucket list "${remainingBuckets.join(',')}"`;
if (this._VersionIdMarker || this._KeyMarker) {
message += ' and the following environment variables set: '
+ `KEY_MARKER=${this._KeyMarker} `
+ `VERSION_ID_MARKER=${this._VersionIdMarker}`;
}
this.log.info(message);
return false;
}
if (this._VersionIdMarker || this._KeyMarker) {
return true;
}
return false;
},
err => {
this._bucketInProgress = null;
if (err) {
this.log.error('error marking objects for crr', { bucket });
cb(err);
return;
}
this._logProgress();
this.log.info(`completed task for bucket: ${bucket}`);
cb();
},
);
}
/**
* Runs the CRR process on all configured buckets.
* @param {Function} cb - Callback function.
* @returns {void}
*/
run(cb) {
return eachSeries(this.buckets, this._triggerCRROnBucket.bind(this), err => {
clearInterval(this.logProgressInterval);
if (err) {
cb(err);
return;
}
cb();
});
}
/**
* Stops the execution of the CRR process.
* NOTE: This method terminates the node.js process, and hence it does not return a value.
* @returns {void}
*/
stop() {
this.log.warn('stopping execution');
this._logProgress();
clearInterval(this.logProgressInterval);
process.exit(1);
}
}
module.exports = ReplicationStatusUpdater;