Search in sources :

Example 76 with KafkaException

use of org.apache.kafka.common.KafkaException in project kafka by apache.

the class ListSerializer method serialize.

@Override
public byte[] serialize(String topic, List<Inner> data) {
    if (data == null) {
        return null;
    }
    try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final DataOutputStream out = new DataOutputStream(baos)) {
        // write serialization strategy flag
        out.writeByte(serStrategy.ordinal());
        if (serStrategy == SerializationStrategy.CONSTANT_SIZE) {
            // In CONSTANT_SIZE strategy, indexes of null entries are encoded in a null index list
            serializeNullIndexList(out, data);
        }
        final int size = data.size();
        out.writeInt(size);
        for (Inner entry : data) {
            if (entry == null) {
                if (serStrategy == SerializationStrategy.VARIABLE_SIZE) {
                    out.writeInt(Serdes.ListSerde.NULL_ENTRY_VALUE);
                }
            } else {
                final byte[] bytes = inner.serialize(topic, entry);
                if (serStrategy == SerializationStrategy.VARIABLE_SIZE) {
                    out.writeInt(bytes.length);
                }
                out.write(bytes);
            }
        }
        return baos.toByteArray();
    } catch (IOException e) {
        log.error("Failed to serialize list due to", e);
        // avoid logging actual data above TRACE level since it may contain sensitive information
        log.trace("List that could not be serialized: {}", data);
        throw new KafkaException("Failed to serialize List", e);
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) KafkaException(org.apache.kafka.common.KafkaException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 77 with KafkaException

use of org.apache.kafka.common.KafkaException in project kafka by apache.

the class ListDeserializer method configureListClass.

private void configureListClass(Map<String, ?> configs, boolean isKey) {
    String listTypePropertyName = isKey ? CommonClientConfigs.DEFAULT_LIST_KEY_SERDE_TYPE_CLASS : CommonClientConfigs.DEFAULT_LIST_VALUE_SERDE_TYPE_CLASS;
    final Object listClassOrName = configs.get(listTypePropertyName);
    if (listClassOrName == null) {
        throw new ConfigException("Not able to determine the list class because it was neither passed via the constructor nor set in the config.");
    }
    try {
        if (listClassOrName instanceof String) {
            listClass = Utils.loadClass((String) listClassOrName, Object.class);
        } else if (listClassOrName instanceof Class) {
            listClass = (Class<?>) listClassOrName;
        } else {
            throw new KafkaException("Could not determine the list class instance using \"" + listTypePropertyName + "\" property.");
        }
    } catch (final ClassNotFoundException e) {
        throw new ConfigException(listTypePropertyName, listClassOrName, "Deserializer's list class \"" + listClassOrName + "\" could not be found.");
    }
}
Also used : ConfigException(org.apache.kafka.common.config.ConfigException) KafkaException(org.apache.kafka.common.KafkaException)

Example 78 with KafkaException

use of org.apache.kafka.common.KafkaException in project kafka by apache.

the class SslFactory method configure.

@SuppressWarnings("unchecked")
@Override
public void configure(Map<String, ?> configs) throws KafkaException {
    if (sslEngineFactory != null) {
        throw new IllegalStateException("SslFactory was already configured.");
    }
    this.endpointIdentification = (String) configs.get(SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG);
    // The input map must be a mutable RecordingMap in production.
    Map<String, Object> nextConfigs = (Map<String, Object>) configs;
    if (clientAuthConfigOverride != null) {
        nextConfigs.put(BrokerSecurityConfigs.SSL_CLIENT_AUTH_CONFIG, clientAuthConfigOverride);
    }
    SslEngineFactory builder = instantiateSslEngineFactory(nextConfigs);
    if (keystoreVerifiableUsingTruststore) {
        try {
            SslEngineValidator.validate(builder, builder);
        } catch (Exception e) {
            throw new ConfigException("A client SSLEngine created with the provided settings " + "can't connect to a server SSLEngine created with those settings.", e);
        }
    }
    this.sslEngineFactory = builder;
}
Also used : SslEngineFactory(org.apache.kafka.common.security.auth.SslEngineFactory) ConfigException(org.apache.kafka.common.config.ConfigException) HashMap(java.util.HashMap) Map(java.util.Map) KafkaException(org.apache.kafka.common.KafkaException) GeneralSecurityException(java.security.GeneralSecurityException) ConfigException(org.apache.kafka.common.config.ConfigException) SSLException(javax.net.ssl.SSLException)

Example 79 with KafkaException

use of org.apache.kafka.common.KafkaException in project kafka by apache.

the class TransactionManagerTest method testHasOngoingTransactionAbortableError.

@Test
public void testHasOngoingTransactionAbortableError() {
    TopicPartition partition = new TopicPartition("foo", 0);
    assertFalse(transactionManager.hasOngoingTransaction());
    doInitTransactions();
    assertFalse(transactionManager.hasOngoingTransaction());
    transactionManager.beginTransaction();
    assertTrue(transactionManager.hasOngoingTransaction());
    transactionManager.maybeAddPartition(partition);
    assertTrue(transactionManager.hasOngoingTransaction());
    prepareAddPartitionsToTxn(partition, Errors.NONE);
    runUntil(() -> transactionManager.isPartitionAdded(partition));
    transactionManager.transitionToAbortableError(new KafkaException());
    assertTrue(transactionManager.hasOngoingTransaction());
    transactionManager.beginAbort();
    assertTrue(transactionManager.hasOngoingTransaction());
    prepareEndTxnResponse(Errors.NONE, TransactionResult.ABORT, producerId, epoch);
    runUntil(() -> !transactionManager.hasOngoingTransaction());
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) KafkaException(org.apache.kafka.common.KafkaException) Test(org.junit.jupiter.api.Test)

Example 80 with KafkaException

use of org.apache.kafka.common.KafkaException in project kafka by apache.

the class TransactionManagerTest method testNoFailedBatchHandlingWhenTxnManagerIsInFatalError.

@Test
public void testNoFailedBatchHandlingWhenTxnManagerIsInFatalError() {
    initializeTransactionManager(Optional.empty());
    long producerId = 15L;
    short epoch = 5;
    initializeIdempotentProducerId(producerId, epoch);
    TopicPartition tp0 = new TopicPartition("foo", 0);
    ProducerBatch b1 = writeIdempotentBatchWithValue(transactionManager, tp0, "1");
    // Handling b1 should bump the epoch after OutOfOrderSequenceException
    transactionManager.handleFailedBatch(b1, new OutOfOrderSequenceException("out of sequence"), false);
    transactionManager.bumpIdempotentEpochAndResetIdIfNeeded();
    ProducerIdAndEpoch idAndEpochAfterFirstBatch = new ProducerIdAndEpoch(producerId, (short) (epoch + 1));
    assertEquals(idAndEpochAfterFirstBatch, transactionManager.producerIdAndEpoch());
    transactionManager.transitionToFatalError(new KafkaException());
    // The second batch should not bump the epoch as txn manager is already in fatal error state
    ProducerBatch b2 = writeIdempotentBatchWithValue(transactionManager, tp0, "2");
    transactionManager.handleFailedBatch(b2, new TimeoutException(), true);
    transactionManager.bumpIdempotentEpochAndResetIdIfNeeded();
    assertEquals(idAndEpochAfterFirstBatch, transactionManager.producerIdAndEpoch());
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) KafkaException(org.apache.kafka.common.KafkaException) ProducerIdAndEpoch(org.apache.kafka.common.utils.ProducerIdAndEpoch) OutOfOrderSequenceException(org.apache.kafka.common.errors.OutOfOrderSequenceException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Test(org.junit.jupiter.api.Test)

Aggregations

KafkaException (org.apache.kafka.common.KafkaException)262 Test (org.junit.Test)69 TopicPartition (org.apache.kafka.common.TopicPartition)56 Test (org.junit.jupiter.api.Test)47 HashMap (java.util.HashMap)40 IOException (java.io.IOException)39 StreamsException (org.apache.kafka.streams.errors.StreamsException)34 Map (java.util.Map)32 TimeoutException (org.apache.kafka.common.errors.TimeoutException)28 ArrayList (java.util.ArrayList)27 List (java.util.List)21 ByteBuffer (java.nio.ByteBuffer)19 ExecutionException (java.util.concurrent.ExecutionException)19 ConfigException (org.apache.kafka.common.config.ConfigException)16 TopicAuthorizationException (org.apache.kafka.common.errors.TopicAuthorizationException)14 HashSet (java.util.HashSet)13 Properties (java.util.Properties)13 Set (java.util.Set)11 Collectors (java.util.stream.Collectors)11 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)11