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);
}
}
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);
}
};
}
Aggregations