Search in sources :

Example 26 with ConnectorInfo

use of org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo in project kafka by apache.

the class ConnectorsResource method createConnector.

@POST
@Path("/")
public Response createConnector(@QueryParam("forward") final Boolean forward, @Context final HttpHeaders headers, final CreateConnectorRequest createRequest) throws Throwable {
    // Trim leading and trailing whitespaces from the connector name, replace null with empty string
    // if no name element present to keep validation within validator (NonEmptyStringWithoutControlChars
    // allows null values)
    String name = createRequest.name() == null ? "" : createRequest.name().trim();
    Map<String, String> configs = createRequest.config();
    checkAndPutConnectorConfigName(name, configs);
    FutureCallback<Herder.Created<ConnectorInfo>> cb = new FutureCallback<>();
    herder.putConnectorConfig(name, configs, false, cb);
    Herder.Created<ConnectorInfo> info = completeOrForwardRequest(cb, "/connectors", "POST", headers, createRequest, new TypeReference<ConnectorInfo>() {
    }, new CreatedConnectorInfoTranslator(), forward);
    URI location = UriBuilder.fromUri("/connectors").path(name).build();
    return Response.created(location).entity(info.result()).build();
}
Also used : ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) Herder(org.apache.kafka.connect.runtime.Herder) URI(java.net.URI) FutureCallback(org.apache.kafka.connect.util.FutureCallback) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 27 with ConnectorInfo

use of org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo in project kafka by apache.

the class DistributedHerderTest method testAccessors.

@Test
public void testAccessors() throws Exception {
    EasyMock.expect(member.memberId()).andStubReturn("leader");
    EasyMock.expect(member.currentProtocolVersion()).andStubReturn(CONNECT_PROTOCOL_V0);
    EasyMock.expect(worker.getPlugins()).andReturn(plugins).anyTimes();
    expectRebalance(1, Collections.emptyList(), Collections.emptyList());
    EasyMock.expect(configBackingStore.snapshot()).andReturn(SNAPSHOT).times(2);
    WorkerConfigTransformer configTransformer = EasyMock.mock(WorkerConfigTransformer.class);
    EasyMock.expect(configTransformer.transform(EasyMock.eq(CONN1), EasyMock.anyObject())).andThrow(new AssertionError("Config transformation should not occur when requesting connector or task info"));
    EasyMock.replay(configTransformer);
    ClusterConfigState snapshotWithTransform = new ClusterConfigState(1, null, Collections.singletonMap(CONN1, 3), Collections.singletonMap(CONN1, CONN1_CONFIG), Collections.singletonMap(CONN1, TargetState.STARTED), TASK_CONFIGS_MAP, Collections.emptySet(), configTransformer);
    expectPostRebalanceCatchup(snapshotWithTransform);
    member.wakeup();
    PowerMock.expectLastCall().anyTimes();
    // list connectors, get connector info, get connector config, get task configs
    member.poll(EasyMock.anyInt());
    PowerMock.expectLastCall();
    EasyMock.expect(member.currentProtocolVersion()).andStubReturn(CONNECT_PROTOCOL_V0);
    PowerMock.replayAll();
    FutureCallback<Collection<String>> listConnectorsCb = new FutureCallback<>();
    herder.connectors(listConnectorsCb);
    FutureCallback<ConnectorInfo> connectorInfoCb = new FutureCallback<>();
    herder.connectorInfo(CONN1, connectorInfoCb);
    FutureCallback<Map<String, String>> connectorConfigCb = new FutureCallback<>();
    herder.connectorConfig(CONN1, connectorConfigCb);
    FutureCallback<List<TaskInfo>> taskConfigsCb = new FutureCallback<>();
    herder.taskConfigs(CONN1, taskConfigsCb);
    herder.tick();
    assertTrue(listConnectorsCb.isDone());
    assertEquals(Collections.singleton(CONN1), listConnectorsCb.get());
    assertTrue(connectorInfoCb.isDone());
    ConnectorInfo info = new ConnectorInfo(CONN1, CONN1_CONFIG, Arrays.asList(TASK0, TASK1, TASK2), ConnectorType.SOURCE);
    assertEquals(info, connectorInfoCb.get());
    assertTrue(connectorConfigCb.isDone());
    assertEquals(CONN1_CONFIG, connectorConfigCb.get());
    assertTrue(taskConfigsCb.isDone());
    assertEquals(Arrays.asList(new TaskInfo(TASK0, TASK_CONFIG), new TaskInfo(TASK1, TASK_CONFIG), new TaskInfo(TASK2, TASK_CONFIG)), taskConfigsCb.get());
    PowerMock.verifyAll();
}
Also used : ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) WorkerConfigTransformer(org.apache.kafka.connect.runtime.WorkerConfigTransformer) TaskInfo(org.apache.kafka.connect.runtime.rest.entities.TaskInfo) Collection(java.util.Collection) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) FutureCallback(org.apache.kafka.connect.util.FutureCallback) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 28 with ConnectorInfo

use of org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo in project kafka by apache.

