optimiser-solver/ExecutionControl.cpp
Geir Horn d2324e42a3 Merge all outstanding changesets
Commit messages as follows:

Change-Id: I631d374144efc540b158868fa65a0bac232a7548
---
Changed the comments for the SLO Violation handler
---
Performance update:
Adding a boolean flag to indicate when all metrics have been set to avoid a linear scan of all metrics on each SLO Violation message.
---
Metric list and reconfiguration wait
Metric updater now listening for a metric list from the Optimiser Controller and not frmo the EMS, and discards SLO Violations until the Optimiser Controller sends a message indicating that the previous application reconfiguration has finished.
---
Log message to indicate that the "reconfiguration done" even message has been received
---
Added the right topic for the metric list
---
New messages
Metric list from the controller
New message format for AMPL model definition
Fixed the AMQ message property settings
2024-04-17 14:27:21 +02:00

106 lines
3.8 KiB
C++

/*==============================================================================
Execution control
The source file implements the static variables and functions of the Execution
control actor.
Author and Copyright: Geir Horn, University of Oslo
Contact: Geir.Horn@mn.uio.no
License: MPL2.0 (https://www.mozilla.org/en-US/MPL/2.0/)
==============================================================================*/
#include "Actor.hpp"
#include "Communication/NetworkEndpoint.hpp"
#include "Communication/AMQ/AMQEndpoint.hpp"
#include "ExecutionControl.hpp"
namespace NebulOuS
{
// -----------------------------------------------------------------------------
// Static variables
// -----------------------------------------------------------------------------
bool ExecutionControl::Running = true;
std::mutex ExecutionControl::TerminationLock;
std::condition_variable ExecutionControl::ReadyToTerminate;
// -----------------------------------------------------------------------------
// Waiting function
// -----------------------------------------------------------------------------
//
// The function used to wait for the termination message simply waits on the
// condition variable until it is signalled by the message handler. As there
// could be spurious wake-ups it is necessary to check if the actor is still
// running when the condition variable is signalled, and if so the calling
// thread will just block again in another wait.
void ExecutionControl::WaitForTermination( void )
{
while( Running )
{
std::unique_lock< std::mutex > Lock( TerminationLock );
ReadyToTerminate.wait( Lock );
}
}
// -----------------------------------------------------------------------------
// Stop message handler
// -----------------------------------------------------------------------------
//
// The stop message handler will first send the network stop message to the
// session layer requesting it to coordinate the network shutdown and close all
// externally communicating actors.
void ExecutionControl::StopMessageHandler( const StopMessage & Command,
const Address Sender )
{
std::lock_guard< std::mutex > Lock( TerminationLock );
Send( StatusMessage( StatusMessage::State::Stopped ),
Address( StatusMessage::AMQTopic ) );
Send( Theron::Network::ShutDown(),
Theron::Network::GetAddress( Theron::Network::Layer::Session ) );
Running = false;
ReadyToTerminate.notify_all();
}
// -----------------------------------------------------------------------------
// Constructor & destructor
// -----------------------------------------------------------------------------
//
// The constructor registers the stop message handler and sets up a publisher
// for the status topic, and then post a message that the solver is starting.
ExecutionControl::ExecutionControl( const std::string & TheActorName )
: Actor( TheActorName ),
StandardFallbackHandler( Actor::GetAddress().AsString() ),
NetworkingActor( Actor::GetAddress().AsString() )
{
RegisterHandler( this, &ExecutionControl::StopMessageHandler );
Send( Theron::AMQ::NetworkLayer::TopicSubscription(
Theron::AMQ::NetworkLayer::TopicSubscription::Action::Publisher,
StatusMessage::AMQTopic
), GetSessionLayerAddress() );
Send( StatusMessage( StatusMessage::State::Starting ),
Address( StatusMessage::AMQTopic ) );
}
// The destructor simply closes the publisher if the network is still active
// when the actor closes.
ExecutionControl::~ExecutionControl( void )
{
if( HasNetwork() )
Send( Theron::AMQ::NetworkLayer::TopicSubscription(
Theron::AMQ::NetworkLayer::TopicSubscription::Action::ClosePublisher,
StatusMessage::AMQTopic
), GetSessionLayerAddress() );
}
} // namespace NebulOuS