Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:15:47

0001 #include "HeterogeneousCore/SonicCore/interface/SonicClientBase.h"
0002 #include "FWCore/Utilities/interface/Exception.h"
0003 #include "FWCore/ParameterSet/interface/allowedValues.h"
0004 
0005 SonicClientBase::SonicClientBase(const edm::ParameterSet& params,
0006                                  const std::string& debugName,
0007                                  const std::string& clientName)
0008     : allowedTries_(params.getUntrackedParameter<unsigned>("allowedTries", 0)),
0009       debugName_(debugName),
0010       clientName_(clientName),
0011       fullDebugName_(debugName_) {
0012   if (!clientName_.empty())
0013     fullDebugName_ += ":" + clientName_;
0014 
0015   std::string modeName(params.getParameter<std::string>("mode"));
0016   if (modeName == "Sync")
0017     setMode(SonicMode::Sync);
0018   else if (modeName == "Async")
0019     setMode(SonicMode::Async);
0020   else if (modeName == "PseudoAsync")
0021     setMode(SonicMode::PseudoAsync);
0022   else
0023     throw cms::Exception("Configuration") << "Unknown mode for SonicClient: " << modeName;
0024 }
0025 
0026 void SonicClientBase::setMode(SonicMode mode) {
0027   if (dispatcher_ and mode_ == mode)
0028     return;
0029   mode_ = mode;
0030 
0031   //get correct dispatcher for mode
0032   if (mode_ == SonicMode::Sync or mode_ == SonicMode::Async)
0033     dispatcher_ = std::make_unique<SonicDispatcher>(this);
0034   else if (mode_ == SonicMode::PseudoAsync)
0035     dispatcher_ = std::make_unique<SonicDispatcherPseudoAsync>(this);
0036 }
0037 
0038 void SonicClientBase::start(edm::WaitingTaskWithArenaHolder holder) {
0039   start();
0040   holder_ = std::move(holder);
0041 }
0042 
0043 void SonicClientBase::start() { tries_ = 0; }
0044 
0045 void SonicClientBase::finish(bool success, std::exception_ptr eptr) {
0046   //retries are only allowed if no exception was raised
0047   if (!success and !eptr) {
0048     ++tries_;
0049     //if max retries has not been exceeded, call evaluate again
0050     if (tries_ < allowedTries_) {
0051       evaluate();
0052       //avoid calling doneWaiting() twice
0053       return;
0054     }
0055     //prepare an exception if exceeded
0056     else {
0057       edm::Exception ex(edm::errors::ExternalFailure);
0058       ex << "SonicCallFailed: call failed after max " << tries_ << " tries";
0059       eptr = make_exception_ptr(ex);
0060     }
0061   }
0062   if (holder_) {
0063     holder_->doneWaiting(eptr);
0064     holder_.reset();
0065   } else if (eptr)
0066     std::rethrow_exception(eptr);
0067 
0068   //reset client data now (usually done at end of produce())
0069   if (eptr)
0070     reset();
0071 }
0072 
0073 void SonicClientBase::fillBasePSetDescription(edm::ParameterSetDescription& desc, bool allowRetry) {
0074   //restrict allowed values
0075   desc.ifValue(edm::ParameterDescription<std::string>("mode", "PseudoAsync", true),
0076                edm::allowedValues<std::string>("Sync", "Async", "PseudoAsync"));
0077   if (allowRetry)
0078     desc.addUntracked<unsigned>("allowedTries", 0);
0079   desc.addUntracked<bool>("verbose", false);
0080 }