Search in sources :

Example 26 with SegmentId

use of org.apache.druid.timeline.SegmentId in project druid by druid-io.

the class CoordinatorServerView method serverRemovedSegment.

private void serverRemovedSegment(DruidServerMetadata server, DataSegment segment) {
    SegmentId segmentId = segment.getId();
    synchronized (lock) {
        log.debug("Removing segment[%s] from server[%s].", segmentId, server);
        final SegmentLoadInfo segmentLoadInfo = segmentLoadInfos.get(segmentId);
        if (segmentLoadInfo == null) {
            log.warn("Told to remove non-existant segment[%s]", segmentId);
            return;
        }
        segmentLoadInfo.removeServer(server);
        if (segmentLoadInfo.isEmpty()) {
            VersionedIntervalTimeline<String, SegmentLoadInfo> timeline = timelines.get(segment.getDataSource());
            segmentLoadInfos.remove(segmentId);
            final PartitionChunk<SegmentLoadInfo> removedPartition = timeline.remove(segment.getInterval(), segment.getVersion(), segment.getShardSpec().createChunk(new SegmentLoadInfo(segment)));
            if (removedPartition == null) {
                log.warn("Asked to remove timeline entry[interval: %s, version: %s] that doesn't exist", segment.getInterval(), segment.getVersion());
            }
        }
    }
}
Also used : SegmentId(org.apache.druid.timeline.SegmentId)

Example 27 with SegmentId

use of org.apache.druid.timeline.SegmentId in project druid by druid-io.

the class BrokerServerView method serverAddedSegment.

private void serverAddedSegment(final DruidServerMetadata server, final DataSegment segment) {
    SegmentId segmentId = segment.getId();
    synchronized (lock) {
        // loop...
        if (!server.getType().equals(ServerType.BROKER)) {
            log.debug("Adding segment[%s] for server[%s]", segment, server);
            ServerSelector selector = selectors.get(segmentId);
            if (selector == null) {
                selector = new ServerSelector(segment, tierSelectorStrategy);
                VersionedIntervalTimeline<String, ServerSelector> timeline = timelines.get(segment.getDataSource());
                if (timeline == null) {
                    timeline = new VersionedIntervalTimeline<>(Ordering.natural());
                    timelines.put(segment.getDataSource(), timeline);
                }
                timeline.add(segment.getInterval(), segment.getVersion(), segment.getShardSpec().createChunk(selector));
                selectors.put(segmentId, selector);
            }
            QueryableDruidServer queryableDruidServer = clients.get(server.getName());
            if (queryableDruidServer == null) {
                queryableDruidServer = addServer(baseView.getInventoryValue(server.getName()));
            }
            selector.addServerAndUpdateSegment(queryableDruidServer, segment);
        }
        // run the callbacks, even if the segment came from a broker, lets downstream watchers decide what to do with it
        runTimelineCallbacks(callback -> callback.segmentAdded(server, segment));
    }
}
Also used : ServerSelector(org.apache.druid.client.selector.ServerSelector) SegmentId(org.apache.druid.timeline.SegmentId) QueryableDruidServer(org.apache.druid.client.selector.QueryableDruidServer)

Example 28 with SegmentId

use of org.apache.druid.timeline.SegmentId in project druid by druid-io.

the class BrokerServerView method serverRemovedSegment.

private void serverRemovedSegment(DruidServerMetadata server, DataSegment segment) {
    SegmentId segmentId = segment.getId();
    final ServerSelector selector;
    synchronized (lock) {
        log.debug("Removing segment[%s] from server[%s].", segmentId, server);
        // since the broker segments are not stored on the timeline, do not fire segmentRemoved event
        if (server.getType().equals(ServerType.BROKER)) {
            runTimelineCallbacks(callback -> callback.serverSegmentRemoved(server, segment));
            return;
        }
        selector = selectors.get(segmentId);
        if (selector == null) {
            log.warn("Told to remove non-existant segment[%s]", segmentId);
            return;
        }
        QueryableDruidServer queryableDruidServer = clients.get(server.getName());
        if (!selector.removeServer(queryableDruidServer)) {
            log.warn("Asked to disassociate non-existant association between server[%s] and segment[%s]", server, segmentId);
        } else {
            runTimelineCallbacks(callback -> callback.serverSegmentRemoved(server, segment));
        }
        if (selector.isEmpty()) {
            VersionedIntervalTimeline<String, ServerSelector> timeline = timelines.get(segment.getDataSource());
            selectors.remove(segmentId);
            final PartitionChunk<ServerSelector> removedPartition = timeline.remove(segment.getInterval(), segment.getVersion(), segment.getShardSpec().createChunk(selector));
            if (removedPartition == null) {
                log.warn("Asked to remove timeline entry[interval: %s, version: %s] that doesn't exist", segment.getInterval(), segment.getVersion());
            } else {
                runTimelineCallbacks(callback -> callback.segmentRemoved(segment));
            }
        }
    }
}
Also used : ServerSelector(org.apache.druid.client.selector.ServerSelector) SegmentId(org.apache.druid.timeline.SegmentId) QueryableDruidServer(org.apache.druid.client.selector.QueryableDruidServer)

