Search in sources :

Example 1 with FutureCallback

use of org.apache.kafka.connect.util.FutureCallback in project kafka by apache.

the class ConnectorsResource method restartTask.

@POST
@Path("/{connector}/tasks/{task}/restart")
public void restartTask(@PathParam("connector") final String connector, @PathParam("task") final Integer task, @QueryParam("forward") final Boolean forward) throws Throwable {
    FutureCallback<Void> cb = new FutureCallback<>();
    ConnectorTaskId taskId = new ConnectorTaskId(connector, task);
    herder.restartTask(taskId, cb);
    completeOrForwardRequest(cb, "/connectors/" + connector + "/tasks/" + task + "/restart", "POST", null, forward);
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) FutureCallback(org.apache.kafka.connect.util.FutureCallback) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 2 with FutureCallback

use of org.apache.kafka.connect.util.FutureCallback in project kafka by apache.

the class ConnectorsResource method putConnectorConfig.

@PUT
@Path("/{connector}/config")
public Response putConnectorConfig(@PathParam("connector") final String connector, @QueryParam("forward") final Boolean forward, final Map<String, String> connectorConfig) throws Throwable {
    FutureCallback<Herder.Created<ConnectorInfo>> cb = new FutureCallback<>();
    String includedName = connectorConfig.get(ConnectorConfig.NAME_CONFIG);
    if (includedName != null) {
        if (!includedName.equals(connector))
            throw new BadRequestException("Connector name configuration (" + includedName + ") doesn't match connector name in the URL (" + connector + ")");
    } else {
        connectorConfig.put(ConnectorConfig.NAME_CONFIG, connector);
    }
    herder.putConnectorConfig(connector, connectorConfig, true, cb);
    Herder.Created<ConnectorInfo> createdInfo = completeOrForwardRequest(cb, "/connectors/" + connector + "/config", "PUT", connectorConfig, new TypeReference<ConnectorInfo>() {
    }, new CreatedConnectorInfoTranslator(), forward);
    Response.ResponseBuilder response;
    if (createdInfo.created())
        response = Response.created(URI.create("/connectors/" + connector));
    else
        response = Response.ok();
    return response.entity(createdInfo.result()).build();
}
Also used : Response(javax.ws.rs.core.Response) ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) BadRequestException(javax.ws.rs.BadRequestException) Herder(org.apache.kafka.connect.runtime.Herder) FutureCallback(org.apache.kafka.connect.util.FutureCallback) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Example 3 with FutureCallback

use of org.apache.kafka.connect.util.FutureCallback in project ignite by apache.

the class IgniteSinkConnectorTest method testSinkPuts.

/**
 * Tests the whole data flow from injecting data to Kafka to transferring it to the grid. It reads from two
 * specified Kafka topics, because a sink task can read from multiple topics.
 *
 * @param sinkProps Sink properties.
 * @param keyless Tests on Kafka stream with null keys if true.
 * @throws Exception Thrown in case of the failure.
 */
private void testSinkPuts(Map<String, String> sinkProps, boolean keyless) throws Exception {
    FutureCallback<Herder.Created<ConnectorInfo>> cb = new FutureCallback<>(new Callback<Herder.Created<ConnectorInfo>>() {

        @Override
        public void onCompletion(Throwable error, Herder.Created<ConnectorInfo> info) {
            if (error != null)
                throw new RuntimeException("Failed to create a job!");
        }
    });
    herder.putConnectorConfig(sinkProps.get(ConnectorConfig.NAME_CONFIG), sinkProps, false, cb);
    cb.get();
    final CountDownLatch latch = new CountDownLatch(EVENT_CNT * TOPICS.length);
    final IgnitePredicate<Event> putLsnr = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            assert evt != null;
            latch.countDown();
            return true;
        }
    };
    grid.events(grid.cluster().forCacheNodes(CACHE_NAME)).localListen(putLsnr, EVT_CACHE_OBJECT_PUT);
    IgniteCache<String, String> cache = grid.cache(CACHE_NAME);
    assertEquals(0, cache.size(CachePeekMode.PRIMARY));
    Map<String, String> keyValMap = new HashMap<>(EVENT_CNT * TOPICS.length);
    // Produces events for the specified number of topics
    for (String topic : TOPICS) keyValMap.putAll(produceStream(topic, keyless));
    // Checks all events successfully processed in 10 seconds.
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    grid.events(grid.cluster().forCacheNodes(CACHE_NAME)).stopLocalListen(putLsnr);
    // Checks that each event was processed properly.
    for (Map.Entry<String, String> entry : keyValMap.entrySet()) assertEquals(entry.getValue(), cache.get(entry.getKey()));
    assertEquals(EVENT_CNT * TOPICS.length, cache.size(CachePeekMode.PRIMARY));
}
Also used : ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) HashMap(java.util.HashMap) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CountDownLatch(java.util.concurrent.CountDownLatch) Event(org.apache.ignite.events.Event) Herder(org.apache.kafka.connect.runtime.Herder) StandaloneHerder(org.apache.kafka.connect.runtime.standalone.StandaloneHerder) HashMap(java.util.HashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap) FutureCallback(org.apache.kafka.connect.util.FutureCallback)

