Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/java/org/apache/cassandra/journal/Compactor.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
Expand All @@ -29,6 +30,7 @@

import org.apache.cassandra.concurrent.ScheduledExecutorPlus;
import org.apache.cassandra.concurrent.Shutdownable;
import org.apache.cassandra.utils.concurrent.Ref;
import org.apache.cassandra.utils.concurrent.WaitQueue;

import static org.apache.cassandra.concurrent.ExecutorFactory.Global.executorFactory;
Expand Down Expand Up @@ -87,6 +89,24 @@ public void run()
toCompact.subList(limit, toCompact.size()).clear();
}

// Hold refs on all segments to prevent them from being cleaned up (mmap unmapped)
// while the compactor is still reading from them. Segments whose ref can't be
// acquired have already been released and must be skipped.
List<Ref<Segment<K, V>>> refs = new ArrayList<>(toCompact.size());
Iterator<StaticSegment<K, V>> it = toCompact.iterator();
while (it.hasNext())
{
StaticSegment<K, V> segment = it.next();
Ref<Segment<K, V>> ref = segment.tryRef();
if (ref == null)
it.remove();
else
refs.add(ref);
}

if (toCompact.isEmpty())
return;

try
{
Collection<StaticSegment<K, V>> newSegments = segmentCompactor.compact(toCompact);
Expand All @@ -104,6 +124,11 @@ public void run()
{
throw new RuntimeException("Could not compact segments: " + toCompact);
}
finally
{
for (Ref<Segment<K, V>> ref : refs)
ref.release();
}
}

@Override
Expand Down