-
Notifications
You must be signed in to change notification settings - Fork 5
/
garbage-uploader.js
165 lines (152 loc) · 5.11 KB
/
garbage-uploader.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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright 2020 Joyent, Inc.
*/
//
// This program exists to consume files from the:
//
// * /var/spool/manta_gc/<storageId>/*
//
// directories and send them to the storage zones (mako) for processing. Once
// the files have been accepted by the appropriate storage zone, they will be
// deleted locally.
//
//
// Config Options:
//
// * admin_ip
// * datacenter
// * instance
// * server_uuid
//
//
// Assumptions:
//
// * new directories will show up, but we don't care if they disappear (without
// restart)
//
//
// Known issues:
//
// - When we can't unlink an instruction, we'll just retry again next time.
// It's possible we should change this so we put the file on a blacklist for
// some amount of time, or otherwise avoid resending the same file over and
// over.
//
// - If we have too many files in a dir we might run out of memory on
// fs.readdir. The best solution to this would be to use node 12+ so that we
// can have fs.opendir. In the meantime if it gets too big we'll just start
// crashing when trying to read the directory, which we hopefully will
// notice. To recover from this, the mako should be fixed so it accepts
// files again (it must have been backed up if our queue is that big) and
// then we can move some of the files out of the dir, catch up and then move
// the remaining files back in. Not ideal, but should work.
//
//
// Things for future consideration:
//
// - Make concurrency/etc tunable via config file.
//
// - Add support for backing off broken Makos?
//
// - If we need files for "new" makos to show up faster (instead of just on the
// next loop) we can add a fs.watch() watcher for the instruction root dir.
// Seems unlikely to be worth it.
//
// - There are a few things that would be nice for metrics but would blow up
// cardinality and therefore probably are not going to happen. Such as:
//
// - histogram for upload times
// - storageId label on relevant metrics
//
var createMetricsManager = require('triton-metrics').createMetricsManager;
var restify = require('restify');
var vasync = require('vasync');
var common = require('../lib/common');
var GarbageUploader = require('../lib/garbage-uploader');
var elapsedSince = common.elapsedSince;
var METRICS_SERVER_PORT = 8883;
var SERVICE_NAME = 'garbage-uploader';
function main() {
var beginning;
var logger;
beginning = process.hrtime();
vasync.pipeline(
{
arg: {},
funcs: [
function _createLogger(_, cb) {
logger = common.createLogger({
level: 'info',
name: SERVICE_NAME
});
cb();
},
function _loadConfig(ctx, cb) {
common.loadConfig({log: logger}, function _onConfig(
err,
cfg
) {
if (!err) {
ctx.config = cfg;
}
cb(err);
});
},
function _validateConfig(ctx, cb) {
common.validateConfig(ctx.config, function _onValidated(
err
) {
cb(err);
});
},
function _setupMetrics(ctx, cb) {
var metricsManager = createMetricsManager({
address: ctx.config.admin_ip,
log: logger,
staticLabels: {
datacenter: ctx.config.datacenter,
instance: ctx.config.instance,
server: ctx.config.server_uuid,
service: SERVICE_NAME
},
port: METRICS_SERVER_PORT,
restify: restify
});
metricsManager.createNodejsMetrics();
metricsManager.listen(cb);
ctx.metricsManager = metricsManager;
},
function _createUploader(ctx, cb) {
var gu = new GarbageUploader({
config: ctx.config,
log: logger,
metricsManager: ctx.metricsManager
});
gu.start(cb);
}
]
},
function _doneMain(err) {
if (err) {
logger.error(
{elapsed: elapsedSince(beginning), err: err},
'Startup failed.'
);
return;
}
logger.info(
{elapsed: elapsedSince(beginning)},
'Startup complete.'
);
}
);
}
if (require.main === module) {
main();
}
module.exports = GarbageUploader;