Search in sources :

Example 11 with ConnectorStatus

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

the class KafkaStatusBackingStoreTest method putConnectorStateRetriableFailure.

@Test
public void putConnectorStateRetriableFailure() {
    byte[] value = new byte[0];
    when(converter.fromConnectData(eq(STATUS_TOPIC), any(Schema.class), any(Struct.class))).thenReturn(value);
    doAnswer(invocation -> {
        ((Callback) invocation.getArgument(2)).onCompletion(null, new TimeoutException());
        return null;
    }).doAnswer(invocation -> {
        ((Callback) invocation.getArgument(2)).onCompletion(null, null);
        return null;
    }).when(kafkaBasedLog).send(eq("status-connector-conn"), eq(value), any(Callback.class));
    ConnectorStatus status = new ConnectorStatus(CONNECTOR, ConnectorStatus.State.RUNNING, WORKER_ID, 0);
    store.put(status);
    // state is not visible until read back from the log
    assertNull(store.get(CONNECTOR));
}
Also used : WorkerConfig(org.apache.kafka.connect.runtime.WorkerConfig) KafkaBasedLog(org.apache.kafka.connect.util.KafkaBasedLog) MockTime(org.apache.kafka.common.utils.MockTime) ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) Assert.assertThrows(org.junit.Assert.assertThrows) HashMap(java.util.HashMap) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) Schema(org.apache.kafka.connect.data.Schema) HashSet(java.util.HashSet) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) ArgumentCaptor(org.mockito.ArgumentCaptor) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) TimestampType(org.apache.kafka.common.record.TimestampType) Before(org.junit.Before) TimeoutException(org.apache.kafka.common.errors.TimeoutException) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ConfigException(org.apache.kafka.common.config.ConfigException) DistributedConfig(org.apache.kafka.connect.runtime.distributed.DistributedConfig) Mockito.verify(org.mockito.Mockito.verify) ConnectorStatus(org.apache.kafka.connect.runtime.ConnectorStatus) Mockito.never(org.mockito.Mockito.never) Assert.assertNull(org.junit.Assert.assertNull) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Struct(org.apache.kafka.connect.data.Struct) Optional(java.util.Optional) Mockito.any(org.mockito.Mockito.any) Callback(org.apache.kafka.clients.producer.Callback) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) TaskStatus(org.apache.kafka.connect.runtime.TaskStatus) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) Callback(org.apache.kafka.clients.producer.Callback) Schema(org.apache.kafka.connect.data.Schema) ConnectorStatus(org.apache.kafka.connect.runtime.ConnectorStatus) Struct(org.apache.kafka.connect.data.Struct) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Test(org.junit.Test)

Example 12 with ConnectorStatus

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

the class KafkaStatusBackingStoreTest method readConnectorState.

@Test
public void readConnectorState() {
    byte[] value = new byte[0];
    Map<String, Object> statusMap = new HashMap<>();
    statusMap.put("worker_id", WORKER_ID);
    statusMap.put("state", "RUNNING");
    statusMap.put("generation", 0L);
    when(converter.toConnectData(STATUS_TOPIC, value)).thenReturn(new SchemaAndValue(null, statusMap));
    store.read(consumerRecord(0, "status-connector-conn", value));
    ConnectorStatus status = new ConnectorStatus(CONNECTOR, ConnectorStatus.State.RUNNING, WORKER_ID, 0);
    assertEquals(status, store.get(CONNECTOR));
}
Also used : HashMap(java.util.HashMap) ConnectorStatus(org.apache.kafka.connect.runtime.ConnectorStatus) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Example 13 with ConnectorStatus

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

the class KafkaStatusBackingStoreTest method putConnectorStateNonRetriableFailure.

