AmandeepSingh285 commented on code in PR #3695: URL: https://github.com/apache/celeborn/pull/3695#discussion_r3294228308
########## worker/src/test/java/org/apache/celeborn/service/deploy/worker/shuffledb/RocksDBRecoverySuiteJ.java: ########## @@ -0,0 +1,230 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.celeborn.service.deploy.worker.shuffledb; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import org.apache.celeborn.common.CelebornConf; +import org.apache.celeborn.common.util.JavaUtils; +import org.apache.celeborn.service.deploy.worker.WorkerSource; + +public class RocksDBRecoverySuiteJ { + + private File dbDir; + private File dbFile; + private CelebornConf defaultConf; + private CelebornConf confWithRecovery; + private WorkerSource workerSource; + private StoreVersion version; + + @Before + public void setUp() throws IOException { + dbDir = Files.createTempDirectory("rocksdb-recovery-test").toFile(); + dbFile = new File(dbDir, "test-db"); + defaultConf = new CelebornConf(); + confWithRecovery = new CelebornConf(); + confWithRecovery.set("celeborn.metadata.autoRecovery.enabled", "true"); + workerSource = new WorkerSource(defaultConf); + version = new StoreVersion(1, 0); + } + + @After + public void tearDown() throws IOException { + workerSource.destroy(); + JavaUtils.deleteRecursively(dbDir); + } + + @Test + public void testRecoveryAfterCorruption() throws Exception { + DB db = DBProvider.initDB(DBBackend.ROCKSDB, dbFile, version, workerSource, defaultConf); + assertNotNull(db); + + byte[] key = "test-key".getBytes(StandardCharsets.UTF_8); + byte[] value = "test-value".getBytes(StandardCharsets.UTF_8); + db.put(key, value); + + byte[] result = db.get(key); + assertNotNull(result); + assertEquals("test-value", new String(result, StandardCharsets.UTF_8)); + + db.close(); + + // Corrupt the DB by overwriting SST files + corruptDbFiles(dbFile); + + // Reopen — initRockDB will wipe and recreate since files are corrupt + db = DBProvider.initDB(DBBackend.ROCKSDB, dbFile, version, workerSource, defaultConf); + assertNotNull(db); + + // Data is gone after wipe-and-recreate, but DB is functional + byte[] newKey = "new-key".getBytes(StandardCharsets.UTF_8); + byte[] newValue = "new-value".getBytes(StandardCharsets.UTF_8); + db.put(newKey, newValue); + + result = db.get(newKey); + assertNotNull(result); + assertEquals("new-value", new String(result, StandardCharsets.UTF_8)); + db.close(); + } + + @Test + public void testConcurrentRecoveryOnlyReopensOnce() throws Exception { + DB db = DBProvider.initDB(DBBackend.ROCKSDB, dbFile, version, workerSource, confWithRecovery); + assertNotNull(db); + + byte[] key = "key".getBytes(StandardCharsets.UTF_8); + byte[] value = "value".getBytes(StandardCharsets.UTF_8); + db.put(key, value); + + RocksDB rocksDB = (RocksDB) db; + assertEquals(0, rocksDB.getDbGeneration()); + + int threadCount = 8; + CyclicBarrier barrier = new CyclicBarrier(threadCount); + ExecutorService executor = Executors.newFixedThreadPool(threadCount); + + // Force a recovery to simulate a RocksDBException scenario + rocksDB.forceRecovery(); + long genAfterFirstRecovery = rocksDB.getDbGeneration(); + assertEquals(1, genAfterFirstRecovery); + + // Now launch concurrent threads that all try to trigger recovery at the same generation. + // genAfterFirstRecovery is captured once so every thread calls forceRecovery with the + // same stale-generation value; only one can win the write-lock check and actually reopen. + List<Future<?>> futures = new ArrayList<>(); + for (int i = 0; i < threadCount; i++) { + futures.add( + executor.submit( + () -> { + try { + barrier.await(); + rocksDB.forceRecovery(genAfterFirstRecovery); + } catch (Exception e) { + throw new RuntimeException(e); + } + })); + } + + for (Future<?> f : futures) { + f.get(); + } + executor.shutdown(); Review Comment: Done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
