3131import org .apache .iotdb .db .storageengine .dataregion .memtable .IMemTable ;
3232import org .apache .iotdb .db .storageengine .dataregion .memtable .PrimitiveMemTable ;
3333import org .apache .iotdb .db .storageengine .dataregion .wal .utils .WALMode ;
34+ import org .apache .iotdb .db .storageengine .dataregion .wal .utils .listener .AbstractResultListener .Status ;
35+ import org .apache .iotdb .db .storageengine .dataregion .wal .utils .listener .WALFlushListener ;
3436import org .apache .iotdb .db .utils .EnvironmentUtils ;
3537import org .apache .iotdb .db .utils .constant .TestConstant ;
3638
4042import org .apache .tsfile .utils .Binary ;
4143import org .apache .tsfile .utils .BitMap ;
4244import org .apache .tsfile .write .schema .MeasurementSchema ;
43- import org .awaitility .Awaitility ;
4445import org .junit .After ;
4546import org .junit .Before ;
4647import org .junit .Test ;
5556import java .util .concurrent .atomic .AtomicBoolean ;
5657import java .util .concurrent .atomic .AtomicReference ;
5758
59+ import static org .junit .Assert .assertEquals ;
5860import static org .junit .Assert .assertFalse ;
5961import static org .junit .Assert .assertNotNull ;
6062import static org .junit .Assert .assertTrue ;
@@ -124,12 +126,11 @@ public void testWaitForNextReadyNotWokenByFlushWithoutRoll() throws Exception {
124126 // write a small amount of data (not enough to trigger roll)
125127 InsertTabletNode insertTabletNode = getInsertTabletNode (devicePath , new long [] {1 });
126128 insertTabletNode .setSearchIndex (1 );
127- walNode .log (
128- memTable .getMemTableId (),
129- insertTabletNode ,
130- Collections .singletonList (new int [] {0 , insertTabletNode .getRowCount ()}));
131-
132- Awaitility .await ().atMost (10 , TimeUnit .SECONDS ).until (() -> walNode .isAllWALEntriesConsumed ());
129+ waitForFlush (
130+ walNode .log (
131+ memTable .getMemTableId (),
132+ insertTabletNode ,
133+ Collections .singletonList (new int [] {0 , insertTabletNode .getRowCount ()})));
133134
134135 // data is flushed to buffer but no WAL file roll happened yet, iterator at search index 1
135136 // should not find data (because the current-writing WAL file is not readable by the iterator)
@@ -160,17 +161,15 @@ public void testWaitForNextReadySucceedsAfterRollFile() throws Exception {
160161 for (int i = 1 ; i <= 5 ; i ++) {
161162 InsertTabletNode insertTabletNode = getInsertTabletNode (devicePath , new long [] {i });
162163 insertTabletNode .setSearchIndex (i );
163- walNode .log (
164- memTable .getMemTableId (),
165- insertTabletNode ,
166- Collections .singletonList (new int [] {0 , insertTabletNode .getRowCount ()}));
164+ waitForFlush (
165+ walNode .log (
166+ memTable .getMemTableId (),
167+ insertTabletNode ,
168+ Collections .singletonList (new int [] {0 , insertTabletNode .getRowCount ()})));
167169 }
168170
169- Awaitility .await ().atMost (10 , TimeUnit .SECONDS ).until (() -> walNode .isAllWALEntriesConsumed ());
170-
171171 // roll the WAL file so the data is in a closed file readable by the iterator
172172 walNode .rollWALFile ();
173- Awaitility .await ().atMost (10 , TimeUnit .SECONDS ).until (() -> walNode .isAllWALEntriesConsumed ());
174173
175174 // iterator at search index 1 should find the data after roll
176175 ConsensusReqReader .ReqIterator iterator = walNode .getReqIterator (1 );
@@ -185,16 +184,15 @@ public void testLegacySeparatorStillWorksAfterRollFile() throws Exception {
185184
186185 InsertTabletNode insertTabletNode = getInsertTabletNode (devicePath , new long [] {1 });
187186 insertTabletNode .setSearchIndex (1 );
188- walNode . log (
189- memTable . getMemTableId (),
190- insertTabletNode ,
191- Collections . singletonList ( new int [] { 0 , insertTabletNode . getRowCount ()}));
192- walNode . log ( memTable . getMemTableId (), new ContinuousSameSearchIndexSeparatorNode ( ));
193-
194- Awaitility . await (). atMost ( 10 , TimeUnit . SECONDS ). until (() -> walNode . isAllWALEntriesConsumed ( ));
187+ waitForFlush (
188+ walNode . log (
189+ memTable . getMemTableId () ,
190+ insertTabletNode ,
191+ Collections . singletonList ( new int [] { 0 , insertTabletNode . getRowCount ()}) ));
192+ waitForFlush (
193+ walNode . log ( memTable . getMemTableId (), new ContinuousSameSearchIndexSeparatorNode () ));
195194
196195 walNode .rollWALFile ();
197- Awaitility .await ().atMost (10 , TimeUnit .SECONDS ).until (() -> walNode .isAllWALEntriesConsumed ());
198196
199197 ConsensusReqReader .ReqIterator iterator = walNode .getReqIterator (1 );
200198 assertTrue (iterator .hasNext ());
@@ -214,12 +212,11 @@ public void testWaitForNextReadyWakesUpOnConcurrentRoll() throws Exception {
214212 InsertTabletNode insertTabletNode = getInsertTabletNode (devicePath , new long [] {1 });
215213 insertTabletNode .setSearchIndex (1 );
216214 insertTabletNode .setLastFragment (true );
217- walNode .log (
218- memTable .getMemTableId (),
219- insertTabletNode ,
220- Collections .singletonList (new int [] {0 , insertTabletNode .getRowCount ()}));
221-
222- Awaitility .await ().atMost (10 , TimeUnit .SECONDS ).until (() -> walNode .isAllWALEntriesConsumed ());
215+ waitForFlush (
216+ walNode .log (
217+ memTable .getMemTableId (),
218+ insertTabletNode ,
219+ Collections .singletonList (new int [] {0 , insertTabletNode .getRowCount ()})));
223220
224221 ConsensusReqReader .ReqIterator iterator = walNode .getReqIterator (1 );
225222
@@ -246,7 +243,6 @@ public void testWaitForNextReadyWakesUpOnConcurrentRoll() throws Exception {
246243
247244 // trigger WAL file roll — this should signal rollLogWriterCondition and wake up the iterator
248245 walNode .rollWALFile ();
249- Awaitility .await ().atMost (10 , TimeUnit .SECONDS ).until (() -> walNode .isAllWALEntriesConsumed ());
250246
251247 waitFuture .get (20 , TimeUnit .SECONDS );
252248 executor .shutdown ();
@@ -274,14 +270,11 @@ public void testWaitForNextReadyWithAutoRollOnSizeThreshold() throws Exception {
274270 // write initial data with search index
275271 InsertTabletNode first = getInsertTabletNode (devicePath , new long [] {1 });
276272 first .setSearchIndex (1 );
277- walNode .log (
278- memTable .getMemTableId (),
279- first ,
280- Collections .singletonList (new int [] {0 , first .getRowCount ()}));
281-
282- Awaitility .await ()
283- .atMost (10 , TimeUnit .SECONDS )
284- .until (() -> walNode .isAllWALEntriesConsumed ());
273+ waitForFlush (
274+ walNode .log (
275+ memTable .getMemTableId (),
276+ first ,
277+ Collections .singletonList (new int [] {0 , first .getRowCount ()})));
285278
286279 ConsensusReqReader .ReqIterator iterator = walNode .getReqIterator (1 );
287280
@@ -305,14 +298,17 @@ public void testWaitForNextReadyWithAutoRollOnSizeThreshold() throws Exception {
305298 Thread .sleep (500 );
306299
307300 // write more data to exceed the small threshold and trigger auto-roll
301+ WALFlushListener lastFlushListener = null ;
308302 for (int i = 2 ; i <= 50 ; i ++) {
309303 InsertTabletNode node = getInsertTabletNode (devicePath , new long [] {i });
310304 node .setSearchIndex (i );
311- walNode .log (
312- memTable .getMemTableId (),
313- node ,
314- Collections .singletonList (new int [] {0 , node .getRowCount ()}));
305+ lastFlushListener =
306+ walNode .log (
307+ memTable .getMemTableId (),
308+ node ,
309+ Collections .singletonList (new int [] {0 , node .getRowCount ()}));
315310 }
311+ waitForFlush (lastFlushListener );
316312
317313 waitFuture .get (40 , TimeUnit .SECONDS );
318314 executor .shutdown ();
@@ -341,12 +337,11 @@ public void testWaitForNextReadyAutoTriggersRollOnTimeout() throws Exception {
341337 InsertTabletNode insertTabletNode = getInsertTabletNode (devicePath , new long [] {1 });
342338 insertTabletNode .setSearchIndex (1 );
343339 insertTabletNode .setLastFragment (true );
344- walNode .log (
345- memTable .getMemTableId (),
346- insertTabletNode ,
347- Collections .singletonList (new int [] {0 , insertTabletNode .getRowCount ()}));
348-
349- Awaitility .await ().atMost (10 , TimeUnit .SECONDS ).until (() -> walNode .isAllWALEntriesConsumed ());
340+ waitForFlush (
341+ walNode .log (
342+ memTable .getMemTableId (),
343+ insertTabletNode ,
344+ Collections .singletonList (new int [] {0 , insertTabletNode .getRowCount ()})));
350345
351346 // iterator cannot read the active WAL file, so hasNext() should be false
352347 ConsensusReqReader .ReqIterator iterator = walNode .getReqIterator (1 );
@@ -443,4 +438,14 @@ private InsertTabletNode getInsertTabletNode(String devicePath, long[] times)
443438 columns ,
444439 times .length );
445440 }
441+
442+ private void waitForFlush (WALFlushListener flushListener ) throws Exception {
443+ ExecutorService executor = Executors .newSingleThreadExecutor ();
444+ try {
445+ Future <Status > waitFuture = executor .submit (flushListener ::waitForResult );
446+ assertEquals (Status .SUCCESS , waitFuture .get (10 , TimeUnit .SECONDS ));
447+ } finally {
448+ executor .shutdownNow ();
449+ }
450+ }
446451}
0 commit comments