gemmellr commented on a change in pull request #3983: URL: https://github.com/apache/activemq-artemis/pull/3983#discussion_r830181055
########## File path: artemis-commons/src/main/java/org/apache/activemq/artemis/utils/SizeAwareMetric.java ########## @@ -0,0 +1,236 @@ +/** + * 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 org.jboss.logging.Logger; + +public class SizeAwareMetric { + + public interface AddCallback { + void add(int delta, boolean sizeOnly); + } + + 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 AddCallback onSizeCallback; + 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; // equivalent to 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(AddCallback onSize) { + this.onSizeCallback = onSize; + } + + public void setOverCallback(Runnable over) { + this.overCallback = over; + } + + 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 final long addSize(final int delta) { + return addSize(delta, false); + } + + public final long addSize(final int delta, final boolean sizeOnly) { + + if (delta == 0) { + if (logger.isDebugEnabled()) { + logger.debug("SizeAwareMetric ignored with size 0", new Exception("trace")); + } + return sizeUpdater.get(this); + } + + changeFlag(NOT_USED, FREE); + + if (onSizeCallback != null) { + try { + onSizeCallback.add(delta, sizeOnly); + } catch (Throwable e) { + logger.warn(e.getMessage(), e); + } + } + + long currentSize = sizeUpdater.addAndGet(this, delta); + + long currentElements; + if (sizeOnly) { + currentElements = elementsUpdater.get(this); + } else if (delta > 0) { + currentElements = elementsUpdater.incrementAndGet(this); + } else { + currentElements = elementsUpdater.decrementAndGet(this); + } + + if (delta > 0) { + checkOver(currentElements, currentSize); + } else { // (delta < 0) + checkUnder(currentElements, 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)) { + // checking if we need to switch from OVER_SIZE to OVER_ELEMENTS, to avoid calling under needless + if (!(elementsEnabled && currentElements >= maxElements && changeFlag(FREE, OVER_ELEMENTS))) { + under(); + } + return; // we must return now as we already checked for the elements portion + } + } + + if (elementsEnabled) { + if (currentElements <= lowerMarkElements && changeFlag(OVER_ELEMENTS, FREE)) { + // checking if we need to switch from OVER_ELEMENTS to OVER_SIZE, to avoid calling under needless + if (!(sizeEnabled && currentSize >= maxSize && changeFlag(FREE, OVER_SIZE))) { + // this is checking the other side from size (elements). + // on this case we are just switching sides and we should not fire under(); + under(); + } + return; // this return here is moot I know. I am keeping it here for consistence with the size portion + // and in case eventually further checks are added on this method, this needs to be reviewed. + } + } Review comment: This stops it firing 'under' here if the _other_ threshold is still over when the first goes down again, yes. However, my point was around that it still sets the state to FREE first, so could something else happen that makes it realise the other threshhold is breached, which will set that as the state, and possibly fire _over_ again having never fired _under_. Though in retrospect I guess it will fire _under_ here though if it fails to _changeFlag(FREE, <other>)_. So it will fire, just maybe not when expected (could be after it fired _over_ in the other check). -- 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]
