DDS plugin: Adapt command ui to changes in DDS plugin

* Add COMPLETE INIT support
* Add -n option, shall be replaced with inspecting DDS topo spec in the
future
* Update DDS example to work again
This commit is contained in:
Dennis Klein 2019-07-26 10:06:24 +02:00 committed by Dennis Klein
parent b54df715ea
commit 6c07920fc6
5 changed files with 65 additions and 42 deletions

View File

@ -48,13 +48,26 @@ echo "TOPOLOGY FILE: ${topologyFile}"
# dds-info --active-topology # dds-info --active-topology
dds-topology --disable-validation --activate ${topologyFile} dds-topology --disable-validation --activate ${topologyFile}
# dds-info --active-topology # dds-info --active-topology
# dds-info --wait-for-executing-agents ${requiredNofAgents}
sleep 1
echo "------------------------" echo "------------------------"
echo "...waiting for Topology to finish..." echo "...waiting for Topology to finish..."
# TODO Retrieve number of devices from DDS topology API instead of having the user pass it explicitely
fairmq-dds-command-ui -w "IDLE" -n ${requiredNofAgents}
fairmq-dds-command-ui -c i -w "INITIALIZING DEVICE" -n ${requiredNofAgents}
fairmq-dds-command-ui -c k -w "INITIALIZED" -n ${requiredNofAgents}
fairmq-dds-command-ui -c b -w "BOUND" -n ${requiredNofAgents}
fairmq-dds-command-ui -c x -w "DEVICE READY" -n ${requiredNofAgents}
fairmq-dds-command-ui -c j -w "READY" -n ${requiredNofAgents}
fairmq-dds-command-ui -c r
sampler_and_sink="main/(Sampler|Sink)" sampler_and_sink="main/(Sampler|Sink)"
fairmq-dds-command-ui -p $sampler_and_sink -w "RUNNING->READY" fairmq-dds-command-ui -p $sampler_and_sink -w "RUNNING->READY" -n 2
echo "...$sampler_and_sink are READY, sending shutdown..." echo "...$sampler_and_sink are READY, sending shutdown..."
fairmq-dds-command-ui -c q! -w "EXITING" fairmq-dds-command-ui -c s -w "RUNNING->READY" -n ${requiredNofAgents}
fairmq-dds-command-ui -c t -w "DEVICE READY" -n ${requiredNofAgents}
fairmq-dds-command-ui -c d -w "IDLE" -n ${requiredNofAgents}
fairmq-dds-command-ui -c q
echo "...waiting for ${requiredNofAgents} idle agents..." echo "...waiting for ${requiredNofAgents} idle agents..."
dds-info --wait-for-idle-agents ${requiredNofAgents} dds-info --wait-for-idle-agents ${requiredNofAgents}
echo "------------------------" echo "------------------------"

View File

