Search in sources :

Example 1 with LifecycleStart

use of org.apache.druid.java.util.common.lifecycle.LifecycleStart in project druid by druid-io.

the class FileRequestLogger method start.

@LifecycleStart
@Override
public void start() {
    try {
        FileUtils.mkdirp(baseDir);
        MutableDateTime mutableDateTime = DateTimes.nowUtc().toMutableDateTime(ISOChronology.getInstanceUTC());
        mutableDateTime.setMillisOfDay(0);
        synchronized (lock) {
            currentDay = mutableDateTime.toDateTime(ISOChronology.getInstanceUTC());
            fileWriter = getFileWriter();
        }
        long nextDay = currentDay.plusDays(1).getMillis();
        Duration initialDelay = new Duration(nextDay - System.currentTimeMillis());
        ScheduledExecutors.scheduleWithFixedDelay(exec, initialDelay, Duration.standardDays(1), new Callable<ScheduledExecutors.Signal>() {

            @Override
            public ScheduledExecutors.Signal call() {
                try {
                    synchronized (lock) {
                        currentDay = currentDay.plusDays(1);
                        CloseableUtils.closeAndSuppressExceptions(fileWriter, e -> log.warn("Could not close log file for %s. Creating new log file anyway.", currentDay));
                        fileWriter = getFileWriter();
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                return ScheduledExecutors.Signal.REPEAT;
            }
        });
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Logger(org.apache.druid.java.util.common.logger.Logger) DateTimeFormat(org.joda.time.format.DateTimeFormat) DateTimes(org.apache.druid.java.util.common.DateTimes) ScheduledExecutors(org.apache.druid.java.util.common.concurrent.ScheduledExecutors) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) Duration(org.joda.time.Duration) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) LifecycleStart(org.apache.druid.java.util.common.lifecycle.LifecycleStart) MutableDateTime(org.joda.time.MutableDateTime) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) StandardCharsets(java.nio.charset.StandardCharsets) RequestLogLine(org.apache.druid.server.RequestLogLine) LifecycleStop(org.apache.druid.java.util.common.lifecycle.LifecycleStop) ISOChronology(org.joda.time.chrono.ISOChronology) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) OutputStreamWriter(java.io.OutputStreamWriter) CloseableUtils(org.apache.druid.utils.CloseableUtils) FileUtils(org.apache.druid.java.util.common.FileUtils) MutableDateTime(org.joda.time.MutableDateTime) Duration(org.joda.time.Duration) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) LifecycleStart(org.apache.druid.java.util.common.lifecycle.LifecycleStart)

Example 2 with LifecycleStart

use of org.apache.druid.java.util.common.lifecycle.LifecycleStart in project druid by druid-io.

the class ZkCoordinator method start.

@LifecycleStart
public void start() throws IOException {
    synchronized (lock) {
        if (started) {
            return;
        }
        log.info("Starting zkCoordinator for server[%s]", me.getName());
        final String loadQueueLocation = ZKPaths.makePath(zkPaths.getLoadQueuePath(), me.getName());
        final String servedSegmentsLocation = ZKPaths.makePath(zkPaths.getServedSegmentsPath(), me.getName());
        final String liveSegmentsLocation = ZKPaths.makePath(zkPaths.getLiveSegmentsPath(), me.getName());
        loadQueueCache = new PathChildrenCache(curator, loadQueueLocation, true, true, Execs.singleThreaded("ZkCoordinator"));
        try {
            curator.newNamespaceAwareEnsurePath(loadQueueLocation).ensure(curator.getZookeeperClient());
            curator.newNamespaceAwareEnsurePath(servedSegmentsLocation).ensure(curator.getZookeeperClient());
            curator.newNamespaceAwareEnsurePath(liveSegmentsLocation).ensure(curator.getZookeeperClient());
            loadQueueCache.getListenable().addListener((client, event) -> {
                final ChildData child = event.getData();
                switch(event.getType()) {
                    case CHILD_ADDED:
                        childAdded(child);
                        break;
                    case CHILD_REMOVED:
                        log.info("zNode[%s] was removed", event.getData().getPath());
                        break;
                    default:
                        log.info("Ignoring event[%s]", event);
                }
            });
            loadQueueCache.start();
        } catch (Exception e) {
            Throwables.propagateIfPossible(e, IOException.class);
            throw new RuntimeException(e);
        }
        started = true;
    }
}
Also used : PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ChildData(org.apache.curator.framework.recipes.cache.ChildData) IOException(java.io.IOException) IOException(java.io.IOException) LifecycleStart(org.apache.druid.java.util.common.lifecycle.LifecycleStart)

Example 3 with LifecycleStart

use of org.apache.druid.java.util.common.lifecycle.LifecycleStart in project druid by druid-io.

the class TieredBrokerHostSelector method start.

