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  std::vector<TestValue*> lambdaUniqueTestPointers_;
88  std::vector<TestValue*> lambdaOptionalTestPointers_;
89  };
90 
92  : numberOfIOVsToAccumulate_(pset.getUntrackedParameter<unsigned int>("numberOfIOVsToAccumulate")),
93  secondsToWaitForWork_(pset.getUntrackedParameter<unsigned int>("secondsToWaitForWork")) {
94  auto collector = setWhatAcquiredProduced(this, "fromAcquireIntESProducer");
95  token_ = collector.consumes<IOVTestInfo>(edm::ESInputTag{"", ""});
96 
98 
102  edm::es::Label("uniquePtr"));
103 
107  edm::es::Label("optional"));
108 
110  [this](ESTestRecordI const& record, edm::WaitingTaskWithArenaHolder holder) {
111  usleep(200000);
112  auto returnValue = std::make_unique<TestValue>(kAcquireTestValueUniquePtr1);
113  lambdaUniqueTestPointers_[record.iovIndex()] = returnValue.get();
114  return returnValue;
115  },
116  [this](ESTestRecordI const& record, auto testValue) {
117  usleep(200000);
118  if (testValue.get() != lambdaUniqueTestPointers_[record.iovIndex()]) {
119  throw cms::Exception("TestFailure") << "AcquireIntESProducer::<lambda produceUniquePtr>"
120  << " unexpected value passed in as argument";
121  }
122  return std::make_unique<ESTestDataI>(kAcquireTestValueUniquePtr2);
123  },
124  edm::es::Label("uniquePtrLambda"));
125 
127  [this](ESTestRecordI const& record, edm::WaitingTaskWithArenaHolder holder) {
128  usleep(200000);
129  std::vector<TestValue> testVector;
130  testVector.push_back(kAcquireTestValueOptional1);
131  auto returnValue = std::make_optional<std::vector<TestValue>>(std::move(testVector));
132  lambdaOptionalTestPointers_[record.iovIndex()] = &returnValue.value()[0];
133  return returnValue;
134  },
135  [this](ESTestRecordI const& record, std::optional<std::vector<TestValue>> testValue) {
136  usleep(200000);
137  if (&testValue.value()[0] != lambdaOptionalTestPointers_[record.iovIndex()]) {
138  throw cms::Exception("TestFailure") << "AcquireIntESProducer::<lambda produceOptional>"
139  << " unexpected value passed in as argument";
140  }
141  return std::make_unique<ESTestDataI>(kAcquireTestValueOptional2);
142  },
143  edm::es::Label("optionalLambda"));
144  }
145 
147  if (server_) {
148  server_->stop();
149  }
150  server_.reset();
151  }
152 
153  void AcquireIntESProducer::initConcurrentIOVs(EventSetupRecordKey const& key, unsigned int nConcurrentIOVs) {
154  if (key == EventSetupRecordKey::makeKey<ESTestRecordI>()) {
155  caches_.resize(nConcurrentIOVs);
156  server_ = std::make_unique<test_acquire::WaitingServer>(
158  server_->start();
159  uniqueTestPointers_.resize(nConcurrentIOVs);
160  optionalTestPointers_.resize(nConcurrentIOVs);
161  lambdaUniqueTestPointers_.resize(nConcurrentIOVs);
162  lambdaOptionalTestPointers_.resize(nConcurrentIOVs);
163  }
164  }
165 
167  usleep(200000);
168 
169  test_acquire::Cache& iovCache = caches_[record.iovIndex()];
170  iovCache.retrieved().clear();
171  iovCache.processed().clear();
172 
173  // Get some data and push it into the input cache for the ExternalWork.
174  // There is no significance to the particular data we are using.
175  // Using anything from the EventSetup would be good enough for the test.
176  // I already had test modules around that would make IOVTestInfo
177  // data, so that was easy to use. We put in known values and later
178  // check that we get the expected result (they get incremented by one
179  // to simulate some "external work", then summed in the produce method
180  // calculate a result we can check easily).
181  IOVTestInfo const& iovTestInfo = record.get(token_);
182  std::vector<int>& retrieved = iovCache.retrieved();
183  retrieved.push_back(iovTestInfo.iovStartRun_);
184  retrieved.push_back(iovTestInfo.iovStartLumi_);
185  retrieved.push_back(iovTestInfo.iovEndRun_);
186  retrieved.push_back(iovTestInfo.iovEndLumi_);
187  retrieved.push_back(iovTestInfo.cacheIdentifier_);
188 
189  server_->requestValuesAsync(record.iovIndex(), &iovCache.retrieved(), &iovCache.processed(), holder);
190 
191  edm::ValidityInterval iov = record.validityInterval();
192  if (iovTestInfo.iovStartLumi_ != iov.first().luminosityBlockNumber() ||
193  iovTestInfo.iovEndLumi_ != iov.last().luminosityBlockNumber() || iovTestInfo.iovIndex_ != record.iovIndex() ||
194  iovTestInfo.cacheIdentifier_ != record.cacheIdentifier()) {
195  throw cms::Exception("TestFailure") << "AcquireIntESProducer::acquire"
196  << "read values do not agree with record";
197  }
198  return kAcquireTestValue;
199  }
200 
201  std::unique_ptr<ESTestDataI> AcquireIntESProducer::produce(ESTestRecordI const& record, int valueReturnedByAcquire) {
202  usleep(200000);
203 
204  if (valueReturnedByAcquire != kAcquireTestValue) {
205  throw cms::Exception("TestFailure") << "AcquireIntESProducer::produce"
206  << " unexpected value passed in as argument";
207  }
208 
209  edm::ESHandle<IOVTestInfo> iovTestInfo = record.getHandle(token_);
210  edm::ValidityInterval iov = record.validityInterval();
211  if (iovTestInfo->iovStartLumi_ != iov.first().luminosityBlockNumber() ||
212  iovTestInfo->iovEndLumi_ != iov.last().luminosityBlockNumber() || iovTestInfo->iovIndex_ != record.iovIndex() ||
213  iovTestInfo->cacheIdentifier_ != record.cacheIdentifier()) {
214  throw cms::Exception("TestFailure") << "AcquireIntESProducer::produce"
215  << "read values do not agree with record";
216  }
217 
218  test_acquire::Cache& iovCache = caches_[record.iovIndex()];
219  int sum = 0;
220  for (auto v : iovCache.processed()) {
221  sum += v;
222  }
223  return std::make_unique<ESTestDataI>(sum);
224  }
225 
226  std::unique_ptr<ESTestDataB> AcquireIntESProducer::produceESTestDataB(ESTestRecordB const&) {
227  return std::make_unique<ESTestDataB>(11);
228  }
229 
230  std::unique_ptr<AcquireIntESProducer::TestValue> AcquireIntESProducer::acquireUniquePtr(
232  usleep(200000);
233  auto returnValue = std::make_unique<TestValue>(kAcquireTestValueUniquePtr1);
234  uniqueTestPointers_[record.iovIndex()] = returnValue.get();
235  return returnValue;
236  }
237 
238  std::unique_ptr<ESTestDataI> AcquireIntESProducer::produceUniquePtr(ESTestRecordI const& record,
239  std::unique_ptr<TestValue> testValue) {
240  usleep(200000);
241  if (testValue.get() != uniqueTestPointers_[record.iovIndex()]) {
242  throw cms::Exception("TestFailure") << "AcquireIntESProducer::produceUniquePtr"
243  << " unexpected value passed in as argument";
244  }
245  return std::make_unique<ESTestDataI>(kAcquireTestValueUniquePtr2);
246  }
247 
248  std::optional<std::vector<AcquireIntESProducer::TestValue>> AcquireIntESProducer::acquireOptional(
250  usleep(200000);
251  std::vector<TestValue> testVector;
252  testVector.push_back(kAcquireTestValueOptional1);
253  auto returnValue = std::make_optional<std::vector<TestValue>>(std::move(testVector));
254  optionalTestPointers_[record.iovIndex()] = &returnValue.value()[0];
255  return returnValue;
256  }
257 
258  std::unique_ptr<ESTestDataI> AcquireIntESProducer::produceOptional(ESTestRecordI const& record,
259  std::optional<std::vector<TestValue>> testValue) {
260  usleep(200000);
261  if (&testValue.value()[0] != optionalTestPointers_[record.iovIndex()]) {
262  throw cms::Exception("TestFailure") << "AcquireIntESProducer::produceOptional"
263  << " unexpected value passed in as argument";
264  }
265  return std::make_unique<ESTestDataI>(kAcquireTestValueOptional2);
266  }
267 
270  desc.addUntracked<unsigned int>("numberOfIOVsToAccumulate", 8);
271  desc.addUntracked<unsigned int>("secondsToWaitForWork", 1);
272  descriptions.addDefault(desc);
273  }
274 
275 } // namespace edmtest
276 using namespace edmtest;
auto setWhatAcquiredProducedWithLambda(TAcquireFunc &&acquireFunc, TProduceFunc &&produceFunc, const es::Label &iLabel={})
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:245
const unsigned int secondsToWaitForWork_
std::unique_ptr< TestValue > acquireUniquePtr(ESTestRecordI const &, edm::WaitingTaskWithArenaHolder)
Definition: value.py:1
std::vector< TestValue * > lambdaOptionalTestPointers_
std::vector< TestValue * > uniqueTestPointers_
#define DEFINE_FWK_EVENTSETUP_MODULE(type)
Definition: ModuleFactory.h:61
std::unique_ptr< ESTestDataI > produce(ESTestRecordI const &, int)
std::vector< TestValue * > lambdaUniqueTestPointers_
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