From 56981efdb83120cdff0f668d323113cbdb804b6b Mon Sep 17 00:00:00 2001 From: Megh Bhatt Date: Wed, 16 Sep 2015 10:40:42 -0700 Subject: [PATCH] Return failure from InitGenerator instead of asserting if sandesh HTTP initialization fails. Partial-Bug: #1433668 Change-Id: I86acb9d4c60b6c3747018588f88a0d43188929a9 (cherry picked from commit 1c2c85e8a37f9e02c5a08eff3ac64281fae05ef0) --- library/cpp/sandesh.cc | 46 +++++++++++++++++---------- library/cpp/sandesh.h | 12 +++---- library/cpp/sandesh_http.cc | 25 ++++++++++----- library/cpp/sandesh_http.h | 4 +-- library/cpp/test/sandesh_http_test.cc | 6 ++-- 5 files changed, 58 insertions(+), 35 deletions(-) diff --git a/library/cpp/sandesh.cc b/library/cpp/sandesh.cc index eeecdbd0..a7fda403 100644 --- a/library/cpp/sandesh.cc +++ b/library/cpp/sandesh.cc @@ -42,7 +42,7 @@ using namespace contrail::sandesh::transport; Sandesh::SandeshRole::type Sandesh::role_ = SandeshRole::Invalid; bool Sandesh::enable_local_log_ = false; bool Sandesh::enable_flow_log_ = false; -uint32_t Sandesh::http_port_ = 0; +int Sandesh::http_port_ = 0; bool Sandesh::enable_trace_print_ = false; bool Sandesh::send_queue_enabled_ = true; bool Sandesh::connect_to_collector_ = false; @@ -110,7 +110,7 @@ extern int PullSandeshGenStatsReq; extern int PullSandeshUVE; extern int PullSandeshTraceReq; -void Sandesh::Initialize(SandeshRole::type role, +bool Sandesh::Initialize(SandeshRole::type role, const std::string &module, const std::string &source, const std::string &node_type, @@ -123,7 +123,7 @@ void Sandesh::Initialize(SandeshRole::type role, PullSandeshTraceReq = 1; if (role_ != SandeshRole::Invalid || role == SandeshRole::Invalid) { - return; + return true; } SANDESH_LOG(INFO, "SANDESH: ROLE : " << SandeshRoleToString(role)); @@ -147,9 +147,15 @@ void Sandesh::Initialize(SandeshRole::type role, } InitReceive(Task::kTaskInstanceAny); - http_port_ = SandeshHttp::Init(evm, module, http_port, &SandeshHttpCallback); - + bool success(SandeshHttp::Init(evm, module, http_port, + &SandeshHttpCallback, &http_port_)); + if (!success) { + SANDESH_LOG(ERROR, "SANDESH: HTTP INIT FAILED (PORT " << + http_port << ")"); + return false; + } RecordPort("http", module_, http_port_); + return true; } void Sandesh::RecordPort(const std::string& name, const std::string& module, @@ -246,15 +252,15 @@ bool Sandesh::InitClient(EventManager *evm, return true; } -void Sandesh::InitGenerator(const std::string &module, +bool Sandesh::InitGenerator(const std::string &module, const std::string &source, const std::string &node_type, const std::string &instance_id, EventManager *evm, unsigned short http_port, SandeshContext *client_context) { - Initialize(SandeshRole::Generator, module, source, node_type, instance_id, - evm, http_port, client_context); + return Initialize(SandeshRole::Generator, module, source, node_type, + instance_id, evm, http_port, client_context); } bool Sandesh::InitGenerator(const std::string &module, @@ -266,13 +272,16 @@ bool Sandesh::InitGenerator(const std::string &module, CollectorSubFn csf, const std::vector &collectors, SandeshContext *client_context) { - Initialize(SandeshRole::Generator, module, source, node_type, instance_id, - evm, http_port, client_context); + bool success(Initialize(SandeshRole::Generator, module, source, node_type, + instance_id, evm, http_port, client_context)); + if (!success) { + return false; + } return InitClient(evm, collectors, csf); } // Collector -void Sandesh::InitCollector(const std::string &module, +bool Sandesh::InitCollector(const std::string &module, const std::string &source, const std::string &node_type, const std::string &instance_id, @@ -280,20 +289,23 @@ void Sandesh::InitCollector(const std::string &module, const std::string &collector_ip, int collector_port, unsigned short http_port, SandeshContext *client_context) { - Initialize(SandeshRole::Collector, module, source, node_type, instance_id, - evm, http_port, client_context); - ConnectToCollector(collector_ip, collector_port); + bool success(Initialize(SandeshRole::Collector, module, source, node_type, + instance_id, evm, http_port, client_context)); + if (!success) { + return false; + } + return ConnectToCollector(collector_ip, collector_port); } -void Sandesh::InitGeneratorTest(const std::string &module, +bool Sandesh::InitGeneratorTest(const std::string &module, const std::string &source, const std::string &node_type, const std::string &instance_id, EventManager *evm, unsigned short http_port, SandeshContext *client_context) { - Initialize(SandeshRole::Test, module, source, node_type, instance_id, evm, - http_port, client_context); + return Initialize(SandeshRole::Test, module, source, node_type, + instance_id, evm, http_port, client_context); } static void WaitForIdle() { diff --git a/library/cpp/sandesh.h b/library/cpp/sandesh.h index 23c486ac..a0c7d679 100644 --- a/library/cpp/sandesh.h +++ b/library/cpp/sandesh.h @@ -150,7 +150,7 @@ class Sandesh { CollectorSubFn csf, const std::vector &collectors, SandeshContext *client_context = NULL); - static void InitGenerator(const std::string &module, + static bool InitGenerator(const std::string &module, const std::string &source, const std::string &node_type, const std::string &instance_id, @@ -160,7 +160,7 @@ class Sandesh { static void RecordPort(const std::string& name, const std::string& module, unsigned short port); // Collector - static void InitCollector(const std::string &module, + static bool InitCollector(const std::string &module, const std::string &source, const std::string &node_type, const std::string &instance_id, @@ -169,7 +169,7 @@ class Sandesh { unsigned short http_port, SandeshContext *client_context = NULL); // Test - static void InitGeneratorTest(const std::string &module, + static bool InitGeneratorTest(const std::string &module, const std::string &source, const std::string &node_type, const std::string &instance_id, @@ -262,7 +262,7 @@ class Sandesh { static void set_node_type(std::string &node_type) { node_type_ = node_type; } static std::string node_type() { return node_type_; } static SandeshRole::type role() { return role_; } - static uint32_t http_port() { return http_port_; } + static int http_port() { return http_port_; } static SandeshRxQueue* recv_queue() { return recv_queue_.get(); } static SandeshContext* client_context() { return client_context_; } static void set_client_context(SandeshContext *context) { client_context_ = context; } @@ -346,7 +346,7 @@ class Sandesh { const std::vector &collectors, CollectorSubFn csf); static bool ProcessRecv(SandeshRequest *); - static void Initialize(SandeshRole::type role, const std::string &module, + static bool Initialize(SandeshRole::type role, const std::string &module, const std::string &source, const std::string &node_type, const std::string &instance_id, @@ -359,7 +359,7 @@ class Sandesh { static std::string source_; static std::string node_type_; static std::string instance_id_; - static uint32_t http_port_; + static int http_port_; static std::auto_ptr recv_queue_; static int recv_task_id_; static SandeshContext *client_context_; diff --git a/library/cpp/sandesh_http.cc b/library/cpp/sandesh_http.cc index a7f91305..7888a384 100644 --- a/library/cpp/sandesh_http.cc +++ b/library/cpp/sandesh_http.cc @@ -352,11 +352,13 @@ SandeshHttp::Response(Sandesh *snh, std::string context) { // module : Name of module // port : Port number for HTTP Server (e.g. 8080) // -int +bool SandeshHttp::Init(EventManager *evm, const string module, - short port, RequestCallbackFn reqcb) { - - if (hServ_) return hServ_->GetPort(); + short port, RequestCallbackFn reqcb, int *hport) { + if (hServ_) { + *hport = hServ_->GetPort(); + return true; + } ostringstream index_ss; SandeshTraceBufferPtr httpbuf(SandeshTraceBufferCreate("httpbuf", 100)); @@ -402,10 +404,17 @@ SandeshHttp::Init(EventManager *evm, const string module, boost::bind(&HttpSandeshRequestCallback, hServ_, _1, _2)); } - assert(hServ_->Initialize(port)); - SANDESH_LOG(DEBUG, "Sandesh Http Server Port " << hServ_->GetPort()); - - return hServ_->GetPort(); + bool success(hServ_->Initialize(port)); + if (success) { + int lport(hServ_->GetPort()); + SANDESH_LOG(DEBUG, "Sandesh Http Server Port: " << lport); + *hport = lport; + return true; + } else { + SANDESH_LOG(ERROR, "Failed to initialize Sandesh Http Server Port: " + << port); + return false; + } } // Function to shut down HTTP Server diff --git a/library/cpp/sandesh_http.h b/library/cpp/sandesh_http.h index 2270decd..5819b04d 100644 --- a/library/cpp/sandesh_http.h +++ b/library/cpp/sandesh_http.h @@ -26,8 +26,8 @@ class SandeshHttp { typedef boost::function RequestCallbackFn; static void Response(Sandesh *snh, std::string context); - static int Init(EventManager *evm, const std::string module, - short port, RequestCallbackFn reqcb); + static bool Init(EventManager *evm, const std::string module, + short port, RequestCallbackFn reqcb, int *hport); static void Uninit(void); class HtmlInfo { diff --git a/library/cpp/test/sandesh_http_test.cc b/library/cpp/test/sandesh_http_test.cc index 708806ea..a29e1f29 100644 --- a/library/cpp/test/sandesh_http_test.cc +++ b/library/cpp/test/sandesh_http_test.cc @@ -240,8 +240,10 @@ class SandeshHttpTest : public ::testing::Test { evm_.reset(new EventManager()); ServerThread *st = new ServerThread(evm_.get()); thread_.reset(st); - int port = SandeshHttp::Init(evm_.get(), "sandesh_http_test", 0, - boost::bind(&CallbackFn, this, _1)); + int port; + bool success(SandeshHttp::Init(evm_.get(), "sandesh_http_test", 0, + boost::bind(&CallbackFn, this, _1), &port)); + ASSERT_TRUE(success); host_url_ << "http://localhost:"; host_url_ << port << "/"; LOG(DEBUG, "Serving " << host_url_.str());