-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: Add microLB example from IncludeOS repo
- Loading branch information
Showing
8 changed files
with
282 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
cmake_minimum_required(VERSION 3.0) | ||
# IncludeOS install location | ||
if (NOT DEFINED INCLUDEOS_PREFIX) | ||
if (NOT DEFINED ENV{INCLUDEOS_PREFIX}) | ||
set(INCLUDEOS_PREFIX /usr/local/includeos) | ||
else() | ||
set(INCLUDEOS_PREFIX $ENV{INCLUDEOS_PREFIX}) | ||
endif() | ||
endif() | ||
|
||
if (NOT EXISTS "${INCLUDEOS_PREFIX}/cmake/os.cmake") | ||
MESSAGE(FATAL_ERROR "IncludeOS does not appear to be installed at ${INCLUDEOS_PREFIX}") | ||
endif() | ||
list(APPEND CMAKE_MODULE_PATH ${INCLUDEOS_PREFIX}/cmake) | ||
|
||
#service | ||
project (service) | ||
include(os) | ||
|
||
set(SOURCES | ||
service.cpp | ||
) | ||
|
||
os_add_executable(microlb "microLB Service" ${SOURCES}) | ||
|
||
# DRIVERS / PLUGINS: | ||
|
||
if ("$ENV{PLATFORM}" STREQUAL "x86_solo5") | ||
os_add_drivers(microlb solo5net) | ||
else() | ||
os_add_drivers(microlb | ||
virtionet # Virtio networking | ||
vmxnet3 | ||
boot_logger # Display boot information | ||
# Use "boot --drivers ." to see other drivers | ||
# virtioblk # Virtio block device | ||
# ... Others from src/drivers | ||
) | ||
endif() | ||
|
||
os_add_stdout(microlb default_stdout) | ||
os_add_os_library(microlb liveupdate) | ||
os_add_os_library(microlb microlb) | ||
# os_add_plugins( ... ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
### microLB demo | ||
|
||
Start the nodeJS demo services first: | ||
``` | ||
node server.js | ||
``` | ||
|
||
Build and run the load balancer: | ||
``` | ||
boot . --create-bridge | ||
``` | ||
|
||
Connect to the load balancer: | ||
``` | ||
curl 10.0.0.42 | ||
``` | ||
|
||
The load balancer should be configured to round-robin on 10.0.0.1 ports 6001-6004. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"net" : [ | ||
{ | ||
"iface": 0, | ||
"config": "static", | ||
"address": "10.0.0.42", | ||
"netmask": "255.255.255.0", | ||
"gateway": "10.0.0.1" | ||
}, | ||
{ | ||
"iface": 1, | ||
"config": "static", | ||
"address": "10.0.0.44", | ||
"netmask": "255.255.255.0", | ||
"gateway": "10.0.0.1" | ||
} | ||
], | ||
|
||
"load_balancer" : { | ||
"clients" : { | ||
"iface" : 0, | ||
"port" : 80, | ||
"waitq_limit" : 1000, | ||
"session_limit" : 1000 | ||
}, | ||
"nodes" : { | ||
"iface" : 1, | ||
"algo" : "round_robin", | ||
"list" : [ | ||
["10.0.0.1", 6001], | ||
["10.0.0.1", 6002], | ||
["10.0.0.1", 6003], | ||
["10.0.0.1", 6004] | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var http = require('http'); | ||
|
||
var stringToColour = function(str) { | ||
var hash = 0; | ||
for (var i = 0; i < str.length; i++) { | ||
hash = str.charCodeAt(i) + ((hash << 5) - hash); | ||
} | ||
var colour = '#'; | ||
for (var i = 0; i < 3; i++) { | ||
var value = (hash >> (i * 8)) & 0xFF; | ||
colour += ('00' + value.toString(16)).substr(-2); | ||
} | ||
return colour; | ||
} | ||
|
||
//We need a function which handles requests and send response | ||
function handleRequest(request, response){ | ||
response.setTimeout(500); | ||
var addr = request.connection.localPort; | ||
var bgcolor = stringToColour(addr + "42"); | ||
var body = '<h1>'+ addr +'</h1><br/>' + 'Link established with IP <strong>' + addr + '</strong>'; | ||
var page = "<html><head></head><body bgcolor=\""+ bgcolor +"\">" + body + "</body></html>"; | ||
response.end(page); | ||
} | ||
|
||
http.createServer(handleRequest).listen(6001, '10.0.0.1'); | ||
http.createServer(handleRequest).listen(6002, '10.0.0.1'); | ||
http.createServer(handleRequest).listen(6003, '10.0.0.1'); | ||
http.createServer(handleRequest).listen(6004, '10.0.0.1'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// This file is a part of the IncludeOS unikernel - www.includeos.org | ||
// | ||
// Copyright 2015 Oslo and Akershus University College of Applied Sciences | ||
// and Alfred Bratterud | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <os> | ||
#include <profile> | ||
#include <microLB> | ||
#include <net/interfaces> | ||
static void print_stats(int); | ||
#define STATS_PERIOD 5s | ||
|
||
#include "../LiveUpdate/liu.hpp" | ||
static void save_state(liu::Storage& store, const liu::buffer_t*) | ||
{ | ||
|
||
} | ||
|
||
static microLB::Balancer* balancer = nullptr; | ||
void Service::start() | ||
{ | ||
balancer = microLB::Balancer::from_config(); | ||
|
||
Timers::periodic(1s, STATS_PERIOD, print_stats); | ||
StackSampler::begin(); | ||
//StackSampler::set_mode(StackSampler::MODE_CURRENT); | ||
|
||
// raw TCP liveupdate server | ||
auto& inet = net::Interfaces::get(0); | ||
setup_liveupdate_server(inet, 666, save_state); | ||
} | ||
|
||
/// statistics /// | ||
#include <timers> | ||
#include <ctime> | ||
using namespace std::chrono; | ||
|
||
static std::string now() | ||
{ | ||
auto tnow = time(0); | ||
auto* curtime = localtime(&tnow); | ||
|
||
char buff[48]; | ||
int len = strftime(buff, sizeof(buff), "%c", curtime); | ||
return std::string(buff, len); | ||
} | ||
|
||
static void print_heap_info() | ||
{ | ||
static auto last = os::total_memuse(); | ||
// show information on heap status, to discover leaks etc. | ||
auto heap_usage = os::total_memuse(); | ||
auto diff = heap_usage - last; | ||
printf("Mem usage %zu Kb diff %zu (%zu Kb)\n", | ||
heap_usage / 1024, diff, diff / 1024); | ||
last = heap_usage; | ||
} | ||
|
||
template <int N, typename T> | ||
struct rolling_avg { | ||
std::deque<T> values; | ||
|
||
void push(T value) { | ||
if (values.size() >= N) values.pop_front(); | ||
values.push_back(value); | ||
} | ||
double avg() const { | ||
double ps = 0.0; | ||
if (values.empty()) return ps; | ||
for (auto v : values) ps += v; | ||
return ps / values.size(); | ||
} | ||
}; | ||
|
||
void print_stats(int) | ||
{ | ||
static int64_t last = 0; | ||
const auto& nodes = balancer->nodes; | ||
|
||
auto totals = nodes.total_sessions(); | ||
int growth = totals - last; last = totals; | ||
|
||
printf("*** [%s] ***\n", now().c_str()); | ||
printf("Total %ld (%+d) Sess %d Wait %d TO %d - Pool %d C.Att %d Err %d\n", | ||
totals, growth, nodes.open_sessions(), balancer->wait_queue(), | ||
nodes.timed_out_sessions(), nodes.pool_size(), | ||
nodes.pool_connecting(), balancer->connect_throws()); | ||
|
||
// node information | ||
int n = 0; | ||
for (auto& node : nodes) { | ||
printf("[%s %s P=%d C=%d] ", node.address().to_string().c_str(), | ||
(node.is_active() ? "ONL" : "OFF"), | ||
node.pool_size(), node.connection_attempts()); | ||
if (++n == 2) { n = 0; printf("\n"); } | ||
} | ||
if (n > 0) printf("\n"); | ||
|
||
// CPU-usage statistics | ||
static uint64_t last_total = 0, last_asleep = 0; | ||
uint64_t tdiff = StackSampler::samples_total() - last_total; | ||
last_total = StackSampler::samples_total(); | ||
uint64_t adiff = StackSampler::samples_asleep() - last_asleep; | ||
last_asleep = StackSampler::samples_asleep(); | ||
|
||
if (tdiff > 0) | ||
{ | ||
double asleep = adiff / (double) tdiff; | ||
static rolling_avg<5, double> asleep_avg; | ||
asleep_avg.push(asleep); | ||
|
||
printf("CPU usage: %.2f%% Idle: %.2f%% Active: %ld Existing: %ld Free: %ld\n", | ||
(1.0 - asleep) * 100.0, asleep * 100.0, | ||
Timers::active(), Timers::existing(), Timers::free()); | ||
} | ||
else { | ||
printf("CPU usage unavailable due to lack of samples\n"); | ||
} | ||
|
||
// heap statistics | ||
print_heap_info(); | ||
// stack sampling | ||
StackSampler::print(3); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/bash | ||
BFOLD=build | ||
mkdir -p $BFOLD | ||
pushd $BFOLD | ||
cmake .. | ||
make -j8 | ||
popd | ||
dd if=$BFOLD/microlb > /dev/tcp/10.0.0.42/666 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"description" : "VM with 3 interfaces for load balancing", | ||
"net" : [ | ||
{"device" : "virtio"}, | ||
{"device" : "virtio"}, | ||
{"device" : "virtio"} | ||
], | ||
"mem" : 512 | ||
} |