@Test
public void putConnectorStateNonRetriableFailure() {
    byte[] value = new byte[0];
    when(converter.fromConnectData(eq(STATUS_TOPIC), any(Schema.class), any(Struct.class))).thenReturn(value);
    doAnswer(invocation -> {
        ((Callback) invocation.getArgument(2)).onCompletion(null, new UnknownServerException());
        return null;
    }).when(kafkaBasedLog).send(eq("status-connector-conn"), eq(value), any(Callback.class));
    // the error is logged and ignored
    ConnectorStatus status = new ConnectorStatus(CONNECTOR, ConnectorStatus.State.RUNNING, WORKER_ID, 0);
    store.put(status);
    // state is not visible until read back from the log
    assertNull(store.get(CONNECTOR));
}
Also used : Callback(org.apache.kafka.clients.producer.Callback) Schema(org.apache.kafka.connect.data.Schema) ConnectorStatus(org.apache.kafka.connect.runtime.ConnectorStatus) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.Test)

Example 14 with ConnectorStatus

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

the class MemoryStatusBackingStoreTest method putAndGetConnectorStatus.

@Test
public void putAndGetConnectorStatus() {
    MemoryStatusBackingStore store = new MemoryStatusBackingStore();
    ConnectorStatus status = new ConnectorStatus("connector", ConnectorStatus.State.RUNNING, "localhost:8083", 0);
    store.put(status);
    assertEquals(status, store.get("connector"));
}
Also used : ConnectorStatus(org.apache.kafka.connect.runtime.ConnectorStatus) Test(org.junit.Test)

Example 15 with ConnectorStatus

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

the class KafkaStatusBackingStore method parseConnectorStatus.

private ConnectorStatus parseConnectorStatus(String connector, byte[] data) {
    try {
        SchemaAndValue schemaAndValue = converter.toConnectData(topic, data);
        if (!(schemaAndValue.value() instanceof Map)) {
            log.error("Invalid connector status type {}", schemaAndValue.value().getClass());
            return null;
        }
        @SuppressWarnings("unchecked") Map<String, Object> statusMap = (Map<String, Object>) schemaAndValue.value();
        TaskStatus.State state = TaskStatus.State.valueOf((String) statusMap.get(STATE_KEY_NAME));
        String trace = (String) statusMap.get(TRACE_KEY_NAME);
        String workerUrl = (String) statusMap.get(WORKER_ID_KEY_NAME);
        int generation = ((Long) statusMap.get(GENERATION_KEY_NAME)).intValue();
        return new ConnectorStatus(connector, state, trace, workerUrl, generation);
    } catch (Exception e) {
        log.error("Failed to deserialize connector status", e);
        return null;
    }
}
Also used : ConnectorStatus(org.apache.kafka.connect.runtime.ConnectorStatus) TaskStatus(org.apache.kafka.connect.runtime.TaskStatus) HashMap(java.util.HashMap) Map(java.util.Map) RetriableException(org.apache.kafka.common.errors.RetriableException) ConfigException(org.apache.kafka.common.config.ConfigException) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue)

Aggregations

ConnectorStatus (org.apache.kafka.connect.runtime.ConnectorStatus)27 Test (org.junit.Test)23 Callback (org.apache.kafka.clients.producer.Callback)14 Schema (org.apache.kafka.connect.data.Schema)13 Struct (org.apache.kafka.connect.data.Struct)13 HashMap (java.util.HashMap)12 SchemaAndValue (org.apache.kafka.connect.data.SchemaAndValue)12 MockTime (org.apache.kafka.common.utils.MockTime)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 TaskStatus (org.apache.kafka.connect.runtime.TaskStatus)5 EasyMock.anyObject (org.easymock.EasyMock.anyObject)4 Map (java.util.Map)3 ConfigException (org.apache.kafka.common.config.ConfigException)3 UnknownServerException (org.apache.kafka.common.errors.UnknownServerException)3 ExecutionException (java.util.concurrent.ExecutionException)2 RetriableException (org.apache.kafka.common.errors.RetriableException)2 TimeoutException (org.apache.kafka.common.errors.TimeoutException)2 Connector (org.apache.kafka.connect.connector.Connector)2 NotFoundException (org.apache.kafka.connect.errors.NotFoundException)2 WorkerConnector (org.apache.kafka.connect.runtime.WorkerConnector)2