bitflicker64 commented on code in PR #3309: URL: https://github.com/apache/hugegraph/pull/3309#discussion_r4245451487
########## hugegraph-store/hg-store-test/src/main/java/org/apache/hugegraph/store/rocksdb/SessionOperatorTest.java: ########## @@ -0,0 +1,268 @@ +/* + * 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.hugegraph.store.rocksdb; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import org.apache.hugegraph.rocksdb.access.DBStoreException; +import org.apache.hugegraph.rocksdb.access.RocksDBSession; +import org.apache.hugegraph.rocksdb.access.RocksDBSession.BackendColumn; +import org.apache.hugegraph.rocksdb.access.ScanIterator; +import org.apache.hugegraph.rocksdb.access.SessionOperator; +import org.apache.hugegraph.rocksdb.access.SessionOperatorImpl; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +public class SessionOperatorTest extends BaseRocksDbTest { + + private static final String TABLE = "tbl"; + private static final String GRAPH = "op-graph"; + + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + private ExecutorService prober; + private String dataPath; + private RocksDBSession session; + private int probes; + + private static byte[] b(String s) { + return s.getBytes(StandardCharsets.UTF_8); + } + + private static List<String> keys(ScanIterator it) { + List<String> keys = new ArrayList<>(); + try { + while (it.hasNext()) { + BackendColumn col = it.next(); + keys.add(new String(col.name, StandardCharsets.UTF_8)); + } + } finally { + it.close(); + } + return keys; + } + + @Before + public void setUp() throws Exception { + this.prober = Executors.newSingleThreadExecutor(r -> { + Thread t = new Thread(r, "cf-lock-prober"); + t.setDaemon(true); + return t; + }); + this.dataPath = this.folder.newFolder("data").getAbsolutePath(); + this.session = new RocksDBSession(hConfig, this.dataPath, GRAPH, 0); + this.session.checkTable(TABLE); + } + + @After + public void tearDown() { + if (this.session != null) { + this.session.close(); Review Comment: Important: `tearDown()` hangs instead of reporting a failure whenever a test ends with the CF read lock still held by the test thread. `prepare()` takes `cfHandleLock.readLock()` on the calling thread (`SessionOperatorImpl.java:219`). `session.close()` on this line reaches `RocksDBSession.shutdown()`, which calls `cfHandleLock.writeLock().lock()` (`RocksDBSession.java:626`) on the same thread. `ReentrantReadWriteLock` does not upgrade a read hold, so that call parks forever, `prober.shutdownNow()` below is never reached, and no test in this class has a timeout. A lock-leak bug in production code is not needed to hit this. Any assertion that fails between `prepare()` and `commit()` leaves the read hold in place, for example the `assertNull` on line 138. A timeout in `assertCfLockReleased()` (line 118), which is the case the probe exists to report, ends the same way. Measured at 03ffdf0bc on Linux, JDK 17.0.20, Maven 3.8.7: I changed line 138 to an assertion that fails and ran ``` mvn test -pl hugegraph-store/hg-store-test -am -P store-rocksdb-test \ -Dtest=SessionOperatorTest#testCommitAppliesTheBatchAndReturnsItsSize ``` The fork logged `shutdown db op-graph` and then nothing. After 65 s `jstack` showed `main` parked in `ReentrantReadWriteLock$WriteLock.lock` called from `RocksDBSession.shutdown(RocksDBSession.java:626)`, `RocksDBSession.close(RocksDBSession.java:1016)`, `SessionOperatorTest.tearDown(SessionOperatorTest.java:93)`. Surefire never printed a test result. In CI the `store` job would sit until its 45 minute `timeout-minutes` and name no failing test, so the first regression these tests catch shows up as a job timeout. Please make the teardown safe before the class is registered in `RocksDbSuiteTest`: - release the read holds the test thread still owns before `close()`. `getCfHandleReadLock()` returns a `ReentrantReadWriteLock.ReadLock`, and its `unlock()` throws `IllegalMonitorStateException` once the thread has no hold left, so a loop that unlocks until that exception is enough; - add a JUnit `Timeout` rule as a backstop, so a stuck lock is reported as a failed test. The rule runs `@Before`, the test and `@After` on one thread, so the unlock loop still sees the test's holds. `@Test(timeout = ...)` does not help here: it moves only the test method to another thread and leaves `tearDown()` outside the timeout. -- 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]
