CMS 3D CMS Logo

AcquireIntESProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: FWCore/Integration
4 // Class: AcquireIntESProducer
5 //
10 // Original Author: W. David Dagenhart
11 // Created: 12 January 2023
12 
22 #include "WaitingServer.h"
29 
30 #include <memory>
31 #include <optional>
32 #include <unistd.h>
33 #include <vector>
34 
35 namespace {
36  constexpr int kAcquireTestValue = 11;
37  constexpr int kAcquireTestValueUniquePtr1 = 101;
38  constexpr int kAcquireTestValueUniquePtr2 = 102;
39  constexpr int kAcquireTestValueOptional1 = 201;
40  constexpr int kAcquireTestValueOptional2 = 202;
41 } // namespace
42 
43 namespace edmtest {
44 
46  public:
48 
49  ~AcquireIntESProducer() override;
54 
55  void initConcurrentIOVs(EventSetupRecordKey const&, unsigned int nConcurrentIOVs) override;
56 
58 
59  std::unique_ptr<ESTestDataI> produce(ESTestRecordI const&, int);
60 
61  std::unique_ptr<ESTestDataB> produceESTestDataB(ESTestRecordB const&);
62 
63  class TestValue {
64  public:
66  int value_;
67  };
68 
69  std::unique_ptr<TestValue> acquireUniquePtr(ESTestRecordI const&, edm::WaitingTaskWithArenaHolder);
70 
71  std::unique_ptr<ESTestDataI> produceUniquePtr(ESTestRecordI const&, std::unique_ptr<TestValue>);
72 
73  std::optional<std::vector<TestValue>> acquireOptional(ESTestRecordI const&, edm::WaitingTaskWithArenaHolder);
74 
75  std::unique_ptr<ESTestDataI> produceOptional(ESTestRecordI const&, std::optional<std::vector<TestValue>>);
76 
78 
79  private:
81  std::vector<test_acquire::Cache> caches_;
82  std::unique_ptr<test_acquire::WaitingServer> server_;
83  const unsigned int numberOfIOVsToAccumulate_;
84  const unsigned int secondsToWaitForWork_;
85  std::vector<TestValue*> uniqueTestPointers_;
86  std::vector<TestValue*> optionalTestPointers_;
87  };
88 
90  : numberOfIOVsToAccumulate_(pset.getUntrackedParameter<unsigned int>("numberOfIOVsToAccumulate")),
91  secondsToWaitForWork_(pset.getUntrackedParameter<unsigned int>("secondsToWaitForWork")) {
92  auto collector = setWhatAcquiredProduced(this, "fromAcquireIntESProducer");
93  token_ = collector.consumes<IOVTestInfo>(edm::ESInputTag{"", ""});
94 
96 
100  edm::es::Label("uniquePtr"));
101 
105  edm::es::Label("optional"));
106  }
107 
109  if (server_) {
110  server_->stop();
111  }
112  server_.reset();
113  }
114 
115  void AcquireIntESProducer::initConcurrentIOVs(EventSetupRecordKey const& key, unsigned int nConcurrentIOVs) {
116  if (key == EventSetupRecordKey::makeKey<ESTestRecordI>()) {
117  caches_.resize(nConcurrentIOVs);
118  server_ = std::make_unique<test_acquire::WaitingServer>(
120  server_->start();
121  uniqueTestPointers_.resize(nConcurrentIOVs);
122  optionalTestPointers_.resize(nConcurrentIOVs);
123  }
124  }
125 
127  usleep(200000);
128 
129  test_acquire::Cache& iovCache = caches_[record.iovIndex()];
130  iovCache.retrieved().clear();
131  iovCache.processed().clear();
132 
133  // Get some data and push it into the input cache for the ExternalWork.
134  // There is no significance to the particular data we are using.
135  // Using anything from the EventSetup would be good enough for the test.
136  // I already had test modules around that would make IOVTestInfo
137  // data, so that was easy to use. We put in known values and later
138  // check that we get the expected result (they get incremented by one
139  // to simulate some "external work", then summed in the produce method
140  // calculate a result we can check easily).
141  IOVTestInfo const& iovTestInfo = record.get(token_);
142  std::vector<int>& retrieved = iovCache.retrieved();
143  retrieved.push_back(iovTestInfo.iovStartRun_);
144  retrieved.push_back(iovTestInfo.iovStartLumi_);
145  retrieved.push_back(iovTestInfo.iovEndRun_);
146  retrieved.push_back(iovTestInfo.iovEndLumi_);
147  retrieved.push_back(iovTestInfo.cacheIdentifier_);
148 
149  server_->requestValuesAsync(record.iovIndex(), &iovCache.retrieved(), &iovCache.processed(), holder);
150 
151  edm::ValidityInterval iov = record.validityInterval();
152  if (iovTestInfo.iovStartLumi_ != iov.first().luminosityBlockNumber() ||
153  iovTestInfo.iovEndLumi_ != iov.last().luminosityBlockNumber() || iovTestInfo.iovIndex_ != record.iovIndex() ||
154  iovTestInfo.cacheIdentifier_ != record.cacheIdentifier()) {
155  throw cms::Exception("TestFailure") << "AcquireIntESProducer::acquire"
156  << "read values do not agree with record";
157  }
158  return kAcquireTestValue;
159  }
160 
161  std::unique_ptr<ESTestDataI> AcquireIntESProducer::produce(ESTestRecordI const& record, int valueReturnedByAcquire) {
162  usleep(200000);
163 
164  if (valueReturnedByAcquire != kAcquireTestValue) {
165  throw cms::Exception("TestFailure") << "AcquireIntESProducer::produce"
166  << " unexpected value passed in as argument";
167  }
168 
169  edm::ESHandle<IOVTestInfo> iovTestInfo = record.getHandle(token_);
170  edm::ValidityInterval iov = record.validityInterval();
171  if (iovTestInfo->iovStartLumi_ != iov.first().luminosityBlockNumber() ||
172  iovTestInfo->iovEndLumi_ != iov.last().luminosityBlockNumber() || iovTestInfo->iovIndex_ != record.iovIndex() ||
173  iovTestInfo->cacheIdentifier_ != record.cacheIdentifier()) {
174  throw cms::Exception("TestFailure") << "AcquireIntESProducer::produce"
175  << "read values do not agree with record";
176  }
177 
178  test_acquire::Cache& iovCache = caches_[record.iovIndex()];
179  int sum = 0;
180  for (auto v : iovCache.processed()) {
181  sum += v;
182  }
183  return std::make_unique<ESTestDataI>(sum);
184  }
185 
186  std::unique_ptr<ESTestDataB> AcquireIntESProducer::produceESTestDataB(ESTestRecordB const&) {
187  return std::make_unique<ESTestDataB>(11);
188  }
189 
190  std::unique_ptr<AcquireIntESProducer::TestValue> AcquireIntESProducer::acquireUniquePtr(
192  usleep(200000);
193  auto returnValue = std::make_unique<TestValue>(kAcquireTestValueUniquePtr1);
194  uniqueTestPointers_[record.iovIndex()] = returnValue.get();
195  return returnValue;
196  }
197 
198  std::unique_ptr<ESTestDataI> AcquireIntESProducer::produceUniquePtr(ESTestRecordI const& record,
199  std::unique_ptr<TestValue> testValue) {
200  usleep(200000);
201  if (testValue.get() != uniqueTestPointers_[record.iovIndex()]) {
202  throw cms::Exception("TestFailure") << "AcquireIntESProducer::produceUniquePtr"
203  << " unexpected value passed in as argument";
204  }
205  return std::make_unique<ESTestDataI>(kAcquireTestValueUniquePtr2);
206  }
207 
208  std::optional<std::vector<AcquireIntESProducer::TestValue>> AcquireIntESProducer::acquireOptional(
210  usleep(200000);
211  std::vector<TestValue> testVector;
212  testVector.push_back(kAcquireTestValueOptional1);
213  auto returnValue = std::make_optional<std::vector<TestValue>>(std::move(testVector));
214  optionalTestPointers_[record.iovIndex()] = &returnValue.value()[0];
215  return returnValue;
216  }
217 
218  std::unique_ptr<ESTestDataI> AcquireIntESProducer::produceOptional(ESTestRecordI const& record,
219  std::optional<std::vector<TestValue>> testValue) {
220  usleep(200000);
221  if (&testValue.value()[0] != optionalTestPointers_[record.iovIndex()]) {
222  throw cms::Exception("TestFailure") << "AcquireIntESProducer::produceOptional"
223  << " unexpected value passed in as argument";
224  }
225  return std::make_unique<ESTestDataI>(kAcquireTestValueOptional2);
226  }
227 
230  desc.addUntracked<unsigned int>("numberOfIOVsToAccumulate", 8);
231  desc.addUntracked<unsigned int>("secondsToWaitForWork", 1);
232  descriptions.addDefault(desc);
233  }
234 
235 } // namespace edmtest
236 using namespace edmtest;
auto setWhatProduced(T *iThis, const es::Label &iLabel={})
Definition: ESProducer.h:166
const IOVSyncValue & last() const
LuminosityBlockNumber_t luminosityBlockNumber() const
Definition: IOVSyncValue.h:41
const IOVSyncValue & first() const
unsigned int iovEndLumi_
Definition: IOVTestInfo.h:24
unsigned int iovIndex_
Definition: IOVTestInfo.h:25
std::unique_ptr< ESTestDataI > produceOptional(ESTestRecordI const &, std::optional< std::vector< TestValue >>)
const unsigned int numberOfIOVsToAccumulate_
std::optional< std::vector< TestValue > > acquireOptional(ESTestRecordI const &, edm::WaitingTaskWithArenaHolder)
int acquire(ESTestRecordI const &, edm::WaitingTaskWithArenaHolder)
auto setWhatAcquiredProduced(T *iThis, const es::Label &iLabel={})
std::unique_ptr< test_acquire::WaitingServer > server_
void addDefault(ParameterSetDescription const &psetDescription)
std::vector< TestValue * > optionalTestPointers_
edm::ESGetToken< IOVTestInfo, ESTestRecordI > token_
unsigned int iovEndRun_
Definition: IOVTestInfo.h:23
unsigned int iovStartLumi_
Definition: IOVTestInfo.h:22
std::vector< test_acquire::Cache > caches_
key
prepare the HTCondor submission files and eventually submit them
optional
Definition: Types.py:239
const unsigned int secondsToWaitForWork_
std::unique_ptr< TestValue > acquireUniquePtr(ESTestRecordI const &, edm::WaitingTaskWithArenaHolder)
Definition: value.py:1
std::vector< TestValue * > uniqueTestPointers_
#define DEFINE_FWK_EVENTSETUP_MODULE(type)
Definition: ModuleFactory.h:61
std::unique_ptr< ESTestDataI > produce(ESTestRecordI const &, int)
unsigned long long cacheIdentifier_
Definition: IOVTestInfo.h:26
static void fillDescriptions(edm::ConfigurationDescriptions &)
AcquireIntESProducer(edm::ParameterSet const &)
unsigned int iovStartRun_
Definition: IOVTestInfo.h:21
std::vector< int > const & processed() const
Definition: WaitingServer.h:62
std::unique_ptr< ESTestDataB > produceESTestDataB(ESTestRecordB const &)
AcquireIntESProducer & operator=(const AcquireIntESProducer &)=delete
void initConcurrentIOVs(EventSetupRecordKey const &, unsigned int nConcurrentIOVs) override
std::unique_ptr< ESTestDataI > produceUniquePtr(ESTestRecordI const &, std::unique_ptr< TestValue >)
def move(src, dest)
Definition: eostools.py:511
std::vector< int > const & retrieved() const
Definition: WaitingServer.h:59