Search in sources :

Example 11 with TargetState

use of org.apache.kafka.connect.runtime.TargetState in project apache-kafka-on-k8s by banzaicloud.

the class WorkerCoordinatorTest method setup.

@Before
public void setup() {
    LogContext loggerFactory = new LogContext();
    this.time = new MockTime();
    this.client = new MockClient(time);
    this.metadata = new Metadata(0, Long.MAX_VALUE, true);
    this.metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
    this.consumerClient = new ConsumerNetworkClient(loggerFactory, client, metadata, time, 100, 1000, heartbeatIntervalMs);
    this.metrics = new Metrics(time);
    this.rebalanceListener = new MockRebalanceListener();
    this.configStorage = PowerMock.createMock(KafkaConfigBackingStore.class);
    client.setNode(node);
    this.coordinator = new WorkerCoordinator(loggerFactory, consumerClient, groupId, rebalanceTimeoutMs, sessionTimeoutMs, heartbeatIntervalMs, metrics, "consumer" + groupId, time, retryBackoffMs, LEADER_URL, configStorage, rebalanceListener);
    configState1 = new ClusterConfigState(1L, Collections.singletonMap(connectorId1, 1), Collections.singletonMap(connectorId1, (Map<String, String>) new HashMap<String, String>()), Collections.singletonMap(connectorId1, TargetState.STARTED), Collections.singletonMap(taskId1x0, (Map<String, String>) new HashMap<String, String>()), Collections.<String>emptySet());
    Map<String, Integer> configState2ConnectorTaskCounts = new HashMap<>();
    configState2ConnectorTaskCounts.put(connectorId1, 2);
    configState2ConnectorTaskCounts.put(connectorId2, 1);
    Map<String, Map<String, String>> configState2ConnectorConfigs = new HashMap<>();
    configState2ConnectorConfigs.put(connectorId1, new HashMap<String, String>());
    configState2ConnectorConfigs.put(connectorId2, new HashMap<String, String>());
    Map<String, TargetState> configState2TargetStates = new HashMap<>();
    configState2TargetStates.put(connectorId1, TargetState.STARTED);
    configState2TargetStates.put(connectorId2, TargetState.STARTED);
    Map<ConnectorTaskId, Map<String, String>> configState2TaskConfigs = new HashMap<>();
    configState2TaskConfigs.put(taskId1x0, new HashMap<String, String>());
    configState2TaskConfigs.put(taskId1x1, new HashMap<String, String>());
    configState2TaskConfigs.put(taskId2x0, new HashMap<String, String>());
    configState2 = new ClusterConfigState(2L, configState2ConnectorTaskCounts, configState2ConnectorConfigs, configState2TargetStates, configState2TaskConfigs, Collections.<String>emptySet());
    Map<String, Integer> configStateSingleTaskConnectorsConnectorTaskCounts = new HashMap<>();
    configStateSingleTaskConnectorsConnectorTaskCounts.put(connectorId1, 1);
    configStateSingleTaskConnectorsConnectorTaskCounts.put(connectorId2, 1);
    configStateSingleTaskConnectorsConnectorTaskCounts.put(connectorId3, 1);
    Map<String, Map<String, String>> configStateSingleTaskConnectorsConnectorConfigs = new HashMap<>();
    configStateSingleTaskConnectorsConnectorConfigs.put(connectorId1, new HashMap<String, String>());
    configStateSingleTaskConnectorsConnectorConfigs.put(connectorId2, new HashMap<String, String>());
    configStateSingleTaskConnectorsConnectorConfigs.put(connectorId3, new HashMap<String, String>());
    Map<String, TargetState> configStateSingleTaskConnectorsTargetStates = new HashMap<>();
    configStateSingleTaskConnectorsTargetStates.put(connectorId1, TargetState.STARTED);
    configStateSingleTaskConnectorsTargetStates.put(connectorId2, TargetState.STARTED);
    configStateSingleTaskConnectorsTargetStates.put(connectorId3, TargetState.STARTED);
    Map<ConnectorTaskId, Map<String, String>> configStateSingleTaskConnectorsTaskConfigs = new HashMap<>();
    configStateSingleTaskConnectorsTaskConfigs.put(taskId1x0, new HashMap<String, String>());
    configStateSingleTaskConnectorsTaskConfigs.put(taskId2x0, new HashMap<String, String>());
    configStateSingleTaskConnectorsTaskConfigs.put(taskId3x0, new HashMap<String, String>());
    configStateSingleTaskConnectors = new ClusterConfigState(2L, configStateSingleTaskConnectorsConnectorTaskCounts, configStateSingleTaskConnectorsConnectorConfigs, configStateSingleTaskConnectorsTargetStates, configStateSingleTaskConnectorsTaskConfigs, Collections.<String>emptySet());
}
Also used : TargetState(org.apache.kafka.connect.runtime.TargetState) ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) HashMap(java.util.HashMap) ProtocolMetadata(org.apache.kafka.common.requests.JoinGroupRequest.ProtocolMetadata) Metadata(org.apache.kafka.clients.Metadata) LogContext(org.apache.kafka.common.utils.LogContext) KafkaConfigBackingStore(org.apache.kafka.connect.storage.KafkaConfigBackingStore) Metrics(org.apache.kafka.common.metrics.Metrics) ConsumerNetworkClient(org.apache.kafka.clients.consumer.internals.ConsumerNetworkClient) HashMap(java.util.HashMap) Map(java.util.Map) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) Before(org.junit.Before)

