Commit df51f208 authored by nilanjandaw's avatar nilanjandaw

multi-host setup wotking with minor bugs

parent 826b3465
...@@ -2,4 +2,5 @@ test/ ...@@ -2,4 +2,5 @@ test/
bitnami* bitnami*
node_modules node_modules
package-lock.json package-lock.json
firecracker* firecracker*
\ No newline at end of file secrets.json
\ No newline at end of file
...@@ -17,7 +17,7 @@ function runIsolate(filename) { ...@@ -17,7 +17,7 @@ function runIsolate(filename) {
console.log(result); console.log(result);
let timeDifference = Math.ceil((Date.now() - timeStart)) let timeDifference = Math.ceil((Date.now() - timeStart))
console.log("isolate time taken: ", timeDifference); console.log("isolate time taken: ", timeDifference);
resolve(); resolve(result);
}); });
}).catch(err => { reject(err) }) }).catch(err => { reject(err) })
}); });
...@@ -29,9 +29,10 @@ function runProcess(filename) { ...@@ -29,9 +29,10 @@ function runProcess(filename) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let timeStart = Date.now() let timeStart = Date.now()
const process = spawn('node', [filename]); const process = spawn('node', [filename]);
let result = "";
process.stdout.on('data', (data) => { process.stdout.on('data', (data) => {
console.log(`stdout: ${data}`); console.log(`stdout: ${data}`);
result += data;
let timeDifference = Math.ceil((Date.now() - timeStart)) let timeDifference = Math.ceil((Date.now() - timeStart))
console.log("process time taken: ", timeDifference); console.log("process time taken: ", timeDifference);
}); });
...@@ -43,7 +44,7 @@ function runProcess(filename) { ...@@ -43,7 +44,7 @@ function runProcess(filename) {
process.on('close', (code) => { process.on('close', (code) => {
console.log(`child process exited with code ${code}`); console.log(`child process exited with code ${code}`);
resolve(); resolve(result);
}); });
}) })
...@@ -56,11 +57,12 @@ function runContainer(imageName) { ...@@ -56,11 +57,12 @@ function runContainer(imageName) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let timeStart = Date.now() let timeStart = Date.now()
const process = spawn('docker', ["run", "--name", imageName, imageName]); const process = spawn('docker', ["run", "--name", imageName, imageName]);
let result = "";
process.stdout.on('data', (data) => { process.stdout.on('data', (data) => {
console.log(`stdout: ${data}`); console.log(`stdout: ${data}`);
let timeDifference = Math.ceil((Date.now() - timeStart)) let timeDifference = Math.ceil((Date.now() - timeStart))
console.log("container run time taken: ", timeDifference); console.log("container run time taken: ", timeDifference);
result += data;
}); });
process.stderr.on('data', (data) => { process.stderr.on('data', (data) => {
...@@ -69,7 +71,7 @@ function runContainer(imageName) { ...@@ -69,7 +71,7 @@ function runContainer(imageName) {
}); });
process.on('close', (code) => { process.on('close', (code) => {
resolve(code); resolve(result);
}) })
}) })
......
...@@ -17,24 +17,44 @@ client.on('message', function (topic, message) { ...@@ -17,24 +17,44 @@ client.on('message', function (topic, message) {
// message is Buffer // message is Buffer
message = JSON.parse(message) message = JSON.parse(message)
if (message.type !== 'heartbeat') if (message.type !== 'heartbeat') {
console.log(message.toString()) let runtime = message.runtime
let runtime = message.runtime let functionHash = message.functionHash
let functionHash = message.functionHash let function_id = message.function_id
console.log("function_id", function_id);
if (message.type === "execute") {
if (runtime === "isolate") if (message.type === "execute") {
execute.runIsolate('../dispatcher/test/' + functionHash).then(() => client.publish(node_id, JSON.stringify({ status: "success" }))) if (runtime === "isolate")
else if (runtime === "process") execute.runIsolate('../dispatcher/test/' + functionHash).then( result => {
execute.runProcess('../dispatcher/test/' + functionHash).then(() => client.publish(node_id, JSON.stringify({ status: "success" }))) client.publish("response", JSON.stringify({
else if (runtime === "container") status: "success",
execute.runContainer(functionHash).then(() => client.publish(node_id, JSON.stringify({ status: "success" }))) result,
else { function_id
client.publish(node_id, JSON.stringify({ status: "unknown runtime" })) }))
return })
else if (runtime === "process")
execute.runProcess('../dispatcher/test/' + functionHash).then( result => {
client.publish("response", JSON.stringify({
status: "success",
result,
function_id
}))
})
else if (runtime === "container")
execute.runContainer(functionHash).then(result => {
client.publish("response", JSON.stringify({
status: "success",
result,
function_id
}))
})
else {
client.publish("response", JSON.stringify({ status: "unknown runtime" }))
return
}
} }
} }
}) })
function heartbeat() { function heartbeat() {
......
...@@ -3,23 +3,29 @@ ...@@ -3,23 +3,29 @@
const express = require('express') const express = require('express')
const bodyParser = require('body-parser') const bodyParser = require('body-parser')
const fileUpload = require('express-fileupload'); const fileUpload = require('express-fileupload');
const fs = require('fs') const fs = require('fs')
const { spawn } = require('child_process'); const { spawn } = require('child_process');
const redis = require('redis')
const morgan = require('morgan') const morgan = require('morgan')
const mqtt = require('mqtt') const mqtt = require('mqtt')
const client = mqtt.connect('mqtt://localhost') const client = mqtt.connect('mqtt://localhost')
const fs = require('fs')
const app = express() const app = express()
const libSupport = require('./lib')
// const secret = require('../secrets.json')
// const db = require('nano')(`http://${secret.dispatched_db_username}:${secret.dispatched_db_password}@localhost:5984/dispatched`)
let db = new Map()
app.use(morgan('combined')) app.use(morgan('combined'))
app.use(bodyParser.urlencoded({ extended: false })) app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json()) app.use(bodyParser.json())
app.use(fileUpload()) app.use(fileUpload())
const node_id = "20sez54hq8" const node_id = "20sez54hq8"
const WINDOW_SIZE = 1
const port = 8080 const port = 8080
let requestQueue = []
app.post('/serverless/deploy', (req, res) => { app.post('/serverless/deploy', (req, res) => {
let runtime = req.body.runtime let runtime = req.body.runtime
...@@ -101,17 +107,49 @@ function deployContainer(path, imageName) { ...@@ -101,17 +107,49 @@ function deployContainer(path, imageName) {
app.post('/serverless/execute/:id', (req, res) => { app.post('/serverless/execute/:id', (req, res) => {
let runtime = req.body.runtime requestQueue.push({
let functionHash = req.params.id req, res
client.publish(node_id, JSON.stringify({
"type": "execute",
runtime, functionHash
}))
res.json({
"status": "success"
}) })
if (requestQueue.length >= WINDOW_SIZE)
dispatch()
}) })
function dispatch() {
for (let i = 0; i < WINDOW_SIZE; i++) {
let {req, res} = requestQueue.shift()
let runtime = req.body.runtime
let functionHash = req.params.id
let function_id = libSupport.makeid(20)
console.log("Dispatching function with Id", function_id);
client.publish(node_id, JSON.stringify({
"type": "execute",
function_id,
runtime, functionHash
}))
db.set(function_id, res)
}
}
client.on('message', function (topic, message) {
if (topic === "response") {
message = JSON.parse(message)
console.log(message);
let res = db.get(message.function_id)
res.json({
"status": "success",
"reply": message.result
})
}
})
app.listen(port, () => console.log(`Server listening on port ${port}!`))
app.listen(port, () => console.log(`Server listening on port ${port}!`)) client.on('connect', function () {
\ No newline at end of file client.subscribe("response")
})
\ No newline at end of file
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
"express-fileupload": "^1.1.6", "express-fileupload": "^1.1.6",
"isolated-vm": "^3.0.0", "isolated-vm": "^3.0.0",
"morgan": "^1.9.1", "morgan": "^1.9.1",
"redis": "^2.8.0" "nano": "^8.1.0",
"redis": "^2.8.0",
"save": "^2.4.0"
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment