Search in sources :

Example 1 with ReferenceCountingSegment

use of org.apache.druid.segment.ReferenceCountingSegment in project druid by druid-io.

the class FireHydrant method swapSegment.

public void swapSegment(@Nullable Segment newSegment) {
    while (true) {
        ReferenceCountingSegment currentSegment = adapter.get();
        if (currentSegment == null && newSegment == null) {
            return;
        }
        if (currentSegment != null && newSegment != null && !newSegment.getId().equals(currentSegment.getId())) {
            // Sanity check: identifier should not change
            throw new ISE("Cannot swap identifier[%s] -> [%s]", currentSegment.getId(), newSegment.getId());
        }
        if (currentSegment == newSegment) {
            throw new ISE("Cannot swap to the same segment");
        }
        ReferenceCountingSegment newReferenceCountingSegment = newSegment != null ? ReferenceCountingSegment.wrapRootGenerationSegment(newSegment) : null;
        if (adapter.compareAndSet(currentSegment, newReferenceCountingSegment)) {
            if (currentSegment != null) {
                currentSegment.close();
            }
            index = null;
            return;
        }
    }
}
Also used : ReferenceCountingSegment(org.apache.druid.segment.ReferenceCountingSegment) ISE(org.apache.druid.java.util.common.ISE)

Example 2 with ReferenceCountingSegment

use of org.apache.druid.segment.ReferenceCountingSegment in project druid by druid-io.

the class FireHydrantTest method testGetAndIncrementSegment.

@Test
public void testGetAndIncrementSegment() throws IOException {
    ReferenceCountingSegment incrementalSegmentReference = hydrant.getHydrantSegment();
    Assert.assertEquals(0, incrementalSegmentReference.getNumReferences());
    Pair<ReferenceCountingSegment, Closeable> segmentAndCloseable = hydrant.getAndIncrementSegment();
    Assert.assertEquals(1, segmentAndCloseable.lhs.getNumReferences());
    segmentAndCloseable.rhs.close();
    Assert.assertEquals(0, segmentAndCloseable.lhs.getNumReferences());
}
Also used : ReferenceCountingSegment(org.apache.druid.segment.ReferenceCountingSegment) Closeable(java.io.Closeable) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 3 with ReferenceCountingSegment

use of org.apache.druid.segment.ReferenceCountingSegment in project druid by druid-io.

the class FireHydrantTest method testGetSegmentForQuerySwapped.

@Test
public void testGetSegmentForQuerySwapped() throws IOException {
    ReferenceCountingSegment incrementalSegmentReference = hydrant.getHydrantSegment();
    hydrant.swapSegment(queryableIndexSegment);
    ReferenceCountingSegment queryableSegmentReference = hydrant.getHydrantSegment();
    Assert.assertEquals(0, incrementalSegmentReference.getNumReferences());
    Assert.assertEquals(0, queryableSegmentReference.getNumReferences());
    Optional<Pair<SegmentReference, Closeable>> maybeSegmentAndCloseable = hydrant.getSegmentForQuery(Function.identity());
    Assert.assertTrue(maybeSegmentAndCloseable.isPresent());
    Assert.assertEquals(0, incrementalSegmentReference.getNumReferences());
    Assert.assertEquals(1, queryableSegmentReference.getNumReferences());
    Pair<SegmentReference, Closeable> segmentAndCloseable = maybeSegmentAndCloseable.get();
    segmentAndCloseable.rhs.close();
    Assert.assertEquals(0, incrementalSegmentReference.getNumReferences());
    Assert.assertEquals(0, queryableSegmentReference.getNumReferences());
}
Also used : ReferenceCountingSegment(org.apache.druid.segment.ReferenceCountingSegment) Closeable(java.io.Closeable) SegmentReference(org.apache.druid.segment.SegmentReference) Pair(org.apache.druid.java.util.common.Pair) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 4 with ReferenceCountingSegment

use of org.apache.druid.segment.ReferenceCountingSegment in project druid by druid-io.

the class FireHydrantTest method testGetSegmentForQuery.