Example 4 with FutureCallback

use of org.apache.kafka.connect.util.FutureCallback in project ignite by apache.

the class IgniteSourceConnectorTest method doTest.

/**
 * Tests the source with the specified source configurations.
 *
 * @param srcProps Source properties.
 * @param conditioned Flag indicating whether filtering is enabled.
 * @throws Exception Fails if error.
 */
private void doTest(Map<String, String> srcProps, boolean conditioned) throws Exception {
    FutureCallback<Herder.Created<ConnectorInfo>> cb = new FutureCallback<>(new Callback<Herder.Created<ConnectorInfo>>() {

        @Override
        public void onCompletion(Throwable error, Herder.Created<ConnectorInfo> info) {
            if (error != null)
                throw new RuntimeException("Failed to create a job!", error);
        }
    });
    herder.putConnectorConfig(srcProps.get(ConnectorConfig.NAME_CONFIG), srcProps, true, cb);
    cb.get();
    // Ugh! To be sure Kafka Connect's worker thread is properly started...
    Thread.sleep(5000);
    final CountDownLatch latch = new CountDownLatch(EVENT_CNT);
    final IgnitePredicate<CacheEvent> locLsnr = new IgnitePredicate<CacheEvent>() {

        @Override
        public boolean apply(CacheEvent evt) {
            assert evt != null;
            latch.countDown();
            return true;
        }
    };
    grid.events(grid.cluster().forCacheNodes(CACHE_NAME)).localListen(locLsnr, EVT_CACHE_OBJECT_PUT);
    IgniteCache<String, String> cache = grid.cache(CACHE_NAME);
    assertEquals(0, cache.size(CachePeekMode.PRIMARY));
    Map<String, String> keyValMap = new HashMap<>(EVENT_CNT);
    keyValMap.putAll(sendData());
    // Checks all events are processed.
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    grid.events(grid.cluster().forCacheNodes(CACHE_NAME)).stopLocalListen(locLsnr);
    assertEquals(EVENT_CNT, cache.size(CachePeekMode.PRIMARY));
    // Checks the events are transferred to Kafka broker.
    if (conditioned)
        checkDataDelivered(EVENT_CNT * TOPICS.length / 2);
    else
        checkDataDelivered(EVENT_CNT * TOPICS.length);
}
Also used : ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) HashMap(java.util.HashMap) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEvent(org.apache.ignite.events.CacheEvent) Herder(org.apache.kafka.connect.runtime.Herder) StandaloneHerder(org.apache.kafka.connect.runtime.standalone.StandaloneHerder) FutureCallback(org.apache.kafka.connect.util.FutureCallback)

Example 5 with FutureCallback

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

the class ConnectorsResource method createConnector.

@POST
@Path("/")
public Response createConnector(@QueryParam("forward") final Boolean forward, 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", 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)

Aggregations

FutureCallback (org.apache.kafka.connect.util.FutureCallback)46 Test (org.junit.Test)29 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 ConnectorInfo (org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo)24 Herder (org.apache.kafka.connect.runtime.Herder)23 Connector (org.apache.kafka.connect.connector.Connector)18 SinkConnector (org.apache.kafka.connect.sink.SinkConnector)18 SourceConnector (org.apache.kafka.connect.source.SourceConnector)18 ExecutionException (java.util.concurrent.ExecutionException)17 WorkerConnector (org.apache.kafka.connect.runtime.WorkerConnector)17 ConnectorTaskId (org.apache.kafka.connect.util.ConnectorTaskId)13 HashMap (java.util.HashMap)11 Path (javax.ws.rs.Path)11 RestartRequest (org.apache.kafka.connect.runtime.RestartRequest)11 ConnectorStateInfo (org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo)11 Map (java.util.Map)9 NotFoundException (org.apache.kafka.connect.errors.NotFoundException)9 POST (javax.ws.rs.POST)7 ConnectException (org.apache.kafka.connect.errors.ConnectException)7 URI (java.net.URI)6