abhishekrb19 commented on code in PR #16685: URL: https://github.com/apache/druid/pull/16685#discussion_r1667739687
########## server/src/main/java/org/apache/druid/server/coordination/SegmentBootstrapper.java: ########## @@ -0,0 +1,439 @@ +/* + * 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.druid.server.coordination; + +import com.google.common.base.Throwables; +import com.google.common.collect.ImmutableList; +import com.google.common.util.concurrent.SettableFuture; +import com.google.inject.Inject; +import org.apache.druid.client.BootstrapSegmentsResponse; +import org.apache.druid.client.coordinator.CoordinatorClient; +import org.apache.druid.common.guava.FutureUtils; +import org.apache.druid.guice.ManageLifecycle; +import org.apache.druid.guice.ServerTypeConfig; +import org.apache.druid.java.util.common.ISE; +import org.apache.druid.java.util.common.Stopwatch; +import org.apache.druid.java.util.common.concurrent.Execs; +import org.apache.druid.java.util.common.lifecycle.LifecycleStart; +import org.apache.druid.java.util.common.lifecycle.LifecycleStop; +import org.apache.druid.java.util.emitter.EmittingLogger; +import org.apache.druid.java.util.emitter.service.ServiceEmitter; +import org.apache.druid.java.util.emitter.service.ServiceMetricEvent; +import org.apache.druid.segment.loading.SegmentLoaderConfig; +import org.apache.druid.segment.loading.SegmentLoadingException; +import org.apache.druid.server.SegmentManager; +import org.apache.druid.timeline.DataSegment; + +import javax.annotation.Nullable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * Responsible for bootstrapping cached segments from disk and bootstrap segments from the coordinator. + * Also, responsible for announcing itself as a data server if applicable once the bootstrapping functions + * are complete. + */ +@ManageLifecycle +public class SegmentBootstrapper +{ + private final SegmentLoadDropHandler loadDropHandler; + private final SegmentLoaderConfig config; + private final DataSegmentAnnouncer segmentAnnouncer; + private final DataSegmentServerAnnouncer serverAnnouncer; + private final SegmentManager segmentManager; + private final ServerTypeConfig serverTypeConfig; + private final CoordinatorClient coordinatorClient; + private final ServiceEmitter emitter; + + private volatile boolean started = false; + + // Synchronizes start/stop of this object. + private final Object startStopLock = new Object(); + + private static final EmittingLogger log = new EmittingLogger(SegmentBootstrapper.class); + + @Inject + SegmentBootstrapper( + SegmentLoadDropHandler loadDropHandler, + SegmentLoaderConfig config, + DataSegmentAnnouncer segmentAnnouncer, + DataSegmentServerAnnouncer serverAnnouncer, + SegmentManager segmentManager, + ServerTypeConfig serverTypeConfig, + CoordinatorClient coordinatorClient, + ServiceEmitter emitter + ) + { + this.loadDropHandler = loadDropHandler; + this.config = config; + this.segmentAnnouncer = segmentAnnouncer; + this.serverAnnouncer = serverAnnouncer; + this.segmentManager = segmentManager; + this.serverTypeConfig = serverTypeConfig; + this.coordinatorClient = coordinatorClient; + this.emitter = emitter; + } + + @LifecycleStart + public void start() throws IOException + { + synchronized (startStopLock) { + if (started) { + return; + } + + log.info("Starting..."); + try { + if (segmentManager.canHandleSegments()) { + loadSegmentsOnStartup(); + } + + if (shouldAnnounce()) { + serverAnnouncer.announce(); + } + } + catch (Exception e) { + Throwables.propagateIfPossible(e, IOException.class); + throw new RuntimeException(e); + } + started = true; + log.info("Started."); + } + } + + @LifecycleStop + public void stop() + { + synchronized (startStopLock) { + if (!started) { + return; + } + + log.info("Stopping..."); + try { + if (shouldAnnounce()) { + serverAnnouncer.unannounce(); + } + } + catch (Exception e) { + throw new RuntimeException(e); + } + finally { + started = false; + } + log.info("Stopped."); + } + } + + public boolean isStarted() Review Comment: Yes, good suggestion, thanks! -- 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]
