org.apache.kafka.connect.runtime.WorkerSinkTaskContext Maven / Gradle / Ivy
/**
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
* applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
**/
package org.apache.kafka.connect.runtime;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.connect.errors.IllegalWorkerStateException;
import org.apache.kafka.connect.sink.SinkTaskContext;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class WorkerSinkTaskContext implements SinkTaskContext {
private Map offsets;
private long timeoutMs;
private KafkaConsumer consumer;
private final Set pausedPartitions;
public WorkerSinkTaskContext(KafkaConsumer consumer) {
this.offsets = new HashMap<>();
this.timeoutMs = -1L;
this.consumer = consumer;
this.pausedPartitions = new HashSet<>();
}
@Override
public void offset(Map offsets) {
this.offsets.putAll(offsets);
}
@Override
public void offset(TopicPartition tp, long offset) {
offsets.put(tp, offset);
}
public void clearOffsets() {
offsets.clear();
}
/**
* Get offsets that the SinkTask has submitted to be reset. Used by the Kafka Connect framework.
* @return the map of offsets
*/
public Map offsets() {
return offsets;
}
@Override
public void timeout(long timeoutMs) {
this.timeoutMs = timeoutMs;
}
/**
* Get the timeout in milliseconds set by SinkTasks. Used by the Kafka Connect framework.
* @return the backoff timeout in milliseconds.
*/
public long timeout() {
return timeoutMs;
}
@Override
public Set assignment() {
if (consumer == null) {
throw new IllegalWorkerStateException("SinkTaskContext may not be used to look up partition assignment until the task is initialized");
}
return consumer.assignment();
}
@Override
public void pause(TopicPartition... partitions) {
if (consumer == null) {
throw new IllegalWorkerStateException("SinkTaskContext may not be used to pause consumption until the task is initialized");
}
try {
for (TopicPartition partition : partitions)
pausedPartitions.add(partition);
consumer.pause(partitions);
} catch (IllegalStateException e) {
throw new IllegalWorkerStateException("SinkTasks may not pause partitions that are not currently assigned to them.", e);
}
}
@Override
public void resume(TopicPartition... partitions) {
if (consumer == null) {
throw new IllegalWorkerStateException("SinkTaskContext may not be used to resume consumption until the task is initialized");
}
try {
for (TopicPartition partition : partitions)
pausedPartitions.remove(partition);
consumer.resume(partitions);
} catch (IllegalStateException e) {
throw new IllegalWorkerStateException("SinkTasks may not resume partitions that are not currently assigned to them.", e);
}
}
public Set pausedPartitions() {
return pausedPartitions;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy