-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathLifecycleObjectTransitionProcessor.js
More file actions
302 lines (269 loc) · 11.7 KB
/
Copy pathLifecycleObjectTransitionProcessor.js
File metadata and controls
302 lines (269 loc) · 11.7 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
'use strict';
const assert = require('assert');
const async = require('async');
const { errors } = require('arsenal');
const ColdStorageStatusQueueEntry = require('../../../lib/models/ColdStorageStatusQueueEntry');
const { LifecycleMetrics } = require('../LifecycleMetrics');
const LifecycleObjectProcessor = require('./LifecycleObjectProcessor');
const LifecycleUpdateExpirationTask = require('../tasks/LifecycleUpdateExpirationTask');
const LifecycleUpdateTransitionTask = require('../tasks/LifecycleUpdateTransitionTask');
const LifecycleColdStatusArchiveTask = require('../tasks/LifecycleColdStatusArchiveTask');
const { LifecycleResetTransitionInProgressTask } =
require('../tasks/LifecycleResetTransitionInProgressTask');
const { updateCircuitBreakerConfigForImplicitOutputQueue } = require('../../../lib/CircuitBreaker');
const { LifecycleRetriggerRestoreTask } = require('../tasks/LifecycleRetriggerRestoreTask');
const BackbeatProducer = require('../../../lib/BackbeatProducer');
const GarbageCollectorProducer = require('../../gc/GarbageCollectorProducer');
const VaultClientWrapper = require('../../utils/VaultClientWrapper');
const { AccountIdCache } = require('../../utils/AccountIdCache');
const { authTypeAssumeRole } = require('../../../lib/constants');
class LifecycleObjectTransitionProcessor extends LifecycleObjectProcessor {
/**
* Constructor of LifecycleObjectProcessor
*
* @constructor
* @param {Object} zkConfig - zookeeper configuration object
* @param {String} zkConfig.connectionString - zookeeper connection string
* as "host:port[/chroot]"
* @param {Object} kafkaConfig - kafka configuration object
* @param {string} kafkaConfig.hosts - list of kafka brokers
* as "host:port[,host:port...]"
* @param {Object} [kafkaConfig.backlogMetrics] - param object to
* publish kafka topic metrics to zookeeper (see {@link
* BackbeatConsumer} constructor)
* @param {Object} lcConfig - lifecycle configuration object
* @param {String} lcConfig.auth - authentication info
* @param {String} lcConfig.objectTasksTopic - lifecycle object topic name
* @param {String} lcConfig.transitionTasksTopic - lifecycle transition topic name
* @param {Object} lcConfig.transitionProcessor - kafka consumer object
* @param {String} lcConfig.transitionProcessor.groupId - kafka
* consumer group id
* @param {Number} [lcConfig.transitionProcessor.concurrency] - number
* of max allowed concurrent operations
* @param {Object} s3Config - S3 configuration
* @param {Object} s3Config.host - s3 endpoint host
* @param {Number} s3Config.port - s3 endpoint port
* @param {String} [transport="http"] - transport method ("http"
* or "https")
* @param {Object} [vaultAdminConfig] - vault admin endpoint, used to
* resolve canonical ids into account ids
*/
constructor(zkConfig, kafkaConfig, lcConfig, s3Config, transport = 'http',
vaultAdminConfig = undefined) {
super(zkConfig, kafkaConfig, lcConfig, s3Config, transport);
const authConfig = this.getAuthConfig(this._lcConfig);
if (authConfig.type === authTypeAssumeRole) {
this.vaultClientWrapper = new VaultClientWrapper(
`lifecycle:${this.getProcessorType()}`,
vaultAdminConfig,
authConfig,
this._log,
);
this._accountIdCache = new AccountIdCache(
this._processConfig.concurrency);
}
}
/**
* Resolve the account id of a canonical id. Actions published by the
* lifecycle conductor already carry the account id; those published by the
* queue populator (pull replication) only know the canonical id.
* @param {String} ownerId - canonical id of the object owner
* @param {Logger} log - logger instance
* @param {Function} cb - callback: cb(err, accountId)
* @return {undefined}
*/
getAccountId(ownerId, log, cb) {
if (!this.vaultClientWrapper) {
log.debug('skipping: not assume role auth type');
return process.nextTick(cb);
}
// A cached miss must fail like a fresh lookup would: `isKnown()` is also
// true for misses, and `get()` would then hand back `undefined`.
if (this._accountIdCache.isMiss(ownerId)) {
log.error('canonical id does not exist (cached)', { ownerId });
return process.nextTick(cb, errors.NoSuchEntity);
}
if (this._accountIdCache.has(ownerId)) {
return process.nextTick(cb, null, this._accountIdCache.get(ownerId));
}
return this.vaultClientWrapper.getAccountId(ownerId, (err, accountId) => {
if (err) {
if (err.NoSuchEntity) {
log.error('canonical id does not exist', { error: err, ownerId });
this._accountIdCache.miss(ownerId);
} else {
log.error('could not get account id', { error: err, ownerId });
}
return cb(err);
}
this._accountIdCache.set(ownerId, accountId);
this._accountIdCache.expireOldest();
return cb(null, accountId);
});
}
/**
* Start kafka consumer. Emits a 'ready' event when
* consumer is ready.
* @param {function} done - callback
* @return {undefined}
*/
start(done) {
this.vaultClientWrapper?.init();
async.waterfall([
next => super.start(next),
next => {
this._gcProducer = new GarbageCollectorProducer();
this._gcProducer.setupProducer(next);
},
next => this.setupProducer(next),
], done);
}
/**
* Set up Kafka producer
* @param {function} cb callback called when producer
* startup is complete
* @return {undefined}
*/
setupProducer(cb) {
const producer = new BackbeatProducer({
kafka: { hosts: this._kafkaConfig.hosts },
maxRequestSize: this._kafkaConfig.maxRequestSize,
compressionType: this._kafkaConfig.compressionType,
requiredAcks: this._kafkaConfig.requiredAcks,
producerParams: this._kafkaConfig.producerParams,
});
producer.once('error', cb);
producer.once('ready', () => {
producer.removeAllListeners('error');
producer.on('error', err =>
this._log.error('error from backbeat producer', {
method: 'LifecycleObjectTransitionProcessor.setupProducer',
error: err,
}));
this._coldProducer = producer;
return cb();
});
}
getProcessorType() {
return 'transition-processor';
}
getConsumerParams() {
const consumerParams = super.getConsumerParams(this._lcConfig.transitionTasksTopic);
consumerParams[this._lcConfig.transitionTasksTopic].fromOffset = 'earliest';
const locations = require('../../../conf/locationConfig.json') || {};
this._lcConfig.coldStorageTopics.forEach(topic => {
if (!topic.startsWith(this._lcConfig.coldStorageStatusTopicPrefix)) {
return;
}
const coldLocation = topic.slice(this._lcConfig.coldStorageStatusTopicPrefix.length);
assert(locations[coldLocation], `${coldLocation}: unknown location`);
assert(locations[coldLocation].isCold, `${coldLocation} is not a valid cold storage location`);
const circuitBreaker = updateCircuitBreakerConfigForImplicitOutputQueue(
this._lcConfig.objectProcessor.circuitBreaker,
null,
topic,
);
consumerParams[topic] = {
zookeeper: {
connectionString: this._zkConfig.connectionString,
},
kafka: {
hosts: this._kafkaConfig.hosts,
site: this._kafkaConfig.site,
backlogMetrics: this._kafkaConfig.backlogMetrics,
compressionType: this._kafkaConfig.compressionType,
requiredAcks: this._kafkaConfig.requiredAcks,
},
topic,
groupId: this._processConfig.groupId,
concurrency: this._processConfig.concurrency,
maxQueued: this._processConfig.maxQueued,
queueProcessor: this.processColdStorageStatusEntry.bind(this),
circuitBreaker,
fromOffset: 'earliest',
};
});
return consumerParams;
}
getProcessConfig(lcConfig) {
return lcConfig.transitionProcessor;
}
getAuthConfig(lcConfig) {
if (lcConfig.transitionProcessor.auth) {
return lcConfig.transitionProcessor.auth;
}
return lcConfig.auth;
}
getTask(actionEntry) {
const actionType = actionEntry.getActionType();
switch (actionType) {
case 'requeueTransition':
return new LifecycleResetTransitionInProgressTask(this);
case 'requeueRestore':
return new LifecycleRetriggerRestoreTask(this);
case 'gc':
return new LifecycleUpdateExpirationTask(this);
case 'copyLocation':
if (actionEntry.getContextAttribute('ruleType') === 'transition') {
return new LifecycleUpdateTransitionTask(this);
}
// fall through
default:
this._log.warn(`skipped unsupported action ${actionType}`,
actionEntry.getLogInfo());
return null;
}
}
processColdStorageStatusEntry(kafkaEntry, done) {
const coldLocation = kafkaEntry.topic.slice(this._lcConfig.coldStorageStatusTopicPrefix.length);
const entry = ColdStorageStatusQueueEntry.createFromKafkaEntry(kafkaEntry);
if (entry.error) {
this._log.error('malformed status entry', {
error: entry.error,
entry: kafkaEntry.value,
});
return process.nextTick(done);
}
this._log.debug('processing cold storage entry', entry.getLogInfo());
let task = null;
switch (entry.op) {
case 'archive':
task = new LifecycleColdStatusArchiveTask(this);
break;
default:
return process.nextTick(done);
}
return this.retryWrapper.retry({
actionDesc: 'process cold storage status entry',
logFields: entry.getLogInfo(),
actionFunc: done => task.processEntry(coldLocation, entry, done),
shouldRetryFunc: err => err.retryable,
log: this._log,
}, err => {
if (err) {
this._log.error('task failed permanently after retries, committing offset', {
method: 'LifecycleObjectTransitionProcessor.processColdStorageStatusEntry',
error: err.message,
op: entry.op,
coldLocation,
...entry.getLogInfo(),
});
LifecycleMetrics.onLifecycleFailed(this._log, this.getProcessorType(),
entry.op, coldLocation);
}
return done(err);
});
}
getStateVars() {
return {
...super.getStateVars(),
coldProducer: this._coldProducer,
gcProducer: this._gcProducer,
getAccountId: this.getAccountId.bind(this),
};
}
isReady() {
return super.isReady() && (!this.vaultClientWrapper || this.vaultClientWrapper.tempCredentialsReady());
}
}
module.exports = LifecycleObjectTransitionProcessor;