Example 29 with SegmentId

use of org.apache.druid.timeline.SegmentId in project druid by druid-io.

the class LoggerTest method testLogSegmentsMany.

@Test
public void testLogSegmentsMany() {
    final int numSegments = 100000;
    final MutableInt msgCount = new MutableInt();
    final Stream<SegmentId> segments = makeDataSegments(numSegments).map(DataSegment::getId);
    final Logger.LogFunction logger = (msg, format) -> {
        String message = StringUtils.format(msg, format);
        Assert.assertTrue(message.startsWith("Many segments: ["));
        Assert.assertTrue(message.endsWith("]"));
        msgCount.increment();
    };
    Logger.logSegmentIds(logger, segments, "Many segments");
    final int expected = (int) Math.ceil((double) numSegments / Logger.SEGMENTS_PER_LOG_MESSAGE);
    Assert.assertEquals(expected, msgCount.getValue());
}
Also used : IntStream(java.util.stream.IntStream) DateTimes(org.apache.druid.java.util.common.DateTimes) DateTime(org.joda.time.DateTime) StringUtils(org.apache.druid.java.util.common.StringUtils) Test(org.junit.Test) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Interval(org.joda.time.Interval) List(java.util.List) Stream(java.util.stream.Stream) DataSegment(org.apache.druid.timeline.DataSegment) SegmentId(org.apache.druid.timeline.SegmentId) Assert(org.junit.Assert) Collections(java.util.Collections) MutableInt(org.apache.commons.lang.mutable.MutableInt) SegmentId(org.apache.druid.timeline.SegmentId) MutableInt(org.apache.commons.lang.mutable.MutableInt) DataSegment(org.apache.druid.timeline.DataSegment) Test(org.junit.Test)

Example 30 with SegmentId

use of org.apache.druid.timeline.SegmentId in project druid by druid-io.

the class Logger method logSegmentIds.

/**
 * Logs all the segment ids you could ever want, {@link #SEGMENTS_PER_LOG_MESSAGE} at a time, as a comma separated
 * list.
 */
@VisibleForTesting
static void logSegmentIds(Logger.LogFunction logger, @Nullable final Stream<SegmentId> stream, @Nullable String preamble) {
    Preconditions.checkNotNull(preamble);
    if (stream == null) {
        return;
    }
    final Iterator<SegmentId> iterator = stream.iterator();
    if (!iterator.hasNext()) {
        return;
    }
    final String logFormat = preamble + ": %s";
    int counter = 0;
    StringBuilder sb = null;
    while (iterator.hasNext()) {
        SegmentId nextId = iterator.next();
        if (counter == 0) {
            // use segmentId string length of first as estimate for total size of builder for this batch
            sb = new StringBuilder(SEGMENTS_PER_LOG_MESSAGE * (2 + nextId.safeUpperLimitOfStringSize())).append("[");
        }
        sb.append(nextId);
        if (++counter < SEGMENTS_PER_LOG_MESSAGE && iterator.hasNext()) {
            sb.append(", ");
        }
        counter = counter % SEGMENTS_PER_LOG_MESSAGE;
        if (counter == 0) {
            // flush
            sb.append("]");
            logger.log(logFormat, sb.toString());
        }
    }
    // check for stragglers
    if (counter > 0) {
        sb.append("]");
        logger.log(logFormat, sb.toString());
    }
}
Also used : SegmentId(org.apache.druid.timeline.SegmentId) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

SegmentId (org.apache.druid.timeline.SegmentId)63 DataSegment (org.apache.druid.timeline.DataSegment)32 Test (org.junit.Test)21 Interval (org.joda.time.Interval)14 ISE (org.apache.druid.java.util.common.ISE)13 ArrayList (java.util.ArrayList)12 Map (java.util.Map)12 Set (java.util.Set)12 ImmutableDruidDataSource (org.apache.druid.client.ImmutableDruidDataSource)12 List (java.util.List)11 ImmutableMap (com.google.common.collect.ImmutableMap)10 IOException (java.io.IOException)9 TreeMap (java.util.TreeMap)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 VisibleForTesting (com.google.common.annotations.VisibleForTesting)8 Collectors (java.util.stream.Collectors)8 Optional (java.util.Optional)7 Sets (com.google.common.collect.Sets)6 Nullable (javax.annotation.Nullable)6 Response (javax.ws.rs.core.Response)6