Search in sources :

Example 1 with TaskStatus

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

the class KafkaStatusBackingStore method readTaskStatus.

private void readTaskStatus(String key, byte[] value) {
    ConnectorTaskId id = parseConnectorTaskId(key);
    if (id == null) {
        log.warn("Discarding record with invalid task status key {}", key);
        return;
    }
    if (value == null) {
        log.trace("Removing task status for {}", id);
        remove(id);
        return;
    }
    TaskStatus status = parseTaskStatus(id, value);
    if (status == null) {
        log.warn("Failed to parse task status with key {}", key);
        return;
    }
    synchronized (this) {
        log.trace("Received task {} status update {}", id, status);
        CacheEntry<TaskStatus> entry = getOrAdd(id);
        entry.put(status);
    }
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) TaskStatus(org.apache.kafka.connect.runtime.TaskStatus)

Example 2 with TaskStatus

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

the class KafkaStatusBackingStore method parseTaskStatus.

private TaskStatus parseTaskStatus(ConnectorTaskId taskId, 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 TaskStatus(taskId, state, workerUrl, generation, trace);
    } catch (Exception e) {
        log.error("Failed to deserialize task status", e);
        return null;
    }
}
Also used : 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)

Example 3 with TaskStatus

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

the class KafkaStatusBackingStoreTest method readTaskState.

@Test
public void readTaskState() {
    byte[] value = new byte[0];
    KafkaBasedLog<String, byte[]> kafkaBasedLog = mock(KafkaBasedLog.class);
    Converter converter = mock(Converter.class);
    KafkaStatusBackingStore store = new KafkaStatusBackingStore(new MockTime(), converter, STATUS_TOPIC, kafkaBasedLog);
    Map<String, Object> statusMap = new HashMap<>();
    statusMap.put("worker_id", WORKER_ID);
    statusMap.put("state", "RUNNING");
    statusMap.put("generation", 0L);
    expect(converter.toConnectData(STATUS_TOPIC, value)).andReturn(new SchemaAndValue(null, statusMap));
    replayAll();
    store.read(consumerRecord(0, "status-task-conn-0", value));
    TaskStatus status = new TaskStatus(TASK, TaskStatus.State.RUNNING, WORKER_ID, 0);
    assertEquals(status, store.get(TASK));
    verifyAll();
}
Also used : HashMap(java.util.HashMap) EasyMock.anyObject(org.easymock.EasyMock.anyObject) TaskStatus(org.apache.kafka.connect.runtime.TaskStatus) MockTime(org.apache.kafka.common.utils.MockTime) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Example 4 with TaskStatus

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

the class KafkaStatusBackingStoreTest method putTaskState.

@Test
public void putTaskState() {
    KafkaBasedLog<String, byte[]> kafkaBasedLog = mock(KafkaBasedLog.class);
    Converter converter = mock(Converter.class);
    KafkaStatusBackingStore store = new KafkaStatusBackingStore(new MockTime(), converter, STATUS_TOPIC, kafkaBasedLog);
    byte[] value = new byte[0];
    expect(converter.fromConnectData(eq(STATUS_TOPIC), anyObject(Schema.class), anyObject(Struct.class))).andStubReturn(value);
    final Capture<Callback> callbackCapture = newCapture();
    kafkaBasedLog.send(eq("status-task-conn-0"), eq(value), capture(callbackCapture));
    expectLastCall().andAnswer(new IAnswer<Void>() {

        @Override
        public Void answer() throws Throwable {
            callbackCapture.getValue().onCompletion(null, null);
            return null;
        }
    });
    replayAll();
    TaskStatus status = new TaskStatus(TASK, TaskStatus.State.RUNNING, WORKER_ID, 0);
    store.put(status);
    // state is not visible until read back from the log
    assertEquals(null, store.get(TASK));
    verifyAll();
}
Also used : Schema(org.apache.kafka.connect.data.Schema) TaskStatus(org.apache.kafka.connect.runtime.TaskStatus) Struct(org.apache.kafka.connect.data.Struct) Callback(org.apache.kafka.clients.producer.Callback) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Example 5 with TaskStatus

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

the class KafkaStatusBackingStore method sendTaskStatus.

private void sendTaskStatus(final TaskStatus status, boolean safeWrite) {
    ConnectorTaskId taskId = status.id();
    CacheEntry<TaskStatus> entry = getOrAdd(taskId);
    String key = TASK_STATUS_PREFIX + taskId.connector() + "-" + taskId.task();
    send(key, status, entry, safeWrite);
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) TaskStatus(org.apache.kafka.connect.runtime.TaskStatus)

Aggregations

TaskStatus (org.apache.kafka.connect.runtime.TaskStatus)7 ConnectorTaskId (org.apache.kafka.connect.util.ConnectorTaskId)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)2 MockTime (org.apache.kafka.common.utils.MockTime)2 SchemaAndValue (org.apache.kafka.connect.data.SchemaAndValue)2 Map (java.util.Map)1 Callback (org.apache.kafka.clients.producer.Callback)1 ConfigException (org.apache.kafka.common.config.ConfigException)1 RetriableException (org.apache.kafka.common.errors.RetriableException)1 Schema (org.apache.kafka.connect.data.Schema)1 Struct (org.apache.kafka.connect.data.Struct)1 EasyMock.anyObject (org.easymock.EasyMock.anyObject)1