|
14 | 14 | |
15 | 15 | package com.rabbitmq.stream.impl; |
16 | 16 |
|
| 17 | +import static com.rabbitmq.stream.impl.CoordinatorUtils.ClientClosedException; |
| 18 | +import static com.rabbitmq.stream.impl.CoordinatorUtils.shouldRefreshCandidates; |
17 | 19 | import static com.rabbitmq.stream.impl.Tuples.pair; |
18 | 20 | import static com.rabbitmq.stream.impl.Utils.AVAILABLE_PROCESSORS; |
19 | 21 | import static com.rabbitmq.stream.impl.Utils.callAndMaybeRetry; |
|
31 | 33 | import com.rabbitmq.stream.Constants; |
32 | 34 | import com.rabbitmq.stream.StreamDoesNotExistException; |
33 | 35 | import com.rabbitmq.stream.StreamException; |
34 | | -import com.rabbitmq.stream.StreamNotAvailableException; |
35 | 36 | import com.rabbitmq.stream.impl.Client.Broker; |
36 | 37 | import com.rabbitmq.stream.impl.Client.ClientParameters; |
37 | 38 | import com.rabbitmq.stream.impl.Client.MetadataListener; |
@@ -193,22 +194,18 @@ private void addToManager(Broker node, List<BrokerWrapper> candidates, AgentTrac |
193 | 194 | this.managers.add(pickedManager); |
194 | 195 | } catch (IllegalStateException e) { |
195 | 196 | pickedManager = null; |
196 | | - } catch (ConnectionStreamException | ClientClosedException | StreamNotAvailableException e) { |
197 | | - // manager connection is dead or stream not available |
198 | | - // scheduling manager closing if necessary in another thread to avoid blocking this one |
199 | | - if (pickedManager.isEmpty()) { |
200 | | - ClientProducersManager manager = pickedManager; |
201 | | - this.environment.execute( |
202 | | - () -> { |
203 | | - manager.closeIfEmpty(); |
204 | | - }, |
205 | | - "Producer manager closing after timeout, producer %d on stream '%s'", |
206 | | - tracker.uniqueId(), |
207 | | - tracker.stream()); |
208 | | - } |
209 | | - throw e; |
210 | 197 | } catch (RuntimeException e) { |
211 | | - if (pickedManager != null) { |
| 198 | + if (shouldRefreshCandidates(e)) { |
| 199 | + // manager connection is dead or stream not available |
| 200 | + // scheduling manager closing if necessary in another thread to avoid blocking this one |
| 201 | + if (pickedManager.isEmpty()) { |
| 202 | + this.environment.execute( |
| 203 | + pickedManager::closeIfEmpty, |
| 204 | + "Producer manager closing after timeout, producer %d on stream '%s'", |
| 205 | + tracker.uniqueId(), |
| 206 | + tracker.stream()); |
| 207 | + } |
| 208 | + } else { |
212 | 209 | pickedManager.closeIfEmpty(); |
213 | 210 | } |
214 | 211 | throw e; |
@@ -337,12 +334,14 @@ public String toString() { |
337 | 334 | managerBuilder.append( |
338 | 335 | m.producers.values().stream() |
339 | 336 | .map( |
340 | | - p -> { |
341 | | - StringBuilder producerBuilder = new StringBuilder("{"); |
342 | | - producerBuilder.append(jsonField("stream", p.stream())).append(","); |
343 | | - producerBuilder.append(jsonField("producer_id", p.publisherId)); |
344 | | - return producerBuilder.append("}").toString(); |
345 | | - }) |
| 337 | + p -> |
| 338 | + "{" |
| 339 | + + jsonField("stream", p.stream()) |
| 340 | + + "," |
| 341 | + + jsonField("producer_id", p.publisherId) |
| 342 | + + "," |
| 343 | + + jsonField("state", p.producer.state()) |
| 344 | + + "}") |
346 | 345 | .collect(Collectors.joining(","))); |
347 | 346 | managerBuilder.append("],"); |
348 | 347 | managerBuilder.append("\"tracking_consumers\" : ["); |
@@ -832,33 +831,33 @@ private void recoverAgent(Broker node, List<BrokerWrapper> candidates, AgentTrac |
832 | 831 | tracker.stream()); |
833 | 832 | } |
834 | 833 | reassignmentCompleted = true; |
835 | | - } catch (ConnectionStreamException |
836 | | - | ClientClosedException |
837 | | - | StreamNotAvailableException e) { |
838 | | - LOGGER.debug( |
839 | | - "{} re-assignment on stream {} (ID {}) timed out or connection closed or stream not available, " |
840 | | - + "refreshing candidate leader and retrying", |
841 | | - tracker.type(), |
842 | | - tracker.identifiable() ? tracker.id() : "N/A", |
843 | | - tracker.stream()); |
844 | | - // maybe not a good candidate, let's refresh and retry for this one |
845 | | - Pair<Broker, List<BrokerWrapper>> brokerAndCandidates = |
846 | | - callAndMaybeRetry( |
847 | | - () -> { |
848 | | - List<BrokerWrapper> cs = findCandidateNodes(tracker.stream(), forceLeader); |
849 | | - return pair(pickBroker(cs), cs); |
850 | | - }, |
851 | | - ex -> !(ex instanceof StreamDoesNotExistException), |
852 | | - environment.recoveryBackOffDelayPolicy(), |
853 | | - "Candidate lookup for %s on stream '%s'", |
854 | | - tracker.type(), |
855 | | - tracker.stream()); |
856 | | - node = brokerAndCandidates.v1(); |
857 | | - candidates = brokerAndCandidates.v2(); |
858 | 834 | } catch (Exception e) { |
859 | | - LOGGER.warn( |
860 | | - "Error while re-assigning {} (stream '{}')", tracker.type(), tracker.stream(), e); |
861 | | - reassignmentCompleted = true; |
| 835 | + if (shouldRefreshCandidates(e)) { |
| 836 | + LOGGER.debug( |
| 837 | + "{} re-assignment on stream {} (ID {}) timed out or connection closed or stream not available, " |
| 838 | + + "refreshing candidate leader and retrying", |
| 839 | + tracker.type(), |
| 840 | + tracker.identifiable() ? tracker.id() : "N/A", |
| 841 | + tracker.stream()); |
| 842 | + // maybe not a good candidate, let's refresh and retry for this one |
| 843 | + Pair<Broker, List<BrokerWrapper>> brokerAndCandidates = |
| 844 | + callAndMaybeRetry( |
| 845 | + () -> { |
| 846 | + List<BrokerWrapper> cs = findCandidateNodes(tracker.stream(), forceLeader); |
| 847 | + return pair(pickBroker(cs), cs); |
| 848 | + }, |
| 849 | + ex -> !(ex instanceof StreamDoesNotExistException), |
| 850 | + environment.recoveryBackOffDelayPolicy(), |
| 851 | + "Candidate lookup for %s on stream '%s'", |
| 852 | + tracker.type(), |
| 853 | + tracker.stream()); |
| 854 | + node = brokerAndCandidates.v1(); |
| 855 | + candidates = brokerAndCandidates.v2(); |
| 856 | + } else { |
| 857 | + LOGGER.warn( |
| 858 | + "Error while re-assigning {} (stream '{}')", tracker.type(), tracker.stream(), e); |
| 859 | + reassignmentCompleted = true; |
| 860 | + } |
862 | 861 | } |
863 | 862 | } |
864 | 863 | } |
@@ -1022,13 +1021,6 @@ public int hashCode() { |
1022 | 1021 | private static final Predicate<Exception> RETRY_ON_TIMEOUT = |
1023 | 1022 | e -> e instanceof TimeoutStreamException; |
1024 | 1023 |
|
1025 | | - private static class ClientClosedException extends StreamException { |
1026 | | - |
1027 | | - public ClientClosedException() { |
1028 | | - super("Client already closed"); |
1029 | | - } |
1030 | | - } |
1031 | | - |
1032 | 1024 | static <T> int pickSlot(ConcurrentMap<Byte, T> map, T tracker, AtomicInteger sequence) { |
1033 | 1025 | int index = -1; |
1034 | 1026 | T previousValue = tracker; |
|
0 commit comments