14#include <catch_amalgamated.hpp>
28 std::atomic<int> failingReads = 0;
29 std::atomic<int> opened = 0;
30 std::atomic<int> reads = 0;
34 FakeSource(std::string u, Behaviour&
b) :
name(
std::move(u)), behaviour(
b) {}
35 uint64_t size()
const override {
return behaviour.size; }
41 void readvInto(std::span<ReadRequest> requests)
override
44 int failing = behaviour.failingReads.load();
45 while (failing > 0 && !behaviour.failingReads.compare_exchange_weak(failing, failing - 1)) {
50 for (
auto&
r : requests) {
51 for (
size_t i = 0;
i <
r.length; ++
i) {
58 bool isRemote()
const override {
return true; }
61 std::string
url()
const override {
return name; }
64 std::atomic<uint64_t>
bytes = 0;
65 std::atomic<uint64_t> calls = 0;
69 std::map<std::string, Behaviour> replicas;
70 std::vector<std::string> log;
72 Setup() { options.retryDelay = 0; }
74 std::unique_ptr<FailoverByteSource> open()
76 std::vector<Replica>
list;
77 for (
auto& [
url, _] : replicas) {
80 auto opener = [
this](std::string
const&
url,
Options const&) -> std::unique_ptr<ByteSource> {
81 auto&
b = replicas.at(
url);
86 return std::make_unique<FakeSource>(
url,
b);
88 return std::make_unique<FailoverByteSource>(list, options, opener, [
this](std::string
const&
m) { log.push_back(
m); });
92bool readsCorrectly(
ByteSource&
s, uint64_t off = 100,
size_t len = 50)
94 std::vector<uint8_t>
buf(
len);
95 s.readInto(off,
len,
buf.data());
96 for (
size_t i = 0;
i <
len; ++
i) {
97 if (
buf[
i] != (off +
i) % 251) {
111 REQUIRE(readsCorrectly(*
f));
112 CHECK(
f->replicaLabel() ==
"SE-a");
113 CHECK(
s.replicas[
"b"].opened == 0);
114 CHECK(
f->bytesRead() == 50);
115 CHECK(
f->readCalls() == 1);
119TEST_CASE(
"FailoverByteSource skips replicas which do not open")
122 s.replicas[
"a"].opens =
false;
125 CHECK(
f->replicaLabel() ==
"SE-b");
126 REQUIRE(readsCorrectly(*
f));
130TEST_CASE(
"FailoverByteSource throws when no replica opens")
133 s.replicas[
"a"].opens =
false;
134 s.replicas[
"b"].opens =
false;
138TEST_CASE(
"FailoverByteSource retries a transient read error on the same replica")
141 s.replicas[
"a"].failingReads = 1;
144 REQUIRE(readsCorrectly(*
f));
145 CHECK(
f->replicaLabel() ==
"SE-a");
146 CHECK(
s.replicas[
"a"].opened == 2);
147 CHECK(
s.replicas[
"b"].opened == 0);
148 REQUIRE(
s.log.size() == 1);
149 CHECK(
s.log[0].find(
"retry 1 of 2") != std::string::npos);
152TEST_CASE(
"FailoverByteSource moves to the next replica after the retries")
155 s.replicas[
"a"].failingReads = 100;
158 REQUIRE(readsCorrectly(*
f));
159 CHECK(
f->replicaLabel() ==
"SE-b");
160 CHECK(
s.replicas[
"a"].reads == 3);
161 CHECK(
s.log.back().find(
"Moving to replica on SE-b") != std::string::npos);
163 REQUIRE(readsCorrectly(*
f, 900, 100));
164 CHECK(
s.replicas[
"a"].reads == 3);
167TEST_CASE(
"FailoverByteSource does not use a replica of a different size")
170 s.replicas[
"a"].failingReads = 100;
171 s.replicas[
"b"].size = 999;
174 REQUIRE(readsCorrectly(*
f));
175 CHECK(
f->replicaLabel() ==
"SE-c");
178TEST_CASE(
"FailoverByteSource throws when every replica fails")
181 s.replicas[
"a"].failingReads = 100;
182 s.replicas[
"b"].failingReads = 100;
184 std::vector<uint8_t>
buf(10);
186 f->readInto(0, 10,
buf.data());
187 FAIL(
"no exception");
189 CHECK(std::string(
e.what()).find(
"no replica left") != std::string::npos);
191 CHECK(
s.replicas[
"a"].reads == 3);
192 CHECK(
s.replicas[
"b"].reads == 3);
195TEST_CASE(
"FailoverByteSource counts the bytes of retired replicas")
201 REQUIRE(readsCorrectly(*
f, 0, 100));
202 s.replicas[
"a"].failingReads = 100;
203 REQUIRE(readsCorrectly(*
f, 0, 30));
204 CHECK(
f->bytesRead() == 130);
205 CHECK(
f->readCalls() == 2);
211 s.replicas[
"a"].failingReads = 100;
214 std::vector<std::thread> threads;
215 std::atomic<int> good = 0;
216 for (
int t = 0;
t < 4; ++
t) {
217 threads.emplace_back([&,
t] {
218 for (
int i = 0;
i < 20; ++
i) {
219 good += readsCorrectly(*
f, 10 *
t +
i, 40);
223 for (
auto&
t : threads) {
227 CHECK(
f->replicaLabel() ==
"SE-b");
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error s
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error First event to Number of events to Number of iterations to Number of iterations to Number of initial iterations excluded from const char Directory with events to Events directory is and not inside events Show standalone event Run event generator maximum time bin of continuous for triggered for automatic continuous for automatic continuous triggered uint64_t
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error First event to Number of events to Number of iterations to Number of iterations to Number of initial iterations excluded from const char e
virtual std::string url() const =0
virtual uint64_t bytesRead() const =0
virtual uint64_t size() const =0
virtual bool isRemote() const
Whether each read is a network round trip, worth hiding behind decoding.
virtual void readInto(uint64_t off, size_t len, void *dst)=0
Throws IoError, or CorruptData when the range is past the end of the file.
virtual uint64_t readCalls() const =0
virtual void readvInto(std::span< ReadRequest > requests)=0
GLuint const GLchar * name
GLboolean GLboolean GLboolean b
GLenum GLenum GLsizei len
GLenum GLuint GLenum GLsizei const GLchar * buf
bool list(IEventListener &reporter, Config const &config)
Failure of the underlying transport.
TEST_CASE("FailoverByteSource reads the first replica")