Search in sources :

Example 6 with ConnectorStatus

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

the class KafkaStatusBackingStoreTest method putSafeConnectorIgnoresStaleStatus.

@Test
public void putSafeConnectorIgnoresStaleStatus() {
    byte[] value = new byte[0];
    String otherWorkerId = "anotherhost:8083";
    KafkaBasedLog<String, byte[]> kafkaBasedLog = mock(KafkaBasedLog.class);
    Converter converter = mock(Converter.class);
    KafkaStatusBackingStore store = new KafkaStatusBackingStore(new MockTime(), converter, STATUS_TOPIC, kafkaBasedLog);
    // the persisted came from a different host and has a newer generation
    Map<String, Object> statusMap = new HashMap<>();
    statusMap.put("worker_id", otherWorkerId);
    statusMap.put("state", "RUNNING");
    statusMap.put("generation", 1L);
    expect(converter.toConnectData(STATUS_TOPIC, value)).andReturn(new SchemaAndValue(null, statusMap));
    // we're verifying that there is no call to KafkaBasedLog.send
    replayAll();
    store.read(consumerRecord(0, "status-connector-conn", value));
    store.putSafe(new ConnectorStatus(CONNECTOR, ConnectorStatus.State.UNASSIGNED, WORKER_ID, 0));
    ConnectorStatus status = new ConnectorStatus(CONNECTOR, ConnectorStatus.State.RUNNING, otherWorkerId, 1);
    assertEquals(status, store.get(CONNECTOR));
    verifyAll();
}
Also used : HashMap(java.util.HashMap) ConnectorStatus(org.apache.kafka.connect.runtime.ConnectorStatus) EasyMock.anyObject(org.easymock.EasyMock.anyObject) MockTime(org.apache.kafka.common.utils.MockTime) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Example 7 with ConnectorStatus

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

the class MemoryStatusBackingStoreTest method deleteConnectorStatus.

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

Example 8 with ConnectorStatus

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

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 9 with ConnectorStatus

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

the class KafkaStatusBackingStore method parseConnectorStatus.

private ConnectorStatus parseConnectorStatus(String connector, byte[] data) {
    try {
        SchemaAndValue schemaAndValue = converter.toConnectData(statusTopic, 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) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RetriableException(org.apache.kafka.common.errors.RetriableException) ConfigException(org.apache.kafka.common.config.ConfigException) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue)

Example 10 with ConnectorStatus

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

the class KafkaStatusBackingStoreTest method deleteConnectorState.

@Test
public void deleteConnectorState() {
    final 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.fromConnectData(eq(STATUS_TOPIC), any(Schema.class), any(Struct.class))).thenReturn(value);
    when(converter.fromConnectData(eq(STATUS_TOPIC), any(Schema.class), any(Struct.class))).thenReturn(value);
    when(converter.toConnectData(STATUS_TOPIC, value)).thenReturn(new SchemaAndValue(null, statusMap));
    ConnectorStatus connectorStatus = new ConnectorStatus(CONNECTOR, ConnectorStatus.State.RUNNING, WORKER_ID, 0);
    store.put(connectorStatus);
    TaskStatus taskStatus = new TaskStatus(TASK, TaskStatus.State.RUNNING, WORKER_ID, 0);
    store.put(taskStatus);
    store.read(consumerRecord(0, "status-task-conn-0", value));
    verify(kafkaBasedLog).send(eq("status-connector-" + CONNECTOR), eq(value), any(Callback.class));
    verify(kafkaBasedLog).send(eq("status-task-conn-0"), eq(value), any(Callback.class));
    assertEquals(new HashSet<>(Collections.singletonList(CONNECTOR)), store.connectors());
    assertEquals(new HashSet<>(Collections.singletonList(taskStatus)), new HashSet<>(store.getAll(CONNECTOR)));
    store.read(consumerRecord(0, "status-connector-conn", null));
    assertTrue(store.connectors().isEmpty());
    assertTrue(store.getAll(CONNECTOR).isEmpty());
}
Also used : Callback(org.apache.kafka.clients.producer.Callback) HashMap(java.util.HashMap) Schema(org.apache.kafka.connect.data.Schema) ConnectorStatus(org.apache.kafka.connect.runtime.ConnectorStatus) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TaskStatus(org.apache.kafka.connect.runtime.TaskStatus) Struct(org.apache.kafka.connect.data.Struct) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

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