Search in sources :

Example 1 with NotEnoughReplicasException

use of org.apache.kafka.common.errors.NotEnoughReplicasException in project kafka by apache.

the class RequestResponseTest method produceRequestGetErrorResponseTest.

@Test
public void produceRequestGetErrorResponseTest() {
    ProduceRequest request = createProduceRequest();
    Set<TopicPartition> partitions = new HashSet<>(request.partitionRecordsOrFail().keySet());
    ProduceResponse errorResponse = (ProduceResponse) request.getErrorResponse(new NotEnoughReplicasException());
    assertEquals(partitions, errorResponse.responses().keySet());
    ProduceResponse.PartitionResponse partitionResponse = errorResponse.responses().values().iterator().next();
    assertEquals(Errors.NOT_ENOUGH_REPLICAS, partitionResponse.error);
    assertEquals(ProduceResponse.INVALID_OFFSET, partitionResponse.baseOffset);
    assertEquals(Record.NO_TIMESTAMP, partitionResponse.logAppendTime);
    request.clearPartitionRecords();
    // `getErrorResponse` should behave the same after `clearPartitionRecords`
    errorResponse = (ProduceResponse) request.getErrorResponse(new NotEnoughReplicasException());
    assertEquals(partitions, errorResponse.responses().keySet());
    partitionResponse = errorResponse.responses().values().iterator().next();
    assertEquals(Errors.NOT_ENOUGH_REPLICAS, partitionResponse.error);
    assertEquals(ProduceResponse.INVALID_OFFSET, partitionResponse.baseOffset);
    assertEquals(Record.NO_TIMESTAMP, partitionResponse.logAppendTime);
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) NotEnoughReplicasException(org.apache.kafka.common.errors.NotEnoughReplicasException) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with NotEnoughReplicasException

use of org.apache.kafka.common.errors.NotEnoughReplicasException in project apache-kafka-on-k8s by banzaicloud.

the class RequestResponseTest method produceRequestGetErrorResponseTest.

@Test
public void produceRequestGetErrorResponseTest() {
    ProduceRequest request = createProduceRequest(ApiKeys.PRODUCE.latestVersion());
    Set<TopicPartition> partitions = new HashSet<>(request.partitionRecordsOrFail().keySet());
    ProduceResponse errorResponse = (ProduceResponse) request.getErrorResponse(new NotEnoughReplicasException());
    assertEquals(partitions, errorResponse.responses().keySet());
    ProduceResponse.PartitionResponse partitionResponse = errorResponse.responses().values().iterator().next();
    assertEquals(Errors.NOT_ENOUGH_REPLICAS, partitionResponse.error);
    assertEquals(ProduceResponse.INVALID_OFFSET, partitionResponse.baseOffset);
    assertEquals(RecordBatch.NO_TIMESTAMP, partitionResponse.logAppendTime);
    request.clearPartitionRecords();
    // `getErrorResponse` should behave the same after `clearPartitionRecords`
    errorResponse = (ProduceResponse) request.getErrorResponse(new NotEnoughReplicasException());
    assertEquals(partitions, errorResponse.responses().keySet());
    partitionResponse = errorResponse.responses().values().iterator().next();
    assertEquals(Errors.NOT_ENOUGH_REPLICAS, partitionResponse.error);
    assertEquals(ProduceResponse.INVALID_OFFSET, partitionResponse.baseOffset);
    assertEquals(RecordBatch.NO_TIMESTAMP, partitionResponse.logAppendTime);
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) NotEnoughReplicasException(org.apache.kafka.common.errors.NotEnoughReplicasException) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with NotEnoughReplicasException

use of org.apache.kafka.common.errors.NotEnoughReplicasException in project apache-kafka-on-k8s by banzaicloud.

the class WorkerUtils method createTopics.

/**
 * Creates Kafka topics and returns a list of topics that already exist
 * @param log             The logger to use
 * @param adminClient     AdminClient
 * @param topics          List of topics to create
 * @return                Collection of topics names that already exist.
 * @throws Throwable if creation of one or more topics fails (except for topic exists case).
 */
private static Collection<String> createTopics(Logger log, AdminClient adminClient, Collection<NewTopic> topics) throws Throwable {
    long startMs = Time.SYSTEM.milliseconds();
    int tries = 0;
    List<String> existingTopics = new ArrayList<>();
    Map<String, NewTopic> newTopics = new HashMap<>();
    for (NewTopic newTopic : topics) {
        newTopics.put(newTopic.name(), newTopic);
    }
    List<String> topicsToCreate = new ArrayList<>(newTopics.keySet());
    while (true) {
        log.info("Attempting to create {} topics (try {})...", topicsToCreate.size(), ++tries);
        Map<String, Future<Void>> creations = new HashMap<>();
        while (!topicsToCreate.isEmpty()) {
            List<NewTopic> newTopicsBatch = new ArrayList<>();
            for (int i = 0; (i < MAX_CREATE_TOPICS_BATCH_SIZE) && !topicsToCreate.isEmpty(); i++) {
                String topicName = topicsToCreate.remove(0);
                newTopicsBatch.add(newTopics.get(topicName));
            }
            creations.putAll(adminClient.createTopics(newTopicsBatch).values());
        }
        // timeout.  This is a workaround for KAFKA-6368.
        for (Map.Entry<String, Future<Void>> entry : creations.entrySet()) {
            String topicName = entry.getKey();
            Future<Void> future = entry.getValue();
            try {
                future.get();
                log.debug("Successfully created {}.", topicName);
            } catch (Exception e) {
                if ((e.getCause() instanceof TimeoutException) || (e.getCause() instanceof NotEnoughReplicasException)) {
                    log.warn("Attempt to create topic `{}` failed: {}", topicName, e.getCause().getMessage());
                    topicsToCreate.add(topicName);
                } else if (e.getCause() instanceof TopicExistsException) {
                    log.info("Topic {} already exists.", topicName);
                    existingTopics.add(topicName);
                } else {
                    log.warn("Failed to create {}", topicName, e.getCause());
                    throw e.getCause();
                }
            }
        }
        if (topicsToCreate.isEmpty()) {
            break;
        }
        if (Time.SYSTEM.milliseconds() > startMs + CREATE_TOPICS_CALL_TIMEOUT) {
            String str = "Unable to create topic(s): " + Utils.join(topicsToCreate, ", ") + "after " + tries + " attempt(s)";
            log.warn(str);
            throw new TimeoutException(str);
        }
    }
    return existingTopics;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) KafkaException(org.apache.kafka.common.KafkaException) NotEnoughReplicasException(org.apache.kafka.common.errors.NotEnoughReplicasException) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) NotEnoughReplicasException(org.apache.kafka.common.errors.NotEnoughReplicasException) Future(java.util.concurrent.Future) NewTopic(org.apache.kafka.clients.admin.NewTopic) HashMap(java.util.HashMap) Map(java.util.Map) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Aggregations

NotEnoughReplicasException (org.apache.kafka.common.errors.NotEnoughReplicasException)3 HashSet (java.util.HashSet)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Future (java.util.concurrent.Future)1 NewTopic (org.apache.kafka.clients.admin.NewTopic)1 KafkaException (org.apache.kafka.common.KafkaException)1 TimeoutException (org.apache.kafka.common.errors.TimeoutException)1 TopicExistsException (org.apache.kafka.common.errors.TopicExistsException)1