|
| 1 | +package org.dependencytrack.event.kafka.processor.api; |
| 2 | + |
| 3 | +import org.apache.commons.lang3.exception.ExceptionUtils; |
| 4 | +import org.apache.http.conn.ConnectTimeoutException; |
| 5 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 6 | +import org.apache.kafka.common.errors.SerializationException; |
| 7 | +import org.apache.kafka.common.serialization.Serde; |
| 8 | +import org.datanucleus.api.jdo.exceptions.ConnectionInUseException; |
| 9 | +import org.datanucleus.store.query.QueryInterruptedException; |
| 10 | +import org.dependencytrack.event.kafka.processor.exception.RetryableProcessingException; |
| 11 | +import org.postgresql.util.PSQLState; |
| 12 | + |
| 13 | +import javax.jdo.JDOOptimisticVerificationException; |
| 14 | +import java.net.SocketTimeoutException; |
| 15 | +import java.sql.SQLException; |
| 16 | +import java.sql.SQLTransientConnectionException; |
| 17 | +import java.sql.SQLTransientException; |
| 18 | +import java.util.List; |
| 19 | +import java.util.concurrent.TimeoutException; |
| 20 | + |
| 21 | +/** |
| 22 | + * An abstract {@link ProcessingStrategy} that provides various shared functionality. |
| 23 | + * |
| 24 | + * @param <K> Type of the {@link ConsumerRecord} key |
| 25 | + * @param <V> Type of the {@link ConsumerRecord} value |
| 26 | + */ |
| 27 | +abstract class AbstractProcessingStrategy<K, V> implements ProcessingStrategy { |
| 28 | + |
| 29 | + private final Serde<K> keySerde; |
| 30 | + private final Serde<V> valueSerde; |
| 31 | + |
| 32 | + AbstractProcessingStrategy(final Serde<K> keySerde, final Serde<V> valueSerde) { |
| 33 | + this.keySerde = keySerde; |
| 34 | + this.valueSerde = valueSerde; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @param record The {@link ConsumerRecord} to deserialize key and value of |
| 39 | + * @return A {@link ConsumerRecord} with deserialized key and value |
| 40 | + * @throws SerializationException When deserializing the {@link ConsumerRecord} failed |
| 41 | + */ |
| 42 | + ConsumerRecord<K, V> deserialize(final ConsumerRecord<byte[], byte[]> record) { |
| 43 | + final K deserializedKey; |
| 44 | + final V deserializedValue; |
| 45 | + try { |
| 46 | + deserializedKey = keySerde.deserializer().deserialize(record.topic(), record.key()); |
| 47 | + deserializedValue = valueSerde.deserializer().deserialize(record.topic(), record.value()); |
| 48 | + } catch (RuntimeException e) { |
| 49 | + if (e instanceof SerializationException) { |
| 50 | + throw e; |
| 51 | + } |
| 52 | + |
| 53 | + throw new SerializationException(e); |
| 54 | + } |
| 55 | + |
| 56 | + return new ConsumerRecord<>(record.topic(), record.partition(), record.offset(), |
| 57 | + record.timestamp(), record.timestampType(), record.serializedKeySize(), record.serializedValueSize(), |
| 58 | + deserializedKey, deserializedValue, record.headers(), record.leaderEpoch()); |
| 59 | + } |
| 60 | + |
| 61 | + private static final List<Class<? extends Exception>> KNOWN_TRANSIENT_EXCEPTIONS = List.of( |
| 62 | + ConnectTimeoutException.class, |
| 63 | + ConnectionInUseException.class, |
| 64 | + JDOOptimisticVerificationException.class, |
| 65 | + QueryInterruptedException.class, |
| 66 | + SocketTimeoutException.class, |
| 67 | + SQLTransientException.class, |
| 68 | + SQLTransientConnectionException.class, |
| 69 | + TimeoutException.class |
| 70 | + ); |
| 71 | + |
| 72 | + boolean isRetryableException(final Throwable throwable) { |
| 73 | + if (throwable instanceof RetryableProcessingException) { |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + final boolean isKnownTransientException = ExceptionUtils.getThrowableList(throwable).stream() |
| 78 | + .anyMatch(cause -> KNOWN_TRANSIENT_EXCEPTIONS.contains(cause.getClass())); |
| 79 | + if (isKnownTransientException) { |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + return ExceptionUtils.getRootCause(throwable) instanceof final SQLException se |
| 84 | + && PSQLState.isConnectionError(se.getSQLState()); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments