![JAR search and dependency download from the Maven repository](/logo.png)
org.apache.druid.rpc.indexing.SpecificTaskServiceLocator Maven / Gradle / Ivy
/*
* 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.rpc.indexing;
import com.google.common.collect.ImmutableSet;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
import com.google.errorprone.annotations.concurrent.GuardedBy;
import org.apache.druid.client.indexing.TaskStatusResponse;
import org.apache.druid.indexer.TaskLocation;
import org.apache.druid.indexer.TaskState;
import org.apache.druid.indexer.TaskStatus;
import org.apache.druid.indexer.TaskStatusPlus;
import org.apache.druid.java.util.common.StringUtils;
import org.apache.druid.java.util.common.concurrent.Execs;
import org.apache.druid.rpc.ServiceLocation;
import org.apache.druid.rpc.ServiceLocations;
import org.apache.druid.rpc.ServiceLocator;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
/**
* Service locator for a specific task. Uses the {@link OverlordClient#taskStatuses(Set)} API to locate tasks.
*
* This locator has an internal cache that is updated if the last check has been over {@link #LOCATION_CACHE_MS} ago.
*
* This locator is Closeable, like all ServiceLocators, but it is not essential that you actually close it. Closing
* does not free any resources: it merely makes future calls to {@link #locate()} return
* {@link ServiceLocations#closed()}.
*/
public class SpecificTaskServiceLocator implements ServiceLocator
{
private static final String BASE_PATH = "/druid/worker/v1/chat";
private static final long LOCATION_CACHE_MS = 30_000;
private final String taskId;
private final OverlordClient overlordClient;
private final Object lock = new Object();
@GuardedBy("lock")
private TaskState lastKnownState = TaskState.RUNNING; // Assume task starts out running.
@GuardedBy("lock")
private ServiceLocation lastKnownLocation;
@GuardedBy("lock")
private boolean closed = false;
@GuardedBy("lock")
private long lastUpdateTime = -1;
@GuardedBy("lock")
private SettableFuture pendingFuture = null;
public SpecificTaskServiceLocator(final String taskId, final OverlordClient overlordClient)
{
this.taskId = taskId;
this.overlordClient = overlordClient;
}
@Override
public ListenableFuture locate()
{
synchronized (lock) {
if (pendingFuture != null) {
return Futures.nonCancellationPropagating(pendingFuture);
} else if (closed || lastKnownState != TaskState.RUNNING) {
return Futures.immediateFuture(ServiceLocations.closed());
} else if (lastKnownLocation == null || lastUpdateTime + LOCATION_CACHE_MS < System.currentTimeMillis()) {
final ListenableFuture
© 2015 - 2025 Weber Informatics LLC | Privacy Policy