From 098e369a3b9bd0abcd6371f3f7fb356bb8120e14 Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Thu, 20 Jan 2022 00:45:49 +0300 Subject: [PATCH] Fix rand initialization, add etcd connection/disconnection logging --- src/etcd_state_client.cpp | 18 +++++++++++++++++- src/etcd_state_client.h | 2 ++ src/msgr_rdma.cpp | 4 +++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/etcd_state_client.cpp b/src/etcd_state_client.cpp index 66108bf3..4526b93e 100644 --- a/src/etcd_state_client.cpp +++ b/src/etcd_state_client.cpp @@ -194,9 +194,16 @@ void etcd_state_client_t::pick_next_etcd() std::vector ns; for (int i = 0; i < etcd_addresses.size(); i++) ns.push_back(i); + if (!rand_initialized) + { + timespec tv; + clock_gettime(CLOCK_REALTIME, &tv); + srand48(tv.tv_sec*1000000000 + tv.tv_nsec); + rand_initialized = true; + } while (ns.size()) { - int i = rand() % ns.size(); + int i = lrand48() % ns.size(); addresses_to_try.push_back(etcd_addresses[ns[i]]); ns.erase(ns.begin()+i, ns.begin()+i+1); } @@ -244,6 +251,8 @@ void etcd_state_client_t::start_etcd_watcher() { if (data["result"]["created"].bool_value()) { + if (etcd_watches_initialised == 3 && this->log_level > 0) + fprintf(stderr, "Successfully subscribed to etcd at %s\n", selected_etcd_address.c_str()); etcd_watches_initialised++; } if (data["result"]["canceled"].bool_value()) @@ -309,8 +318,11 @@ void etcd_state_client_t::start_etcd_watcher() { if (cur_addr == selected_etcd_address) { + fprintf(stderr, "Disconnected from etcd %s\n", selected_etcd_address.c_str()); selected_etcd_address = ""; } + else + fprintf(stderr, "Disconnected from etcd\n"); http_close(etcd_watch_ws); etcd_watch_ws = NULL; if (etcd_watches_initialised == 0) @@ -374,6 +386,10 @@ void etcd_state_client_t::start_etcd_watcher() } else if (!ws_alive) { + if (this->log_level > 0) + { + fprintf(stderr, "Websocket ping failed, disconnecting from etcd %s\n", selected_etcd_address.c_str()); + } http_close(etcd_watch_ws); etcd_watch_ws = NULL; start_etcd_watcher(); diff --git a/src/etcd_state_client.h b/src/etcd_state_client.h index 9873f993..0c42402b 100644 --- a/src/etcd_state_client.h +++ b/src/etcd_state_client.h @@ -12,6 +12,7 @@ #define ETCD_PG_HISTORY_WATCH_ID 3 #define ETCD_OSD_STATE_WATCH_ID 4 +// FIXME: Remove hardcode #define MAX_ETCD_ATTEMPTS 5 #define ETCD_SLOW_TIMEOUT 5000 #define ETCD_QUICK_TIMEOUT 1000 @@ -85,6 +86,7 @@ protected: http_co_t *etcd_watch_ws = NULL, *keepalive_client = NULL; int ws_keepalive_timer = -1; int ws_alive = 0; + bool rand_initialized = false; uint64_t bs_block_size = DEFAULT_BLOCK_SIZE; int etcd_keepalive_interval = 10; void add_etcd_url(std::string); diff --git a/src/msgr_rdma.cpp b/src/msgr_rdma.cpp index 9191e43a..1d267274 100644 --- a/src/msgr_rdma.cpp +++ b/src/msgr_rdma.cpp @@ -58,7 +58,9 @@ msgr_rdma_context_t *msgr_rdma_context_t::create(const char *ib_devname, uint8_t msgr_rdma_context_t *ctx = new msgr_rdma_context_t(); ctx->mtu = mtu; - srand48(time(NULL)); + timespec tv; + clock_gettime(CLOCK_REALTIME, &tv); + srand48(tv.tv_sec*1000000000 + tv.tv_nsec); dev_list = ibv_get_device_list(NULL); if (!dev_list) {