the class StandaloneHerderTest method testRestartConnectorFailureOnStart.

@Test
public void testRestartConnectorFailureOnStart() throws Exception {
    expectAdd(SourceSink.SOURCE);
    Map<String, String> config = connectorConfig(SourceSink.SOURCE);
    Connector connectorMock = PowerMock.createMock(SourceConnector.class);
    expectConfigValidation(connectorMock, true, config);
    worker.stopAndAwaitConnector(CONNECTOR_NAME);
    EasyMock.expectLastCall();
    Capture<Callback<TargetState>> onStart = EasyMock.newCapture();
    worker.startConnector(EasyMock.eq(CONNECTOR_NAME), EasyMock.eq(config), EasyMock.anyObject(HerderConnectorContext.class), EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED), EasyMock.capture(onStart));
    Exception exception = new ConnectException("Failed to start connector");
    EasyMock.expectLastCall().andAnswer(() -> {
        onStart.getValue().onCompletion(exception, null);
        return true;
    });
    PowerMock.replayAll();
    herder.putConnectorConfig(CONNECTOR_NAME, config, false, createCallback);
    Herder.Created<ConnectorInfo> connectorInfo = createCallback.get(1000L, TimeUnit.SECONDS);
    assertEquals(createdInfo(SourceSink.SOURCE), connectorInfo.result());
    FutureCallback<Void> restartCallback = new FutureCallback<>();
    herder.restartConnector(CONNECTOR_NAME, restartCallback);
    try {
        restartCallback.get(1000L, TimeUnit.MILLISECONDS);
        fail();
    } catch (ExecutionException e) {
        assertEquals(exception, e.getCause());
    }
    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) AlreadyExistsException(org.apache.kafka.connect.errors.AlreadyExistsException) BadRequestException(org.apache.kafka.connect.runtime.rest.errors.BadRequestException) NotFoundException(org.apache.kafka.connect.errors.NotFoundException) ExecutionException(java.util.concurrent.ExecutionException) ConnectException(org.apache.kafka.connect.errors.ConnectException) FutureCallback(org.apache.kafka.connect.util.FutureCallback) Callback(org.apache.kafka.connect.util.Callback) HerderConnectorContext(org.apache.kafka.connect.runtime.HerderConnectorContext) ExecutionException(java.util.concurrent.ExecutionException) Herder(org.apache.kafka.connect.runtime.Herder) FutureCallback(org.apache.kafka.connect.util.FutureCallback) ConnectException(org.apache.kafka.connect.errors.ConnectException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 29 with ConnectorInfo

use of org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo in project kafka by apache.

the class StandaloneHerderTest method testRestartConnectorAndTasksOnlyTasks.

@Test
public void testRestartConnectorAndTasksOnlyTasks() throws Exception {
    ConnectorTaskId taskId = new ConnectorTaskId(CONNECTOR_NAME, 0);
    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(true).anyTimes();
    EasyMock.expect(restartPlan.restartTaskCount()).andReturn(1).anyTimes();
    EasyMock.expect(restartPlan.totalTaskCount()).andReturn(1).anyTimes();
    EasyMock.expect(restartPlan.taskIdsToRestart()).andReturn(Collections.singletonList(taskId)).anyTimes();
    EasyMock.expect(restartPlan.restartConnectorStateInfo()).andReturn(connectorStateInfo).anyTimes();
    EasyMock.expect(herder.buildRestartPlan(restartRequest)).andReturn(Optional.of(restartPlan)).anyTimes();
    herder.onRestart(taskId);
    EasyMock.expectLastCall();
    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);
    worker.stopAndAwaitTasks(Collections.singletonList(taskId));
    EasyMock.expectLastCall();
    ClusterConfigState configState = new ClusterConfigState(-1, null, Collections.singletonMap(CONNECTOR_NAME, 1), Collections.singletonMap(CONNECTOR_NAME, connectorConfig), Collections.singletonMap(CONNECTOR_NAME, TargetState.STARTED), Collections.singletonMap(taskId, taskConfig(SourceSink.SINK)), new HashSet<>(), transformer);
    worker.startTask(taskId, configState, connectorConfig, taskConfig(SourceSink.SINK), herder, TargetState.STARTED);
    EasyMock.expectLastCall().andReturn(true);
    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) ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) RestartRequest(org.apache.kafka.connect.runtime.RestartRequest) RestartPlan(org.apache.kafka.connect.runtime.RestartPlan) Herder(org.apache.kafka.connect.runtime.Herder) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo) ClusterConfigState(org.apache.kafka.connect.runtime.distributed.ClusterConfigState) FutureCallback(org.apache.kafka.connect.util.FutureCallback) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 30 with ConnectorInfo

use of org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo in project kafka by apache.

the class StandaloneHerderTest method testPutConnectorConfig.

