From e2c769f459fc2920518d1a4f5ea478f75acd287b Mon Sep 17 00:00:00 2001 From: rakshasa Date: Sat, 22 Oct 2016 10:51:41 +0900 Subject: [PATCH 1/4] Improved logging of dht manager. --- src/core/dht_manager.cc | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/core/dht_manager.cc b/src/core/dht_manager.cc index 8c4b7b1b..27bf7d00 100644 --- a/src/core/dht_manager.cc +++ b/src/core/dht_manager.cc @@ -54,6 +54,9 @@ #include "download_store.h" #include "manager.h" +#define LT_LOG_THIS(log_fmt, ...) \ + lt_log_print_subsystem(torrent::LOG_DHT_ALL, "dht_manager", log_fmt, __VA_ARGS__); + namespace core { const char* DhtManager::dht_settings[dht_settings_num] = { "disable", "off", "auto", "on" }; @@ -65,8 +68,10 @@ DhtManager::~DhtManager() { void DhtManager::load_dht_cache() { - if (m_start == dht_disable || !control->core()->download_store()->is_enabled()) + if (m_start == dht_disable || !control->core()->download_store()->is_enabled()) { + LT_LOG_THIS("ignoring cache file", 0); return; + } torrent::Object cache = torrent::Object::create_map(); std::fstream cache_file((control->core()->download_store()->path() + "rtorrent.dht_cache").c_str(), std::ios::in | std::ios::binary); @@ -77,8 +82,10 @@ DhtManager::load_dht_cache() { // If the cache file is corrupted we will just discard it with an // error message. if (cache_file.fail()) { - lt_log_print(torrent::LOG_DHT_WARN, "DHT cache file corrupted, discarding."); + LT_LOG_THIS("cache file corrupted, discarding", 0); cache = torrent::Object::create_map(); + } else { + LT_LOG_THIS("cache file loaded", 0); } } @@ -89,7 +96,7 @@ DhtManager::load_dht_cache() { start_dht(); } catch (torrent::local_error& e) { - lt_log_print(torrent::LOG_DHT_WARN, "DHT failed: %s", e.what()); + LT_LOG_THIS("initialization failed (error:%s)", e.what()); } } @@ -97,18 +104,21 @@ void DhtManager::start_dht() { priority_queue_erase(&taskScheduler, &m_stopTimeout); - if (!torrent::dht_manager()->is_valid() || torrent::dht_manager()->is_active()) + if (!torrent::dht_manager()->is_valid() || torrent::dht_manager()->is_active()) { + LT_LOG_THIS("server start skipped", 0); return; + } torrent::ThrottlePair throttles = control->core()->get_throttle(m_throttleName); torrent::dht_manager()->set_upload_throttle(throttles.first); torrent::dht_manager()->set_download_throttle(throttles.second); int port = rpc::call_command_value("dht.port"); + if (port <= 0) return; - lt_log_print(torrent::LOG_DHT_INFO, "Starting DHT server on port %d.", port); + LT_LOG_THIS("starting server (port:%d)", port); try { torrent::dht_manager()->start(port); @@ -125,7 +135,7 @@ DhtManager::start_dht() { m_dhtPrevBytesDown = 0; } catch (torrent::local_error& e) { - lt_log_print(torrent::LOG_DHT_ERROR, "DHT start failed: %s", e.what()); + LT_LOG_THIS("server start failed (error:%s)", e.what()); m_start = dht_off; } } @@ -136,8 +146,9 @@ DhtManager::stop_dht() { priority_queue_erase(&taskScheduler, &m_stopTimeout); if (torrent::dht_manager()->is_active()) { + LT_LOG_THIS("stopping server", 0); + log_statistics(true); - lt_log_print(torrent::LOG_DHT_INFO, "Stopping DHT server."); torrent::dht_manager()->stop(); } } @@ -219,7 +230,7 @@ DhtManager::log_statistics(bool force) { // We should have had clients ping us at least but have received // nothing, that means the UDP port is probably unreachable. if (torrent::dht_manager()->can_receive_queries()) - lt_log_print(torrent::LOG_DHT_WARN, "DHT port appears to be unreachable, no queries received."); + LT_LOG_THIS("listening port appears to be unreachable, no queries received", 0); torrent::dht_manager()->set_can_receive(false); } @@ -227,7 +238,7 @@ DhtManager::log_statistics(bool force) { if (stats.queries_sent - m_dhtPrevQueriesSent > stats.num_nodes * 2 + 20 && stats.replies_received == m_dhtPrevRepliesReceived) { // No replies to over 20 queries plus two per node we have. Probably firewalled. if (!m_warned) - lt_log_print(torrent::LOG_DHT_WARN, "DHT port appears to be firewalled, no replies received."); + LT_LOG_THIS("listening port appears to be firewalled, no replies received", 0); m_warned = true; return false; From e02c6b29fa6b0dfc2e0da563a39458d0eff9e94d Mon Sep 17 00:00:00 2001 From: rakshasa Date: Sat, 22 Oct 2016 11:21:56 +0900 Subject: [PATCH 2/4] More logging stuff. --- src/core/dht_manager.cc | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/core/dht_manager.cc b/src/core/dht_manager.cc index 27bf7d00..ab379048 100644 --- a/src/core/dht_manager.cc +++ b/src/core/dht_manager.cc @@ -55,7 +55,7 @@ #include "manager.h" #define LT_LOG_THIS(log_fmt, ...) \ - lt_log_print_subsystem(torrent::LOG_DHT_ALL, "dht_manager", log_fmt, __VA_ARGS__); + lt_log_print_subsystem(torrent::LOG_DHT_MANAGER, "dht_manager", log_fmt, __VA_ARGS__); namespace core { @@ -73,20 +73,25 @@ DhtManager::load_dht_cache() { return; } - torrent::Object cache = torrent::Object::create_map(); - std::fstream cache_file((control->core()->download_store()->path() + "rtorrent.dht_cache").c_str(), std::ios::in | std::ios::binary); + std::string cache_filename = control->core()->download_store()->path() + "rtorrent.dht_cache"; + std::fstream cache_stream(cache_filename.c_str(), std::ios::in | std::ios::binary); - if (cache_file.is_open()) { - cache_file >> cache; + torrent::Object cache = torrent::Object::create_map(); + + if (cache_stream.is_open()) { + cache_stream >> cache; // If the cache file is corrupted we will just discard it with an // error message. - if (cache_file.fail()) { - LT_LOG_THIS("cache file corrupted, discarding", 0); + if (cache_stream.fail()) { + LT_LOG_THIS("cache file corrupted, discarding (path:%s)", cache_filename.c_str()); cache = torrent::Object::create_map(); } else { - LT_LOG_THIS("cache file loaded", 0); + LT_LOG_THIS("cache file loaded (path:%s)", cache_filename.c_str()); } + + } else { + LT_LOG_THIS("could not open cache file (path:%s)", cache_filename.c_str()); } try { @@ -104,8 +109,13 @@ void DhtManager::start_dht() { priority_queue_erase(&taskScheduler, &m_stopTimeout); - if (!torrent::dht_manager()->is_valid() || torrent::dht_manager()->is_active()) { - LT_LOG_THIS("server start skipped", 0); + if (!torrent::dht_manager()->is_valid()) { + LT_LOG_THIS("server start skipped, manager is invalid", 0); + return; + } + + if (torrent::dht_manager()->is_active()) { + LT_LOG_THIS("server start skipped, already active", 0); return; } From 008561ef47fa39d4c63582d8c54a856ade7598b6 Mon Sep 17 00:00:00 2001 From: rakshasa Date: Sat, 22 Oct 2016 12:33:31 +0900 Subject: [PATCH 3/4] More dht manager logging. --- src/core/dht_manager.cc | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/core/dht_manager.cc b/src/core/dht_manager.cc index ab379048..24a52a36 100644 --- a/src/core/dht_manager.cc +++ b/src/core/dht_manager.cc @@ -87,22 +87,17 @@ DhtManager::load_dht_cache() { LT_LOG_THIS("cache file corrupted, discarding (path:%s)", cache_filename.c_str()); cache = torrent::Object::create_map(); } else { - LT_LOG_THIS("cache file loaded (path:%s)", cache_filename.c_str()); + LT_LOG_THIS("cache file read (path:%s)", cache_filename.c_str()); } } else { LT_LOG_THIS("could not open cache file (path:%s)", cache_filename.c_str()); } - try { - torrent::dht_manager()->initialize(cache); + torrent::dht_manager()->initialize(cache); - if (m_start == dht_on) - start_dht(); - - } catch (torrent::local_error& e) { - LT_LOG_THIS("initialization failed (error:%s)", e.what()); - } + if (m_start == dht_on) + start_dht(); } void @@ -110,7 +105,7 @@ DhtManager::start_dht() { priority_queue_erase(&taskScheduler, &m_stopTimeout); if (!torrent::dht_manager()->is_valid()) { - LT_LOG_THIS("server start skipped, manager is invalid", 0); + LT_LOG_THIS("server start skipped, manager is uninitialized", 0); return; } From 2aa130eda967981073aa8a5eb2d99d5b523c7120 Mon Sep 17 00:00:00 2001 From: rakshasa Date: Sat, 22 Oct 2016 14:19:46 +0900 Subject: [PATCH 4/4] More work on dht manager. --- src/core/dht_manager.cc | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/core/dht_manager.cc b/src/core/dht_manager.cc index 24a52a36..5628cb2b 100644 --- a/src/core/dht_manager.cc +++ b/src/core/dht_manager.cc @@ -123,26 +123,22 @@ DhtManager::start_dht() { if (port <= 0) return; - LT_LOG_THIS("starting server (port:%d)", port); - - try { - torrent::dht_manager()->start(port); - torrent::dht_manager()->reset_statistics(); - - m_updateTimeout.slot() = std::bind(&DhtManager::update, this); - priority_queue_insert(&taskScheduler, &m_updateTimeout, (cachedTime + rak::timer::from_seconds(60)).round_seconds()); - - m_dhtPrevCycle = 0; - m_dhtPrevQueriesSent = 0; - m_dhtPrevRepliesReceived = 0; - m_dhtPrevQueriesReceived = 0; - m_dhtPrevBytesUp = 0; - m_dhtPrevBytesDown = 0; - - } catch (torrent::local_error& e) { - LT_LOG_THIS("server start failed (error:%s)", e.what()); + if (!torrent::dht_manager()->start(port)) { m_start = dht_off; + return; } + + torrent::dht_manager()->reset_statistics(); + + m_updateTimeout.slot() = std::bind(&DhtManager::update, this); + priority_queue_insert(&taskScheduler, &m_updateTimeout, (cachedTime + rak::timer::from_seconds(60)).round_seconds()); + + m_dhtPrevCycle = 0; + m_dhtPrevQueriesSent = 0; + m_dhtPrevRepliesReceived = 0; + m_dhtPrevQueriesReceived = 0; + m_dhtPrevBytesUp = 0; + m_dhtPrevBytesDown = 0; } void