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



##########
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:
       Most or all of the non-test uses appears to only have 0 or 1 callback, 
could this not just be simplified? Anyone needing more can always build upon it 
later (e.g in their callback).

##########
File path: 
artemis-commons/src/main/java/org/apache/activemq/artemis/utils/SizeAwareMetric.java
##########
@@ -0,0 +1,279 @@
+/**
+ * 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");
+

Review comment:
       There is a separating line here but not on the others above and below, 
they should be consistent either way




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