Search in sources :

Example 6 with RestartPlan

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

the class StandaloneHerder method restartConnectorAndTasks.

@Override
public synchronized void restartConnectorAndTasks(RestartRequest request, Callback<ConnectorStateInfo> cb) {
    // Ensure the connector exists
    String connectorName = request.connectorName();
    if (!configState.contains(connectorName)) {
        cb.onCompletion(new NotFoundException("Unknown connector: " + connectorName, null), null);
        return;
    }
    Optional<RestartPlan> maybePlan = buildRestartPlan(request);
    if (!maybePlan.isPresent()) {
        cb.onCompletion(new NotFoundException("Status for connector " + connectorName + " not found", null), null);
        return;
    }
    RestartPlan plan = maybePlan.get();
    // If requested, stop the connector and any tasks, marking each as restarting
    log.info("Received {}", plan);
    if (plan.shouldRestartConnector()) {
        worker.stopAndAwaitConnector(connectorName);
        onRestart(connectorName);
    }
    if (plan.shouldRestartTasks()) {
        // Stop the tasks and mark as restarting
        worker.stopAndAwaitTasks(plan.taskIdsToRestart());
        plan.taskIdsToRestart().forEach(this::onRestart);
    }
    // Now restart the connector and tasks
    if (plan.shouldRestartConnector()) {
        log.debug("Restarting connector '{}'", connectorName);
        startConnector(connectorName, (error, targetState) -> {
            if (error == null) {
                log.info("Connector '{}' restart successful", connectorName);
            } else {
                log.error("Connector '{}' restart failed", connectorName, error);
            }
        });
    }
    if (plan.shouldRestartTasks()) {
        log.debug("Restarting {} of {} tasks for {}", plan.restartTaskCount(), plan.totalTaskCount(), request);
        createConnectorTasks(connectorName, plan.taskIdsToRestart());
        log.debug("Restarted {} of {} tasks for {} as requested", plan.restartTaskCount(), plan.totalTaskCount(), request);
    }
    // Complete the restart request
    log.info("Completed {}", plan);
    cb.onCompletion(null, plan.restartConnectorStateInfo());
}
Also used : RestartPlan(org.apache.kafka.connect.runtime.RestartPlan) NotFoundException(org.apache.kafka.connect.errors.NotFoundException)

Example 7 with RestartPlan

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

the class DistributedHerderTest method testDoRestartConnectorAndTasksNoAssignments.

