1717
1818package org .apache .ignite .cdc ;
1919
20+ import java .io .File ;
2021import java .util .ArrayList ;
2122import java .util .Arrays ;
2223import java .util .Collection ;
2324import java .util .List ;
25+ import java .util .concurrent .ThreadLocalRandom ;
2426import java .util .concurrent .atomic .AtomicInteger ;
2527import java .util .function .Consumer ;
28+ import java .util .function .IntConsumer ;
2629import org .apache .ignite .IgniteCache ;
2730import org .apache .ignite .IgniteCheckedException ;
2831import org .apache .ignite .cache .CacheAtomicityMode ;
3336import org .apache .ignite .configuration .DataStorageConfiguration ;
3437import org .apache .ignite .configuration .IgniteConfiguration ;
3538import org .apache .ignite .internal .IgniteEx ;
39+ import org .apache .ignite .internal .pagemem .wal .IgniteWriteAheadLogManager ;
3640import org .apache .ignite .internal .pagemem .wal .WALIterator ;
3741import org .apache .ignite .internal .pagemem .wal .record .DataRecord ;
3842import org .apache .ignite .internal .pagemem .wal .record .WALRecord ;
3943import org .apache .ignite .internal .processors .cache .persistence .file .RandomAccessFileIOFactory ;
44+ import org .apache .ignite .internal .processors .cache .persistence .wal .FileDescriptor ;
4045import org .apache .ignite .internal .processors .cache .persistence .wal .WALPointer ;
4146import org .apache .ignite .internal .processors .cache .persistence .wal .reader .IgniteWalIteratorFactory ;
4247import org .apache .ignite .internal .processors .cache .persistence .wal .reader .IgniteWalIteratorFactory .IteratorParametersBuilder ;
5358import static org .apache .ignite .cache .CacheMode .PARTITIONED ;
5459import static org .apache .ignite .cache .CacheMode .REPLICATED ;
5560import static org .apache .ignite .cdc .CdcSelfTest .WAL_ARCHIVE_TIMEOUT ;
61+ import static org .apache .ignite .configuration .DataStorageConfiguration .UNLIMITED_WAL_ARCHIVE ;
62+ import static org .apache .ignite .internal .util .IgniteUtils .KB ;
63+ import static org .apache .ignite .internal .util .IgniteUtils .MB ;
5664import static org .apache .ignite .testframework .GridTestUtils .getFieldValue ;
5765import static org .apache .ignite .testframework .GridTestUtils .waitForCondition ;
5866
@@ -76,6 +84,9 @@ public class WalForCdcTest extends GridCommonAbstractTest {
7684 /** */
7785 private boolean cdcEnabled ;
7886
87+ /** */
88+ private long archiveSz = UNLIMITED_WAL_ARCHIVE ;
89+
7990 /** */
8091 @ Parameterized .Parameters (name = "mode={0}, atomicityMode={1}" )
8192 public static Collection <?> parameters () {
@@ -94,6 +105,8 @@ public static Collection<?> parameters() {
94105
95106 cfg .setDataStorageConfiguration (new DataStorageConfiguration ()
96107 .setWalForceArchiveTimeout (WAL_ARCHIVE_TIMEOUT )
108+ .setWalSegmentSize ((int )(2 * MB ))
109+ .setMaxWalArchiveSize (archiveSz )
97110 .setDefaultDataRegionConfiguration (new DataRegionConfiguration ()
98111 .setPersistenceEnabled (persistenceEnabled )
99112 .setCdcEnabled (cdcEnabled )));
@@ -196,6 +209,60 @@ public void testWalDisabledIfPersistenceAndCdcDisabled() throws Exception {
196209 assertNull (getFieldValue (ignite .context ().cache ().context (), "cdcWalMgr" ));
197210 }
198211
212+ /** */
213+ @ Test
214+ public void testArchiveCleared () throws Exception {
215+ persistenceEnabled = false ;
216+ cdcEnabled = true ;
217+ archiveSz = 10 * MB ;
218+
219+ IgniteEx ignite = startGrid (0 );
220+
221+ ignite .cluster ().state (ClusterState .ACTIVE );
222+
223+ IgniteCache <Integer , byte []> cache = ignite .getOrCreateCache (
224+ new CacheConfiguration <Integer , byte []>(DEFAULT_CACHE_NAME )
225+ .setCacheMode (mode )
226+ .setAtomicityMode (atomicityMode ));
227+
228+ IntConsumer createData = (entryCnt ) -> {
229+ for (int i = 0 ; i < entryCnt ; i ++) {
230+ byte [] payload = new byte [(int )KB ];
231+
232+ ThreadLocalRandom .current ().nextBytes (payload );
233+
234+ cache .put (i , payload );
235+ }
236+ };
237+
238+ IgniteWriteAheadLogManager wal = ignite .context ().cache ().context ().wal (true );
239+
240+ long startSgmnt = wal .currentSegment ();
241+
242+ createData .accept ((int )(archiveSz / (2 * KB )));
243+
244+ long finishSgmnt = wal .currentSegment ();
245+
246+ String archive = archive (ignite );
247+
248+ assertTrue (finishSgmnt > startSgmnt );
249+ assertTrue (
250+ "Wait for start segment archivation" ,
251+ waitForCondition (() -> startSgmnt <= wal .lastArchivedSegment (), getTestTimeout ())
252+ );
253+
254+ File startSgmntArchived = new File (archive , FileDescriptor .fileName (startSgmnt ));
255+
256+ assertTrue ("Check archived segment file exists" , startSgmntArchived .exists ());
257+
258+ createData .accept ((int )(archiveSz / KB ));
259+
260+ assertTrue (
261+ "Wait for archived segment cleaned" ,
262+ waitForCondition (() -> !startSgmntArchived .exists (), getTestTimeout ())
263+ );
264+ }
265+
199266 /** */
200267 private void doTestWal (
201268 IgniteEx ignite ,
@@ -223,16 +290,9 @@ private void doTestWal(
223290
224291 /** */
225292 private int checkDataRecords (IgniteEx ignite ) throws IgniteCheckedException {
226- String archive = U .resolveWorkDirectory (
227- U .defaultWorkDirectory (),
228- ignite .configuration ().getDataStorageConfiguration ().getWalArchivePath () + "/" +
229- U .maskForFileName (ignite .configuration ().getIgniteInstanceName ()),
230- false
231- ).getAbsolutePath ();
232-
233293 WALIterator iter = new IgniteWalIteratorFactory (log ).iterator (new IteratorParametersBuilder ()
234294 .ioFactory (new RandomAccessFileIOFactory ())
235- .filesOrDirs (archive ));
295+ .filesOrDirs (archive ( ignite ) ));
236296
237297 int walRecCnt = 0 ;
238298
@@ -254,4 +314,18 @@ private int checkDataRecords(IgniteEx ignite) throws IgniteCheckedException {
254314
255315 return walRecCnt ;
256316 }
317+
318+ /**
319+ * @param ignite Ignite.
320+ * @return WAL archive patch
321+ * @throws IgniteCheckedException If failed
322+ */
323+ private static String archive (IgniteEx ignite ) throws IgniteCheckedException {
324+ return U .resolveWorkDirectory (
325+ U .defaultWorkDirectory (),
326+ ignite .configuration ().getDataStorageConfiguration ().getWalArchivePath () + "/" +
327+ U .maskForFileName (ignite .configuration ().getIgniteInstanceName ()),
328+ false
329+ ).getAbsolutePath ();
330+ }
257331}
0 commit comments