Example 12 with TargetState

use of org.apache.kafka.connect.runtime.TargetState in project kafka by apache.

the class DistributedHerder method processTargetStateChanges.

private void processTargetStateChanges(Set<String> connectorTargetStateChanges) {
    log.trace("Processing target state updates; " + "currently-known connectors are {}, and to-be-updated connectors are {}", configState.connectors(), connectorTargetStateChanges);
    for (String connector : connectorTargetStateChanges) {
        TargetState targetState = configState.targetState(connector);
        if (!configState.connectors().contains(connector)) {
            log.debug("Received target state change for unknown connector: {}", connector);
            continue;
        }
        // we must propagate the state change to the worker so that the connector's
        // tasks can transition to the new target state
        worker.setTargetState(connector, targetState, (error, newState) -> {
            if (error != null) {
                log.error("Failed to transition connector to target state", error);
                return;
            }
            // request reconfiguration to ensure that config changes while paused take effect
            if (newState == TargetState.STARTED) {
                requestTaskReconfiguration(connector);
            }
        });
    }
}
Also used : TargetState(org.apache.kafka.connect.runtime.TargetState)

Example 13 with TargetState

use of org.apache.kafka.connect.runtime.TargetState in project kafka by apache.

the class DistributedHerder method startConnector.

// Helper for starting a connector with the given name, which will extract & parse the config, generate connector
// context and add to the worker. This needs to be called from within the main worker thread for this herder.
// The callback is invoked after the connector has finished startup and generated task configs, or failed in the process.
private void startConnector(String connectorName, Callback<Void> callback) {
    log.info("Starting connector {}", connectorName);
    final Map<String, String> configProps = configState.connectorConfig(connectorName);
    final CloseableConnectorContext ctx = new HerderConnectorContext(this, connectorName);
    final TargetState initialState = configState.targetState(connectorName);
    final Callback<TargetState> onInitialStateChange = (error, newState) -> {
        if (error != null) {
            callback.onCompletion(new ConnectException("Failed to start connector: " + connectorName, error), null);
            return;
        }
        // Use newState here in case the connector has been paused right after being created
        if (newState == TargetState.STARTED) {
            addRequest(() -> {
                // Request configuration since this could be a brand new connector. However, also only update those
                // task configs if they are actually different from the existing ones to avoid unnecessary updates when this is
                // just restoring an existing connector.
                reconfigureConnectorTasksWithRetry(time.milliseconds(), connectorName);
                callback.onCompletion(null, null);
                return null;
            }, forwardErrorCallback(callback));
        } else {
            callback.onCompletion(null, null);
        }
    };
    worker.startConnector(connectorName, configProps, ctx, this, initialState, onInitialStateChange);
}
Also used : TargetState(org.apache.kafka.connect.runtime.TargetState) Worker(org.apache.kafka.connect.runtime.Worker) SinkUtils(org.apache.kafka.connect.util.SinkUtils) Arrays(java.util.Arrays) TimeoutException(java.util.concurrent.TimeoutException) AlreadyExistsException(org.apache.kafka.connect.errors.AlreadyExistsException) KeyGenerator(javax.crypto.KeyGenerator) SessionKey(org.apache.kafka.connect.runtime.SessionKey) ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) RestartRequest(org.apache.kafka.connect.runtime.RestartRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LogContext(org.apache.kafka.common.utils.LogContext) Map(java.util.Map) UriBuilder(javax.ws.rs.core.UriBuilder) ThreadUtils(org.apache.kafka.common.utils.ThreadUtils) RestClient(org.apache.kafka.connect.runtime.rest.RestClient) ConfigDef(org.apache.kafka.common.config.ConfigDef) CommonClientConfigs(org.apache.kafka.clients.CommonClientConfigs) Sensor(org.apache.kafka.common.metrics.Sensor) Time(org.apache.kafka.common.utils.Time) WakeupException(org.apache.kafka.common.errors.WakeupException) Collection(java.util.Collection) CumulativeSum(org.apache.kafka.common.metrics.stats.CumulativeSum) Set(java.util.Set) NavigableSet(java.util.NavigableSet) ConfigValue(org.apache.kafka.common.config.ConfigValue) Collectors(java.util.stream.Collectors) AbstractHerder(org.apache.kafka.connect.runtime.AbstractHerder) Executors(java.util.concurrent.Executors) Objects(java.util.Objects) CONNECT_PROTOCOL_V0(org.apache.kafka.connect.runtime.distributed.ConnectProtocol.CONNECT_PROTOCOL_V0) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo) CloseableConnectorContext(org.apache.kafka.connect.runtime.CloseableConnectorContext) List(java.util.List) EAGER(org.apache.kafka.connect.runtime.distributed.ConnectProtocolCompatibility.EAGER) ConnectMetrics(org.apache.kafka.connect.runtime.ConnectMetrics) SourceConnectorConfig(org.apache.kafka.connect.runtime.SourceConnectorConfig) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) TargetState(org.apache.kafka.connect.runtime.TargetState) SecretKey(javax.crypto.SecretKey) BadRequestException(org.apache.kafka.connect.runtime.rest.errors.BadRequestException) ConnectorClientConfigOverridePolicy(org.apache.kafka.connect.connector.policy.ConnectorClientConfigOverridePolicy) ConnectorConfig(org.apache.kafka.connect.runtime.ConnectorConfig) TaskStatus(org.apache.kafka.connect.runtime.TaskStatus) Max(org.apache.kafka.common.metrics.stats.Max) Connector(org.apache.kafka.connect.connector.Connector) Exit(org.apache.kafka.common.utils.Exit) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) NotFoundException(org.apache.kafka.connect.errors.NotFoundException) ConfigBackingStore(org.apache.kafka.connect.storage.ConfigBackingStore) AtomicReference(java.util.concurrent.atomic.AtomicReference) TOPIC_TRACKING_ENABLE_CONFIG(org.apache.kafka.connect.runtime.WorkerConfig.TOPIC_TRACKING_ENABLE_CONFIG) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CONNECT_PROTOCOL_V2(org.apache.kafka.connect.runtime.distributed.IncrementalCooperativeConnectProtocol.CONNECT_PROTOCOL_V2) CONNECT_PROTOCOL_V1(org.apache.kafka.connect.runtime.distributed.IncrementalCooperativeConnectProtocol.CONNECT_PROTOCOL_V1) MetricGroup(org.apache.kafka.connect.runtime.ConnectMetrics.MetricGroup) NoSuchElementException(java.util.NoSuchElementException) ExecutorService(java.util.concurrent.ExecutorService) SinkConnector(org.apache.kafka.connect.sink.SinkConnector) Utils(org.apache.kafka.common.utils.Utils) HerderConnectorContext(org.apache.kafka.connect.runtime.HerderConnectorContext) Callback(org.apache.kafka.connect.util.Callback) RestartPlan(org.apache.kafka.connect.runtime.RestartPlan) Logger(org.slf4j.Logger) InternalRequestSignature(org.apache.kafka.connect.runtime.rest.InternalRequestSignature) HerderRequest(org.apache.kafka.connect.runtime.HerderRequest) SinkConnectorConfig(org.apache.kafka.connect.runtime.SinkConnectorConfig) ConnectRestException(org.apache.kafka.connect.runtime.rest.errors.ConnectRestException) ConnectMetricsRegistry(org.apache.kafka.connect.runtime.ConnectMetricsRegistry) TaskInfo(org.apache.kafka.connect.runtime.rest.entities.TaskInfo) StatusBackingStore(org.apache.kafka.connect.storage.StatusBackingStore) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) Avg(org.apache.kafka.common.metrics.stats.Avg) ConnectException(org.apache.kafka.connect.errors.ConnectException) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) Collections(java.util.Collections) CloseableConnectorContext(org.apache.kafka.connect.runtime.CloseableConnectorContext) HerderConnectorContext(org.apache.kafka.connect.runtime.HerderConnectorContext) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 14 with TargetState

