Search in sources :

Example 1 with LifecycleLock

use of org.apache.druid.concurrent.LifecycleLock 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 2 with LifecycleLock

use of org.apache.druid.concurrent.LifecycleLock in project druid by druid-io.

the class HttpRemoteTaskRunnerTest method createWorkerHolder.

private static WorkerHolder createWorkerHolder(ObjectMapper smileMapper, HttpClient httpClient, HttpRemoteTaskRunnerConfig config, ScheduledExecutorService workersSyncExec, WorkerHolder.Listener listener, Worker worker, List<TaskAnnouncement> knownAnnouncements, // running/completed on the worker.
List<TaskAnnouncement> preExistingTaskAnnouncements, // defines behavior for what to do when a particular task is assigned
Map<Task, List<TaskAnnouncement>> toBeAssignedTasks, // work completed
AtomicInteger ticks, // happened.
Set<String> actualShutdowns) {
    return new WorkerHolder(smileMapper, httpClient, config, workersSyncExec, listener, worker, knownAnnouncements) {

        private final String workerHost;

        private final int workerPort;

        private final LifecycleLock startStopLock = new LifecycleLock();

        {
            String hostAndPort = worker.getHost();
            int colonIndex = hostAndPort.indexOf(':');
            if (colonIndex == -1) {
                throw new IAE("Invalid host and port: [%s]", colonIndex);
            }
            workerHost = hostAndPort.substring(0, colonIndex);
            workerPort = Integer.parseInt(hostAndPort.substring(colonIndex + 1));
        }

        @Override
        public void start() {
            synchronized (startStopLock) {
                if (!startStopLock.canStart()) {
                    throw new ISE("Can't start worker[%s:%s].", workerHost, workerPort);
                }
                try {
                    disabled.set(false);
                    if (!preExistingTaskAnnouncements.isEmpty()) {
                        workersSyncExec.execute(() -> {
                            for (TaskAnnouncement announcement : preExistingTaskAnnouncements) {
                                tasksSnapshotRef.get().put(announcement.getTaskId(), announcement);
                                listener.taskAddedOrUpdated(announcement, this);
                            }
                            ticks.incrementAndGet();
                        });
                    }
                    startStopLock.started();
                } finally {
                    startStopLock.exitStart();
                }
            }
        }

        @Override
        public void stop() {
            synchronized (startStopLock) {
                if (!startStopLock.canStop()) {
                    throw new ISE("Can't stop worker[%s:%s].", workerHost, workerPort);
                }
                try {
                } finally {
                    startStopLock.exitStop();
                }
            }
        }

        @Override
        public boolean isInitialized() {
            return true;
        }

        @Override
        public void waitForInitialization() {
        }

        @Override
        public boolean assignTask(Task task) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {
                throw new RuntimeException(ex);
            }
            if (toImmutable().getCurrCapacityUsed() > worker.getCapacity()) {
                throw new ISE("Got assigned tasks more than capacity.");
            }
            final List<TaskAnnouncement> announcements;
            if (toBeAssignedTasks.containsKey(task)) {
                announcements = toBeAssignedTasks.get(task);
            } else {
                // no behavior specified for the task, so do default behavior of completing the task
                announcements = new ArrayList<>();
                announcements.add(TaskAnnouncement.create(task, TaskStatus.running(task.getId()), TaskLocation.unknown()));
                announcements.add(TaskAnnouncement.create(task, TaskStatus.running(task.getId()), TaskLocation.create(workerHost, workerPort, -1)));
                announcements.add(TaskAnnouncement.create(task, TaskStatus.success(task.getId()), TaskLocation.create(workerHost, workerPort, -1)));
            }
            workersSyncExec.execute(() -> {
                for (TaskAnnouncement announcement : announcements) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        throw new RuntimeException(ex);
                    }
                    tasksSnapshotRef.get().put(announcement.getTaskId(), announcement);
                    listener.taskAddedOrUpdated(announcement, this);
                }
                ticks.incrementAndGet();
            });
            return true;
        }

        @Override
        public void shutdownTask(String taskId) {
            actualShutdowns.add(taskId);
        }
    };
}
Also used : Task(org.apache.druid.indexing.common.task.Task) NoopTask(org.apache.druid.indexing.common.task.NoopTask) TaskAnnouncement(org.apache.druid.indexing.worker.TaskAnnouncement) ISE(org.apache.druid.java.util.common.ISE) IAE(org.apache.druid.java.util.common.IAE) LifecycleLock(org.apache.druid.concurrent.LifecycleLock)

Aggregations

LifecycleLock (org.apache.druid.concurrent.LifecycleLock)2 IAE (org.apache.druid.java.util.common.IAE)2 ISE (org.apache.druid.java.util.common.ISE)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Function (com.google.common.base.Function)1 Preconditions (com.google.common.base.Preconditions)1 Predicate (com.google.common.base.Predicate)1 Predicates (com.google.common.base.Predicates)1 Collections2 (com.google.common.collect.Collections2)1 Maps (com.google.common.collect.Maps)1 HostAndPort (com.google.common.net.HostAndPort)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1