This repository has been archived by the owner on Sep 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathheal.js
executable file
·105 lines (88 loc) · 3.55 KB
/
heal.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
#!/usr/bin/env node
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';
var ClusterManager = require('./lib/cluster.js');
var assertNoError = require('./lib/util.js').assertNoError;
var program = require('commander');
function safeParseInt(number, defaultValue) {
number = parseInt(number, 10);
if (isNaN(number)) {
return defaultValue;
} else {
return number;
}
}
function getTime() {
var date = new Date();
var hour = date.getHours(); hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes(); min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds(); sec = (sec < 10 ? "0" : "") + sec;
var msec = date.getMilliseconds();
msec = (msec < 10? "00" : msec < 100? "0" : "") + msec;
return hour + ":" + min + ":" + sec + "." + msec;
}
function main() {
program
.description('Start a partition heal coordinated by the coordinator node')
.option('--tchannel-v1')
.option('--tries <tries>', 'Number of times to try the heal', safeParseInt, 10)
.usage('[options] <discoveryUri>');
program.parse(process.argv);
var discoveryUri = program.args[0];
if (!discoveryUri) {
console.error(getTime(), 'Error: discoveryUri is required');
process.exit(1);
}
var clusterManager = new ClusterManager({
useTChannelV1: program.tchannelV1,
discoveryUri: discoveryUri
});
function executeHeal(tries) {
if (tries <= 0) {
console.error(getTime(), 'unable to heal partitions after multiple retries');
process.exit(2);
}
clusterManager.heal(function onHeal(err, resp) {
// assert that there is no error, quit if there is an error.
assertNoError(err);
if (!resp) {
console.error(getTime(), 'did not receive a response during heal.');
process.exit(3);
}
var targets = resp.targets || [];
if (targets.length === 0) {
console.log(getTime(), 'No known partitions left');
// graceful exit
return process.exit(0);
}
console.log(getTime(), 'Executed heal to', targets.length, 'targets');
targets.forEach(function (target) {
console.log(getTime(), ' - ' + target);
});
setTimeout(executeHeal, 1000, tries - 1);
return;
});
}
executeHeal(program.tries);
}
if (require.main === module) {
main();
}