diff --git a/CHANGES.md b/CHANGES.md index be4cf30bc3af..31842ee7241a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -43,6 +43,7 @@ ## Bugfixes +* Fixed JmsIO acknowledgment issue (https://github.com/apache/beam/issues/20814) * Fixed X (Java/Python) ([#X](https://github.com/apache/beam/issues/X)). ## Known Issues diff --git a/sdks/java/io/jms/src/main/java/org/apache/beam/sdk/io/jms/JmsCheckpointMark.java b/sdks/java/io/jms/src/main/java/org/apache/beam/sdk/io/jms/JmsCheckpointMark.java index 0d4a60d29385..244c3cbabb20 100644 --- a/sdks/java/io/jms/src/main/java/org/apache/beam/sdk/io/jms/JmsCheckpointMark.java +++ b/sdks/java/io/jms/src/main/java/org/apache/beam/sdk/io/jms/JmsCheckpointMark.java @@ -25,6 +25,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock; import javax.jms.Message; import org.apache.beam.sdk.io.UnboundedSource; +import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting; import org.checkerframework.checker.nullness.qual.Nullable; import org.joda.time.Instant; import org.slf4j.Logger; @@ -41,13 +42,20 @@ class JmsCheckpointMark implements UnboundedSource.CheckpointMark, Serializable private Instant oldestMessageTimestamp = Instant.now(); private transient List messages = new ArrayList<>(); - private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + @VisibleForTesting transient boolean discarded = false; + + @VisibleForTesting final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); JmsCheckpointMark() {} void add(Message message) throws Exception { lock.writeLock().lock(); try { + if (discarded) { + throw new IllegalStateException( + String.format( + "Attempting to add message %s to checkpoint that is discarded.", message)); + } Instant currentMessageTimestamp = new Instant(message.getJMSTimestamp()); if (currentMessageTimestamp.isBefore(oldestMessageTimestamp)) { oldestMessageTimestamp = currentMessageTimestamp; @@ -67,6 +75,15 @@ Instant getOldestMessageTimestamp() { } } + void discard() { + lock.writeLock().lock(); + try { + this.discarded = true; + } finally { + lock.writeLock().unlock(); + } + } + /** * Acknowledge all outstanding message. Since we believe that messages will be delivered in * timestamp order, and acknowledged messages will not be retried, the newest message in this @@ -76,6 +93,10 @@ Instant getOldestMessageTimestamp() { public void finalizeCheckpoint() { lock.writeLock().lock(); try { + if (discarded) { + messages.clear(); + return; + } for (Message message : messages) { try { message.acknowledge(); @@ -98,6 +119,7 @@ private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); messages = new ArrayList<>(); + discarded = false; } @Override diff --git a/sdks/java/io/jms/src/main/java/org/apache/beam/sdk/io/jms/JmsIO.java b/sdks/java/io/jms/src/main/java/org/apache/beam/sdk/io/jms/JmsIO.java index b8a23867097b..c77c70820b27 100644 --- a/sdks/java/io/jms/src/main/java/org/apache/beam/sdk/io/jms/JmsIO.java +++ b/sdks/java/io/jms/src/main/java/org/apache/beam/sdk/io/jms/JmsIO.java @@ -28,6 +28,8 @@ import java.util.List; import java.util.Map; import java.util.NoSuchElementException; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; import java.util.stream.Stream; import javax.jms.Connection; import javax.jms.ConnectionFactory; @@ -45,6 +47,7 @@ import org.apache.beam.sdk.io.UnboundedSource; import org.apache.beam.sdk.io.UnboundedSource.CheckpointMark; import org.apache.beam.sdk.io.UnboundedSource.UnboundedReader; +import org.apache.beam.sdk.options.ExecutorOptions; import org.apache.beam.sdk.options.PipelineOptions; import org.apache.beam.sdk.transforms.DoFn; import org.apache.beam.sdk.transforms.PTransform; @@ -123,11 +126,13 @@ public class JmsIO { private static final Logger LOG = LoggerFactory.getLogger(JmsIO.class); + private static final Duration DEFAULT_CLOSE_TIMEOUT = Duration.millis(60000L); public static Read read() { return new AutoValue_JmsIO_Read.Builder() .setMaxNumRecords(Long.MAX_VALUE) .setCoder(SerializableCoder.of(JmsRecord.class)) + .setCloseTimeout(DEFAULT_CLOSE_TIMEOUT) .setMessageMapper( (MessageMapper) new MessageMapper() { @@ -162,7 +167,10 @@ public JmsRecord mapMessage(Message message) throws Exception { } public static Read readMessage() { - return new AutoValue_JmsIO_Read.Builder().setMaxNumRecords(Long.MAX_VALUE).build(); + return new AutoValue_JmsIO_Read.Builder() + .setMaxNumRecords(Long.MAX_VALUE) + .setCloseTimeout(DEFAULT_CLOSE_TIMEOUT) + .build(); } public static Write write() { @@ -206,6 +214,8 @@ public abstract static class Read extends PTransform> abstract @Nullable AutoScaler getAutoScaler(); + abstract Duration getCloseTimeout(); + abstract Builder builder(); @AutoValue.Builder @@ -230,6 +240,8 @@ abstract static class Builder { abstract Builder setAutoScaler(AutoScaler autoScaler); + abstract Builder setCloseTimeout(Duration closeTimeout); + abstract Read build(); } @@ -364,6 +376,18 @@ public Read withAutoScaler(AutoScaler autoScaler) { return builder().setAutoScaler(autoScaler).build(); } + /** + * Sets the amount of time to wait for callbacks from the runner stating that the output has + * been durably persisted before closing the connection to the JMS broker. Any callbacks that do + * not occur will cause unacknowledged messages to be returned to the JMS broker and redelivered + * to other clients. + */ + public Read withCloseTimeout(Duration closeTimeout) { + checkArgument(closeTimeout != null, "closeTimeout can not be null"); + checkArgument(closeTimeout.getMillis() >= 0, "Close timeout must be non-negative."); + return builder().setCloseTimeout(closeTimeout).build(); + } + @Override public PCollection expand(PBegin input) { checkArgument(getConnectionFactory() != null, "withConnectionFactory() is required"); @@ -446,7 +470,7 @@ public List> split(int desiredNumSplits, PipelineOptions o @Override public UnboundedJmsReader createReader( PipelineOptions options, JmsCheckpointMark checkpointMark) { - return new UnboundedJmsReader(this, checkpointMark); + return new UnboundedJmsReader(this, options); } @Override @@ -471,15 +495,13 @@ static class UnboundedJmsReader extends UnboundedReader { private T currentMessage; private Instant currentTimestamp; + private PipelineOptions options; - public UnboundedJmsReader(UnboundedJmsSource source, JmsCheckpointMark checkpointMark) { + public UnboundedJmsReader(UnboundedJmsSource source, PipelineOptions options) { this.source = source; - if (checkpointMark != null) { - this.checkpointMark = checkpointMark; - } else { - this.checkpointMark = new JmsCheckpointMark(); - } + this.checkpointMark = new JmsCheckpointMark(); this.currentMessage = null; + this.options = options; } @Override @@ -582,29 +604,84 @@ public long getTotalBacklogBytes() { } @Override - public void close() throws IOException { + public void close() { + doClose(); + } + + @SuppressWarnings("FutureReturnValueIgnored") + private void doClose() { + try { - if (consumer != null) { - consumer.close(); - consumer = null; + closeAutoscaler(); + closeConsumer(); + ScheduledExecutorService executorService = + options.as(ExecutorOptions.class).getScheduledExecutorService(); + executorService.schedule( + () -> { + LOG.debug( + "Closing session and connection after delay {}", source.spec.getCloseTimeout()); + // Discard the checkpoints and set the reader as inactive + checkpointMark.discard(); + closeSession(); + closeConnection(); + }, + source.spec.getCloseTimeout().getMillis(), + TimeUnit.MILLISECONDS); + + } catch (Exception e) { + LOG.error("Error closing reader", e); + } + } + + private void closeConnection() { + try { + if (connection != null) { + connection.stop(); + connection.close(); + connection = null; } + } catch (Exception e) { + LOG.error("Error closing connection", e); + } + } + + private void closeSession() { + try { if (session != null) { session.close(); session = null; } - if (connection != null) { - connection.stop(); - connection.close(); - connection = null; + } catch (Exception e) { + LOG.error("Error closing session" + e.getMessage(), e); + } + } + + private void closeConsumer() { + try { + if (consumer != null) { + consumer.close(); + consumer = null; } + } catch (Exception e) { + LOG.error("Error closing consumer", e); + } + } + + private void closeAutoscaler() { + try { if (autoScaler != null) { autoScaler.stop(); autoScaler = null; } } catch (Exception e) { - throw new IOException(e); + LOG.error("Error closing autoscaler", e); } } + + @Override + protected void finalize() { + doClose(); + } } /** diff --git a/sdks/java/io/jms/src/test/java/org/apache/beam/sdk/io/jms/JmsIOTest.java b/sdks/java/io/jms/src/test/java/org/apache/beam/sdk/io/jms/JmsIOTest.java index 09825d03cd2a..1979d7b4ff60 100644 --- a/sdks/java/io/jms/src/test/java/org/apache/beam/sdk/io/jms/JmsIOTest.java +++ b/sdks/java/io/jms/src/test/java/org/apache/beam/sdk/io/jms/JmsIOTest.java @@ -21,12 +21,17 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; import java.io.IOException; @@ -36,6 +41,8 @@ import java.util.ArrayList; import java.util.Enumeration; import java.util.List; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; import java.util.function.Function; import javax.jms.BytesMessage; import javax.jms.Connection; @@ -57,6 +64,7 @@ import org.apache.activemq.util.Callback; import org.apache.beam.sdk.coders.Coder; import org.apache.beam.sdk.coders.SerializableCoder; +import org.apache.beam.sdk.options.ExecutorOptions; import org.apache.beam.sdk.options.PipelineOptions; import org.apache.beam.sdk.options.PipelineOptionsFactory; import org.apache.beam.sdk.testing.CoderProperties; @@ -67,12 +75,15 @@ import org.apache.beam.sdk.transforms.SerializableBiFunction; import org.apache.beam.sdk.values.PCollection; import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Throwables; +import org.joda.time.Duration; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; /** Tests of {@link JmsIO}. */ @RunWith(JUnit4.class) @@ -505,7 +516,6 @@ public void testCheckpointMarkSafety() throws Exception { @Test public void testCheckpointMarkDefaultCoder() throws Exception { JmsCheckpointMark jmsCheckpointMark = new JmsCheckpointMark(); - jmsCheckpointMark.add(new ActiveMQMessage()); Coder coder = new JmsIO.UnboundedJmsSource(null).getCheckpointMarkCoder(); CoderProperties.coderSerializable(coder); CoderProperties.coderDecodeEncodeEqual(coder, jmsCheckpointMark); @@ -555,6 +565,106 @@ public void testCustomAutoscaler() throws IOException { verify(autoScaler, times(1)).stop(); } + @Test + public void testCloseWithTimeout() throws IOException { + Duration closeTimeout = Duration.millis(2000L); + JmsIO.Read spec = + JmsIO.read() + .withConnectionFactory(connectionFactory) + .withUsername(USERNAME) + .withPassword(PASSWORD) + .withQueue(QUEUE) + .withCloseTimeout(closeTimeout); + + JmsIO.UnboundedJmsSource source = new JmsIO.UnboundedJmsSource(spec); + + ScheduledExecutorService mockScheduledExecutorService = + Mockito.mock(ScheduledExecutorService.class); + ExecutorOptions options = PipelineOptionsFactory.as(ExecutorOptions.class); + options.setScheduledExecutorService(mockScheduledExecutorService); + ArgumentCaptor runnableArgumentCaptor = ArgumentCaptor.forClass(Runnable.class); + when(mockScheduledExecutorService.schedule( + runnableArgumentCaptor.capture(), anyLong(), any(TimeUnit.class))) + .thenReturn(null /* unused */); + + JmsIO.UnboundedJmsReader reader = source.createReader(options, null); + reader.start(); + assertFalse(getDiscardedValue(reader)); + reader.close(); + assertFalse(getDiscardedValue(reader)); + verify(mockScheduledExecutorService) + .schedule(any(Runnable.class), eq(closeTimeout.getMillis()), eq(TimeUnit.MILLISECONDS)); + runnableArgumentCaptor.getValue().run(); + assertTrue(getDiscardedValue(reader)); + verifyNoMoreInteractions(mockScheduledExecutorService); + } + + private boolean getDiscardedValue(JmsIO.UnboundedJmsReader reader) { + JmsCheckpointMark checkpoint = (JmsCheckpointMark) reader.getCheckpointMark(); + checkpoint.lock.readLock().lock(); + try { + return checkpoint.discarded; + } finally { + checkpoint.lock.readLock().unlock(); + } + } + + @Test + public void testDiscardCheckpointMark() throws Exception { + Connection connection = + connectionFactoryWithSyncAcksAndWithoutPrefetch.createConnection(USERNAME, PASSWORD); + connection.start(); + Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer producer = session.createProducer(session.createQueue(QUEUE)); + for (int i = 0; i < 10; i++) { + producer.send(session.createTextMessage("test " + i)); + } + producer.close(); + session.close(); + connection.close(); + + JmsIO.Read spec = + JmsIO.read() + .withConnectionFactory(connectionFactoryWithSyncAcksAndWithoutPrefetch) + .withUsername(USERNAME) + .withPassword(PASSWORD) + .withQueue(QUEUE); + JmsIO.UnboundedJmsSource source = new JmsIO.UnboundedJmsSource(spec); + JmsIO.UnboundedJmsReader reader = source.createReader(null, null); + + // start the reader and move to the first record + assertTrue(reader.start()); + + // consume 3 more messages (NB: start already consumed the first message) + for (int i = 0; i < 3; i++) { + assertTrue(reader.advance()); + } + + // the messages are still pending in the queue (no ACK yet) + assertEquals(10, count(QUEUE)); + + // we finalize the checkpoint + reader.getCheckpointMark().finalizeCheckpoint(); + + // the checkpoint finalize ack the messages, and so they are not pending in the queue anymore + assertEquals(6, count(QUEUE)); + + // we read the 6 pending messages + for (int i = 0; i < 6; i++) { + assertTrue(reader.advance()); + } + + // still 6 pending messages as we didn't finalize the checkpoint + assertEquals(6, count(QUEUE)); + + // But here we discard the checkpoint + ((JmsCheckpointMark) reader.getCheckpointMark()).discard(); + // we finalize the checkpoint: no messages should be acked + reader.getCheckpointMark().finalizeCheckpoint(); + + assertEquals(6, count(QUEUE)); + } + private int count(String queue) throws Exception { Connection connection = connectionFactory.createConnection(USERNAME, PASSWORD); connection.start();