yandrey321 commented on code in PR #9904: URL: https://github.com/apache/ozone/pull/9904#discussion_r2919554280
########## hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/TestRDBStoreIteratorWithDBClose.java: ########## @@ -0,0 +1,232 @@ +/* + * 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.hadoop.hdds.utils.db; + +import static org.apache.hadoop.hdds.utils.db.IteratorType.KEY_AND_VALUE; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import org.apache.hadoop.hdds.StringUtils; +import org.apache.hadoop.hdds.utils.db.managed.ManagedColumnFamilyOptions; +import org.apache.hadoop.hdds.utils.db.managed.ManagedDBOptions; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.rocksdb.RocksDB; + +/** + * Tests that RDBStoreAbstractIterator handles concurrent DB close safely. + */ +public class TestRDBStoreIteratorWithDBClose { + + private static final String TABLE_NAME = "TestTable"; + private static final int ENTRY_COUNT = 100; + + private RDBStore rdbStore; + private ManagedDBOptions options; + + @BeforeEach + public void setUp(@TempDir File tempDir) throws Exception { + options = TestRDBStore.newManagedDBOptions(); + Set<TableConfig> configSet = new HashSet<>(); + // RocksDB always requires the default column family to be present + configSet.add(new TableConfig( + StringUtils.bytes2String(RocksDB.DEFAULT_COLUMN_FAMILY), + new ManagedColumnFamilyOptions())); + configSet.add(new TableConfig(TABLE_NAME, new ManagedColumnFamilyOptions())); + rdbStore = TestRDBStore.newRDBStore(tempDir, options, configSet); + + RDBTable table = rdbStore.getTable(TABLE_NAME); + for (int i = 0; i < ENTRY_COUNT; i++) { + byte[] key = ("key-" + i).getBytes(StandardCharsets.UTF_8); + byte[] value = ("value-" + i).getBytes(StandardCharsets.UTF_8); + table.put(key, value); + } + } + + @AfterEach + public void tearDown() throws Exception { + if (rdbStore != null && !rdbStore.isClosed()) { + rdbStore.close(); + } + if (options != null) { + options.close(); + } + } + + /** + * Validates the fast-fail check (isClosed guard). + */ + @Test + public void testHasNextReturnsFalseAfterDBClosed() throws Exception { Review Comment: this should is probabilistic, in order to catch something we need to spawn at least 10 concurrent threads that open iterators and read from them in a long cycle (at least 60 seconds) wait for 5 seconds in main thread and then close DB. Assert that all the thread successfully complete after closing DB. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
