33 : tail read during open write
", "[
mxl flows]
")
34{
35 auto const flowId = "5fbec3b1-1b0f-417d-9059-8b94a47197ed";
36 auto const flowDef = mxl::tests::readFile("data/v210_flow.json");
37
38 // Init the domain instance.
39 auto instance = mxlCreateInstance(domain.string().c_str(), "");
40 REQUIRE(instance != nullptr);
41
42 // Init a flow writer.
43 auto writer = mxlFlowWriter{};
44 auto configInfo = mxlFlowConfigInfo{};
45 auto flowWasCreated = false;
46
47 REQUIRE(mxlCreateFlowWriter(instance, flowDef.c_str(), "", &writer, &configInfo, &flowWasCreated) == MXL_STATUS_OK);
48 REQUIRE(flowWasCreated);
49
50 // Init headIndex from the current MXL time.
51 auto const now = mxlGetTime();
52 auto headIndex = mxlTimestampToIndex(&configInfo.common.grainRate, now);
53 REQUIRE(headIndex != MXL_UNDEFINED_INDEX);
54
55 auto const grainCount = static_cast<std::uint64_t>(configInfo.discrete.grainCount);
56
57 REQUIRE(grainCount > 1U);
58 REQUIRE(headIndex > grainCount);
59
60 // Init each grain in the ring buffer.
61 auto tailIndex = headIndex - grainCount + 1;
62 for (auto i = std::uint64_t{0}; i < grainCount; ++i)
63 {
64 auto grainInfo = mxlGrainInfo{};
65 std::uint8_t* buffer = nullptr;
66
67 REQUIRE(mxlFlowWriterOpenGrain(writer, tailIndex + i, &grainInfo, &buffer) == MXL_STATUS_OK);
68
69 buffer[0] = 0xCA;
70 buffer[grainInfo.grainSize - 1] = 0xFE;
71 grainInfo.validSlices = grainInfo.totalSlices;
72
73 REQUIRE(mxlFlowWriterCommitGrain(writer, &grainInfo) == MXL_STATUS_OK);
74 }
75
76 // Init a reader and sanity check the flow's headIndex.
77 auto reader = mxlFlowReader{};
78 REQUIRE(mxlCreateFlowReader(instance, flowId, "", &reader) == MXL_STATUS_OK);
79
80 auto runtimeInfo = mxlFlowRuntimeInfo{};
81 REQUIRE(mxlFlowReaderGetRuntimeInfo(reader, &runtimeInfo) == MXL_STATUS_OK);
82 REQUIRE(runtimeInfo.headIndex == headIndex);
83
84 // Reading the tailIndex grain should be TOO_LATE when write is
85 // not in progress.
86 auto readGrainInfo = mxlGrainInfo{};
87 std::uint8_t* readBuffer = nullptr;
88 auto const grainReadStatusOutsideWrite = mxlFlowReaderGetGrainNonBlocking(reader, tailIndex, &readGrainInfo, &readBuffer);
89 {
90 INFO("The tailIndex grain read should return TOO_LATE even when no write "
91 "is in progress because the tail grain is reserved
for the
writer.
");
92 CHECK(grainReadStatusOutsideWrite == MXL_ERR_OUT_OF_RANGE_TOO_LATE);
93 }
94
95 // Reading the tailIndex grain through the slice API should be
96 // TOO_LATE when write is not in progress.
97 readBuffer = nullptr;
98 readGrainInfo = {};
99 auto const sliceReadStatusOutsideWrite = mxlFlowReaderGetGrainSliceNonBlocking(
100 reader, tailIndex, MXL_GRAIN_VALID_SLICES_ANY, &readGrainInfo, &readBuffer);
101 {
102 INFO("The tailIndex slice read should return TOO_LATE even when no write "
103 "is in progress because the tail grain is reserved
for the
writer.
");
104 CHECK(sliceReadStatusOutsideWrite == MXL_ERR_OUT_OF_RANGE_TOO_LATE);
105 }
106
107 // Open a write to the next sequential grain at headIndex+1. This
108 // grain shares a ring-buffer position with the tailIndex grain.
109 auto writeGrainInfo = mxlGrainInfo{};
110 std::uint8_t* writeBuffer = nullptr;
111 {
112 REQUIRE(mxlFlowWriterOpenGrain(writer, headIndex + 1, &writeGrainInfo, &writeBuffer) == MXL_STATUS_OK);
113
114 INFO("The newly opened grain should identify headIndex+1.");
115 CAPTURE(writeGrainInfo.index, headIndex + 1, writeGrainInfo.validSlices, writeGrainInfo.totalSlices);
116 CHECK(writeGrainInfo.index == headIndex + 1);
117 }
118
119 // Sanity check: verify that headIndex has not yet moved.
120 REQUIRE(mxlFlowReaderGetRuntimeInfo(reader, &runtimeInfo) == MXL_STATUS_OK);
121 REQUIRE(runtimeInfo.headIndex == headIndex);
122
123 // Reading a grain at tailIndex during an open write of
124 // headIndex+1 should return TOO_LATE to prevent reader/writer
125 // conflict during the writer's open/commit interval.
126 constexpr auto NOT_CHANGED_INDEX_SENTINEL_VALUE = std::numeric_limits<decltype(readGrainInfo.index)>::max();
127 readBuffer = nullptr;
128 readGrainInfo = {};
129 readGrainInfo.index = NOT_CHANGED_INDEX_SENTINEL_VALUE;
130 auto const grainReadStatusInsideWrite = mxlFlowReaderGetGrainNonBlocking(reader, tailIndex, &readGrainInfo, &readBuffer);
131 {
132 INFO("A tailIndex read should return TOO_LATE while a write of "
133 "headIndex+1 is open because both logical grains alias the "
134 "same physical ring-
buffer position.
"
135
136 );
137 CHECK(grainReadStatusInsideWrite == MXL_ERR_OUT_OF_RANGE_TOO_LATE);
138 }
139 {
140 INFO("Because the tailIndex grain read is expected to return TOO_LATE, "
141 "it should not
return a payload or modify the grain
index.
");
142 CHECK(readBuffer == nullptr);
143 CHECK(readGrainInfo.index == NOT_CHANGED_INDEX_SENTINEL_VALUE);
144 }
145
146 // Reading tailIndex through the slice API during an open write of
147 // headIndex+1 should return TOO_LATE, should not return a payload,
148 // and should not modify readGrainInfo.validSlices.
149 constexpr auto NOT_CHANGED_SLICE_SENTINEL_VALUE = std::numeric_limits<decltype(readGrainInfo.validSlices)>::max();
150 readBuffer = nullptr;
151 readGrainInfo = {};
152 readGrainInfo.validSlices = NOT_CHANGED_SLICE_SENTINEL_VALUE;
153 auto const sliceReadStatus = mxlFlowReaderGetGrainSliceNonBlocking(reader, tailIndex, MXL_GRAIN_VALID_SLICES_ANY, &readGrainInfo, &readBuffer);
154 {
155 INFO("A tailIndex slice read should return TOO_LATE while a write of "
156 "headIndex+1 is open because both logical grains alias the "
157 "same physical ring-
buffer position.
");
158 CHECK(sliceReadStatus == MXL_ERR_OUT_OF_RANGE_TOO_LATE);
159 }
160 {
161 INFO("Because the tailIndex slice read is expected to return TOO_LATE, "
162 "it should not return a payload or modify validSlices.");
163 CHECK(readBuffer == nullptr);
164 CHECK(readGrainInfo.validSlices == NOT_CHANGED_SLICE_SENTINEL_VALUE);
165 }
166
167 // Commit the write and update local headIndex.
168 writeBuffer[0] = 0xCA;
169 writeBuffer[writeGrainInfo.grainSize - 1] = 0xFE;
170 writeGrainInfo.validSlices = writeGrainInfo.totalSlices;
171 REQUIRE(mxlFlowWriterCommitGrain(writer, &writeGrainInfo) == MXL_STATUS_OK);
172
173 headIndex = headIndex + 1;
174 tailIndex = headIndex - grainCount + 1;
175
176 // Sanity check local headIndex matches flow's headIndex.
177 REQUIRE(mxlFlowReaderGetRuntimeInfo(reader, &runtimeInfo) == MXL_STATUS_OK);
178 REQUIRE(runtimeInfo.headIndex == headIndex);
179
180 // Post commit read of the tailIndex grain should continue to be
181 // TOO_LATE.
182 readBuffer = nullptr;
183 readGrainInfo = {};
184 auto const grainReadStatusPostCommit = mxlFlowReaderGetGrainNonBlocking(reader, tailIndex, &readGrainInfo, &readBuffer);
185 {
186 INFO("A post commit tailIndex read should return TOO_LATE after a "
187 "write of headIndex+1 is committed because both logical grains "
188 "alias the same physical ring-
buffer position.
");
189 CHECK(grainReadStatusPostCommit == MXL_ERR_OUT_OF_RANGE_TOO_LATE);
190 }
191
192 // Tear down.
193 REQUIRE(mxlReleaseFlowReader(instance, reader) == MXL_STATUS_OK);
194
195 REQUIRE(mxlReleaseFlowWriter(instance, writer) == MXL_STATUS_OK);
196
197 REQUIRE(mxlDestroyInstance(instance) == MXL_STATUS_OK);
198}
uint8_t * buffer
Set a mark at the beginning and the end of the grain payload.