-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_orchestration.js
More file actions
162 lines (151 loc) · 5.18 KB
/
server_orchestration.js
File metadata and controls
162 lines (151 loc) · 5.18 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
//rules can be written in config, we will read it and initialize as a global variable here
var rules = [{"server": "A","dependent_on": ["C","B"]},{"server": "B","dependent_on": ["C"]},{"server": "C","dependent_on": ['E']},{"server": "D","dependent_on": []},{"server": "E","dependent_on": []}];
//this map represents the state of the instances. False means Off and True is On
var instanceStatus = {'A': false,'B': false,'C': false, 'D': false, 'E': false};
//it will store the instances which are not independent and can be spawned initially in any order
var spawnSequence = [];
var depenedencies = {};
//read the rules and spawn the clusters
function initClusters() {
//re-hash the rules
reHash();
for (var k in rules) {
var instanceName = rules[k]['server'];
if ((rules[k]['dependent_on']).length == 0) {
spawnSequence.push(instanceName);
}
}
spawnCluster(spawnSequence);
}
function spawnCluster(spawnList) {
var unSequenced = [];
var visited = [];
if (spawnList.length == 0) {
//no independent node found, throw an error
console.log("Can not spawn the cluster");
return false
}
else {
//add remaining instances to spawnSequence
for (var j in rules) {
var instanceName = rules[j]['server'];
var dependencies = rules[j]['dependent_on'];
//add in spanSequence only if it has not been added before
if(spawnSequence.indexOf(instanceName) == -1) {
//check if all dependent servers are already in the spawnSequence
var dependencyCheck = true;
for (var key in dependencies) {
if(spawnSequence.indexOf(dependencies[key]) == -1) {
dependencyCheck = false;
visited.push(dependencies[key]);
}
}
if(dependencyCheck == false) {
unSequenced.push(instanceName);
}
else {
spawnSequence.push(instanceName);
}
}
}
//check if instancs are remaining and recursively iterate on them
if(unSequenced.length > 0) {
var cyclic = true;
for (var i in spawnSequence) {
if (visited.indexOf(spawnSequence[i]) != -1) {
cyclic = false;
break;
}
}
if(cyclic == false) {
spawnCluster(unSequenced)
}
else {
console.log("Can not spawn the cluster");
return false
}
}
else {
//all done
console.log("Instances will spawn in this sequence : "+spawnSequence);
//start the instances
for (var i in spawnSequence) {
var instanceName = spawnSequence[i];
if(instanceStatus[instanceName] == false) {
instanceStatus[instanceName] = true; //start the instance
}
}
return spawnSequence;
}
}
}
function handleDown(instance) {
//check if other instances are dependent on it
var tempSequence = spawnSequence.slice();
var reSpawn = false;
var index = spawnSequence.indexOf(instance);
if (index == spawnSequence.length-1) {
//if it last in the sequence, other instances are not dependant on it
instanceStatus[instance] = true;
console.log(instance+" is up");
return true;
}
else {
//check if next instances in spawnSequence is dependant on this one
var nextInstance = spawnSequence[index + 1];
var downInstances = [];
var terminationIndex = index;
for (var key = index + 1; key <= spawnSequence.length-1; key++) {
var name = spawnSequence[key];
if ((depenedencies[name]).indexOf(instance) != -1) {
//downInstances.push(name);
nextInstance = name;
terminationIndex = key;
break;
}
}
if(index != terminationIndex){
for (var k in spawnSequence) {
//next instance is dependent on this instance
tempSequence = tempSequence.slice(terminationIndex,(spawnSequence.length)+1); //remove all other instances from the sequence
//terminate the dependant instances
for (var k in tempSequence) {
if(tempSequence[k] != instance) {
console.log("Terminating instance "+ tempSequence[k]);
instanceStatus[tempSequence[k]] = false;
}
}
//re-spawn the instances
for (var i = 0; i <= spawnSequence.length-1; i++) {
if (instanceStatus[spawnSequence[i]] == false) {
instanceStatus[spawnSequence[i]] = true;
console.log(spawnSequence[i]+" is up");
}
}
break;
}
}
else {
//next instance is not dependent on this instance and hence the rest of the instances are not dependent too
instanceStatus[instance] = true;
console.log(instance+" is up");
}
}
}
function reHash() {
//store dependencies in separate hash
for (var key in rules) {
depenedencies[rules[key]['server']] = rules[key]['dependent_on'];
}
}
``
//-------------------------------------Functions and calls for testing---------------------------------
//function to shutdown an Instance
function shutDown(instance) {
//change status
console.log(instance+" is Down");
instanceStatus[instance] = false;
handleDown(instance);
}
initClusters(); //start the Clusters
shutDown("D"); //shutDown an instance