@Test
public void testPutConnectorConfig() throws Exception {
    Map<String, String> connConfig = connectorConfig(SourceSink.SOURCE);
    Map<String, String> newConnConfig = new HashMap<>(connConfig);
    newConnConfig.put("foo", "bar");
    Callback<Map<String, String>> connectorConfigCb = PowerMock.createMock(Callback.class);
    // Callback<Herder.Created<ConnectorInfo>> putConnectorConfigCb = PowerMock.createMock(Callback.class);
    // Create
    connector = PowerMock.createMock(BogusSourceConnector.class);
    expectAdd(SourceSink.SOURCE);
    Connector connectorMock = PowerMock.createMock(SourceConnector.class);
    expectConfigValidation(connectorMock, true, connConfig);
    // Should get first config
    connectorConfigCb.onCompletion(null, connConfig);
    EasyMock.expectLastCall();
    // Update config, which requires stopping and restarting
    worker.stopAndAwaitConnector(CONNECTOR_NAME);
    EasyMock.expectLastCall();
    Capture<Map<String, String>> capturedConfig = EasyMock.newCapture();
    Capture<Callback<TargetState>> onStart = EasyMock.newCapture();
    worker.startConnector(EasyMock.eq(CONNECTOR_NAME), EasyMock.capture(capturedConfig), EasyMock.anyObject(), EasyMock.eq(herder), EasyMock.eq(TargetState.STARTED), EasyMock.capture(onStart));
    EasyMock.expectLastCall().andAnswer(() -> {
        onStart.getValue().onCompletion(null, TargetState.STARTED);
        return true;
    });
    EasyMock.expect(worker.isRunning(CONNECTOR_NAME)).andReturn(true);
    EasyMock.expect(worker.isTopicCreationEnabled()).andReturn(true);
    // Generate same task config, which should result in no additional action to restart tasks
    EasyMock.expect(worker.connectorTaskConfigs(CONNECTOR_NAME, new SourceConnectorConfig(plugins, newConnConfig, true))).andReturn(singletonList(taskConfig(SourceSink.SOURCE)));
    worker.isSinkConnector(CONNECTOR_NAME);
    EasyMock.expectLastCall().andReturn(false);
    expectConfigValidation(connectorMock, false, newConnConfig);
    connectorConfigCb.onCompletion(null, newConnConfig);
    EasyMock.expectLastCall();
    EasyMock.expect(worker.getPlugins()).andReturn(plugins).anyTimes();
    PowerMock.replayAll();
    herder.putConnectorConfig(CONNECTOR_NAME, connConfig, false, createCallback);
    Herder.Created<ConnectorInfo> connectorInfo = createCallback.get(1000L, TimeUnit.SECONDS);
    assertEquals(createdInfo(SourceSink.SOURCE), connectorInfo.result());
    herder.connectorConfig(CONNECTOR_NAME, connectorConfigCb);
    FutureCallback<Herder.Created<ConnectorInfo>> reconfigureCallback = new FutureCallback<>();
    herder.putConnectorConfig(CONNECTOR_NAME, newConnConfig, true, reconfigureCallback);
    Herder.Created<ConnectorInfo> newConnectorInfo = reconfigureCallback.get(1000L, TimeUnit.SECONDS);
    ConnectorInfo newConnInfo = new ConnectorInfo(CONNECTOR_NAME, newConnConfig, Arrays.asList(new ConnectorTaskId(CONNECTOR_NAME, 0)), ConnectorType.SOURCE);
    assertEquals(newConnInfo, newConnectorInfo.result());
    assertEquals("bar", capturedConfig.getValue().get("foo"));
    herder.connectorConfig(CONNECTOR_NAME, connectorConfigCb);
    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) ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) HashMap(java.util.HashMap) FutureCallback(org.apache.kafka.connect.util.FutureCallback) Callback(org.apache.kafka.connect.util.Callback) SourceConnectorConfig(org.apache.kafka.connect.runtime.SourceConnectorConfig) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) Herder(org.apache.kafka.connect.runtime.Herder) FutureCallback(org.apache.kafka.connect.util.FutureCallback) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ConnectorInfo (org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo)78 Herder (org.apache.kafka.connect.runtime.Herder)65 Test (org.junit.Test)63 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)61 Callback (org.apache.kafka.connect.util.Callback)35 Connector (org.apache.kafka.connect.connector.Connector)28 SinkConnector (org.apache.kafka.connect.sink.SinkConnector)28 SourceConnector (org.apache.kafka.connect.source.SourceConnector)28 FutureCallback (org.apache.kafka.connect.util.FutureCallback)25 WorkerConnector (org.apache.kafka.connect.runtime.WorkerConnector)20 CreateConnectorRequest (org.apache.kafka.connect.runtime.rest.entities.CreateConnectorRequest)19 HashMap (java.util.HashMap)17 URI (java.net.URI)13 Map (java.util.Map)13 ConnectorConfig (org.apache.kafka.connect.runtime.ConnectorConfig)11 ConnectorTaskId (org.apache.kafka.connect.util.ConnectorTaskId)11 Config (org.apache.kafka.common.config.Config)10 ConfigDef (org.apache.kafka.common.config.ConfigDef)10 ConfigValue (org.apache.kafka.common.config.ConfigValue)10 TaskConfig (org.apache.kafka.connect.runtime.TaskConfig)10