-
Notifications
You must be signed in to change notification settings - Fork 25
Use the new collector internal service for process information #2063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Molter73
wants to merge
14
commits into
master
Choose a base branch
from
mauro/ROX-28526-use-collector-iservice
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
92e7202
Basic implementation using the new collector internal service
Molter73 bfe29a8
Add CollectorOutput component
Molter73 46ff087
Cleanup
Molter73 8f22e7a
Update mock sensor to use the newer collector-sensor service
Molter73 b91ffa3
Restore ProcessSignalFormatter, add SensorClientFormatter
Molter73 8e0e05c
Unify client reconnection thread in CollectorOutput
Molter73 6750efd
Remove unneeded operator= overloads
Molter73 e0649e7
Add some unit tests
Molter73 fc606f5
Use an env variable for legacy services configuration
Molter73 27ce867
Restore some log messages needed for testing in the main repo
Molter73 ccf5257
Address PR comments
Molter73 055f3a5
Minor cleanup
Molter73 c411fe1
Rename Refresh to Recreate
Molter73 a1d1772
Use pragma once on new header files
Molter73 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
#include "CollectorOutput.h" | ||
|
||
#include "internalapi/sensor/collector_iservice.pb.h" | ||
|
||
#include "GRPCUtil.h" | ||
#include "HostInfo.h" | ||
|
||
namespace collector { | ||
|
||
CollectorOutput::CollectorOutput(const CollectorConfig& config) | ||
: use_sensor_client_(!config.UseLegacyServices()) { | ||
if (config.grpc_channel != nullptr) { | ||
channel_ = config.grpc_channel; | ||
|
||
if (use_sensor_client_) { | ||
auto sensor_client = std::make_unique<SensorClient>(channel_); | ||
sensor_clients_.emplace_back(std::move(sensor_client)); | ||
} else { | ||
auto signal_client = std::make_unique<SignalServiceClient>(channel_); | ||
signal_clients_.emplace_back(std::move(signal_client)); | ||
} | ||
} | ||
|
||
if (config.grpc_channel == nullptr || config.UseStdout()) { | ||
if (use_sensor_client_) { | ||
auto sensor_client = std::make_unique<SensorClientStdout>(); | ||
sensor_clients_.emplace_back(std::move(sensor_client)); | ||
} else { | ||
auto signal_client = std::make_unique<StdoutSignalServiceClient>(); | ||
signal_clients_.emplace_back(std::move(signal_client)); | ||
} | ||
} | ||
|
||
thread_.Start([this] { EstablishGrpcStream(); }); | ||
} | ||
|
||
void CollectorOutput::HandleOutputError() { | ||
CLOG(ERROR) << "GRPC stream interrupted"; | ||
stream_active_.store(false, std::memory_order_release); | ||
stream_interrupted_.notify_one(); | ||
} | ||
|
||
SignalHandler::Result CollectorOutput::SensorOutput(const sensor::MsgFromCollector& msg) { | ||
for (auto& client : sensor_clients_) { | ||
auto res = client->SendMsg(msg); | ||
switch (res) { | ||
case SignalHandler::PROCESSED: | ||
break; | ||
|
||
case SignalHandler::ERROR: | ||
HandleOutputError(); | ||
return res; | ||
|
||
case SignalHandler::IGNORED: | ||
case SignalHandler::NEEDS_REFRESH: | ||
case SignalHandler::FINISHED: | ||
return res; | ||
} | ||
} | ||
return SignalHandler::PROCESSED; | ||
} | ||
|
||
SignalHandler::Result CollectorOutput::SignalOutput(const sensor::SignalStreamMessage& msg) { | ||
for (auto& client : signal_clients_) { | ||
auto res = client->PushSignals(msg); | ||
switch (res) { | ||
case SignalHandler::PROCESSED: | ||
break; | ||
|
||
case SignalHandler::ERROR: | ||
HandleOutputError(); | ||
return res; | ||
|
||
case SignalHandler::IGNORED: | ||
case SignalHandler::NEEDS_REFRESH: | ||
case SignalHandler::FINISHED: | ||
return res; | ||
} | ||
} | ||
return SignalHandler::PROCESSED; | ||
} | ||
|
||
SignalHandler::Result CollectorOutput::SendMsg(const MessageType& msg) { | ||
auto visitor = [this](auto&& m) { | ||
using T = std::decay_t<decltype(m)>; | ||
if constexpr (std::is_same_v<T, sensor::MsgFromCollector>) { | ||
return SensorOutput(m); | ||
} else if constexpr (std::is_same_v<T, sensor::SignalStreamMessage>) { | ||
return SignalOutput(m); | ||
} | ||
|
||
// Unknown type | ||
return SignalHandler::ERROR; | ||
}; | ||
|
||
return std::visit(visitor, msg); | ||
} | ||
|
||
void CollectorOutput::EstablishGrpcStream() { | ||
while (EstablishGrpcStreamSingle()) { | ||
} | ||
CLOG(INFO) << "Service client terminating."; | ||
} | ||
|
||
bool CollectorOutput::EstablishGrpcStreamSingle() { | ||
std::mutex mtx; | ||
std::unique_lock<std::mutex> lock(mtx); | ||
stream_interrupted_.wait(lock, [this]() { return !stream_active_.load(std::memory_order_acquire) || thread_.should_stop(); }); | ||
if (thread_.should_stop()) { | ||
return false; | ||
} | ||
|
||
CLOG(INFO) << "Trying to establish GRPC stream..."; | ||
|
||
if (!WaitForChannelReady(channel_, [this]() { return thread_.should_stop(); })) { | ||
return false; | ||
} | ||
if (thread_.should_stop()) { | ||
return false; | ||
} | ||
|
||
// Refresh all clients | ||
bool success = true; | ||
for (const auto& client : signal_clients_) { | ||
success &= client->Recreate(); | ||
} | ||
|
||
for (const auto& client : sensor_clients_) { | ||
success &= client->Recreate(); | ||
} | ||
|
||
if (success) { | ||
CLOG(INFO) << "Successfully established GRPC stream."; | ||
stream_active_.store(true, std::memory_order_release); | ||
} else { | ||
CLOG(WARNING) << "Failed to establish GRPC stream, retrying..."; | ||
} | ||
return true; | ||
} | ||
} // namespace collector |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#pragma once | ||
|
||
#include <variant> | ||
|
||
#include "internalapi/sensor/collector_iservice.pb.h" | ||
#include "internalapi/sensor/signal_iservice.pb.h" | ||
|
||
#include "CollectorConfig.h" | ||
#include "SensorClient.h" | ||
#include "SignalHandler.h" | ||
#include "SignalServiceClient.h" | ||
#include "StoppableThread.h" | ||
|
||
namespace collector { | ||
|
||
using MessageType = std::variant<sensor::MsgFromCollector, sensor::SignalStreamMessage>; | ||
|
||
class CollectorOutput { | ||
public: | ||
CollectorOutput(const CollectorOutput&) = delete; | ||
CollectorOutput(CollectorOutput&&) = delete; | ||
CollectorOutput& operator=(const CollectorOutput&) = delete; | ||
CollectorOutput& operator=(CollectorOutput&&) = delete; | ||
|
||
CollectorOutput(const CollectorConfig& config); | ||
|
||
~CollectorOutput() { | ||
stream_interrupted_.notify_one(); | ||
if (thread_.running()) { | ||
thread_.Stop(); | ||
} | ||
} | ||
|
||
// Constructor for tests | ||
CollectorOutput(std::unique_ptr<ISensorClient>&& sensor_client, | ||
std::unique_ptr<ISignalServiceClient>&& signal_client) { | ||
sensor_clients_.emplace_back(std::move(sensor_client)); | ||
signal_clients_.emplace_back(std::move(signal_client)); | ||
} | ||
|
||
/** | ||
* Send a message to sensor. | ||
* | ||
* @param msg One of sensor::MsgFromCollector or | ||
* sensor::SignalStreamMessage, the proper service to be | ||
* used will be determined from the type held in msg. | ||
* @returns A SignalHandler::Result with the outcome of the send | ||
* operation | ||
*/ | ||
SignalHandler::Result SendMsg(const MessageType& msg); | ||
|
||
/** | ||
* Whether we should use the new iservice or not. | ||
* | ||
* @returns true if configuration indicates we should use the new | ||
* iservice, false otherwise. | ||
*/ | ||
bool UseSensorClient() const { return use_sensor_client_; } | ||
|
||
private: | ||
void EstablishGrpcStream(); | ||
bool EstablishGrpcStreamSingle(); | ||
|
||
void HandleOutputError(); | ||
SignalHandler::Result SensorOutput(const sensor::MsgFromCollector& msg); | ||
SignalHandler::Result SignalOutput(const sensor::SignalStreamMessage& msg); | ||
|
||
std::vector<std::unique_ptr<ISensorClient>> sensor_clients_; | ||
std::vector<std::unique_ptr<ISignalServiceClient>> signal_clients_; | ||
|
||
bool use_sensor_client_ = true; | ||
|
||
StoppableThread thread_; | ||
std::atomic<bool> stream_active_ = false; | ||
std::condition_variable stream_interrupted_; | ||
std::shared_ptr<grpc::Channel> channel_; | ||
}; | ||
|
||
} // namespace collector |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.