-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassign-missing-slots.js
121 lines (106 loc) · 3.29 KB
/
assign-missing-slots.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
const Redis = require('ioredis');
var sleep = require('sleep');
var async = require('async');
const { StringDecoder } = require('string_decoder');
var yesno = require('yesno');
var existingRedisConn = {
"host": "127.0.0.1",
"port": 7001
};
var existingRedis = new Redis(existingRedisConn);
var newRedisConn = {
"host": "127.0.0.1",
"port": 7003
};
var newRedis = new Redis(newRedisConn);
async.waterfall([
getClusterInfo,
parseClusterInfo,
forgetBrokenNode,
addNewNode,
assignSlotsToNewNode
], function(err, results) {
console.log('done');
});
function getClusterInfo(callback) {
arbitraryCommand(existingRedis, 'cluster', ['nodes'], function (err, value) {
var decoder = new StringDecoder('utf8');
var clusterInfo = decoder.write(Buffer.from(value));
console.log("cluster info:");
console.log(clusterInfo);
callback(null, {
'clusterInfo': clusterInfo
});
});
}
function parseClusterInfo(results, callback) {
var lines = results.clusterInfo.split("\n");
for (var i in lines) {
var line = lines[i];
var regex = /([^\s]+).*disconnected\s+([^\s]+)/g;
var match = regex.exec(line);
if (match) {
results['nodeId'] = match[1];
results['missingSlots'] = match[2];
break;
}
}
callback(null, results);
}
function forgetBrokenNode(results, callback) {
console.log("~/Downloads/redis-3.2.10/src/redis-trib.rb call " + existingRedisConn.host + ":" + existingRedisConn.port + " cluster forget " + results.nodeId);
yesno.ask('Please run forget command, ok to continue?', true, function(ok) {
if(ok) {
callback(null, results);
} else {
throw 'exiting';
}
});
}
function addNewNode(results, callback) {
console.log("~/Downloads/redis-3.2.10/src/redis-trib.rb add-node " + newRedisConn.host + ":" + newRedisConn.port + " " + existingRedisConn.host + ":" + existingRedisConn.port);
yesno.ask('Please add new node, ok to continue?', true, function(ok) {
if(ok) {
callback(null, results);
} else {
throw 'exiting';
}
});
}
function assignSlotsToNewNode(results, callback) {
var args = ['addslots'];
var slotsStr = results.missingSlots;
var slotsChunks = slotsStr.split(",");
for (var i in slotsChunks) {
var slotsChunk = slotsChunks[i];
var slotsSplit = slotsChunk.split("-");
if (slotsSplit.length > 1) {
for (var j = slotsSplit[0]; j <= slotsSplit[1]; j++) {
args.push(j + "");
}
} else {
args.push(slotsSplit[0]);
}
}
yesno.ask("Assigning slots " + slotsStr + " to new node, ok to continue?", true, function(ok) {
if (ok) {
arbitraryCommand(newRedis, 'cluster', args, function (err, value) {
if (err) throw err;
console.log(value.toString()); //-> 'OK'
callback(null, results);
});
} else {
throw exiting;
}
});
}
function arbitraryCommand(redis, command, args, callback) {
redis.sendCommand(
new Redis.Command(
command,
args,
'utf-8',
callback
)
);
}