allthingssecurity commented on code in PR #28264: URL: https://github.com/apache/camel/pull/28264#discussion_r4345651002
########## components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/listener/CamelReliableMessageListener.java: ########## @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.hazelcast.listener; + +import com.hazelcast.topic.ReliableMessageListener; +import org.apache.camel.component.hazelcast.HazelcastDefaultConsumer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The listener of a reliable topic consumer. A reliable topic keeps its messages in a ringbuffer: a listener that falls + * behind by more than its capacity (or its time to live) loses the messages that were overwritten (or expired). + * Hazelcast removes a listener that is not loss tolerant at that point, for good, which would leave the consumer + * started without receiving anything again. This listener reports the loss and goes on with the oldest message still in + * the ringbuffer. + */ +public class CamelReliableMessageListener extends CamelMessageListener implements ReliableMessageListener<Object> { + + private static final Logger LOG = LoggerFactory.getLogger(CamelReliableMessageListener.class); + + private final String cacheName; + // the sequence of the last message received, -1 before the first one + private long lastSequence = -1; + + public CamelReliableMessageListener(HazelcastDefaultConsumer consumer, String cacheName) { + super(consumer, cacheName); + this.cacheName = cacheName; + } + + @Override + public long retrieveInitialSequence() { + // the messages published from now on, as with a plain message listener + return -1; + } + + @Override + public void storeSequence(long sequence) { + // called by one thread at a time, for each message in order + if (lastSequence >= 0 && sequence > lastSequence + 1) { + LOG.warn("The consumer of the reliable topic {} lost {} messages: they were overwritten or expired in the" + + " ringbuffer before they were received", + cacheName, sequence - lastSequence - 1); + } + lastSequence = sequence; + } + + @Override + public boolean isLossTolerant() { + return true; + } + + @Override + public boolean isTerminal(Throwable failure) { + // the route's failures are handled by the consumer's exception handler and do not reach Hazelcast + return false; + } + + @Override + public void onCancel() { + // Hazelcast cancels the listener when the consumer removes it as it stops, and when it gives up on it: an error + // reading the ringbuffer, such as a destroyed topic or a Hazelcast instance that is no longer active. Then the + // consumer receives nothing more, so report it unless the consumer is stopping + if (getConsumer().isStarted()) { Review Comment: Done in 9224021e8268. I checked it in the 5.4.0 jar: `MessageRunner.handleInternalException` returns false for `HazelcastInstanceNotActiveException` (and `ClientReliableMessageRunner` for `HazelcastClientNotActiveException`), so the runner cancels. A member sets `shuttingDown` in `Node.shutdown` before it shuts its services down, and a client clears `active` at the start of `doShutdown`, so `getLifecycleService().isRunning()` is already false when the runner fails. `HazelcastInstanceProxy.getLifecycleService()` returns a `TerminatedLifecycleService` after the instance is gone, so the check does not throw either. `onCancel()` now only logs at DEBUG when the instance's lifecycle service is not running or the CamelContext is stopping. A destroyed topic is still reported. The listener gets the consumer's `HazelcastInstance` in its constructor. New test `listenerCancelledWhenTheMemberShutsDownIsNotReported` shuts the member down while the route is started and checks for 1 s that nothing is reported. Without the check it fails with the report. The upgrade-guide entry says this too. camel-hazelcast: 250 tests, 0 failures, 1 error (`HazelcastUserConfigSerializationFilterWarningTest`, the same as on main). _Claude Code on behalf of allthingssecurity_ -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
