-
Notifications
You must be signed in to change notification settings - Fork 22
CASSANALYTICS-87: Fix LEAK DETECTED errors during bulk read #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from 2 commits
207d495
18ad5cb
61add5b
3e01b99
2224751
2991034
4190d40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,8 +25,11 @@ | |
| import java.util.stream.IntStream; | ||
|
|
||
| import com.google.common.collect.ImmutableMap; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import com.github.valfirst.slf4jtest.TestLogger; | ||
| import com.github.valfirst.slf4jtest.TestLoggerFactory; | ||
| import org.apache.cassandra.bridge.CassandraBridgeImplementation; | ||
| import org.apache.cassandra.config.DatabaseDescriptor; | ||
| import org.apache.cassandra.db.DecoratedKey; | ||
|
|
@@ -52,6 +55,13 @@ | |
| public class SSTableCacheTests | ||
| { | ||
| private static final CassandraBridgeImplementation BRIDGE = new CassandraBridgeImplementation(); | ||
| private static final TestLogger REF_LOGGER = TestLoggerFactory.getTestLogger("org.apache.cassandra.utils.concurrent.Ref"); | ||
|
|
||
| @AfterEach | ||
| void cleanup() | ||
| { | ||
| REF_LOGGER.clear(); | ||
| } | ||
|
|
||
| // CHECKSTYLE IGNORE: Long method | ||
| @Test | ||
|
|
@@ -191,4 +201,35 @@ public void testCache() | |
| } | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void testNoLeakDetectedError() throws Exception | ||
| { | ||
| Partitioner partitioner = Partitioner.Murmur3Partitioner; | ||
| try (TemporaryDirectory directory = new TemporaryDirectory()) | ||
| { | ||
| // Write an SSTable | ||
| TestSchema schema = TestSchema.basic(BRIDGE); | ||
| schema.writeSSTable(directory, BRIDGE, partitioner, | ||
| writer -> IntStream.range(0, 10).forEach(index -> writer.write(index, 0, index))); | ||
| SSTable sstable = TestSSTable.firstIn(directory.path()); | ||
| TableMetadata metadata = new SchemaBuilder(schema.createStatement, | ||
| schema.keyspace, | ||
| new ReplicationFactor(ReplicationFactor.ReplicationStrategy.SimpleStrategy, | ||
| ImmutableMap.of("replication_factor", 1)), | ||
| partitioner).tableMetaData(); | ||
|
|
||
| SummaryDbUtils.Summary summary = SSTableCache.INSTANCE.keysFromSummary(metadata, sstable); | ||
| assertThat(summary).isNotNull(); | ||
| assertThat(SSTableCache.INSTANCE.containsSummary(sstable)).isTrue(); | ||
| SSTableCache.INSTANCE.invalidate(sstable); | ||
| summary = null; | ||
| assertThat(SSTableCache.INSTANCE.containsSummary(sstable)).isFalse(); | ||
| // trigger GC and wait a bit before asserting LEAK DETECTED is not logged. | ||
| System.gc(); | ||
| Thread.sleep(1000); | ||
|
||
| System.gc(); | ||
| assertThat(REF_LOGGER.getAllLoggingEvents()).isEmpty(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |
| import java.util.concurrent.TimeUnit; | ||
| import java.util.function.Function; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.cache.Cache; | ||
| import com.google.common.cache.CacheBuilder; | ||
| import org.slf4j.Logger; | ||
|
|
@@ -107,6 +108,28 @@ private <T> Cache<SSTable, T> buildCache(int size, int expireAfterMins) | |
| return CacheBuilder.newBuilder() | ||
| .expireAfterAccess(expireAfterMins, TimeUnit.MINUTES) | ||
| .maximumSize(size) | ||
| .removalListener(notification -> { | ||
| // The function is to eliminate the LEAK DETECTED errors. | ||
| // How it happens: | ||
| // 1. AutoCloseable objects (e.g. IndexSummary and BloomFilter) are evicted from cache | ||
| // 2. JVM GC and the close method is not called explicitly to reduce the reference count | ||
| // 3. Reference-Reaper thread release the object and print the LEAK DETECTED error | ||
| // The function fixes it by closing the object when evicting. | ||
| Object val = notification.getValue(); | ||
| if (val instanceof AutoCloseable) | ||
| { | ||
| String typeLiteral = val.getClass().getName(); | ||
| try | ||
| { | ||
| LOGGER.debug("Evicting auto-closable of type: {}", typeLiteral); | ||
| ((AutoCloseable) val).close(); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| LOGGER.error("Exception closing cached instance of {}", typeLiteral, e); | ||
| } | ||
| } | ||
| }) | ||
|
Comment on lines
+118
to
+139
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If commenting it out, the new test fails with "LEAK DETECTED" errors |
||
| .build(); | ||
| } | ||
|
|
||
|
|
@@ -211,4 +234,14 @@ private static IOException toIOException(Throwable throwable) | |
| IOException ioException = ThrowableUtils.rootCause(throwable, IOException.class); | ||
| return ioException != null ? ioException : new IOException(ThrowableUtils.rootCause(throwable)); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| void invalidate(SSTable sstable) | ||
| { | ||
| summary.invalidate(sstable); | ||
| index.invalidate(sstable); | ||
| stats.invalidate(sstable); | ||
| filter.invalidate(sstable); | ||
| compressionMetadata.invalidate(sstable); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be careful with this, I remember trying this once before but it resulted in seg. faults (
SIGSEGV).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is ref. counted in the
org.apache.cassandra.io.util.MemoryclassThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack. I will double check. Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be fixed in 61add5b