From 2f83d4542f2ed344d25d22799736c64bb7d49b8b Mon Sep 17 00:00:00 2001
From: Sergey Yakubov <sergey.yakubov@desy.de>
Date: Fri, 11 Oct 2019 13:09:03 +0200
Subject: [PATCH] advertise ip for receiver, update config files

---
 common/cpp/src/system_io/system_io_windows.cpp   |  2 ++
 deploy/docker/cluster/scripts/receiver.json.tpl  |  1 +
 deploy/nomad_jobs/receiver.json.tpl              |  1 +
 receiver/src/receiver_config.cpp                 |  6 +-----
 receiver/src/receiver_config.h                   |  2 +-
 receiver/src/request_handler_db_write.cpp        |  2 +-
 receiver/unittests/mock_receiver_config.cpp      | 16 ++--------------
 receiver/unittests/test_config.cpp               |  8 +++++---
 .../unittests/test_request_handler_db_writer.cpp |  6 +++---
 .../automatic/settings/receiver.json.tpl.lin.in  |  1 +
 .../automatic/settings/receiver.json.tpl.win.in  |  1 +
 .../python_tests/producer/receiver.json.tpl      |  1 +
 12 files changed, 20 insertions(+), 27 deletions(-)

diff --git a/common/cpp/src/system_io/system_io_windows.cpp b/common/cpp/src/system_io/system_io_windows.cpp
index 81272027b..a8b90ee39 100644
--- a/common/cpp/src/system_io/system_io_windows.cpp
+++ b/common/cpp/src/system_io/system_io_windows.cpp
@@ -49,6 +49,8 @@ Error IOErrorFromGetLastError() {
         return IOErrorTemplates::kPermissionDenied.Generate();
     case ERROR_CONNECTION_REFUSED:
         return IOErrorTemplates::kConnectionRefused.Generate();
+    case WSAEADDRNOTAVAIL:
+        return IOErrorTemplates::kUnreachableNetwork.Generate();
     case WSAEFAULT:
         return IOErrorTemplates::kInvalidMemoryAddress.Generate();
     case WSAECONNRESET:
diff --git a/deploy/docker/cluster/scripts/receiver.json.tpl b/deploy/docker/cluster/scripts/receiver.json.tpl
index b1948f8b1..6a9236bcf 100644
--- a/deploy/docker/cluster/scripts/receiver.json.tpl
+++ b/deploy/docker/cluster/scripts/receiver.json.tpl
@@ -1,4 +1,5 @@
 {
+  "AdvertiseIP": "{{ env "NOMAD_IP_recv" }}",
   "PerformanceDbServer":"localhost:8400/influxdb",
   "PerformanceDbName": "asapo_receivers",
   "DatabaseServer":"auto",
diff --git a/deploy/nomad_jobs/receiver.json.tpl b/deploy/nomad_jobs/receiver.json.tpl
index 648f8b59f..8db2c97b3 100644
--- a/deploy/nomad_jobs/receiver.json.tpl
+++ b/deploy/nomad_jobs/receiver.json.tpl
@@ -1,4 +1,5 @@
 {
+  "AdvertiseIP": "127.0.0.1",
   "PerformanceDbServer":"localhost:8400/influxdb",
   "PerformanceDbName": "asapo_receivers",
   "DatabaseServer":"auto",
diff --git a/receiver/src/receiver_config.cpp b/receiver/src/receiver_config.cpp
index ae55ea992..8401555df 100644
--- a/receiver/src/receiver_config.cpp
+++ b/receiver/src/receiver_config.cpp
@@ -34,6 +34,7 @@ Error ReceiverConfigFactory::SetConfig(std::string file_name) {
     (err = parser.GetUInt64("AuthorizationInterval", &config.authorization_interval_ms)) ||
     (err = parser.GetString("RootFolder", &config.root_folder)) ||
     (err = parser.GetString("PerformanceDbName", &config.performance_db_name)) ||
+    (err = parser.GetString("AdvertiseIP", &config.advertise_ip)) ||
     (err = parser.GetString("LogLevel", &log_level));
 
     if (err) {
@@ -42,11 +43,6 @@ Error ReceiverConfigFactory::SetConfig(std::string file_name) {
 
     config.dataserver.tag = config.tag + "_ds";
 
-    config.source_host = io__->GetHostName(&err);
-    if (err) {
-        return err;
-    }
-
     config.log_level = StringToLogLevel(log_level, &err);
     return err;
 
diff --git a/receiver/src/receiver_config.h b/receiver/src/receiver_config.h
index 7052ac71e..831e72232 100644
--- a/receiver/src/receiver_config.h
+++ b/receiver/src/receiver_config.h
@@ -23,7 +23,7 @@ struct ReceiverConfig {
     uint64_t datacache_reserved_share = 0;
     LogLevel log_level = LogLevel::Info;
     std::string tag;
-    std::string source_host;
+    std::string advertise_ip;
     ReceiverDataCenterConfig dataserver;
     std::string discovery_server;
 };
diff --git a/receiver/src/request_handler_db_write.cpp b/receiver/src/request_handler_db_write.cpp
index 19670a85b..91580bfae 100644
--- a/receiver/src/request_handler_db_write.cpp
+++ b/receiver/src/request_handler_db_write.cpp
@@ -56,7 +56,7 @@ FileInfo RequestHandlerDbWrite::PrepareFileInfo(const Request* request) const {
     file_info.size = request->GetDataSize();
     file_info.id = request->GetDataID();
     file_info.buf_id = request->GetSlotId();
-    file_info.source = GetReceiverConfig()->source_host + ":" + string_format("%ld",
+    file_info.source = GetReceiverConfig()->advertise_ip + ":" + string_format("%ld",
                        GetReceiverConfig()->dataserver.listen_port);
     file_info.metadata = request->GetMetaData();
     return file_info;
diff --git a/receiver/unittests/mock_receiver_config.cpp b/receiver/unittests/mock_receiver_config.cpp
index 29805ebde..1ddc3ed4c 100644
--- a/receiver/unittests/mock_receiver_config.cpp
+++ b/receiver/unittests/mock_receiver_config.cpp
@@ -61,6 +61,7 @@ Error SetReceiverConfig (const ReceiverConfig& config, std::string error_field)
     config_string += "," +  Key("WriteToDisk", error_field) + (config.write_to_disk ? "true" : "false");
     config_string += "," +  Key("WriteToDb", error_field) + (config.write_to_db ? "true" : "false");
     config_string += "," +  Key("LogLevel", error_field) + "\"" + log_level + "\"";
+    config_string += "," +  Key("AdvertiseIP", error_field) + "\"" + config.advertise_ip + "\"";
     config_string += "," +  Key("Tag", error_field) + "\"" + config.tag + "\"";
     config_string += "," +  Key("RootFolder", error_field) + "\"" + config.root_folder + "\"";
     config_string += "}";
@@ -70,20 +71,7 @@ Error SetReceiverConfig (const ReceiverConfig& config, std::string error_field)
         testing::Return(config_string)
     );
 
-    if (error_field == "SourceHost") {
-        EXPECT_CALL(mock_io, GetHostName_t(_)).
-        WillOnce(
-            DoAll(SetArgPointee<0>(asapo::IOErrorTemplates::kUnknownIOError.Generate().release()),
-                  Return("")
-                 ));
-    } else if (error_field == "none") {
-        EXPECT_CALL(mock_io, GetHostName_t(_)).
-        WillOnce(
-            DoAll(SetArgPointee<0>(nullptr),
-                  Return(config.source_host)
-                 ));
-    }
-
+    printf("%s\n", config_string.c_str());
     auto err = config_factory.SetConfig("fname");
 
     config_factory.io__.release();
diff --git a/receiver/unittests/test_config.cpp b/receiver/unittests/test_config.cpp
index 6231fed90..aca938cff 100644
--- a/receiver/unittests/test_config.cpp
+++ b/receiver/unittests/test_config.cpp
@@ -59,9 +59,10 @@ class ConfigTests : public Test {
         test_config.use_datacache = false;
         test_config.datacache_reserved_share = 10;
         test_config.datacache_size_gb = 2;
-        test_config.source_host = "host";
+        test_config.advertise_ip = "host";
         test_config.dataserver.nthreads = 5;
         test_config.discovery_server = "discovery";
+        test_config.advertise_ip = "0.0.0.1";
     }
 
 };
@@ -90,10 +91,11 @@ TEST_F(ConfigTests, ReadSettings) {
     ASSERT_THAT(config->use_datacache, Eq(false));
     ASSERT_THAT(config->datacache_reserved_share, Eq(10));
     ASSERT_THAT(config->datacache_size_gb, Eq(2));
-    ASSERT_THAT(config->source_host, Eq("host"));
     ASSERT_THAT(config->dataserver.nthreads, Eq(5));
     ASSERT_THAT(config->dataserver.tag, Eq("receiver1_ds"));
     ASSERT_THAT(config->discovery_server, Eq("discovery"));
+    ASSERT_THAT(config->advertise_ip, Eq("0.0.0.1"));
+
 }
 
 
@@ -103,7 +105,7 @@ TEST_F(ConfigTests, ErrorReadSettings) {
     std::vector<std::string>fields {"PerformanceDbServer", "ListenPort", "DataServer", "ListenPort", "WriteToDisk",
                                     "WriteToDb", "DataCache", "Use", "SizeGB", "ReservedShare", "DatabaseServer", "Tag",
                                     "AuthorizationServer", "AuthorizationInterval", "RootFolder", "PerformanceDbName", "LogLevel",
-                                    "SourceHost", "NThreads", "DiscoveryServer"};
+                                    "NThreads", "DiscoveryServer", "AdvertiseIP"};
     for (const auto& field : fields) {
         auto err = asapo::SetReceiverConfig(test_config, field);
         ASSERT_THAT(err, Ne(nullptr));
diff --git a/receiver/unittests/test_request_handler_db_writer.cpp b/receiver/unittests/test_request_handler_db_writer.cpp
index 11047e9e9..19c1ec322 100644
--- a/receiver/unittests/test_request_handler_db_writer.cpp
+++ b/receiver/unittests/test_request_handler_db_writer.cpp
@@ -71,7 +71,7 @@ class DbWriterHandlerTests : public Test {
     std::string expected_beamtime_id = "beamtime_id";
     std::string expected_default_stream = "detector";
     std::string expected_stream = "stream";
-    std::string expected_hostname = "host";
+    std::string expected_host_ip = "127.0.0.1";
     uint64_t expected_port = 1234;
     uint64_t expected_buf_id = 18446744073709551615ull;
     std::string expected_file_name = "2";
@@ -89,7 +89,7 @@ class DbWriterHandlerTests : public Test {
         handler.log__ = &mock_logger;
         mock_request.reset(new NiceMock<MockRequest> {request_header, 1, ""});
         config.database_uri = "127.0.0.1:27017";
-        config.source_host = expected_hostname;
+        config.advertise_ip = expected_host_ip;
         config.dataserver.listen_port = expected_port;
         SetReceiverConfig(config, "none");
 
@@ -173,7 +173,7 @@ FileInfo DbWriterHandlerTests::PrepareFileInfo() {
     file_info.name = expected_file_name;
     file_info.id = expected_id;
     file_info.buf_id = expected_buf_id;
-    file_info.source = expected_hostname + ":" + std::to_string(expected_port);
+    file_info.source = expected_host_ip + ":" + std::to_string(expected_port);
     file_info.metadata = expected_metadata;
     return file_info;
 }
diff --git a/tests/automatic/settings/receiver.json.tpl.lin.in b/tests/automatic/settings/receiver.json.tpl.lin.in
index 2f27019b7..86fa61c0d 100644
--- a/tests/automatic/settings/receiver.json.tpl.lin.in
+++ b/tests/automatic/settings/receiver.json.tpl.lin.in
@@ -1,4 +1,5 @@
 {
+  "AdvertiseIP": "127.0.0.1",
   "PerformanceDbServer":"localhost:8086",
   "PerformanceDbName": "db_test",
   "DatabaseServer":"auto",
diff --git a/tests/automatic/settings/receiver.json.tpl.win.in b/tests/automatic/settings/receiver.json.tpl.win.in
index 7fcedb822..4cc81d11d 100644
--- a/tests/automatic/settings/receiver.json.tpl.win.in
+++ b/tests/automatic/settings/receiver.json.tpl.win.in
@@ -1,4 +1,5 @@
 {
+  "AdvertiseIP": "127.0.0.1",
   "PerformanceDbServer":"localhost:8086",
   "PerformanceDbName": "db_test",
   "DatabaseServer":"auto",
diff --git a/tests/manual/python_tests/producer/receiver.json.tpl b/tests/manual/python_tests/producer/receiver.json.tpl
index 88be63ae3..0eaaa9848 100644
--- a/tests/manual/python_tests/producer/receiver.json.tpl
+++ b/tests/manual/python_tests/producer/receiver.json.tpl
@@ -1,4 +1,5 @@
 {
+  "AdvertiseIP": "127.0.0.1",
   "PerformanceDbServer":"localhost:8086",
   "PerformanceDbName": "db_test",
   "DatabaseServer":"localhost:27017",
-- 
GitLab