@LifecycleStart
public void start() {
    synchronized (lock) {
        if (started) {
            return;
        }
        for (Map.Entry<String, String> entry : tierConfig.getTierToBrokerMap().entrySet()) {
            servers.put(entry.getValue(), new NodesHolder());
        }
        DruidNodeDiscovery druidNodeDiscovery = druidNodeDiscoveryProvider.getForNodeRole(NodeRole.BROKER);
        druidNodeDiscovery.registerListener(new DruidNodeDiscovery.Listener() {

            @Override
            public void nodesAdded(Collection<DiscoveryDruidNode> nodes) {
                nodes.forEach((node) -> {
                    NodesHolder nodesHolder = servers.get(node.getDruidNode().getServiceName());
                    if (nodesHolder != null) {
                        nodesHolder.add(node.getDruidNode().getHostAndPortToUse(), TO_SERVER.apply(node));
                    }
                });
            }

            @Override
            public void nodesRemoved(Collection<DiscoveryDruidNode> nodes) {
                nodes.forEach((node) -> {
                    NodesHolder nodesHolder = servers.get(node.getDruidNode().getServiceName());
                    if (nodesHolder != null) {
                        nodesHolder.remove(node.getDruidNode().getHostAndPortToUse());
                    }
                });
            }
        });
        started = true;
    }
}
Also used : DiscoveryDruidNode(org.apache.druid.discovery.DiscoveryDruidNode) Iterables(com.google.common.collect.Iterables) DruidNodeDiscoveryProvider(org.apache.druid.discovery.DruidNodeDiscoveryProvider) Inject(com.google.inject.Inject) HashMap(java.util.HashMap) LifecycleStart(org.apache.druid.java.util.common.lifecycle.LifecycleStart) Pair(org.apache.druid.java.util.common.Pair) Interval(org.joda.time.Interval) DruidNodeDiscovery(org.apache.druid.discovery.DruidNodeDiscovery) ImmutableList(com.google.common.collect.ImmutableList) Query(org.apache.druid.query.Query) LifecycleStop(org.apache.druid.java.util.common.lifecycle.LifecycleStop) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Optional(com.google.common.base.Optional) Map(java.util.Map) DateTimes(org.apache.druid.java.util.common.DateTimes) SqlQuery(org.apache.druid.sql.http.SqlQuery) Function(com.google.common.base.Function) EmittingLogger(org.apache.druid.java.util.emitter.EmittingLogger) Collection(java.util.Collection) DateTime(org.joda.time.DateTime) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Rule(org.apache.druid.server.coordinator.rules.Rule) Maps(com.google.common.collect.Maps) QueryContexts(org.apache.druid.query.QueryContexts) List(java.util.List) NodeRole(org.apache.druid.discovery.NodeRole) LoadRule(org.apache.druid.server.coordinator.rules.LoadRule) Server(org.apache.druid.client.selector.Server) DiscoveryDruidNode(org.apache.druid.discovery.DiscoveryDruidNode) DruidNodeDiscovery(org.apache.druid.discovery.DruidNodeDiscovery) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LifecycleStart(org.apache.druid.java.util.common.lifecycle.LifecycleStart)

Example 4 with LifecycleStart

use of org.apache.druid.java.util.common.lifecycle.LifecycleStart in project druid by druid-io.

the class HttpServerInventoryView method start.

@LifecycleStart
public void start() {
    synchronized (lifecycleLock) {
        if (!lifecycleLock.canStart()) {
            throw new ISE("can't start.");
        }
        log.info("Starting %s.", execNamePrefix);
        try {
            executor = ScheduledExecutors.fixed(config.getNumThreads(), execNamePrefix + "-%s");
            DruidNodeDiscovery druidNodeDiscovery = druidNodeDiscoveryProvider.getForService(DataNodeService.DISCOVERY_SERVICE_KEY);
            druidNodeDiscovery.registerListener(new DruidNodeDiscovery.Listener() {

                private final AtomicBoolean initialized = new AtomicBoolean(false);

                @Override
                public void nodesAdded(Collection<DiscoveryDruidNode> nodes) {
                    nodes.forEach(node -> serverAdded(toDruidServer(node)));
                }

                @Override
                public void nodesRemoved(Collection<DiscoveryDruidNode> nodes) {
                    nodes.forEach(node -> serverRemoved(toDruidServer(node)));
                }

                @Override
                public void nodeViewInitialized() {
                    if (!initialized.getAndSet(true)) {
                        executor.execute(HttpServerInventoryView.this::serverInventoryInitialized);
                    }
                }

                private DruidServer toDruidServer(DiscoveryDruidNode node) {
                    return new DruidServer(node.getDruidNode().getHostAndPortToUse(), node.getDruidNode().getHostAndPort(), node.getDruidNode().getHostAndTlsPort(), ((DataNodeService) node.getServices().get(DataNodeService.DISCOVERY_SERVICE_KEY)).getMaxSize(), ((DataNodeService) node.getServices().get(DataNodeService.DISCOVERY_SERVICE_KEY)).getServerType(), ((DataNodeService) node.getServices().get(DataNodeService.DISCOVERY_SERVICE_KEY)).getTier(), ((DataNodeService) node.getServices().get(DataNodeService.DISCOVERY_SERVICE_KEY)).getPriority());
                }
            });
            scheduleSyncMonitoring();
            lifecycleLock.started();
        } finally {
            lifecycleLock.exitStart();
        }
        log.info("Started %s.", execNamePrefix);
    }
}
Also used : DiscoveryDruidNode(org.apache.druid.discovery.DiscoveryDruidNode) DruidNodeDiscoveryProvider(org.apache.druid.discovery.DruidNodeDiscoveryProvider) ScheduledExecutors(org.apache.druid.java.util.common.concurrent.ScheduledExecutors) HttpClient(org.apache.druid.java.util.http.client.HttpClient) ChangeRequestHttpSyncer(org.apache.druid.server.coordination.ChangeRequestHttpSyncer) URL(java.net.URL) ChangeRequestsSnapshot(org.apache.druid.server.coordination.ChangeRequestsSnapshot) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Collections2(com.google.common.collect.Collections2) LifecycleStart(org.apache.druid.java.util.common.lifecycle.LifecycleStart) SegmentChangeRequestLoad(org.apache.druid.server.coordination.SegmentChangeRequestLoad) Pair(org.apache.druid.java.util.common.Pair) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) DruidNodeDiscovery(org.apache.druid.discovery.DruidNodeDiscovery) LifecycleStop(org.apache.druid.java.util.common.lifecycle.LifecycleStop) DruidServerMetadata(org.apache.druid.server.coordination.DruidServerMetadata) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Predicates(com.google.common.base.Predicates) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IAE(org.apache.druid.java.util.common.IAE) Function(com.google.common.base.Function) EmittingLogger(org.apache.druid.java.util.emitter.EmittingLogger) Iterator(java.util.Iterator) RE(org.apache.druid.java.util.common.RE) MalformedURLException(java.net.MalformedURLException) Executor(java.util.concurrent.Executor) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ISE(org.apache.druid.java.util.common.ISE) SegmentChangeRequestDrop(org.apache.druid.server.coordination.SegmentChangeRequestDrop) Maps(com.google.common.collect.Maps) HostAndPort(com.google.common.net.HostAndPort) TimeUnit(java.util.concurrent.TimeUnit) DataNodeService(org.apache.druid.discovery.DataNodeService) List(java.util.List) Predicate(com.google.common.base.Predicate) LifecycleLock(org.apache.druid.concurrent.LifecycleLock) DataSegment(org.apache.druid.timeline.DataSegment) Preconditions(com.google.common.base.Preconditions) SegmentId(org.apache.druid.timeline.SegmentId) DataSegmentChangeRequest(org.apache.druid.server.coordination.DataSegmentChangeRequest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DiscoveryDruidNode(org.apache.druid.discovery.DiscoveryDruidNode) DruidNodeDiscovery(org.apache.druid.discovery.DruidNodeDiscovery) ISE(org.apache.druid.java.util.common.ISE) DataNodeService(org.apache.druid.discovery.DataNodeService) LifecycleStart(org.apache.druid.java.util.common.lifecycle.LifecycleStart)

Example 5 with LifecycleStart

use of org.apache.druid.java.util.common.lifecycle.LifecycleStart in project druid by druid-io.

the class DerivativeDataSourceManager method start.

@LifecycleStart
public void start() {
    log.info("starting derivatives manager.");
    synchronized (lock) {
        if (started) {
            return;
        }
        exec = MoreExecutors.listeningDecorator(Execs.scheduledSingleThreaded("DerivativeDataSourceManager-Exec-%d"));
        final Duration delay = config.getPollDuration().toStandardDuration();
        future = exec.scheduleWithFixedDelay(new Runnable() {

            @Override
            public void run() {
                try {
                    updateDerivatives();
                } catch (Exception e) {
                    log.makeAlert(e, "uncaught exception in derivatives manager updating thread").emit();
                }
            }
        }, 0, delay.getMillis(), TimeUnit.MILLISECONDS);
        started = true;
    }
    log.info("Derivatives manager started.");
}
Also used : Duration(org.joda.time.Duration) LifecycleStart(org.apache.druid.java.util.common.lifecycle.LifecycleStart)

Aggregations

LifecycleStart (org.apache.druid.java.util.common.lifecycle.LifecycleStart)15 ISE (org.apache.druid.java.util.common.ISE)8 Duration (org.joda.time.Duration)8 IOException (java.io.IOException)5 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 Function (com.google.common.base.Function)3 File (java.io.File)3 LifecycleStop (org.apache.druid.java.util.common.lifecycle.LifecycleStop)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Maps (com.google.common.collect.Maps)2 Collection (java.util.Collection)2 List (java.util.List)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 DiscoveryDruidNode (org.apache.druid.discovery.DiscoveryDruidNode)2 DruidNodeDiscovery (org.apache.druid.discovery.DruidNodeDiscovery)2 DruidNodeDiscoveryProvider (org.apache.druid.discovery.DruidNodeDiscoveryProvider)2 DateTimes (org.apache.druid.java.util.common.DateTimes)2 Pair (org.apache.druid.java.util.common.Pair)2 ScheduledExecutors (org.apache.druid.java.util.common.concurrent.ScheduledExecutors)2