NikitaShupletsov commented on code in PR #848:
URL: https://github.com/apache/activemq/pull/848#discussion_r1065079995


##########
activemq-broker/src/main/java/org/apache/activemq/replica/ReplicaCompactor.java:
##########
@@ -0,0 +1,303 @@
+/**
+ * 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.activemq.replica;
+
+import org.apache.activemq.broker.Broker;
+import org.apache.activemq.broker.ConnectionContext;
+import org.apache.activemq.broker.ConsumerBrokerExchange;
+import org.apache.activemq.broker.region.MessageReference;
+import org.apache.activemq.broker.region.PrefetchSubscription;
+import org.apache.activemq.broker.region.Queue;
+import org.apache.activemq.broker.region.QueueMessageReference;
+import org.apache.activemq.command.ActiveMQMessage;
+import org.apache.activemq.command.ConnectionId;
+import org.apache.activemq.command.LocalTransactionId;
+import org.apache.activemq.command.MessageAck;
+import org.apache.activemq.command.MessageDispatchNotification;
+import org.apache.activemq.command.MessageId;
+import org.apache.activemq.command.TransactionId;
+import org.apache.activemq.util.LongSequenceGenerator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class ReplicaCompactor {
+    private static final Logger logger = 
LoggerFactory.getLogger(ReplicaCompactor.class);
+    private static final String CONSUMER_SELECTOR = String.format("%s LIKE 
'%s'", ReplicaEventType.EVENT_TYPE_PROPERTY, ReplicaEventType.MESSAGE_ACK);
+    public static final int MAXIMUM_MESSAGES = 1_000;
+
+    private final Broker broker;
+    private final ConnectionContext connectionContext;
+    private final ReplicaReplicationQueueSupplier queueProvider;
+    private final PrefetchSubscription subscription;
+
+    private final Queue intermediateQueue;
+
+    public ReplicaCompactor(Broker broker, ConnectionContext 
connectionContext, ReplicaReplicationQueueSupplier queueProvider, 
PrefetchSubscription subscription) {
+        this.broker = broker;
+        this.connectionContext = connectionContext;
+        this.queueProvider = queueProvider;
+        this.subscription = subscription;
+
+        intermediateQueue = 
broker.getDestinations(queueProvider.getIntermediateQueue()).stream().findFirst()
+                .map(DestinationExtractor::extractQueue).orElseThrow();
+    }
+
+    List<MessageReference> compactAndFilter(List<MessageReference> list, 
boolean withAdditionalMessages) throws Exception {
+        List<DeliveredMessageReference> toProcess = list.stream()
+                .map(DeliveredMessageReference::new)
+                .collect(Collectors.toList());
+
+        int prefetchSize = subscription.getPrefetchSize();
+        try {
+            if (withAdditionalMessages) {
+                subscription.setPrefetchSize(0);
+                toProcess.addAll(getAdditionalMessages());
+            }
+
+            List<DeliveredMessageReference> processed = 
compactAndFilter0(toProcess);
+
+            Set<MessageId> messageIds = 
list.stream().map(MessageReference::getMessageId).collect(Collectors.toSet());
+
+            return processed.stream()
+                    .map(dmr -> dmr.messageReference)
+                    .filter(mr -> messageIds.contains(mr.getMessageId()))
+                    .collect(Collectors.toList());
+        } finally {
+            subscription.setPrefetchSize(prefetchSize);
+        }
+    }
+
+    private List<DeliveredMessageReference> getAdditionalMessages() throws 
Exception {
+        List<DeliveredMessageReference> result = new ArrayList<>();
+        List<QueueMessageReference> additionalMessages = 
intermediateQueue.getMatchingMessages(connectionContext, CONSUMER_SELECTOR, 
MAXIMUM_MESSAGES);

Review Comment:
   it will do a full scan only if there are less than 1000 messages for this 
selector. but yeah, I agree it may be expensive. I will take a look what we can 
do about it. but any ideas are highly appreciated 



-- 
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]

Reply via email to