diff --git a/overlay.c b/overlay.c index fdc895f1..62e4d1a3 100644 --- a/overlay.c +++ b/overlay.c @@ -112,12 +112,6 @@ int overlayServerMode() } overlay_queue_init(); - if (is_rhizome_enabled()){ - rhizome_opendb(); - if (config.rhizome.clean_on_start && !config.rhizome.clean_on_open) - rhizome_cleanup(NULL); - } - time_ms_t now = gettime_ms(); // Periodically check for server shut down diff --git a/rhizome_database.c b/rhizome_database.c index 04214a9c..b5bd4f1a 100644 --- a/rhizome_database.c +++ b/rhizome_database.c @@ -461,8 +461,7 @@ sqlite3_stmt *_sqlite_prepare(struct __sourceloc __whence, int log_level, sqlite { IN(); sqlite3_stmt *statement = NULL; - if (!rhizome_db && rhizome_opendb() == -1) - RETURN(NULL); + assert(rhizome_db); while (1) { switch (sqlite3_prepare_v2(rhizome_db, sqltext, -1, &statement, NULL)) { case SQLITE_OK: diff --git a/rhizome_direct.c b/rhizome_direct.c index 312007ee..ddd788fc 100644 --- a/rhizome_direct.c +++ b/rhizome_direct.c @@ -515,6 +515,7 @@ int app_rhizome_direct_sync(const struct cli_parsed *parsed, struct cli_context if (!strcasecmp(modeName,"push")) mode=1; /* push only */ if (!strcasecmp(modeName,"pull")) mode=2; /* pull only */ DEBUGF("sync direction = %d",mode); + rhizome_opendb(); if (parsed->args[3]) { struct config_rhizome_peer peer; const struct config_rhizome_peer *peers[1] = { &peer }; diff --git a/rhizome_sync.c b/rhizome_sync.c index 7d67773f..fdb746bf 100644 --- a/rhizome_sync.c +++ b/rhizome_sync.c @@ -482,7 +482,7 @@ void rhizome_sync_announce(struct sched_ent *alarm) int overlay_mdp_service_rhizome_sync(struct internal_mdp_header *header, struct overlay_buffer *payload) { - if (!config.rhizome.enable) + if (!config.rhizome.enable || !rhizome_db) return 0; struct rhizome_sync *state = header->source->sync_state; if (!state){ diff --git a/server.c b/server.c index 1094347c..56a0dae8 100644 --- a/server.c +++ b/server.c @@ -270,6 +270,16 @@ void server_watchdog(struct sched_ent *alarm) } } +DEFINE_ALARM(rhizome_open_db); +void rhizome_open_db(struct sched_ent *UNUSED(alarm)) +{ + if (config.rhizome.enable && !rhizome_db){ + rhizome_opendb(); + if (config.rhizome.clean_on_start && !config.rhizome.clean_on_open) + rhizome_cleanup(NULL); + } +} + void cf_on_config_change() { if (!serverMode) @@ -299,6 +309,12 @@ void cf_on_config_change() now+config.server.config_reload_interval_ms, TIME_MS_NEVER_WILL, now+config.server.config_reload_interval_ms+100); + + if (config.rhizome.enable){ + RESCHEDULE(&ALARM_STRUCT(rhizome_open_db), now+100, now+100, TIME_MS_NEVER_WILL); + }else if(rhizome_db){ + rhizome_close_db(); + } } /* Called periodically by the server process in its main loop. @@ -319,7 +335,7 @@ void server_shutdown_check(struct sched_ent *alarm) } } if (alarm){ - RESCHEDULE(alarm, now+1000, now+30000, now+5000); + RESCHEDULE(alarm, now+1000, now+30000, now+1100); } } diff --git a/tests/rhizomeprotocol b/tests/rhizomeprotocol index 2e2cfe86..8cb5ab45 100755 --- a/tests/rhizomeprotocol +++ b/tests/rhizomeprotocol @@ -503,6 +503,32 @@ test_MissingPayload() { executeOk $servald rhizome extract file $BID file1a } +doc_ConnectOnEnable="Enable and disable rhizome while fetching" +setup_ConnectOnEnable(){ + setup_common + set_instance +A + dd if=/dev/urandom of=file1 bs=1k count=1k 2>&1 + echo x >>file1 + rhizome_add_file file1 + foreach_instance +A +B \ + executeOk_servald config \ + set rhizome.enable off + start_servald_instances +A +B +} +test_ConnectOnEnable(){ + # TODO try to force disabling rhizome mid transfer + foreach_instance +A +B \ + executeOk_servald config \ + set rhizome.enable on \ + sync + sleep 2 + foreach_instance +A +B \ + executeOk_servald config \ + set rhizome.enable off \ + sync + sleep 3 +} + doc_HttpFetchRange="Fetch a file range using HTTP GET" setup_HttpFetchRange() { setup_curl 7