clebertsuconic commented on a change in pull request #3983:
URL: https://github.com/apache/activemq-artemis/pull/3983#discussion_r829128412



##########
File path: 
artemis-commons/src/main/java/org/apache/activemq/artemis/utils/SizeAwareMetric.java
##########
@@ -0,0 +1,304 @@
+/**
+ * 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 volatile long elements;
+
+   private static final AtomicLongFieldUpdater<SizeAwareMetric> sizeUpdater = 
AtomicLongFieldUpdater.newUpdater(SizeAwareMetric.class, "size");
+
+   private volatile long 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 int flag = NOT_USED;
+
+   public boolean isOver() {
+      return flag != FREE && flag != NOT_USED;
+   }
+
+   public boolean isOverSize() {
+      return flag == OVER_SIZE;
+   }
+
+   public boolean isOverElements() {
+      return flag == OVER_ELEMENTS;
+   }
+
+
+   private long maxElements;
+
+   private long lowerMarkElements;
+
+   private long maxSize;
+
+   private long lowerMark;
+
+   private boolean sizeEnabled = false;
+
+   private boolean elementsEnabled = false;
+
+   private SizeAwareMetric[] parentSlots;
+
+   private IntConsumer[] onSizeSlots;
+
+   private IntConsumer[] onSizeOnlySlots;
+
+   private Runnable[] overSlots;
+
+   private Runnable[] underSlots;
+
+   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 addOnSizeCallback(IntConsumer onSize, boolean sizeOnly) {
+
+      IntConsumer[] slots = sizeOnly ? onSizeOnlySlots : onSizeSlots;
+
+      for (int i = 0; i < slots.length; i++) {
+         if (slots[i] == null) {
+            slots[i] = onSize;
+            return;
+         }
+      }
+
+      throw new IllegalStateException("no open slot available");
+   }
+
+   public void addOverCallback(Runnable over) {
+      for (int i = 0; i < overSlots.length; i++) {
+         if (overSlots[i] == null) {
+            overSlots[i] = over;
+            return;
+         }
+      }
+
+      throw new IllegalStateException("no open slot available");
+   }
+
+   public void addParent(SizeAwareMetric parent) {
+      for (int i = 0; i < parentSlots.length; i++) {
+         if (parentSlots[i] == null) {
+            parentSlots[i] = parent;
+            return;
+         }
+      }
+
+      throw new IllegalStateException("no open slot available");
+   }
+
+
+   public void addUnderCallback(Runnable under) {
+      for (int i = 0; i < underSlots.length; i++) {
+         if (underSlots[i] == null) {
+            underSlots[i] = under;
+            return;
+         }
+      }
+
+      throw new IllegalStateException("no open slot available");
+   }
+
+   public SizeAwareMetric(long maxSize, long lowerMark, long maxElements, long 
lowerMarkElements, SizeAwareMetric[] parentSlots, IntConsumer[] onSizeSlots, 
IntConsumer[] onSizeOnlySlots, Runnable[] overSlots, Runnable[] underSlots) {

Review comment:
       I have future uses for this class.. I will keep this constructor private 
for now.

##########
File path: 
artemis-server/src/main/java/org/apache/activemq/artemis/core/paging/impl/PagingStoreImpl.java
##########
@@ -189,13 +198,45 @@ public PagingStoreImpl(final SimpleString address,
       this.usingGlobalMaxSize = pagingManager.isUsingGlobalSize();
    }
 
+   private static SizeAwareMetric getGlobalMetric(PagingManager manager) {
+      if (manager instanceof PagingManagerImpl) {
+         return ((PagingManagerImpl) manager).getSizeAwareMetric();
+      } else {
+         // for unit / mock tests
+         SizeAwareMetric metric = new SizeAwareMetric(-1, -1, -1, -1, -1, 1, 
1, 0, 0);
+         metric.addOnSizeCallback(manager::addSize, false);
+         metric.addOnSizeCallback((l) -> manager.addSize(l, true), true);
+         return metric;
+      }
+   }

Review comment:
       I did not want to add SizeAwareMetric to the interface... I wanted it to 
be an internal implementation detail.
   
   if the implementation (PagingManagerImpl) is using it, it could use it on a 
parent call for the metric. Otherwise the proxy Metric would use the already 
existing API.
   
   I'm adding it to the interface now with a default implementation returning 
the proxy. I didn't want to do it.. but I will do it just to settle this.

##########
File path: 
artemis-commons/src/main/java/org/apache/activemq/artemis/utils/SizeAwareMetric.java
##########
@@ -0,0 +1,304 @@
+/**
+ * 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 volatile long elements;
+
+   private static final AtomicLongFieldUpdater<SizeAwareMetric> sizeUpdater = 
AtomicLongFieldUpdater.newUpdater(SizeAwareMetric.class, "size");
+
+   private volatile long 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 int flag = NOT_USED;
+
+   public boolean isOver() {
+      return flag != FREE && flag != NOT_USED;
+   }
+
+   public boolean isOverSize() {
+      return flag == OVER_SIZE;
+   }
+
+   public boolean isOverElements() {
+      return flag == OVER_ELEMENTS;
+   }
+
+
+   private long maxElements;
+
+   private long lowerMarkElements;
+
+   private long maxSize;
+
+   private long lowerMark;
+
+   private boolean sizeEnabled = false;
+
+   private boolean elementsEnabled = false;
+
+   private SizeAwareMetric[] parentSlots;
+
+   private IntConsumer[] onSizeSlots;
+
+   private IntConsumer[] onSizeOnlySlots;
+
+   private Runnable[] overSlots;
+
+   private Runnable[] underSlots;
+
+   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 addOnSizeCallback(IntConsumer onSize, boolean sizeOnly) {
+
+      IntConsumer[] slots = sizeOnly ? onSizeOnlySlots : onSizeSlots;
+
+      for (int i = 0; i < slots.length; i++) {
+         if (slots[i] == null) {
+            slots[i] = onSize;
+            return;
+         }
+      }

Review comment:
       planned for future usage (e.g. Flow control on Actor).
   
   this is going to always be null and every usage will happen through Parent. 
(which will be a single instance now after my update.. no more array).

##########
File path: 
artemis-commons/src/main/java/org/apache/activemq/artemis/utils/SizeAwareMetric.java
##########
@@ -0,0 +1,304 @@
+/**
+ * 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 volatile long elements;
+
+   private static final AtomicLongFieldUpdater<SizeAwareMetric> sizeUpdater = 
AtomicLongFieldUpdater.newUpdater(SizeAwareMetric.class, "size");
+
+   private volatile long 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 int flag = NOT_USED;
+
+   public boolean isOver() {
+      return flag != FREE && flag != NOT_USED;
+   }
+
+   public boolean isOverSize() {
+      return flag == OVER_SIZE;
+   }
+
+   public boolean isOverElements() {
+      return flag == OVER_ELEMENTS;
+   }
+
+
+   private long maxElements;
+
+   private long lowerMarkElements;
+
+   private long maxSize;
+
+   private long lowerMark;
+
+   private boolean sizeEnabled = false;
+
+   private boolean elementsEnabled = false;
+
+   private SizeAwareMetric[] parentSlots;
+
+   private IntConsumer[] onSizeSlots;
+
+   private IntConsumer[] onSizeOnlySlots;
+
+   private Runnable[] overSlots;
+
+   private Runnable[] underSlots;
+
+   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 addOnSizeCallback(IntConsumer onSize, boolean sizeOnly) {
+
+      IntConsumer[] slots = sizeOnly ? onSizeOnlySlots : onSizeSlots;
+
+      for (int i = 0; i < slots.length; i++) {
+         if (slots[i] == null) {
+            slots[i] = onSize;
+            return;
+         }
+      }

Review comment:
       ok.. fair enough, I will simplify it.

##########
File path: 
artemis-server/src/main/java/org/apache/activemq/artemis/core/paging/impl/PagingStoreImpl.java
##########
@@ -189,13 +198,45 @@ public PagingStoreImpl(final SimpleString address,
       this.usingGlobalMaxSize = pagingManager.isUsingGlobalSize();
    }
 
+   private static SizeAwareMetric getGlobalMetric(PagingManager manager) {
+      if (manager instanceof PagingManagerImpl) {
+         return ((PagingManagerImpl) manager).getSizeAwareMetric();
+      } else {
+         // for unit / mock tests
+         SizeAwareMetric metric = new SizeAwareMetric(-1, -1, -1, -1, -1, 1, 
1, 0, 0);
+         metric.addOnSizeCallback(manager::addSize, false);
+         metric.addOnSizeCallback((l) -> manager.addSize(l, true), true);
+         return metric;
+      }
+   }

Review comment:
       actually, it will look better to just use the SizeAwareMetric.

##########
File path: 
artemis-commons/src/main/java/org/apache/activemq/artemis/utils/SizeAwareMetric.java
##########
@@ -0,0 +1,304 @@
+/**
+ * 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 volatile long elements;
+
+   private static final AtomicLongFieldUpdater<SizeAwareMetric> sizeUpdater = 
AtomicLongFieldUpdater.newUpdater(SizeAwareMetric.class, "size");
+
+   private volatile long 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 int flag = NOT_USED;
+
+   public boolean isOver() {
+      return flag != FREE && flag != NOT_USED;
+   }
+
+   public boolean isOverSize() {
+      return flag == OVER_SIZE;
+   }
+
+   public boolean isOverElements() {
+      return flag == OVER_ELEMENTS;
+   }
+
+
+   private long maxElements;
+
+   private long lowerMarkElements;
+
+   private long maxSize;
+
+   private long lowerMark;
+
+   private boolean sizeEnabled = false;
+
+   private boolean elementsEnabled = false;
+
+   private SizeAwareMetric[] parentSlots;
+
+   private IntConsumer[] onSizeSlots;
+
+   private IntConsumer[] onSizeOnlySlots;
+
+   private Runnable[] overSlots;
+
+   private Runnable[] underSlots;
+
+   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 addOnSizeCallback(IntConsumer onSize, boolean sizeOnly) {
+
+      IntConsumer[] slots = sizeOnly ? onSizeOnlySlots : onSizeSlots;
+
+      for (int i = 0; i < slots.length; i++) {
+         if (slots[i] == null) {
+            slots[i] = onSize;
+            return;
+         }
+      }
+
+      throw new IllegalStateException("no open slot available");
+   }
+
+   public void addOverCallback(Runnable over) {
+      for (int i = 0; i < overSlots.length; i++) {
+         if (overSlots[i] == null) {
+            overSlots[i] = over;
+            return;
+         }
+      }
+
+      throw new IllegalStateException("no open slot available");
+   }
+
+   public void addParent(SizeAwareMetric parent) {
+      for (int i = 0; i < parentSlots.length; i++) {
+         if (parentSlots[i] == null) {
+            parentSlots[i] = parent;
+            return;
+         }
+      }
+
+      throw new IllegalStateException("no open slot available");
+   }
+
+
+   public void addUnderCallback(Runnable under) {
+      for (int i = 0; i < underSlots.length; i++) {
+         if (underSlots[i] == null) {
+            underSlots[i] = under;
+            return;
+         }
+      }
+
+      throw new IllegalStateException("no open slot available");
+   }
+
+   public SizeAwareMetric(long maxSize, long lowerMark, long maxElements, long 
lowerMarkElements, SizeAwareMetric[] parentSlots, IntConsumer[] onSizeSlots, 
IntConsumer[] onSizeOnlySlots, Runnable[] overSlots, Runnable[] underSlots) {

Review comment:
       I will just remove it...

##########
File path: 
artemis-server/src/main/java/org/apache/activemq/artemis/core/paging/impl/PagingStoreImpl.java
##########
@@ -189,13 +198,45 @@ public PagingStoreImpl(final SimpleString address,
       this.usingGlobalMaxSize = pagingManager.isUsingGlobalSize();
    }
 
+   private static SizeAwareMetric getGlobalMetric(PagingManager manager) {
+      if (manager instanceof PagingManagerImpl) {
+         return ((PagingManagerImpl) manager).getSizeAwareMetric();
+      } else {
+         // for unit / mock tests
+         SizeAwareMetric metric = new SizeAwareMetric(-1, -1, -1, -1, -1, 1, 
1, 0, 0);
+         metric.addOnSizeCallback(manager::addSize, false);
+         metric.addOnSizeCallback((l) -> manager.addSize(l, true), true);
+         return metric;
+      }
+   }

Review comment:
       the other bits is just returning a proxy to the pagingManager API.
   
   anyway I'm adding SizeAwareMetric to the public api with a default 
implementation to the proxy version now.

##########
File path: 
artemis-commons/src/main/java/org/apache/activemq/artemis/utils/SizeAwareMetric.java
##########
@@ -0,0 +1,304 @@
+/**
+ * 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 volatile long elements;
+
+   private static final AtomicLongFieldUpdater<SizeAwareMetric> sizeUpdater = 
AtomicLongFieldUpdater.newUpdater(SizeAwareMetric.class, "size");
+
+   private volatile long 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 int flag = NOT_USED;
+
+   public boolean isOver() {
+      return flag != FREE && flag != NOT_USED;
+   }
+
+   public boolean isOverSize() {
+      return flag == OVER_SIZE;
+   }
+
+   public boolean isOverElements() {
+      return flag == OVER_ELEMENTS;
+   }
+
+
+   private long maxElements;
+
+   private long lowerMarkElements;
+
+   private long maxSize;
+
+   private long lowerMark;
+
+   private boolean sizeEnabled = false;
+
+   private boolean elementsEnabled = false;
+
+   private SizeAwareMetric[] parentSlots;
+
+   private IntConsumer[] onSizeSlots;
+
+   private IntConsumer[] onSizeOnlySlots;
+
+   private Runnable[] overSlots;
+
+   private Runnable[] underSlots;
+
+   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 addOnSizeCallback(IntConsumer onSize, boolean sizeOnly) {
+
+      IntConsumer[] slots = sizeOnly ? onSizeOnlySlots : onSizeSlots;
+
+      for (int i = 0; i < slots.length; i++) {
+         if (slots[i] == null) {
+            slots[i] = onSize;
+            return;
+         }
+      }
+
+      throw new IllegalStateException("no open slot available");
+   }
+
+   public void addOverCallback(Runnable over) {
+      for (int i = 0; i < overSlots.length; i++) {
+         if (overSlots[i] == null) {
+            overSlots[i] = over;
+            return;
+         }
+      }
+
+      throw new IllegalStateException("no open slot available");
+   }
+
+   public void addParent(SizeAwareMetric parent) {
+      for (int i = 0; i < parentSlots.length; i++) {
+         if (parentSlots[i] == null) {
+            parentSlots[i] = parent;
+            return;
+         }
+      }
+
+      throw new IllegalStateException("no open slot available");
+   }
+
+
+   public void addUnderCallback(Runnable under) {
+      for (int i = 0; i < underSlots.length; i++) {
+         if (underSlots[i] == null) {
+            underSlots[i] = under;
+            return;
+         }
+      }
+
+      throw new IllegalStateException("no open slot available");
+   }
+
+   public SizeAwareMetric(long maxSize, long lowerMark, long maxElements, long 
lowerMarkElements, SizeAwareMetric[] parentSlots, IntConsumer[] onSizeSlots, 
IntConsumer[] onSizeOnlySlots, Runnable[] overSlots, Runnable[] underSlots) {
+      if (lowerMark > maxSize) {
+         throw new IllegalArgumentException("lowerMark must be <= maxSize");
+      }
+      this.maxElements = maxElements;
+      this.lowerMarkElements = lowerMarkElements;
+      this.maxSize = maxSize;
+      this.lowerMark = lowerMark;
+      this.onSizeSlots = onSizeSlots;
+      this.parentSlots = parentSlots;
+      this.onSizeOnlySlots = onSizeOnlySlots;
+      this.overSlots = overSlots;
+      this.underSlots = underSlots;
+      this.sizeEnabled = maxSize >= 0;
+      this.elementsEnabled = maxElements >= 0;
+   }
+
+   public SizeAwareMetric(long maxSize, long lowerMark, long maxElements, long 
lowerMarkElements, int parentSlots, int onSizeSlotNumber, int 
onSizeOnlySlotNumber, int overSlotNumber, int underSlotNumber) {
+      this (maxSize, lowerMark, maxElements,
+            lowerMarkElements,
+            parentSlots > 0 ? new SizeAwareMetric[parentSlots] : null,
+            onSizeSlotNumber > 0 ? new IntConsumer[onSizeSlotNumber] : null,
+            onSizeOnlySlotNumber > 0 ? new IntConsumer[onSizeOnlySlotNumber] : 
null,
+            overSlotNumber > 0 ? new Runnable[overSlotNumber] : null,
+            underSlotNumber > 0 ? new Runnable[underSlotNumber] : null);
+   }
+
+   protected void over() {
+      if (overSlots != null) {
+         for (Runnable over: overSlots) {
+            if (over != null) {
+               try {
+                  over.run();
+               } catch (Throwable e) {
+                  logger.warn(e.getMessage(), e);
+               }
+            }
+         }
+      }
+   }
+
+   protected void under() {
+      if (underSlots != null) {
+         for (Runnable under: underSlots) {
+            if (under != null) {
+               try {
+                  under.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 addSize(int delta, boolean sizeOnly) {
+
+      if (delta == 0) {
+         logger.debug("SizeAwareMetric ignored with size 0", new 
Exception("trace"));

Review comment:
       impossible to happen. I will add a test to make sure it's validated and 
add the logger.isDebugEnabled(); 
   
   

##########
File path: artemis-server/src/main/resources/schema/artemis-configuration.xsd
##########
@@ -3934,6 +3943,14 @@
                </xsd:annotation>
             </xsd:element>
 
+            <xsd:element name="max-size-messages" type="xsd:long" default="-1" 
maxOccurs="1" minOccurs="0">

Review comment:
       from the point of view of the config I prefer max-size-messages TBH

##########
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) {
+            parentMetric.addSize(delta, sizeOnly);
+         }
+      }
+
+      IntConsumer theFunction = sizeOnly ? onSizeOnlyCallback : onSizeCallback;
+
+      if (theFunction != null) {
+         try {
+            theFunction.accept(delta);
+         } catch (Throwable e) {
+            logger.warn(e.getMessage(), e);
+         }
+      }
+
+      long currentSize = sizeUpdater.addAndGet(this, delta);
+
+      if (sizeOnly) {
+         long currentElements = elementsUpdater.get(this);
+         if (delta > 0) {
+            checkOver(currentElements, currentSize);
+         } else { // (delta < 0)
+            checkUnder(currentElements, currentSize);
+         }
+      } else {
+         if (delta > 0) {
+            checkOver(elementsUpdater.incrementAndGet(this), currentSize);
+         } else { // (delta < 0)
+            checkUnder(elementsUpdater.decrementAndGet(this), currentSize);
+         }
+      }
+
+      return currentSize;
+   }
+
+   public void setMax(long maxSize, long lowerMarkSize, long maxElements, long 
lowerMarkElements) {
+      this.maxSize = maxSize;
+      this.lowerMarkSize = lowerMarkSize;
+      this.maxElements = maxElements;
+      this.lowerMarkElements = lowerMarkElements;
+      long currentSize = sizeUpdater.get(this);
+      long currentElements = elementsUpdater.get(this);
+      checkOver(currentElements, currentSize);
+      checkUnder(currentElements, currentSize);
+   }
+
+   private void checkUnder(long currentElements, long currentSize) {
+      if (sizeEnabled) {
+         if (currentSize <= lowerMarkSize && changeFlag(OVER_SIZE, FREE)) {
+            under();
+         }
+      }
+
+      if (elementsEnabled) {
+         if (currentElements <= lowerMarkElements && changeFlag(OVER_ELEMENTS, 
FREE)) {
+            under();
+         }
+      }
+   }

Review comment:
       once it goes back from size, it can only go back from size... once it 
goes back from elements it can only go back from elements...  whatever hits it 
first.
   
   once it goes back from SIZE, the flag will be OVER_SIZE and the other check 
will be moot. I don't see any need for any extra checks in there.

##########
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) {
+            parentMetric.addSize(delta, sizeOnly);
+         }
+      }
+
+      IntConsumer theFunction = sizeOnly ? onSizeOnlyCallback : onSizeCallback;

Review comment:
       yes.. it can be done.. 
   
   I wanted to avoid the Boxing on a BiConsumer, and I forgot for an instant I 
didn't think about the own interface.. thanks. nice idea.

##########
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) {

Review comment:
       I will only have addSize(int), and addSize(int, boolean)

##########
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...

##########
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) {
+            parentMetric.addSize(delta, sizeOnly);
+         }
+      }
+
+      IntConsumer theFunction = sizeOnly ? onSizeOnlyCallback : onSizeCallback;
+
+      if (theFunction != null) {
+         try {
+            theFunction.accept(delta);
+         } catch (Throwable e) {
+            logger.warn(e.getMessage(), e);
+         }
+      }
+
+      long currentSize = sizeUpdater.addAndGet(this, delta);
+
+      if (sizeOnly) {
+         long currentElements = elementsUpdater.get(this);
+         if (delta > 0) {
+            checkOver(currentElements, currentSize);
+         } else { // (delta < 0)
+            checkUnder(currentElements, currentSize);
+         }
+      } else {
+         if (delta > 0) {
+            checkOver(elementsUpdater.incrementAndGet(this), currentSize);
+         } else { // (delta < 0)
+            checkUnder(elementsUpdater.decrementAndGet(this), currentSize);
+         }
+      }

Review comment:
       it is not really saving much, as the delta>0 will need to increment, and 
delta<0 decrement.

##########
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) {

Review comment:
       I'm also removing setParent and just using the addSize callback as "the 
parent"

##########
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) {
+            parentMetric.addSize(delta, sizeOnly);
+         }
+      }
+
+      IntConsumer theFunction = sizeOnly ? onSizeOnlyCallback : onSizeCallback;
+
+      if (theFunction != null) {
+         try {
+            theFunction.accept(delta);
+         } catch (Throwable e) {
+            logger.warn(e.getMessage(), e);
+         }
+      }
+
+      long currentSize = sizeUpdater.addAndGet(this, delta);
+
+      if (sizeOnly) {
+         long currentElements = elementsUpdater.get(this);
+         if (delta > 0) {
+            checkOver(currentElements, currentSize);
+         } else { // (delta < 0)
+            checkUnder(currentElements, currentSize);
+         }
+      } else {
+         if (delta > 0) {
+            checkOver(elementsUpdater.incrementAndGet(this), currentSize);
+         } else { // (delta < 0)
+            checkUnder(elementsUpdater.decrementAndGet(this), currentSize);
+         }
+      }
+
+      return currentSize;
+   }
+
+   public void setMax(long maxSize, long lowerMarkSize, long maxElements, long 
lowerMarkElements) {
+      this.maxSize = maxSize;
+      this.lowerMarkSize = lowerMarkSize;
+      this.maxElements = maxElements;
+      this.lowerMarkElements = lowerMarkElements;
+      long currentSize = sizeUpdater.get(this);
+      long currentElements = elementsUpdater.get(this);
+      checkOver(currentElements, currentSize);
+      checkUnder(currentElements, currentSize);
+   }
+
+   private void checkUnder(long currentElements, long currentSize) {
+      if (sizeEnabled) {
+         if (currentSize <= lowerMarkSize && changeFlag(OVER_SIZE, FREE)) {
+            under();
+         }
+      }
+
+      if (elementsEnabled) {
+         if (currentElements <= lowerMarkElements && changeFlag(OVER_ELEMENTS, 
FREE)) {
+            under();
+         }
+      }
+   }

Review comment:
       I see what you mean now...
   
   I would think that is fine as the next addSize would switch it back.
   
   I will check for over again on the release, so it will just return to over 
and its fire immediately in case the switch happens.

##########
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) {
+            parentMetric.addSize(delta, sizeOnly);
+         }
+      }
+
+      IntConsumer theFunction = sizeOnly ? onSizeOnlyCallback : onSizeCallback;
+
+      if (theFunction != null) {
+         try {
+            theFunction.accept(delta);
+         } catch (Throwable e) {
+            logger.warn(e.getMessage(), e);
+         }
+      }
+
+      long currentSize = sizeUpdater.addAndGet(this, delta);
+
+      if (sizeOnly) {
+         long currentElements = elementsUpdater.get(this);
+         if (delta > 0) {
+            checkOver(currentElements, currentSize);
+         } else { // (delta < 0)
+            checkUnder(currentElements, currentSize);
+         }
+      } else {
+         if (delta > 0) {
+            checkOver(elementsUpdater.incrementAndGet(this), currentSize);
+         } else { // (delta < 0)
+            checkUnder(elementsUpdater.decrementAndGet(this), currentSize);
+         }
+      }
+
+      return currentSize;
+   }
+
+   public void setMax(long maxSize, long lowerMarkSize, long maxElements, long 
lowerMarkElements) {
+      this.maxSize = maxSize;
+      this.lowerMarkSize = lowerMarkSize;
+      this.maxElements = maxElements;
+      this.lowerMarkElements = lowerMarkElements;
+      long currentSize = sizeUpdater.get(this);
+      long currentElements = elementsUpdater.get(this);
+      checkOver(currentElements, currentSize);
+      checkUnder(currentElements, currentSize);
+   }
+
+   private void checkUnder(long currentElements, long currentSize) {
+      if (sizeEnabled) {
+         if (currentSize <= lowerMarkSize && changeFlag(OVER_SIZE, FREE)) {
+            under();
+         }
+      }
+
+      if (elementsEnabled) {
+         if (currentElements <= lowerMarkElements && changeFlag(OVER_ELEMENTS, 
FREE)) {
+            under();
+         }
+      }
+   }

Review comment:
       @gemmellr fixed.. I amended the commit..
   
   This is actually nice as it ensures the encapsulation here was a nice idea. 
fixing the check on an encapsulated class was better than doing the check on 
PagingStore and PagingManager.




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