[ 
https://issues.apache.org/jira/browse/ARTEMIS-3720?focusedWorklogId=743729&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-743729
 ]

ASF GitHub Bot logged work on ARTEMIS-3720:
-------------------------------------------

                Author: ASF GitHub Bot
            Created on: 18/Mar/22 13:06
            Start Date: 18/Mar/22 13:06
    Worklog Time Spent: 10m 
      Work Description: clebertsuconic commented on a change in pull request 
#3983:
URL: https://github.com/apache/activemq-artemis/pull/3983#discussion_r829980293



##########
File path: 
artemis-commons/src/main/java/org/apache/activemq/artemis/utils/SizeAwareMetric.java
##########
@@ -0,0 +1,240 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.artemis.utils;
+
+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
+import java.util.concurrent.atomic.AtomicLongFieldUpdater;
+import java.util.function.IntConsumer;
+
+import org.jboss.logging.Logger;
+
+public class SizeAwareMetric {
+
+   private static final Logger logger = 
Logger.getLogger(SizeAwareMetric.class);
+
+   private static final AtomicLongFieldUpdater<SizeAwareMetric> 
elementsUpdater = AtomicLongFieldUpdater.newUpdater(SizeAwareMetric.class, 
"elements");
+   private static final AtomicLongFieldUpdater<SizeAwareMetric> sizeUpdater = 
AtomicLongFieldUpdater.newUpdater(SizeAwareMetric.class, "size");
+   private static final int FREE = 0, OVER_SIZE = 1, OVER_ELEMENTS = 2, 
NOT_USED = -1;
+   private static final AtomicIntegerFieldUpdater<SizeAwareMetric> flagUpdater 
= AtomicIntegerFieldUpdater.newUpdater(SizeAwareMetric.class, "flag");
+   private volatile long elements;
+   private volatile long size;
+   private volatile int flag = NOT_USED;
+   private long maxElements;
+   private long lowerMarkElements;
+   private long maxSize;
+   private long lowerMarkSize;
+   private boolean sizeEnabled = false;
+   private boolean elementsEnabled = false;
+   private SizeAwareMetric parentMetric;
+   private IntConsumer onSizeCallback;
+   private IntConsumer onSizeOnlyCallback;
+   private Runnable overCallback;
+   private Runnable underCallback;
+
+   public SizeAwareMetric(long maxSize, long lowerMarkSize, long maxElements, 
long lowerMarkElements) {
+      if (lowerMarkSize > maxSize) {
+         throw new IllegalArgumentException("lowerMark must be <= maxSize");
+      }
+      if (lowerMarkElements > maxElements) {
+         throw new IllegalArgumentException("lowerMarkElements must be <= 
maxElements");
+      }
+      this.maxElements = maxElements;
+      this.lowerMarkElements = lowerMarkElements;
+      this.maxSize = maxSize;
+      this.lowerMarkSize = lowerMarkSize;
+      this.sizeEnabled = maxSize >= 0;
+      this.elementsEnabled = maxElements >= 0;
+   }
+
+   public boolean isOver() {
+      return flag != FREE && flag != NOT_USED;
+   }
+
+   public boolean isOverSize() {
+      return flag == OVER_SIZE;
+   }
+
+   public boolean isOverElements() {
+      return flag == OVER_ELEMENTS;
+   }
+
+   public long getSize() {
+      return size;
+   }
+
+   public boolean isElementsEnabled() {
+      return elementsEnabled;
+   }
+
+   public SizeAwareMetric setElementsEnabled(boolean elementsEnabled) {
+      this.elementsEnabled = elementsEnabled;
+      return this;
+   }
+
+   public long getElements() {
+      return elements;
+   }
+
+   public boolean isSizeEnabled() {
+      return sizeEnabled;
+   }
+
+   public SizeAwareMetric setSizeEnabled(boolean sizeEnabled) {
+      this.sizeEnabled = sizeEnabled;
+      return this;
+   }
+
+   public void setOnSizeCallback(IntConsumer onSize) {
+      this.onSizeCallback = onSize;
+   }
+
+   public void setOnSizeOnlyCallback(IntConsumer onSize) {
+      this.onSizeOnlyCallback = onSize;
+   }
+
+   public void setOverCallback(Runnable over) {
+      this.overCallback = over;
+   }
+
+   public void setParentMetric(SizeAwareMetric parent) {
+      parentMetric = parent;
+   }
+
+   public void setUnderCallback(Runnable under) {
+      this.underCallback = under;
+   }
+
+   protected void over() {
+      if (overCallback != null) {
+         try {
+            overCallback.run();
+         } catch (Throwable e) {
+            logger.warn(e.getMessage(), e);
+         }
+      }
+   }
+
+   protected void under() {
+      if (underCallback != null) {
+         try {
+            underCallback.run();
+         } catch (Throwable e) {
+            logger.warn(e.getMessage(), e);
+         }
+      }
+   }
+
+   private boolean changeFlag(int expected, int newValue) {
+      return flagUpdater.compareAndSet(this, expected, newValue);
+   }
+
+   public long addSize(int delta) {
+      return addSize(delta, false);
+   }
+
+   public long addSizeOnly(int delta) {
+      return addSize(delta, true);
+   }
+
+   public long addSize(int delta, boolean sizeOnly) {
+
+      if (delta == 0) {
+         if (logger.isDebugEnabled()) {
+            logger.debug("SizeAwareMetric ignored with size 0", new 
Exception("trace"));
+         }
+         return sizeUpdater.get(this);
+      }
+
+      flagUpdater.compareAndSet(this, NOT_USED, FREE);
+
+      if (parentMetric != null) {
+         if (parentMetric != null) {

Review comment:
       thanks.. kind of a typo during the renaming...




-- 
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: gitbox-unsubscr...@activemq.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 743729)
    Time Spent: 5h 50m  (was: 5h 40m)

> Max number of messages as a deciding factor for Paging
> ------------------------------------------------------
>
>                 Key: ARTEMIS-3720
>                 URL: https://issues.apache.org/jira/browse/ARTEMIS-3720
>             Project: ActiveMQ Artemis
>          Issue Type: Improvement
>          Components: Broker
>            Reporter: Clebert Suconic
>            Assignee: Clebert Suconic
>            Priority: Major
>             Fix For: 2.21.0
>
>          Time Spent: 5h 50m
>  Remaining Estimate: 0h
>




--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to