You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by GitBox <gi...@apache.org> on 2019/10/01 08:43:34 UTC

[GitHub] [sling-org-apache-sling-distribution-journal] tmaret commented on a change in pull request #12: SLING-8751 - Webconsole plugin to download content packages

tmaret commented on a change in pull request #12: SLING-8751 - Webconsole plugin to download content packages
URL: https://github.com/apache/sling-org-apache-sling-distribution-journal/pull/12#discussion_r329939772
 
 

 ##########
 File path: src/main/java/org/apache/sling/distribution/journal/impl/queue/impl/RangePoller.java
 ##########
 @@ -49,42 +50,62 @@
 
     private final Closeable headPoller;
 
-    private final CountDownLatch fetched = new CountDownLatch(1);
+    private final List<FullMessage<PackageMessage>> messages;
 
-    private List<FullMessage<PackageMessage>> messages;
+    private final Semaphore nextMessage;
+    private final AtomicLong lastMessageTime;
+    private final AtomicLong lastOffset;
+    private final AtomicLong numMessages;
     
     public RangePoller(MessagingProvider messagingProvider,
                           String packageTopic,
                           long minOffset,
-                          long maxOffset) {
-        this.maxOffset = maxOffset;
+                          long maxOffsetExclusive) {
+        this.maxOffset = maxOffsetExclusive;
         this.minOffset = minOffset;
         this.messages = new ArrayList<>();
+        this.nextMessage = new Semaphore(0);
+        this.lastMessageTime = new AtomicLong(System.currentTimeMillis());
+        this.lastOffset = new AtomicLong();
+        this.numMessages = new AtomicLong();
         String assign = messagingProvider.assignTo(minOffset);
-        LOG.info("Fetching offsets [{},{}[", minOffset, maxOffset);
+        LOG.info("Fetching offsets [{},{}[", minOffset, maxOffsetExclusive);
         headPoller = messagingProvider.createPoller(
                 packageTopic, Reset.earliest, assign,
                 create(Messages.PackageMessage.class, this::handlePackage));
     }
 
     public List<FullMessage<PackageMessage>> fetchRange() throws InterruptedException {
+        return fetchRange(Integer.MAX_VALUE, Integer.MAX_VALUE);
+    }
+    
+    public List<FullMessage<PackageMessage>> fetchRange(int maxMessages, int timeOutMs) throws InterruptedException {
         try {
-            fetched.await();
-            LOG.info("Fetched offsets [{},{}[", minOffset, maxOffset);
-            return messages;
+            boolean timeout = false;
+            while (lastOffset.get() < maxOffset && !timeout && this.numMessages.get() < maxMessages) {
+                timeout = !nextMessage.tryAcquire(timeOutMs, TimeUnit.MILLISECONDS);
+            }
+            if (timeout) {
 
 Review comment:
   It does not seem correct to simply log a timeout at this level. We should throw when timeout occurs and handle it the way we want at a higher level.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services