Skip to content

Commit

Permalink
Add status.js and its tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkshitJain committed Nov 24, 2017
1 parent 8cf9990 commit b013c72
Show file tree
Hide file tree
Showing 28 changed files with 403 additions and 130 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,6 +5,7 @@ main_server/lab_backups/
main_server/test/npm-debug.log
main_server/test/node_modules/
tests/functional_tests/node_modules/
util/node_modules/
.vagrant
docker-images/*.tar
ubuntu-xenial-16.04-cloudimg-console.log
Expand Down
6 changes: 5 additions & 1 deletion deploy/configs/execution_nodes/execution_node_1/conf.json
@@ -1,13 +1,17 @@
{ "load_balancer" :{
"role": "load_balancer",
"hostname": "localhost",
"port": "8081"
},
"gitlab" :{
"role": "gitlab",
"hostname": "localhost",
"port": "80"
},
"host_port" :
"execution_node" :
{
"role": "execution_node",
"hostname": "localhost",
"port" : "8082"
}
}
@@ -1,6 +1,7 @@
{
"node_details":
{
"role": "execution_node",
"hostname": "localhost",
"port": "8082"
},
Expand Down
6 changes: 5 additions & 1 deletion deploy/configs/execution_nodes/execution_node_2/conf.json
@@ -1,13 +1,17 @@
{ "load_balancer" :{
"role": "load_balancer",
"hostname": "localhost",
"port": "8081"
},
"gitlab" :{
"role": "gitlab",
"hostname": "localhost",
"port": "80"
},
"host_port" :
"execution_node" :
{
"role": "execution_node",
"hostname": "localhost",
"port" : "8083"
}
}
@@ -1,6 +1,7 @@
{
"node_details":
{
"role": "execution_node",
"hostname": "localhost",
"port": "8083"
},
Expand Down
6 changes: 5 additions & 1 deletion deploy/configs/execution_nodes/execution_node_3/conf.json
@@ -1,13 +1,17 @@
{ "load_balancer" :{
"role": "load_balancer",
"hostname": "localhost",
"port": "8081"
},
"gitlab" :{
"role": "gitlab",
"hostname": "localhost",
"port": "80"
},
"host_port" :
"execution_node" :
{
"role": "execution_node",
"hostname": "localhost",
"port" : "8084"
}
}
@@ -1,6 +1,7 @@
{
"node_details":
{
"role": "execution_node",
"hostname": "localhost",
"port": "8084"
},
Expand Down
6 changes: 5 additions & 1 deletion deploy/configs/execution_nodes/execution_node_4/conf.json
@@ -1,13 +1,17 @@
{ "load_balancer" :{
"role": "load_balancer",
"hostname": "localhost",
"port": "8081"
},
"gitlab" :{
"role": "gitlab",
"hostname": "localhost",
"port": "80"
},
"host_port" :
"execution_node" :
{
"role": "execution_node",
"hostname": "localhost",
"port" : "8085"
}
}
@@ -1,6 +1,7 @@
{
"node_details":
{
"role": "execution_node",
"hostname": "localhost",
"port": "8085"
},
Expand Down
6 changes: 5 additions & 1 deletion deploy/configs/execution_nodes/execution_node_5/conf.json
@@ -1,13 +1,17 @@
{ "load_balancer" :{
"role": "load_balancer",
"hostname": "localhost",
"port": "8081"
},
"gitlab" :{
"role": "gitlab",
"hostname": "localhost",
"port": "80"
},
"host_port" :
"execution_node" :
{
"role": "execution_node",
"hostname": "localhost",
"port" : "8086"
}
}
@@ -1,6 +1,7 @@
{
"node_details":
{
"role": "execution_node",
"hostname": "localhost",
"port": "8086"
},
Expand Down
11 changes: 10 additions & 1 deletion deploy/configs/load_balancer/nodes_data_conf.json
@@ -1,27 +1,33 @@
{
"Nodes": [
{
"role": "execution_node",
"hostname": "localhost",
"port": "8082"
},
{
"role": "execution_node",
"hostname": "localhost",
"port": "8083"
},
{
"role": "execution_node",
"hostname": "localhost",
"port": "8084"
},
{
"role": "execution_node",
"hostname": "localhost",
"port": "8085"
},
{
"role": "execution_node",
"hostname": "localhost",
"port": "8086"
}
],
"server_info": {
"role": "main_server",
"hostname": "localhost",
"port": "9000"
},
Expand All @@ -32,10 +38,13 @@
"database": "Autolab"
},
"gitlab": {
"role": "gitlab",
"hostname": "localhost",
"port": "80"
},
"host_port": {
"load_balancer": {
"role": "load_balancer",
"hostname": "localhost",
"port": "8081"
}
}
6 changes: 5 additions & 1 deletion deploy/configs/main_server/conf.json
@@ -1,5 +1,6 @@
{
"load_balancer": {
"role": "load_balancer",
"hostname": "localhost",
"port": "8081"
},
Expand All @@ -12,11 +13,14 @@
},
"gitlab" :
{
"role": "gitlab",
"username":"root",
"password" : "12345678"
},
"host_port" :
"main_server" :
{
"role": "main_server",
"hostname": "localhost",
"port" : "9000"
}
}
1 change: 1 addition & 0 deletions deploy/playbook-single.yml
Expand Up @@ -150,6 +150,7 @@
- "../log/load_balancer:/log"
env:
LOGGERCONFIG: "/etc/util/logger.json"
LBCONFIG: "/etc/load_balancer/nodes_data_conf.json"
GITLAB_IP: "{{ gitlab_hostname }}"

- hosts: mainserver
Expand Down
1 change: 1 addition & 0 deletions deploy/playbook.yml
Expand Up @@ -150,6 +150,7 @@
- "../log/load_balancer:/log"
env:
LOGGERCONFIG: "/etc/util/logger.json"
LBCONFIG: "/etc/load_balancer/nodes_data_conf.json"
GITLAB_IP: "{{ gitlab_hostname }}"

- hosts: mainserver
Expand Down
4 changes: 2 additions & 2 deletions execution_nodes/execute_node.js
Expand Up @@ -156,5 +156,5 @@ request.on('error',function(err)
request.end(body);


server.listen(conf.host_port.port);
console.log("Listening at "+conf.host_port.port);
server.listen(conf.execution_node.port);
console.log("Listening at "+conf.execution_node.port);
119 changes: 24 additions & 95 deletions load_balancer/load_balancer.js
Expand Up @@ -13,13 +13,16 @@ var http = require('http');
var bodyParser = require('body-parser');
var fs = require('fs');
var sys = require('sys');
var exec = require('child_process').exec;
var { exec } = require('child_process');
var nodes_data;
if(process.env.mode == 'TESTING') nodes_data = require('/etc/load_balancer/nodes_data_conf.json');
else nodes_data = require('/etc/load_balancer/nodes_data_conf.json');

var mysql = require('mysql');
const { Status } = require('./status.js');

const status = new Status(nodes_data.Nodes);
const node_queue = [];

app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({extended: true}));
Expand All @@ -30,56 +33,19 @@ app.get('/userCheck', function (req,res) {
res.send(true);
});

app.get('/connectionCheck', function (req,res) {
console.log('connectionCheck requested');
var result = 'Load Balancer Working\n';
var numOfNodes = nodes_data.Nodes.length;
function checkNodeConn(node){
var options = {
host: node.hostname,
port: node.port,
path: '/connectionCheck',
key : fs.readFileSync('./ssl/key.pem'),
cert: fs.readFileSync('./ssl/cert.pem'),
rejectUnauthorized:false,
};
//send a get request and capture the response
var req = https.request(options, function(res){
// Buffer the body entirely for processing as a whole.
var bodyChunks = [];
res.on('data', function(chunk){
bodyChunks.push(chunk);
}).on('end', function(){
var body = Buffer.concat(bodyChunks);
result = result.concat('<br/>Execution Node at '+node.hostname+':'+node.port+' working: ' + body);
console.log("nodeing");
//return if all requets processed
if(--numOfNodes === 0){
console.log("DispRes");
dispResult();
}
});
});
req.on('error', function(e) {
result = result.concat('<br/>Execution Node at '+node.hostname+':'+node.port+' Error: ' + e.message);
//return if all requets processed
if(--numOfNodes === 0){
console.log("DispRes");
dispResult();
}
});
req.end();
} //checkNodeConnection ends

function dispResult(){
res.send(result);
}
//Check connection of all nodes
for(var i=0;i<nodes_data.Nodes.length;i++)
{
console.log(numOfNodes);
checkNodeConn(nodes_data.Nodes[i]);
}
app.get('/connectionCheck', (req, res) => {
console.log('Connection check requested');
const lbStatus = nodes_data.load_balancer;
lbStatus.status = 'up';
status.checkStatus((result) => {
//since the request is being processed, it is assumed that load balancer is up
const statusJson = result;
statusJson.components.push(lbStatus);
statusJson.job_queue_length = job_queue.length;
statusJson.timestamp = (new Date()).toString();
console.log("Connection check request completed");
res.send(statusJson);
});
});

app.post('/submit', function(req, res){
Expand Down Expand Up @@ -258,7 +224,7 @@ app.post('/addNode', function(req, res){
console.log(req.body)
node_queue.push(req.body);
console.log("Added "+req.body.hostname+":"+req.body.port+" to queue");

if(job_queue.length!==0)
{
var assigned_node = node_queue.pop();
Expand Down Expand Up @@ -320,57 +286,20 @@ try {
} finally {

}
var node_queue=[];
for(var i=0;i<nodes_data.Nodes.length;i++)
{
checkNodeConn(nodes_data.Nodes[i]);
function checkNodeConn(node) {

var https_checkConn ={
hostname : node.hostname,
port : node.port,
path : '/connectionCheck',
key : fs.readFileSync('./ssl/key.pem'),
cert: fs.readFileSync('./ssl/cert.pem'),
rejectUnauthorized:false,
};

var checkConnRequest = https.request(https_checkConn,function(res)
{
var bodyChunks =[];
res.on('data',function(chunk)
{
bodyChunks.push(chunk);
}).on('end',function()
{
var body = Buffer.concat(bodyChunks);
if(body.toString()=='true')
{
console.log("Added "+node.hostname+":"+node.port+" to queue");
node_queue.push(node);
}
});
});

checkConnRequest.on('error',function(err)
{
console.log("Error connecting to "+node.hostname+":"+node.port);

/* This will update the node queue and working nodes will be added.
The logger level would be info, when logger.js is integrated. */
status.selectActiveNodes((workingNodes) => {
node_queue.push(...workingNodes);
});
checkConnRequest.end();

}
}

var job_queue = [];
if(process.env.mode !== "TESTING")
{
server.listen(nodes_data.host_port.port);
console.log("Listening at "+nodes_data.host_port.port);
server.listen(nodes_data.load_balancer.port);
console.log("Listening at "+nodes_data.load_balancer.port);
}



setInterval(function () {
connection.query('SELECT 1' ,function(err, rows, fields) {
console.log("keep alive query");
Expand Down

0 comments on commit b013c72

Please sign in to comment.