Skip to content

Commit

Permalink
Problem: writing custom servers is difficult
Browse files Browse the repository at this point in the history
Basically, it requires duplicating a lot of code that is currently
in pumpkindb_server (with respect to mio and putting all the necessary
engine elements together)

Writing such servers is very useful when one needs to add new
built-in functionality to PumpkinDB yet retain its standard communication
protocol.

Solution: extract pumpkindb_mio_server crate that focuses primarily
on the mio part) and let pumpkindb_server handle the rest.

Note that this is still requiring one to at least look at pumpkindb_server
to implement their own custom server, but this is far better than before.

Also, the separation is not very clean, but this will start the ball
rolling.
  • Loading branch information
yrashk committed Jun 5, 2017
1 parent 639131b commit 63c3b66
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 62 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ members = [
"./pumpkinscript",
"./pumpkindb_engine",
"./pumpkindb_client",
"./pumpkindb_mio_server",
"./pumpkindb_server",
"./pumpkindb_term",
"./tests/doctests"
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ COPY Makefile /pumpkindb/
COPY Cargo.* /pumpkindb/
COPY pumpkindb_engine /pumpkindb/pumpkindb_engine
COPY pumpkindb_server /pumpkindb/pumpkindb_server
COPY pumpkindb_mio_server /pumpkindb/pumpkindb_mio_server
COPY pumpkindb_term /pumpkindb/pumpkindb_term
COPY pumpkinscript /pumpkindb/pumpkinscript
COPY tests /pumpkindb/tests
Expand Down
25 changes: 25 additions & 0 deletions pumpkindb_mio_server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "pumpkindb_mio_server"
version = "0.2.0"
license = "MPL-2.0"
repository = "https://github.com/PumpkinDB/PumpkinDB"
homepage = "http://pumpkindb.org"
keywords = [ "pumpkindb", "database" ]
categories = [ "database" ]

authors = ["Yurii Rashkovskii <yrashk@gmail.com>"]

[dependencies]
mio = "0.6.4"
byteorder = "1.0.0"
memmap = "0.5.2"
slab = "0.3.0"
num-bigint = "0.1.35"
num-traits = "0.1.36"
rand = "0.3.15"
log = "0.3.6"
log4rs = { version = "0.6.1", features = ["toml_format"] }
lmdb-zero = "0.4.0"
uuid = { version = "0.4", features = ["v4"] }

pumpkindb_engine = { version = "0.2", path = "../pumpkindb_engine" }
Empty file added pumpkindb_mio_server/README.md
Empty file.
File renamed without changes.
49 changes: 49 additions & 0 deletions pumpkindb_mio_server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) 2017, All Contributors (see CONTRIBUTORS file)
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#![feature(slice_patterns, advanced_slice_patterns)]

extern crate mio;
extern crate memmap;
extern crate byteorder;
extern crate rand;
#[macro_use]
extern crate log;
extern crate log4rs;
extern crate slab;
extern crate num_bigint;
extern crate num_traits;
extern crate lmdb_zero as lmdb;
extern crate uuid;

extern crate pumpkindb_engine;

mod connection;
mod server;

use mio::Poll;
use mio::tcp::TcpListener;

use mio::channel as mio_chan;

use pumpkindb_engine::script;

pub fn run(port: i64,
senders: Vec<script::Sender<script::RequestMessage>>,
relay_sender: mio_chan::Sender<server::RelayedPublishedMessage>,
relay_receiver: mio_chan::Receiver<server::RelayedPublishedMessage>) {
let addr = format!("0.0.0.0:{}", port).parse().unwrap();

info!("Listening on {}", addr);

let sock = TcpListener::bind(&addr).expect("Failed to bind address");

let mut poll = Poll::new().expect("Failed to initialize polling");

let mut server = server::Server::new(sock, relay_sender, relay_receiver, senders);
server.run(&mut poll).expect("Failed to run server");

}

File renamed without changes.
16 changes: 5 additions & 11 deletions pumpkindb_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@ name = "pumpkindb"

