Skip to content

Commit

Permalink
PR comments applied
Browse files Browse the repository at this point in the history
  • Loading branch information
Przemyslaw Susko committed Oct 25, 2023
1 parent a7569bc commit 662f7b6
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 31 deletions.
13 changes: 8 additions & 5 deletions ebpfdiscoverysrv/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static po::options_description getProgramOptions() {
("log-dir", po::value<std::filesystem::path>()->default_value(""), "Log files directory")
("log-no-stdout", po::value<bool>()->default_value(false), "Disable logging to stdout")
("version", "Display program version")
("interval", po::value<int>()->default_value(60), "Services reporting time interval (in seconds)")
("interval", po::value<int>()->default_value(60), "Services reporting time interval (in seconds)")
;
// clang-format on

Expand Down Expand Up @@ -112,11 +112,11 @@ static void initLibbpf() {
}

void servicesProvidingLoop(ebpfdiscovery::Discovery& discoveryInstance, std::chrono::seconds interval) {
while (true) {
if (auto services = discoveryInstance.getServices(); !services.empty()) {
auto servicesProto = proto::Translator::internalToProto(services);
while (programStatus == ProgramStatus::Running) {
if (auto services = discoveryInstance.popServices(); !services.empty()) {
auto servicesProto = proto::internalToProto(services);
LOG_DEBUG("Services list:\n{}\n", servicesProto.DebugString());
auto servicesJson = proto::Translator::protoToJson(servicesProto);
auto servicesJson = proto::protoToJson(servicesProto);
std::cout << servicesJson << std::endl;
}
std::this_thread::sleep_for(interval);
Expand Down Expand Up @@ -193,6 +193,9 @@ int main(int argc, char** argv) {
if (unixSignalThread.joinable()) {
unixSignalThread.join();
}
if (servicesProvider.joinable()) {
servicesProvider.join();
}
instance.stop();
instance.wait();

Expand Down
2 changes: 1 addition & 1 deletion libebpfdiscovery/headers/ebpfdiscovery/Discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Discovery {
void stop();
void wait();

std::vector<service::Service> getServices();
std::vector<service::Service> popServices();

private:
using SavedSessionsCacheType = LRUCache<DiscoverySavedSessionKey, Session, DiscoverySavedSessionKeyHash>;
Expand Down
4 changes: 2 additions & 2 deletions libebpfdiscovery/src/Discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ void Discovery::handleNewRequest(const Session& session, const DiscoverySessionM
serviceAggregator.newRequest(request, meta);
}

std::vector<service::Service> Discovery::getServices() {
return serviceAggregator.getServices();
std::vector<service::Service> Discovery::popServices() {
return serviceAggregator.popServices();
}

void Discovery::handleCloseEvent(DiscoveryEvent& event) {
Expand Down
7 changes: 2 additions & 5 deletions libebpfdiscoveryproto/headers/ebpfdiscoveryproto/Translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@

namespace proto {

class Translator {
public:
static ServicesList internalToProto(const std::vector<service::Service>& internalServices);
ServicesList internalToProto(const std::vector<service::Service>& internalServices);

static std::string protoToJson(const ServicesList& protoServices);
};
std::string protoToJson(const ServicesList& protoServices);

} // namespace proto
4 changes: 2 additions & 2 deletions libebpfdiscoveryproto/src/Translator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace proto {

ServicesList Translator::internalToProto(const std::vector<service::Service>& internalServices) {
ServicesList internalToProto(const std::vector<service::Service>& internalServices) {
ServicesList external;
for (const auto& i : internalServices) {
const auto service{external.add_service()};
Expand All @@ -17,7 +17,7 @@ ServicesList Translator::internalToProto(const std::vector<service::Service>& in
return external;
}

std::string Translator::protoToJson(const ServicesList& protoServices) {
std::string protoToJson(const ServicesList& protoServices) {
std::string json;
google::protobuf::util::JsonPrintOptions options;
options.add_whitespace = false;
Expand Down
5 changes: 2 additions & 3 deletions libebpfdiscoveryproto/test/TranslatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using namespace proto;

struct ProtobufTranslatorTest : public testing::Test {
Translator translator;
const std::string expectedServicesJson{
"{\"service\":[{\"pid\":1,\"endpoint\":\"/endpoint/"
"1\",\"internalClientsNumber\":1,\"externalClientsNumber\":2},{\"pid\":2,\"endpoint\":\"/endpoint/"
Expand All @@ -26,7 +25,7 @@ struct ProtobufTranslatorTest : public testing::Test {
};

TEST_F(ProtobufTranslatorTest, successfulTranslation) {
auto proto = translator.internalToProto(internalServices);
auto json = translator.protoToJson(proto);
auto proto = internalToProto(internalServices);
auto json = protoToJson(proto);
EXPECT_EQ(json, expectedServicesJson);
}
2 changes: 1 addition & 1 deletion libservice/headers/service/Aggregator.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Aggregator {
public:
Aggregator(ebpfdiscovery::IpAddressCheckerInerface& ipChecker);

std::vector<Service> getServices();
std::vector<Service> popServices();

void newRequest(const httpparser::HttpRequest& request, const DiscoverySessionMeta& meta);

Expand Down
2 changes: 1 addition & 1 deletion libservice/src/Aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void Aggregator::newRequest(const httpparser::HttpRequest& request, const Discov
services[key] = std::move(newService);
}

std::vector<Service> Aggregator::getServices() {
std::vector<Service> Aggregator::popServices() {
locked = true;

std::vector<Service> ret;
Expand Down
22 changes: 11 additions & 11 deletions libservice/test/AggregatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class IpAddressCheckerMock : public IpAddressCheckerInerface {
};

struct ServiceAggregatorTest : public testing::Test {
std::pair<httpparser::HttpRequest, DiscoverySessionMeta> getRequest(
std::pair<httpparser::HttpRequest, DiscoverySessionMeta> makeRequest(
int pid, std::string host, std::string url, std::optional<__u8> flags = std::nullopt) {
httpparser::HttpRequest request;
request.host = host;
Expand All @@ -34,50 +34,50 @@ struct ServiceAggregatorTest : public testing::Test {
};

TEST_F(ServiceAggregatorTest, aggregate) {
EXPECT_EQ(aggregator.getServices().size(), 0);
EXPECT_EQ(aggregator.popServices().size(), 0);
// Service 1
{
const auto [request, meta]{getRequest(100, "host", "/url", DISCOVERY_SESSION_FLAGS_IPV4)};
const auto [request, meta]{makeRequest(100, "host", "/url", DISCOVERY_SESSION_FLAGS_IPV4)};
EXPECT_CALL(ipCheckerMock, isAddressExternalLocal).WillOnce(testing::Return(true));
aggregator.newRequest(request, meta);
}
{
auto [request, meta] = getRequest(100, "host", "/url");
auto [request, meta] = makeRequest(100, "host", "/url");
aggregator.newRequest(request, meta);
}
// Service 2
{
auto [request, meta] = getRequest(100, "host", "/url2", DISCOVERY_SESSION_FLAGS_IPV4);
auto [request, meta] = makeRequest(100, "host", "/url2", DISCOVERY_SESSION_FLAGS_IPV4);
EXPECT_CALL(ipCheckerMock, isAddressExternalLocal).WillOnce(testing::Return(false));
aggregator.newRequest(request, meta);
}
// Service 3
{
auto [request, meta] = getRequest(200, "host", "/url2", DISCOVERY_SESSION_FLAGS_IPV4);
auto [request, meta] = makeRequest(200, "host", "/url2", DISCOVERY_SESSION_FLAGS_IPV4);
EXPECT_CALL(ipCheckerMock, isAddressExternalLocal).WillOnce(testing::Return(true));
aggregator.newRequest(request, meta);
}
{
auto [request, meta] = getRequest(200, "host", "/url2", DISCOVERY_SESSION_FLAGS_IPV4);
auto [request, meta] = makeRequest(200, "host", "/url2", DISCOVERY_SESSION_FLAGS_IPV4);
EXPECT_CALL(ipCheckerMock, isAddressExternalLocal).WillOnce(testing::Return(false));
aggregator.newRequest(request, meta);
}
{
auto [request, meta] = getRequest(200, "host", "/url2", DISCOVERY_SESSION_FLAGS_IPV4);
auto [request, meta] = makeRequest(200, "host", "/url2", DISCOVERY_SESSION_FLAGS_IPV4);
EXPECT_CALL(ipCheckerMock, isAddressExternalLocal).WillOnce(testing::Return(true));
aggregator.newRequest(request, meta);
}

{
auto services = aggregator.getServices();
auto services = aggregator.popServices();
EXPECT_EQ(services.size(), 3);

Service expectedService1{.pid{100}, .endpoint{"host/url"}, .internalClientsNumber{0}, .externalClientsNumber{2}};
Service expectedService2{.pid{100}, .endpoint{"host/url2"}, .internalClientsNumber{1}, .externalClientsNumber{0}};
Service expectedService3{.pid{200}, .endpoint{"host/url2"}, .internalClientsNumber{1}, .externalClientsNumber{2}};
EXPECT_THAT(services, testing::Contains(expectedService1));
EXPECT_THAT(services, testing::Contains(expectedService2));
EXPECT_THAT(services, testing::Contains(expectedService3));
}
EXPECT_EQ(aggregator.getServices().size(), 0);
EXPECT_EQ(aggregator.popServices().size(), 0);
}

0 comments on commit 662f7b6

Please sign in to comment.