use of org.apache.kafka.connect.runtime.TargetState in project kafka by apache.

the class StandaloneHerder method startConnector.

private void startConnector(String connName, Callback<TargetState> onStart) {
    Map<String, String> connConfigs = configState.connectorConfig(connName);
    TargetState targetState = configState.targetState(connName);
    worker.startConnector(connName, connConfigs, new HerderConnectorContext(this, connName), this, targetState, onStart);
}
Also used : TargetState(org.apache.kafka.connect.runtime.TargetState) HerderConnectorContext(org.apache.kafka.connect.runtime.HerderConnectorContext)

Example 15 with TargetState

use of org.apache.kafka.connect.runtime.TargetState in project kafka by apache.

the class StandaloneHerder method restartTask.

@Override
public synchronized void restartTask(ConnectorTaskId taskId, Callback<Void> cb) {
    if (!configState.contains(taskId.connector()))
        cb.onCompletion(new NotFoundException("Connector " + taskId.connector() + " not found", null), null);
    Map<String, String> taskConfigProps = configState.taskConfig(taskId);
    if (taskConfigProps == null)
        cb.onCompletion(new NotFoundException("Task " + taskId + " not found", null), null);
    Map<String, String> connConfigProps = configState.connectorConfig(taskId.connector());
    TargetState targetState = configState.targetState(taskId.connector());
    worker.stopAndAwaitTask(taskId);
    if (worker.startTask(taskId, configState, connConfigProps, taskConfigProps, this, targetState))
        cb.onCompletion(null, null);
    else
        cb.onCompletion(new ConnectException("Failed to start task: " + taskId), null);
}
Also used : TargetState(org.apache.kafka.connect.runtime.TargetState) NotFoundException(org.apache.kafka.connect.errors.NotFoundException) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Aggregations

TargetState (org.apache.kafka.connect.runtime.TargetState)16 HerderConnectorContext (org.apache.kafka.connect.runtime.HerderConnectorContext)7 ConnectorTaskId (org.apache.kafka.connect.util.ConnectorTaskId)7 HashMap (java.util.HashMap)4 Map (java.util.Map)4 LogContext (org.apache.kafka.common.utils.LogContext)4 RestartPlan (org.apache.kafka.connect.runtime.RestartPlan)4 RestartRequest (org.apache.kafka.connect.runtime.RestartRequest)4 Callback (org.apache.kafka.connect.util.Callback)4 ConnectException (org.apache.kafka.connect.errors.ConnectException)3 NotFoundException (org.apache.kafka.connect.errors.NotFoundException)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 List (java.util.List)2 NavigableSet (java.util.NavigableSet)2 NoSuchElementException (java.util.NoSuchElementException)2 Objects (java.util.Objects)2