[dependencies]
clap = "2.22.1"
mio = "0.6.4"
byteorder = "1.0.0"
memmap = "0.5.2"
slab = "0.3.0"
num-bigint = "0.1.35"
num-traits = "0.1.36"
rand = "0.3.15"
config = "0.3.1"
lazy_static = "0.2.2"
num_cpus = "1.3.0"
memmap = "0.5.2"
log = "0.3.6"
log4rs = { version = "0.6.1", features = ["toml_format"] }
config = "0.3.1"
lazy_static = "0.2.2"
lmdb-zero = "0.4.0"
uuid = { version = "0.4", features = ["v4"] }
mio = "0.6.4"

pumpkinscript = { version = "0.2", path = "../pumpkinscript" }
pumpkindb_engine = { version = "0.2", path = "../pumpkindb_engine" }
pumpkindb_mio_server = { version = "0.2", path = "../pumpkindb_mio_server" }
72 changes: 21 additions & 51 deletions pumpkindb_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,39 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#![feature(slice_patterns, advanced_slice_patterns)]

extern crate mio;
extern crate memmap;
extern crate byteorder;
extern crate rand;
extern crate num_cpus;
#[macro_use]
extern crate log;
extern crate log4rs;
extern crate slab;
extern crate num_bigint;
extern crate num_traits;
extern crate lmdb_zero as lmdb;
extern crate clap;
#[macro_use]
extern crate lazy_static;
extern crate config;
extern crate pumpkindb_engine;
extern crate num_cpus;
extern crate memmap;
#[macro_use]
extern crate clap;
extern crate uuid;
extern crate log;
extern crate log4rs;
extern crate lmdb_zero as lmdb;
extern crate mio;

extern crate pumpkinscript;
extern crate pumpkindb_engine;
extern crate pumpkindb_mio_server as server;

use pumpkindb_engine::{script, storage, timestamp};
use pumpkindb_engine::script::dispatcher;
use pumpkindb_engine::messaging;

use clap::{App, Arg};
use memmap::{Mmap, Protection};

mod connection;
mod server;
use std::thread;

use std::fs;
use std::fs::OpenOptions;
use std::path::PathBuf;
use std::thread;
use std::sync::Arc;

use memmap::{Mmap, Protection};
use mio::*;
use mio::tcp::*;
use clap::{App, Arg};
use mio::channel as mio_chan;

use pumpkindb_engine::{script, storage, timestamp};
use pumpkindb_engine::script::dispatcher;
use pumpkindb_engine::messaging;

lazy_static! {
static ref ENVIRONMENT: lmdb::Environment = {
Expand All @@ -62,27 +54,6 @@ lazy_static! {
};
}

use mio::channel as mio_chan;



pub fn run(port: i64,
senders: Vec<script::Sender<script::RequestMessage>>,
relay_sender: mio_chan::Sender<server::RelayedPublishedMessage>,
relay_receiver: mio_chan::Receiver<server::RelayedPublishedMessage>) {
let addr = format!("0.0.0.0:{}", port).parse().unwrap();

info!("Listening on {}", addr);

let sock = TcpListener::bind(&addr).expect("Failed to bind address");

let mut poll = Poll::new().expect("Failed to initialize polling");

let mut server = server::Server::new(sock, relay_sender, relay_receiver, senders);
server.run(&mut poll).expect("Failed to run server");

}

/// Accepts storage path, filename and length and prepares the file. It is important that the length
/// is the total length of the memory mapped file, otherwise the application _will segfault_ when
/// trying to read those sections later. There is no way to handle that.
Expand All @@ -103,7 +74,7 @@ fn prepare_mmap(storage_path: &str, filename: &str, length: u64) -> Mmap {
Mmap::open_path(scratchpad_path, Protection::ReadWrite).expect("Could not open scratchpad")
}

fn main() {
pub fn main() {
let args = App::new("PumpkinDB Server")
.version(crate_version!())
.about("Event Sourcing Database Engine http://pumpkindb.org")
Expand Down Expand Up @@ -192,7 +163,6 @@ fn main() {
senders.push(sender)
}

run(config::get_int("server.port").unwrap(),
server::run(config::get_int("server.port").unwrap(),
senders, relay_sender, relay_receiver);

}
}

0 comments on commit 63c3b66

Please sign in to comment.