@Test
public void testDoRestartConnectorAndTasksNoAssignments() {
    ConnectorTaskId taskId = new ConnectorTaskId(CONN1, 0);
    RestartRequest restartRequest = new RestartRequest(CONN1, false, true);
    RestartPlan restartPlan = PowerMock.createMock(RestartPlan.class);
    EasyMock.expect(restartPlan.shouldRestartConnector()).andReturn(true).anyTimes();
    EasyMock.expect(restartPlan.shouldRestartTasks()).andReturn(true).anyTimes();
    EasyMock.expect(restartPlan.taskIdsToRestart()).andReturn(Collections.singletonList(taskId)).anyTimes();
    EasyMock.expect(herder.buildRestartPlan(restartRequest)).andReturn(Optional.of(restartPlan)).anyTimes();
    PowerMock.replayAll();
    herder.assignment = ExtendedAssignment.empty();
    herder.doRestartConnectorAndTasks(restartRequest);
    PowerMock.verifyAll();
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) RestartPlan(org.apache.kafka.connect.runtime.RestartPlan) RestartRequest(org.apache.kafka.connect.runtime.RestartRequest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with RestartPlan

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

the class DistributedHerderTest method testDoRestartConnectorAndTasksBoth.

@Test
public void testDoRestartConnectorAndTasksBoth() {
    ConnectorTaskId taskId = new ConnectorTaskId(CONN1, 0);
    RestartRequest restartRequest = new RestartRequest(CONN1, false, true);
    RestartPlan restartPlan = PowerMock.createMock(RestartPlan.class);
    EasyMock.expect(restartPlan.shouldRestartConnector()).andReturn(true).anyTimes();
    EasyMock.expect(restartPlan.shouldRestartTasks()).andReturn(true).anyTimes();
    EasyMock.expect(restartPlan.taskIdsToRestart()).andReturn(Collections.singletonList(taskId)).anyTimes();
    EasyMock.expect(restartPlan.restartTaskCount()).andReturn(1).anyTimes();
    EasyMock.expect(restartPlan.totalTaskCount()).andReturn(1).anyTimes();
    EasyMock.expect(herder.buildRestartPlan(restartRequest)).andReturn(Optional.of(restartPlan)).anyTimes();
    herder.assignment = PowerMock.createMock(ExtendedAssignment.class);
    EasyMock.expect(herder.assignment.connectors()).andReturn(Collections.singletonList(CONN1)).anyTimes();
    EasyMock.expect(herder.assignment.tasks()).andReturn(Collections.singletonList(taskId)).anyTimes();
    worker.stopAndAwaitConnector(CONN1);
    PowerMock.expectLastCall();
    Capture<Callback<TargetState>> stateCallback = newCapture();
    worker.startConnector(EasyMock.eq(CONN1), EasyMock.anyObject(), EasyMock.anyObject(), EasyMock.eq(herder), EasyMock.anyObject(TargetState.class), capture(stateCallback));
    herder.onRestart(CONN1);
    EasyMock.expectLastCall();
    worker.stopAndAwaitTasks(Collections.singletonList(taskId));
    PowerMock.expectLastCall();
    herder.onRestart(taskId);
    EasyMock.expectLastCall();
    worker.startTask(EasyMock.eq(TASK0), EasyMock.anyObject(), EasyMock.anyObject(), EasyMock.anyObject(), EasyMock.eq(herder), EasyMock.anyObject(TargetState.class));
    PowerMock.expectLastCall().andReturn(true);
    PowerMock.replayAll();
    herder.doRestartConnectorAndTasks(restartRequest);
    PowerMock.verifyAll();
}
Also used : TargetState(org.apache.kafka.connect.runtime.TargetState) FutureCallback(org.apache.kafka.connect.util.FutureCallback) Callback(org.apache.kafka.connect.util.Callback) ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) RestartPlan(org.apache.kafka.connect.runtime.RestartPlan) RestartRequest(org.apache.kafka.connect.runtime.RestartRequest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with RestartPlan

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

the class DistributedHerderTest method testRestartConnectorAndTasksSuccess.

@Test
public void testRestartConnectorAndTasksSuccess() throws Exception {
    RestartPlan restartPlan = PowerMock.createMock(RestartPlan.class);
    ConnectorStateInfo connectorStateInfo = PowerMock.createMock(ConnectorStateInfo.class);
    EasyMock.expect(restartPlan.restartConnectorStateInfo()).andReturn(connectorStateInfo).anyTimes();
    RestartRequest restartRequest = new RestartRequest(CONN1, false, true);
    EasyMock.expect(herder.buildRestartPlan(restartRequest)).andReturn(Optional.of(restartPlan)).anyTimes();
    configBackingStore.putRestartRequest(restartRequest);
    PowerMock.expectLastCall();
    // get the initial assignment
    EasyMock.expect(member.memberId()).andStubReturn("leader");
    EasyMock.expect(member.currentProtocolVersion()).andStubReturn(CONNECT_PROTOCOL_V0);
    expectRebalance(1, Collections.emptyList(), Collections.emptyList());
    expectPostRebalanceCatchup(SNAPSHOT);
    member.poll(EasyMock.anyInt());
    PowerMock.expectLastCall();
    // now handle the connector restart
    member.wakeup();
    PowerMock.expectLastCall();
    member.ensureActive();
    PowerMock.expectLastCall();
    member.poll(EasyMock.anyInt());
    PowerMock.expectLastCall();
    PowerMock.replayAll();
    herder.tick();
    FutureCallback<ConnectorStateInfo> callback = new FutureCallback<>();
    herder.restartConnectorAndTasks(restartRequest, callback);
    herder.tick();
    assertEquals(connectorStateInfo, callback.get(1000L, TimeUnit.MILLISECONDS));
    PowerMock.verifyAll();
}
Also used : RestartPlan(org.apache.kafka.connect.runtime.RestartPlan) RestartRequest(org.apache.kafka.connect.runtime.RestartRequest) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo) FutureCallback(org.apache.kafka.connect.util.FutureCallback) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 10 with RestartPlan

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

