Skip to content

Commit

Permalink
* #213
Browse files Browse the repository at this point in the history
  • Loading branch information
adamansky committed Mar 12, 2019
1 parent 612310f commit e19bb1c
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 27 deletions.
3 changes: 2 additions & 1 deletion ejdb2.project
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<Description/>
<Dependencies/>
<VirtualDirectory Name="src">
<File Name="src/jbserver.c"/>
<VirtualDirectory Name="jbr">
<File Name="src/jbr/jbr.h"/>
<File Name="src/jbr/jbr.c"/>
Expand Down Expand Up @@ -246,7 +247,7 @@
</Compiler>
<Linker Options="" Required="yes"/>
<ResourceCompiler Options="" Required="no"/>
<General OutputFile="$(IntermediateDirectory)/ejdb_test4" IntermediateDirectory="$(ProjectPath)/build/src/tests" Command="$(OutputFile)" CommandArguments="" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes" IsGUIProgram="no" IsEnabled="yes"/>
<General OutputFile="$(IntermediateDirectory)/jbserver" IntermediateDirectory="$(ProjectPath)/build/src" Command="$(OutputFile)" CommandArguments="--file zzz.db" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes" IsGUIProgram="no" IsEnabled="yes"/>
<BuildSystem Name="CMake">-G Ninja -DCMAKE_BUILD_TYPE=Debug</BuildSystem>
<Environment EnvVarSetName="&lt;Use Defaults&gt;" DbgSetName="&lt;Use Defaults&gt;">
<![CDATA[]]>
Expand Down
3 changes: 2 additions & 1 deletion hints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

#Prerequisites

apt-get install libuv1-dev libcunit1-dev
## GDB
handle SIGINT nostop print pass
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ else()
OUTPUT_NAME ejdb2-${PROJECT_VERSION_MAJOR})
endif()

# Executables
add_executable(jbserver jbserver.c)
target_link_libraries(jbserver ejdb2_s)