@ -31,9 +31,22 @@ namespace mq
namespace plugins namespace plugins
{ {
DDS::DDS(const string& name, const Plugin::Version version, const string& maintainer, const string& homepage, PluginServices* pluginServices) DDS::DDS(const string& name,
const Plugin::Version version,
const string& maintainer,
const string& homepage,
PluginServices* pluginServices)
: Plugin(name, version, maintainer, homepage, pluginServices) : Plugin(name, version, maintainer, homepage, pluginServices)
, fTransitions({ "BIND", "CONNECT", "INIT TASK", "RUN", "STOP", "RESET TASK", "RESET DEVICE" }) , fTransitions({"INIT DEVICE",
"COMPLETE INIT",
"BIND",
"CONNECT",
"INIT TASK",
"RUN",
"STOP",
"RESET TASK",
"RESET DEVICE",
"END"})
, fCurrentState(DeviceState::Idle) , fCurrentState(DeviceState::Idle)
, fLastState(DeviceState::Idle) , fLastState(DeviceState::Idle)
, fDeviceTerminationRequested(false) , fDeviceTerminationRequested(false)
@ -282,25 +295,13 @@ auto DDS::SubscribeForCustomCommands() -> void
if (cmd == "check-state") { if (cmd == "check-state") {
fDDS.Send(id + ": " + ToStr(GetCurrentDeviceState()), to_string(senderId)); fDDS.Send(id + ": " + ToStr(GetCurrentDeviceState()), to_string(senderId));
} else if (cmd == "INIT DEVICE") {
if (ChangeDeviceState(ToDeviceStateTransition(cmd))) {
fDDS.Send(id + ": queued, " + cmd, to_string(senderId));
} else {
fDDS.Send(id + ": could not queue, " + cmd, to_string(senderId));
}
} else if (fTransitions.find(cmd) != fTransitions.end()) { } else if (fTransitions.find(cmd) != fTransitions.end()) {
if (ChangeDeviceState(ToDeviceStateTransition(cmd))) { if (ChangeDeviceState(ToDeviceStateTransition(cmd))) {
fDDS.Send(id + ": queued, " + cmd, to_string(senderId)); fDDS.Send(id + ": queued, " + cmd, to_string(senderId));
} else { } else {
fDDS.Send(id + ": could not queue, " + cmd, to_string(senderId)); fDDS.Send(id + ": could not queue, " + cmd, to_string(senderId));
} }
} else if (cmd == "END") { if (cmd == "END" && ToStr(GetCurrentDeviceState()) == "EXITING") {
if (ChangeDeviceState(ToDeviceStateTransition(cmd))) {
fDDS.Send(id + ": queued, " + cmd, to_string(senderId));
} else {
fDDS.Send(id + ": could not queue, " + cmd, to_string(senderId));
}
if (ToStr(GetCurrentDeviceState()) == "EXITING") {
unique_lock<mutex> lock(fStopMutex); unique_lock<mutex> lock(fStopMutex);
fStopCondition.notify_one(); fStopCondition.notify_one();
} }

View File

@ -70,7 +70,7 @@ struct StateSubscription {
void printControlsHelp() void printControlsHelp()
{ {
cout << "Use keys to control the devices:" << endl; cout << "Use keys to control the devices:" << endl;
cout << "[c] check states, [o] dump config, [h] help, [r] run, [s] stop, [t] reset task, [d] reset device, [q] end, [j] init task, [i] init device, [b] bind, [x] connect" << endl; cout << "[c] check states, [o] dump config, [h] help, [r] run, [s] stop, [t] reset task, [d] reset device, [q] end, [j] init task, [i] init device, [k] complete init, [b] bind, [x] connect" << endl;
cout << "To quit press Ctrl+C" << endl; cout << "To quit press Ctrl+C" << endl;
} }
@ -95,6 +95,9 @@ void commandMode(const string& commandIn, const string& topologyPath, CCustomCmd
} else if (command == "i") { } else if (command == "i") {
cout << "> init devices" << endl; cout << "> init devices" << endl;
ddsCustomCmd.send("INIT DEVICE", topologyPath); ddsCustomCmd.send("INIT DEVICE", topologyPath);
} else if (command == "k") {
cout << "> complete init" << endl;
ddsCustomCmd.send("COMPLETE INIT", topologyPath);
} else if (command == "b") { } else if (command == "b") {
cout << "> bind devices" << endl; cout << "> bind devices" << endl;
ddsCustomCmd.send("BIND", topologyPath); ddsCustomCmd.send("BIND", topologyPath);
@ -152,7 +155,11 @@ struct WaitMode
: fTargetState(targetState) : fTargetState(targetState)
{} {}
void Run(const chrono::milliseconds& timeout, const string& topologyPath, CCustomCmd& ddsCustomCmd, const string& command = "") void Run(const chrono::milliseconds& timeout,
const string& topologyPath,
CCustomCmd& ddsCustomCmd,
unsigned int numberDevices,
const string& command = "")
{ {
StateSubscription stateSubscription(topologyPath, ddsCustomCmd); StateSubscription stateSubscription(topologyPath, ddsCustomCmd);
@ -161,11 +168,18 @@ struct WaitMode
} }
// TODO once DDS provides an API to retrieve actual number of tasks, use it here // TODO once DDS provides an API to retrieve actual number of tasks, use it here
auto condition = [&] { return !fTargetStates.empty() && all_of(fTargetStates.cbegin(), auto condition = [&] {
bool res(!fTargetStates.empty()
&& all_of(fTargetStates.cbegin(),
fTargetStates.cend(), fTargetStates.cend(),
[&](unordered_map<uint64_t, string>::value_type i) { [&](unordered_map<uint64_t, string>::value_type i) {
return boost::algorithm::ends_with(i.second, fTargetState); return boost::algorithm::ends_with(i.second, fTargetState);
}); }));
if (numberDevices > 0) {
res = res && (fTargetStates.size() == numberDevices);
}
cout << "waiting for " << numberDevices << " devices to reach " << fTargetState << ", condition check: " << res << endl;
return res;
}; };
unique_lock<mutex> lock(fMtx); unique_lock<mutex> lock(fMtx);
@ -202,6 +216,7 @@ int main(int argc, char* argv[])
string topologyPath; string topologyPath;
string targetState; string targetState;
unsigned int timeout; unsigned int timeout;
unsigned int numberDevices(0);
bpo::options_description options("Common options"); bpo::options_description options("Common options");
@ -217,6 +232,7 @@ int main(int argc, char* argv[])
("path,p", bpo::value<string> (&topologyPath)->default_value(""), "DDS Topology path to send command to (empty - send to all tasks)") ("path,p", bpo::value<string> (&topologyPath)->default_value(""), "DDS Topology path to send command to (empty - send to all tasks)")
("wait-for-state,w", bpo::value<string> (&targetState)->default_value(""), "Wait until targeted FairMQ devices reach the given state") ("wait-for-state,w", bpo::value<string> (&targetState)->default_value(""), "Wait until targeted FairMQ devices reach the given state")
("timeout,t", bpo::value<unsigned int> (&timeout)->default_value(0), "Timeout in milliseconds when waiting for a device state (0 - wait infinitely)") ("timeout,t", bpo::value<unsigned int> (&timeout)->default_value(0), "Timeout in milliseconds when waiting for a device state (0 - wait infinitely)")
("number-devices,n", bpo::value<unsigned int> (&numberDevices)->default_value(0), "Number of devices (will be removed in the future)")
("help,h", "Produce help message"); ("help,h", "Produce help message");
bpo::variables_map vm; bpo::variables_map vm;
@ -224,7 +240,7 @@ int main(int argc, char* argv[])
if (vm.count("help")) { if (vm.count("help")) {
cout << "FairMQ DDS Command UI" << endl << options << endl; cout << "FairMQ DDS Command UI" << endl << options << endl;
cout << "Commands: [c] check state, [o] dump config, [h] help, [r] run, [s] stop, [t] reset task, [d] reset device, [q] end, [j] init task, [i] init device" << endl; cout << "Commands: [c] check state, [o] dump config, [h] help, [r] run, [s] stop, [t] reset task, [d] reset device, [q] end, [j] init task, [i] init device, [k] complete init, [b] bind, [x] connect" << endl;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -241,12 +257,13 @@ int main(int argc, char* argv[])
// subscribe to receive messages from DDS // subscribe to receive messages from DDS
ddsCustomCmd.subscribe([&](const string& msg, const string& /*condition*/, uint64_t senderId) { ddsCustomCmd.subscribe([&](const string& msg, const string& /*condition*/, uint64_t senderId) {
cerr << "Received: " << msg << endl; // cerr << "Received: " << msg << endl;
vector<string> parts; vector<string> parts;
boost::algorithm::split(parts, msg, boost::algorithm::is_any_of(":,")); boost::algorithm::split(parts, msg, boost::algorithm::is_any_of(":,"));
if (parts[0] == "state-change") { if (parts[0] == "state-change") {
// cerr << "Received: " << msg << endl;
boost::trim(parts[2]); boost::trim(parts[2]);
waitMode.AddNewStateEntry(senderId, parts[2]); waitMode.AddNewStateEntry(senderId, parts[3]);
} else if (parts[0] == "state-changes-subscription") { } else if (parts[0] == "state-changes-subscription") {
if (parts[2] != "OK") { if (parts[2] != "OK") {
cerr << "state-changes-subscription failed with return code: " << parts[2]; cerr << "state-changes-subscription failed with return code: " << parts[2];
@ -265,7 +282,7 @@ int main(int argc, char* argv[])
if (targetState == "") { if (targetState == "") {
commandMode(command, topologyPath, ddsCustomCmd); commandMode(command, topologyPath, ddsCustomCmd);
} else { } else {
waitMode.Run(chrono::milliseconds(timeout), topologyPath, ddsCustomCmd, command); waitMode.Run(chrono::milliseconds(timeout), topologyPath, ddsCustomCmd, numberDevices, command);
} }
} catch (exception& e) { } catch (exception& e) {
cerr << "Error: " << e.what() << endl; cerr << "Error: " << e.what() << endl;

View File

@ -72,7 +72,7 @@ Topology::Topology(DDSTopology topo, DDSSession session)
{ {
std::vector<uint64_t> deviceList = fDDSTopo.GetDeviceList(); std::vector<uint64_t> deviceList = fDDSTopo.GetDeviceList();
for (const auto& d : deviceList) { for (const auto& d : deviceList) {
LOG(info) << "Adding device " << d; // LOG(debug) << "Adding device " << d;
fState.emplace(d, DeviceStatus{ false, DeviceState::Ok }); fState.emplace(d, DeviceStatus{ false, DeviceState::Ok });
} }
fDDSSession.SubscribeToCommands([this](const std::string& msg, const std::string& /* condition */, uint64_t senderId) { fDDSSession.SubscribeToCommands([this](const std::string& msg, const std::string& /* condition */, uint64_t senderId) {
@ -122,7 +122,7 @@ auto Topology::ChangeState(TopologyTransition transition, ChangeStateCallback cb
throw std::runtime_error("A state change request is already in progress, concurrent requests are currently not supported"); throw std::runtime_error("A state change request is already in progress, concurrent requests are currently not supported");
lock.unlock(); lock.unlock();
} }
LOG(info) << "Initiating ChangeState with " << transition << " to " << expectedState.at(transition); LOG(debug) << "Initiating ChangeState with " << transition << " to " << expectedState.at(transition);
fStateChangeOngoing = true; fStateChangeOngoing = true;
fChangeStateCallback = cb; fChangeStateCallback = cb;
fStateChangeTimeout = timeout; fStateChangeTimeout = timeout;
@ -204,7 +204,7 @@ void Topology::AddNewStateEntry(uint64_t senderId, const std::string& state)
{ {
std::size_t pos = state.find("->"); std::size_t pos = state.find("->");
std::string endState = state.substr(pos + 2); std::string endState = state.substr(pos + 2);
LOG(info) << "Adding new state entry: " << senderId << ", " << state << ", end state: " << endState; // LOG(debug) << "Adding new state entry: " << senderId << ", " << state << ", end state: " << endState;
{ {
try { try {
std::unique_lock<std::mutex> lock(fMtx); std::unique_lock<std::mutex> lock(fMtx);

View File

@ -43,7 +43,7 @@ TEST_F(Topology, ChangeStateAsync)
Topology topo(mDDSTopo, mDDSSession); Topology topo(mDDSTopo, mDDSSession);
fair::mq::tools::Semaphore blocker; fair::mq::tools::Semaphore blocker;
topo.ChangeState(TopologyTransition::Run, [&blocker, &topo](Topology::ChangeStateResult result) { topo.ChangeState(TopologyTransition::InitDevice, [&blocker, &topo](Topology::ChangeStateResult result) {
LOG(info) << result; LOG(info) << result;
EXPECT_EQ(result.rc, fair::mq::AsyncOpResultCode::Ok); EXPECT_EQ(result.rc, fair::mq::AsyncOpResultCode::Ok);
EXPECT_NO_THROW(fair::mq::sdk::AggregateState(result.state)); EXPECT_NO_THROW(fair::mq::sdk::AggregateState(result.state));
@ -51,14 +51,6 @@ TEST_F(Topology, ChangeStateAsync)
blocker.Signal(); blocker.Signal();
}); });
blocker.Wait(); blocker.Wait();
topo.ChangeState(TopologyTransition::Stop, [&blocker, &topo](Topology::ChangeStateResult result) {
LOG(info) << result;
EXPECT_EQ(result.rc, fair::mq::AsyncOpResultCode::Ok);
EXPECT_NO_THROW(fair::mq::sdk::AggregateState(result.state));
EXPECT_EQ(fair::mq::sdk::StateEqualsTo(result.state, fair::mq::sdk::DeviceState::Ready), true);
blocker.Signal();
});
blocker.Wait();
} }
TEST_F(Topology, ChangeStateSync) TEST_F(Topology, ChangeStateSync)