Skip to content

Commit

Permalink
Return failure from InitGenerator instead of asserting if sandesh
Browse files Browse the repository at this point in the history
HTTP initialization fails.
Partial-Bug: #1433668

Change-Id: I86acb9d4c60b6c3747018588f88a0d43188929a9
  • Loading branch information
Megh Bhatt committed Sep 16, 2015
1 parent 56c4adc commit 1c2c85e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 35 deletions.
46 changes: 29 additions & 17 deletions library/cpp/sandesh.cc
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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));
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -266,34 +272,40 @@ bool Sandesh::InitGenerator(const std::string &module,
CollectorSubFn csf,
const std::vector<std::string> &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,
EventManager *evm,
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() {
Expand Down
12 changes: 6 additions & 6 deletions library/cpp/sandesh.h
Expand Up @@ -150,7 +150,7 @@ class Sandesh {
CollectorSubFn csf,
const std::vector<std::string> &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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -346,7 +346,7 @@ class Sandesh {
const std::vector<std::string> &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,
Expand All @@ -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<SandeshRxQueue> recv_queue_;
static int recv_task_id_;
static SandeshContext *client_context_;
Expand Down
25 changes: 17 additions & 8 deletions library/cpp/sandesh_http.cc
Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions library/cpp/sandesh_http.h
Expand Up @@ -26,8 +26,8 @@ class SandeshHttp {
typedef boost::function<int32_t(SandeshRequest *)> 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 {
Expand Down
6 changes: 4 additions & 2 deletions library/cpp/test/sandesh_http_test.cc
Expand Up @@ -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());
Expand Down

0 comments on commit 1c2c85e

Please sign in to comment.