install(TARGETS ejdb2
EXPORT ejdb2-exports
FRAMEWORK DESTINATION ${FRAMEWORK_INSTALL_DIR}
Expand Down
5 changes: 4 additions & 1 deletion src/ejdb2.c
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,7 @@ iwrc ejdb_open(const EJDB_OPTS *_opts, EJDB *ejdbp) {
rc = jb_db_meta_load(db);
RCGO(rc, finish);

if (db->opts.http.enabled) {
if (db->opts.http.enabled && !db->opts.http.blocking) {
rc = jbr_start(db, &db->opts, &db->jbr);
RCGO(rc, finish);
}
Expand All @@ -1367,6 +1367,9 @@ iwrc ejdb_open(const EJDB_OPTS *_opts, EJDB *ejdbp) {
} else {
db->open = true;
*ejdbp = db;
if (db->opts.http.enabled && db->opts.http.blocking) {
rc = jbr_start(db, &db->opts, &db->jbr);
}
}
return rc;
}
Expand Down
1 change: 1 addition & 0 deletions src/ejdb2.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef struct EJDB_HTTP {
int port;
const char *bind;
const char *access_token;
bool blocking; /**< Block ejdb_open() thread until http service finished. */
} EJDB_HTTP;

typedef struct EJDB_IPC {
Expand Down
75 changes: 51 additions & 24 deletions src/jbr/jbr.c
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
#include "jbr.h"
#include <fio.h>
#include <fiobj.h>
#include <http/http.h>
#include <iowow/iwconv.h>
#include "ejdb2_internal.h"

struct _JBR {
volatile iwrc rc;
volatile bool terminated;
volatile iwrc rc;
pthread_t worker_thread;
pthread_barrier_t start_barrier;
const EJDB_HTTP *http;
EJDB db;
};

static void on_finish(struct http_settings_s *settings) {
iwlog_info2("HTTP server shutdown");
static iwrc jbr_query_visitor(EJDB_EXEC *ctx, EJDB_DOC doc, int64_t *step) {
return 0;
}

static void on_http_request(http_s *h) {
//fiobj_obj2cstr(h->method)
//h->method

//fio_url_parse()
// TODO:
http_send_body(h, "Hello World!", 12);
}

static void on_finish(struct http_settings_s *settings) {
iwlog_info2("HTTP endpoint closed");
}

static void on_pre_start(void *op) {
JBR jbr = op;
pthread_barrier_wait(&jbr->start_barrier);
if (!jbr->http->blocking) {
pthread_barrier_wait(&jbr->start_barrier);
}
}

static void *_jbr_start_thread(void *op) {
Expand All @@ -35,6 +47,7 @@ static void *_jbr_start_thread(void *op) {

iwlog_info("HTTP endpoint at %s:%s", bind, nbuf);
if (http_listen(nbuf, bind,
.udata = jbr,
.on_request = on_http_request,
.on_finish = on_finish,
.max_body_size = 1024 * 1024 * 64, // 64Mb
Expand All @@ -44,7 +57,9 @@ static void *_jbr_start_thread(void *op) {
iwlog_ecode_error2(jbr->rc, "Failed to start HTTP server");
}
if (jbr->rc) {
pthread_barrier_wait(&jbr->start_barrier);
if (!jbr->http->blocking) {
pthread_barrier_wait(&jbr->start_barrier);
}
return 0;
}
fio_state_callback_add(FIO_CALL_PRE_START, on_pre_start, jbr);
Expand All @@ -54,7 +69,6 @@ static void *_jbr_start_thread(void *op) {

static void _jbr_release(JBR *pjbr) {
JBR jbr = *pjbr;
pthread_barrier_destroy(&jbr->start_barrier);
free(jbr);
*pjbr = 0;
}
Expand All @@ -67,28 +81,39 @@ iwrc jbr_start(EJDB db, const EJDB_OPTS *opts, JBR *pjbr) {
}
JBR jbr = calloc(1, sizeof(*jbr));
if (!jbr) return iwrc_set_errno(IW_ERROR_ALLOC, errno);
jbr->db = db;
jbr->terminated = true;
jbr->http = &opts->http;

int rci = pthread_barrier_init(&jbr->start_barrier, 0, 2);
if (rci) {
free(jbr);
return iwrc_set_errno(IW_ERROR_THREADING_ERRNO, rci);
}
rci = pthread_create(&jbr->worker_thread, 0, _jbr_start_thread, jbr);
if (rci) {
if (!jbr->http->blocking) {
int rci = pthread_barrier_init(&jbr->start_barrier, 0, 2);
if (rci) {
free(jbr);
return iwrc_set_errno(IW_ERROR_THREADING_ERRNO, rci);
}
rci = pthread_create(&jbr->worker_thread, 0, _jbr_start_thread, jbr);
if (rci) {
pthread_barrier_destroy(&jbr->start_barrier);
free(jbr);
return iwrc_set_errno(IW_ERROR_THREADING_ERRNO, rci);
}
pthread_barrier_wait(&jbr->start_barrier);
pthread_barrier_destroy(&jbr->start_barrier);
free(jbr);
return iwrc_set_errno(IW_ERROR_THREADING_ERRNO, rci);
}
jbr->terminated = false;
pthread_barrier_wait(&jbr->start_barrier);
rc = jbr->rc;
if (rc) {
jbr_shutdown(pjbr);
return rc;
jbr->terminated = false;
rc = jbr->rc;
if (rc) {
jbr_shutdown(pjbr);
return rc;
}
*pjbr = jbr;
} else {
*pjbr = jbr;
jbr->terminated = false;
_jbr_start_thread(jbr); // Will block here
rc = jbr->rc;
jbr->terminated = true;
IWRC(jbr_shutdown(pjbr), rc);
}
*pjbr = jbr;
return rc;
}

Expand All @@ -100,7 +125,9 @@ iwrc jbr_shutdown(JBR *pjbr) {
if (__sync_bool_compare_and_swap(&jbr->terminated, 0, 1)) {
fio_state_callback_remove(FIO_CALL_PRE_START, on_pre_start, jbr);
fio_stop();
pthread_join(jbr->worker_thread, 0);
if (!jbr->http->blocking) {
pthread_join(jbr->worker_thread, 0);
}
}
_jbr_release(pjbr);
*pjbr = 0;
Expand Down
52 changes: 52 additions & 0 deletions src/jbserver.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "ejdb2.h"
#include "ejdb2cfg.h"
#include <fio.h>
#include <fio_cli.h>
#include <signal.h>

EJDB db;
EJDB_OPTS opts;

int main(int argc, char const *argv[]) {
iwrc rc = 0;
fio_cli_start(argc, argv, 0, 0,
"EJDB " EJDB2_VERSION " standalone server. http://ejdb.org\n",
FIO_CLI_STRING("--file -f Database file path. Default: db.jb"),
FIO_CLI_INT("--port -p HTTP port number to listen to. Default: 9191"),
FIO_CLI_STRING("--bind -b Address server listen. Default: 0.0.0.0"),
FIO_CLI_STRING("--access -a Server access token matched to X-Access-Token HTTP header value"),
FIO_CLI_BOOL("--trunc -t Truncate existing database file on open"),
FIO_CLI_BOOL("--daemon -d Run as daemon"),
FIO_CLI_BOOL("--wal -w Use write ahead logging (WAL)")
);
fio_cli_set_default("--file", "db.jb");
fio_cli_set_default("-f", "db.jb");
fio_cli_set_default("--port", "9191");
fio_cli_set_default("-p", "9191");

EJDB_OPTS ov = {
.kv = {
.path = fio_cli_get("-f"),
.oflags = fio_cli_get_i("-t") ? IWKV_TRUNC : 0
},
.no_wal = !fio_cli_get_i("-w"),
.http = {
.enabled = true,
.blocking = true,
.port = fio_cli_get_i("-p"),
.bind = fio_cli_get("-b"),
.access_token = fio_cli_get("-a")
}
};
memcpy(&opts, &ov, sizeof(ov));

rc = ejdb_open(&opts, &db);
RCGO(rc, finish);
IWRC(ejdb_close(&db), rc);
finish:
fio_cli_end();
if (rc) {
iwlog_ecode_error3(rc);
}
return rc ? 1 : 0;
}

0 comments on commit e19bb1c

Please sign in to comment.