@Test
public void testGetSegmentForQuery() throws IOException {
    ReferenceCountingSegment incrementalSegmentReference = hydrant.getHydrantSegment();
    Assert.assertEquals(0, incrementalSegmentReference.getNumReferences());
    Optional<Pair<SegmentReference, Closeable>> maybeSegmentAndCloseable = hydrant.getSegmentForQuery(Function.identity());
    Assert.assertTrue(maybeSegmentAndCloseable.isPresent());
    Assert.assertEquals(1, incrementalSegmentReference.getNumReferences());
    Pair<SegmentReference, Closeable> segmentAndCloseable = maybeSegmentAndCloseable.get();
    segmentAndCloseable.rhs.close();
    Assert.assertEquals(0, incrementalSegmentReference.getNumReferences());
}
Also used : ReferenceCountingSegment(org.apache.druid.segment.ReferenceCountingSegment) Closeable(java.io.Closeable) SegmentReference(org.apache.druid.segment.SegmentReference) Pair(org.apache.druid.java.util.common.Pair) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 5 with ReferenceCountingSegment

use of org.apache.druid.segment.ReferenceCountingSegment in project druid by druid-io.

the class TimeBoundaryQueryRunnerTest method getCustomRunner.

private QueryRunner getCustomRunner() throws IOException {
    CharSource v_0112 = CharSource.wrap(StringUtils.join(V_0112, "\n"));
    CharSource v_0113 = CharSource.wrap(StringUtils.join(V_0113, "\n"));
    IncrementalIndex index0 = TestIndex.loadIncrementalIndex(newIndex("2011-01-12T00:00:00.000Z"), v_0112);
    IncrementalIndex index1 = TestIndex.loadIncrementalIndex(newIndex("2011-01-14T00:00:00.000Z"), v_0113);
    segment0 = new IncrementalIndexSegment(index0, makeIdentifier(index0, "v1"));
    segment1 = new IncrementalIndexSegment(index1, makeIdentifier(index1, "v1"));
    VersionedIntervalTimeline<String, ReferenceCountingSegment> timeline = new VersionedIntervalTimeline<>(StringComparators.LEXICOGRAPHIC);
    timeline.add(index0.getInterval(), "v1", new SingleElementPartitionChunk<>(ReferenceCountingSegment.wrapRootGenerationSegment(segment0)));
    timeline.add(index1.getInterval(), "v1", new SingleElementPartitionChunk<>(ReferenceCountingSegment.wrapRootGenerationSegment(segment1)));
    return QueryRunnerTestHelper.makeFilteringQueryRunner(timeline, FACTORY);
}
Also used : ReferenceCountingSegment(org.apache.druid.segment.ReferenceCountingSegment) CharSource(com.google.common.io.CharSource) IncrementalIndex(org.apache.druid.segment.incremental.IncrementalIndex) OnheapIncrementalIndex(org.apache.druid.segment.incremental.OnheapIncrementalIndex) IncrementalIndexSegment(org.apache.druid.segment.IncrementalIndexSegment) VersionedIntervalTimeline(org.apache.druid.timeline.VersionedIntervalTimeline)

Aggregations

ReferenceCountingSegment (org.apache.druid.segment.ReferenceCountingSegment)35 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)9 ISE (org.apache.druid.java.util.common.ISE)9 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)8 VersionedIntervalTimeline (org.apache.druid.timeline.VersionedIntervalTimeline)8 Closeable (java.io.Closeable)7 IOException (java.io.IOException)7 Pair (org.apache.druid.java.util.common.Pair)7 QueryableIndex (org.apache.druid.segment.QueryableIndex)7 SegmentReference (org.apache.druid.segment.SegmentReference)7 Closer (org.apache.druid.java.util.common.io.Closer)6 Optional (java.util.Optional)5 SegmentDescriptor (org.apache.druid.query.SegmentDescriptor)5 DataSourceAnalysis (org.apache.druid.query.planning.DataSourceAnalysis)5 DataSegment (org.apache.druid.timeline.DataSegment)5 File (java.io.File)4 Future (java.util.concurrent.Future)4 Nullable (javax.annotation.Nullable)4 Query (org.apache.druid.query.Query)4