the class StandaloneHerderTest method testRestartConnectorAndTasksNoRestarts.

@Test
public void testRestartConnectorAndTasksNoRestarts() throws Exception {
    RestartRequest restartRequest = new RestartRequest(CONNECTOR_NAME, false, true);
    RestartPlan restartPlan = PowerMock.createMock(RestartPlan.class);
    ConnectorStateInfo connectorStateInfo = PowerMock.createMock(ConnectorStateInfo.class);
    EasyMock.expect(restartPlan.shouldRestartConnector()).andReturn(false).anyTimes();
    EasyMock.expect(restartPlan.shouldRestartTasks()).andReturn(false).anyTimes();
    EasyMock.expect(restartPlan.restartConnectorStateInfo()).andReturn(connectorStateInfo).anyTimes();
    EasyMock.expect(herder.buildRestartPlan(restartRequest)).andReturn(Optional.of(restartPlan)).anyTimes();
    connector = PowerMock.createMock(BogusSinkConnector.class);
    expectAdd(SourceSink.SINK);
    Map<String, String> connectorConfig = connectorConfig(SourceSink.SINK);
    Connector connectorMock = PowerMock.createMock(SinkConnector.class);
    expectConfigValidation(connectorMock, true, connectorConfig);
    PowerMock.replayAll();
    herder.putConnectorConfig(CONNECTOR_NAME, connectorConfig, false, createCallback);
    Herder.Created<ConnectorInfo> connectorInfo = createCallback.get(1000L, TimeUnit.SECONDS);
    assertEquals(createdInfo(SourceSink.SINK), connectorInfo.result());
    FutureCallback<ConnectorStateInfo> restartCallback = new FutureCallback<>();
    herder.restartConnectorAndTasks(restartRequest, restartCallback);
    assertEquals(connectorStateInfo, restartCallback.get(1000L, TimeUnit.MILLISECONDS));
    PowerMock.verifyAll();
}
Also used : SourceConnector(org.apache.kafka.connect.source.SourceConnector) WorkerConnector(org.apache.kafka.connect.runtime.WorkerConnector) Connector(org.apache.kafka.connect.connector.Connector) SinkConnector(org.apache.kafka.connect.sink.SinkConnector) ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) RestartPlan(org.apache.kafka.connect.runtime.RestartPlan) RestartRequest(org.apache.kafka.connect.runtime.RestartRequest) Herder(org.apache.kafka.connect.runtime.Herder) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo) FutureCallback(org.apache.kafka.connect.util.FutureCallback) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

RestartPlan (org.apache.kafka.connect.runtime.RestartPlan)12 RestartRequest (org.apache.kafka.connect.runtime.RestartRequest)10 Test (org.junit.Test)9 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 ConnectorTaskId (org.apache.kafka.connect.util.ConnectorTaskId)7 FutureCallback (org.apache.kafka.connect.util.FutureCallback)7 ConnectorStateInfo (org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo)6 Connector (org.apache.kafka.connect.connector.Connector)5 ConnectorInfo (org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo)5 SinkConnector (org.apache.kafka.connect.sink.SinkConnector)5 Callback (org.apache.kafka.connect.util.Callback)5 TargetState (org.apache.kafka.connect.runtime.TargetState)4 Herder (org.apache.kafka.connect.runtime.Herder)3 WorkerConnector (org.apache.kafka.connect.runtime.WorkerConnector)3 SourceConnector (org.apache.kafka.connect.source.SourceConnector)3 NotFoundException (org.apache.kafka.connect.errors.NotFoundException)2 HerderConnectorContext (org.apache.kafka.connect.runtime.HerderConnectorContext)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1