Search in sources :

Example 66 with ConnectorTaskId

use of org.apache.kafka.connect.util.ConnectorTaskId in project apache-kafka-on-k8s by banzaicloud.

the class Worker method awaitStopTasks.

private void awaitStopTasks(Collection<ConnectorTaskId> ids) {
    long now = time.milliseconds();
    long deadline = now + config.getLong(WorkerConfig.TASK_SHUTDOWN_GRACEFUL_TIMEOUT_MS_CONFIG);
    for (ConnectorTaskId id : ids) {
        long remaining = Math.max(0, deadline - time.milliseconds());
        awaitStopTask(id, remaining);
    }
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId)

Example 67 with ConnectorTaskId

use of org.apache.kafka.connect.util.ConnectorTaskId in project apache-kafka-on-k8s by banzaicloud.

the class WorkerTaskTest method cancelBeforeStopping.

@Test
public void cancelBeforeStopping() throws Exception {
    ConnectorTaskId taskId = new ConnectorTaskId("foo", 0);
    WorkerTask workerTask = partialMockBuilder(WorkerTask.class).withConstructor(ConnectorTaskId.class, TaskStatus.Listener.class, TargetState.class, ClassLoader.class, ConnectMetrics.class).withArgs(taskId, statusListener, TargetState.STARTED, loader, metrics).addMockedMethod("initialize").addMockedMethod("execute").addMockedMethod("close").createStrictMock();
    final CountDownLatch stopped = new CountDownLatch(1);
    final Thread thread = new Thread() {

        @Override
        public void run() {
            try {
                stopped.await();
            } catch (Exception e) {
            }
        }
    };
    workerTask.initialize(TASK_CONFIG);
    EasyMock.expectLastCall();
    workerTask.execute();
    expectLastCall().andAnswer(new IAnswer<Void>() {

        @Override
        public Void answer() throws Throwable {
            thread.start();
            return null;
        }
    });
    statusListener.onStartup(taskId);
    expectLastCall();
    workerTask.close();
    expectLastCall();
    workerTask.releaseResources();
    EasyMock.expectLastCall();
    // there should be no call to onShutdown()
    replay(workerTask);
    workerTask.initialize(TASK_CONFIG);
    workerTask.run();
    workerTask.stop();
    workerTask.cancel();
    stopped.countDown();
    thread.join();
    verify(workerTask);
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectException(org.apache.kafka.connect.errors.ConnectException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 68 with ConnectorTaskId

use of org.apache.kafka.connect.util.ConnectorTaskId in project apache-kafka-on-k8s by banzaicloud.

the class WorkerTaskTest method standardStartup.

@Test
public void standardStartup() {
    ConnectorTaskId taskId = new ConnectorTaskId("foo", 0);
    WorkerTask workerTask = partialMockBuilder(WorkerTask.class).withConstructor(ConnectorTaskId.class, TaskStatus.Listener.class, TargetState.class, ClassLoader.class, ConnectMetrics.class).withArgs(taskId, statusListener, TargetState.STARTED, loader, metrics).addMockedMethod("initialize").addMockedMethod("execute").addMockedMethod("close").createStrictMock();
    workerTask.initialize(TASK_CONFIG);
    expectLastCall();
    workerTask.execute();
    expectLastCall();
    statusListener.onStartup(taskId);
    expectLastCall();
    workerTask.close();
    expectLastCall();
    workerTask.releaseResources();
    EasyMock.expectLastCall();
    statusListener.onShutdown(taskId);
    expectLastCall();
    replay(workerTask);
    workerTask.initialize(TASK_CONFIG);
    workerTask.run();
    workerTask.stop();
    workerTask.awaitStop(1000L);
    verify(workerTask);
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 69 with ConnectorTaskId

use of org.apache.kafka.connect.util.ConnectorTaskId in project apache-kafka-on-k8s by banzaicloud.

the class DistributedHerderTest method testRestartUnknownTask.

@Test
public void testRestartUnknownTask() throws Exception {
    // get the initial assignment
    EasyMock.expect(member.memberId()).andStubReturn("member");
    expectRebalance(1, Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList());
    expectPostRebalanceCatchup(SNAPSHOT);
    member.poll(EasyMock.anyInt());
    PowerMock.expectLastCall();
    member.wakeup();
    PowerMock.expectLastCall();
    member.ensureActive();
    PowerMock.expectLastCall();
    member.poll(EasyMock.anyInt());
    PowerMock.expectLastCall();
    PowerMock.replayAll();
    FutureCallback<Void> callback = new FutureCallback<>();
    herder.tick();
    herder.restartTask(new ConnectorTaskId("blah", 0), callback);
    herder.tick();
    try {
        callback.get(1000L, TimeUnit.MILLISECONDS);
        fail("Expected NotLeaderException to be raised");
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof NotFoundException);
    }
    PowerMock.verifyAll();
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) NotFoundException(org.apache.kafka.connect.errors.NotFoundException) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(org.apache.kafka.connect.util.FutureCallback) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 70 with ConnectorTaskId

use of org.apache.kafka.connect.util.ConnectorTaskId 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)

Aggregations

ConnectorTaskId (org.apache.kafka.connect.util.ConnectorTaskId)111 Test (org.junit.Test)59 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)45 HashMap (java.util.HashMap)26 ArrayList (java.util.ArrayList)25 Map (java.util.Map)18 FutureCallback (org.apache.kafka.connect.util.FutureCallback)16 ConnectException (org.apache.kafka.connect.errors.ConnectException)15 Callback (org.apache.kafka.connect.util.Callback)15 Connector (org.apache.kafka.connect.connector.Connector)13 NotFoundException (org.apache.kafka.connect.errors.NotFoundException)12 ConnectorInfo (org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo)12 SinkConnector (org.apache.kafka.connect.sink.SinkConnector)12 TaskStatus (org.apache.kafka.connect.runtime.TaskStatus)11 WorkerConnector (org.apache.kafka.connect.runtime.WorkerConnector)11 SourceConnector (org.apache.kafka.connect.source.SourceConnector)11 Herder (org.apache.kafka.connect.runtime.Herder)10 List (java.util.List)9 StatusBackingStore (org.apache.kafka.connect.storage.StatusBackingStore)9 ConnectorStateInfo (org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo)8