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 <vector>
33 #include <chrono>
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 
45  using namespace std::chrono_literals;
47  public:
49 
50  ~AcquireIntESProducer() override;
55 
56  void initConcurrentIOVs(EventSetupRecordKey const&, unsigned int nConcurrentIOVs) override;
57 
59 
60  std::unique_ptr<ESTestDataI> produce(ESTestRecordI const&, int);
61 
62  std::unique_ptr<ESTestDataB> produceESTestDataB(ESTestRecordB const&);
63 
64  class TestValue {
65  public:
66  TestValue(int value) : value_(value) {}
67  int value_;
68  };
69 
70  std::unique_ptr<TestValue> acquireUniquePtr(ESTestRecordI const&, edm::WaitingTaskWithArenaHolder);
71 
72  std::unique_ptr<ESTestDataI> produceUniquePtr(ESTestRecordI const&, std::unique_ptr<TestValue>);
73 
74  std::optional<std::vector<TestValue>> acquireOptional(ESTestRecordI const&, edm::WaitingTaskWithArenaHolder);
75 
76  std::unique_ptr<ESTestDataI> produceOptional(ESTestRecordI const&, std::optional<std::vector<TestValue>>);
77 
79 
80  private:
82  std::vector<test_acquire::Cache> caches_;
83  std::unique_ptr<test_acquire::WaitingServer> server_;
84  const unsigned int numberOfIOVsToAccumulate_;
85  const unsigned int secondsToWaitForWork_;
86  std::vector<TestValue*> uniqueTestPointers_;
87  std::vector<TestValue*> optionalTestPointers_;
88  std::vector<TestValue*> lambdaUniqueTestPointers_;
89  std::vector<TestValue*> lambdaOptionalTestPointers_;
90  };
91 
93  : numberOfIOVsToAccumulate_(pset.getUntrackedParameter<unsigned int>("numberOfIOVsToAccumulate")),
94  secondsToWaitForWork_(pset.getUntrackedParameter<unsigned int>("secondsToWaitForWork")) {
95  auto collector = setWhatAcquiredProduced(this, "fromAcquireIntESProducer");
96  token_ = collector.consumes<IOVTestInfo>(edm::ESInputTag{"", ""});
97 
99 
103  edm::es::Label("uniquePtr"));
104 
108  edm::es::Label("optional"));
109 
111  [this](ESTestRecordI const& record, edm::WaitingTaskWithArenaHolder holder) {
112  std::this_thread::sleep_for(200ms);
113  auto returnValue = std::make_unique<TestValue>(kAcquireTestValueUniquePtr1);
114  lambdaUniqueTestPointers_[record.iovIndex()] = returnValue.get();
115  return returnValue;
116  },
117  [this](ESTestRecordI const& record, auto testValue) {
118  std::this_thread::sleep_for(200ms);
119  if (testValue.get() != lambdaUniqueTestPointers_[record.iovIndex()]) {
120  throw cms::Exception("TestFailure") << "AcquireIntESProducer::<lambda produceUniquePtr>"
121  << " unexpected value passed in as argument";
122  }
123  return std::make_unique<ESTestDataI>(kAcquireTestValueUniquePtr2);
124  },
125  edm::es::Label("uniquePtrLambda"));
126 
128  [this](ESTestRecordI const& record, edm::WaitingTaskWithArenaHolder holder) {
129  std::this_thread::sleep_for(200ms);
130  std::vector<TestValue> testVector;
131  testVector.push_back(kAcquireTestValueOptional1);
132  auto returnValue = std::make_optional<std::vector<TestValue>>(std::move(testVector));
133  lambdaOptionalTestPointers_[record.iovIndex()] = &returnValue.value()[0];
134  return returnValue;
135  },
136  [this](ESTestRecordI const& record, std::optional<std::vector<TestValue>> testValue) {
137  std::this_thread::sleep_for(200ms);
138  if (&testValue.value()[0] != lambdaOptionalTestPointers_[record.iovIndex()]) {
139  throw cms::Exception("TestFailure") << "AcquireIntESProducer::<lambda produceOptional>"
140  << " unexpected value passed in as argument";
141  }
142  return std::make_unique<ESTestDataI>(kAcquireTestValueOptional2);
143  },
144  edm::es::Label("optionalLambda"));
145  }
146 
148  if (server_) {
149  server_->stop();
150  }
151  server_.reset();
152  }
153 
155  if (key == EventSetupRecordKey::makeKey<ESTestRecordI>()) {
156  caches_.resize(nConcurrentIOVs);
157  server_ = std::make_unique<test_acquire::WaitingServer>(
159  server_->start();
164  }
165  }
166 
168  std::this_thread::sleep_for(200ms);
169 
170  test_acquire::Cache& iovCache = caches_[record.iovIndex()];
171  iovCache.retrieved().clear();
172  iovCache.processed().clear();
173 
174  // Get some data and push it into the input cache for the ExternalWork.
175  // There is no significance to the particular data we are using.
176  // Using anything from the EventSetup would be good enough for the test.
177  // I already had test modules around that would make IOVTestInfo
178  // data, so that was easy to use. We put in known values and later
179  // check that we get the expected result (they get incremented by one
180  // to simulate some "external work", then summed in the produce method
181  // calculate a result we can check easily).
182  IOVTestInfo const& iovTestInfo = record.get(token_);
183  std::vector<int>& retrieved = iovCache.retrieved();
184  retrieved.push_back(iovTestInfo.iovStartRun_);
185  retrieved.push_back(iovTestInfo.iovStartLumi_);
186  retrieved.push_back(iovTestInfo.iovEndRun_);
187  retrieved.push_back(iovTestInfo.iovEndLumi_);
188  retrieved.push_back(iovTestInfo.cacheIdentifier_);
189 
190  server_->requestValuesAsync(record.iovIndex(), &iovCache.retrieved(), &iovCache.processed(), holder);
191 
192  edm::ValidityInterval iov = record.validityInterval();
193  if (iovTestInfo.iovStartLumi_ != iov.first().luminosityBlockNumber() ||
194  iovTestInfo.iovEndLumi_ != iov.last().luminosityBlockNumber() || iovTestInfo.iovIndex_ != record.iovIndex() ||
195  iovTestInfo.cacheIdentifier_ != record.cacheIdentifier()) {
196  throw cms::Exception("TestFailure") << "AcquireIntESProducer::acquire"
197  << "read values do not agree with record";
198  }
199  return kAcquireTestValue;
200  }
201 
202  std::unique_ptr<ESTestDataI> AcquireIntESProducer::produce(ESTestRecordI const& record, int valueReturnedByAcquire) {
203  std::this_thread::sleep_for(200ms);
204 
205  if (valueReturnedByAcquire != kAcquireTestValue) {
206  throw cms::Exception("TestFailure") << "AcquireIntESProducer::produce"
207  << " unexpected value passed in as argument";
208  }
209 
210  edm::ESHandle<IOVTestInfo> iovTestInfo = record.getHandle(token_);
211  edm::ValidityInterval iov = record.validityInterval();
212  if (iovTestInfo->iovStartLumi_ != iov.first().luminosityBlockNumber() ||
213  iovTestInfo->iovEndLumi_ != iov.last().luminosityBlockNumber() || iovTestInfo->iovIndex_ != record.iovIndex() ||
214  iovTestInfo->cacheIdentifier_ != record.cacheIdentifier()) {
215  throw cms::Exception("TestFailure") << "AcquireIntESProducer::produce"
216  << "read values do not agree with record";
217  }
218 
219  test_acquire::Cache& iovCache = caches_[record.iovIndex()];
220  int sum = 0;
221  for (auto v : iovCache.processed()) {
222  sum += v;
223  }
224  return std::make_unique<ESTestDataI>(sum);
225  }
226 
227  std::unique_ptr<ESTestDataB> AcquireIntESProducer::produceESTestDataB(ESTestRecordB const&) {
228  return std::make_unique<ESTestDataB>(11);
229  }
230 
231  std::unique_ptr<AcquireIntESProducer::TestValue> AcquireIntESProducer::acquireUniquePtr(
233  std::this_thread::sleep_for(200ms);
234  auto returnValue = std::make_unique<TestValue>(kAcquireTestValueUniquePtr1);
235  uniqueTestPointers_[record.iovIndex()] = returnValue.get();
236  return returnValue;
237  }
238 
239  std::unique_ptr<ESTestDataI> AcquireIntESProducer::produceUniquePtr(ESTestRecordI const& record,
240  std::unique_ptr<TestValue> testValue) {
241  std::this_thread::sleep_for(200ms);
242  if (testValue.get() != uniqueTestPointers_[record.iovIndex()]) {
243  throw cms::Exception("TestFailure") << "AcquireIntESProducer::produceUniquePtr"
244  << " unexpected value passed in as argument";
245  }
246  return std::make_unique<ESTestDataI>(kAcquireTestValueUniquePtr2);
247  }
248 
249  std::optional<std::vector<AcquireIntESProducer::TestValue>> AcquireIntESProducer::acquireOptional(
251  std::this_thread::sleep_for(200ms);
252  std::vector<TestValue> testVector;
253  testVector.push_back(kAcquireTestValueOptional1);
254  auto returnValue = std::make_optional<std::vector<TestValue>>(std::move(testVector));
255  optionalTestPointers_[record.iovIndex()] = &returnValue.value()[0];
256  return returnValue;
257  }
258 
259  std::unique_ptr<ESTestDataI> AcquireIntESProducer::produceOptional(ESTestRecordI const& record,
260  std::optional<std::vector<TestValue>> testValue) {
261  std::this_thread::sleep_for(200ms);
262  if (&testValue.value()[0] != optionalTestPointers_[record.iovIndex()]) {
263  throw cms::Exception("TestFailure") << "AcquireIntESProducer::produceOptional"
264  << " unexpected value passed in as argument";
265  }
266  return std::make_unique<ESTestDataI>(kAcquireTestValueOptional2);
267  }
268 
271  desc.addUntracked<unsigned int>("numberOfIOVsToAccumulate", 8);
272  desc.addUntracked<unsigned int>("secondsToWaitForWork", 1);
273  descriptions.addDefault(desc);
274  }
275 
276 } // namespace edmtest
277 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
Basic3DVector & operator=(const Basic3DVector &)=default
Assignment operator.
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
std::unique_ptr< TestValue > acquireUniquePtr(ESTestRecordI const &, edm::WaitingTaskWithArenaHolder)
const unsigned int secondsToWaitForWork_
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)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
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 &)
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