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);
}
}
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.");
}
}
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;
}
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());
}
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());
}
Aggregations