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



##########
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:
       From the diff it looked like this constructor is only used by the other 
one...are both needed?




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