gemmellr commented on a change in pull request #3983: URL: https://github.com/apache/activemq-artemis/pull/3983#discussion_r829174919
########## 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: Part of the reason I suggested it is, its all inherently non-thread-safe without outside coordination, and then once I realised it wasnt really being used, it just further seemed like it would be better to only have whats required. ########## 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: Having internal detail is fine. The main point was really the other bit though, its generally a bad idea for non-test code to be just suddenly returning objects only intended for [certain] tests. At some point something will unexpectedly get one of those instead of the real thing, when theres no reason they really should know the behaviour is there. Better for tests that need to to know and apply the test logic they need. ########## 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); Review comment: Could use the changeFlag method to make flagUpdater usage consistently go through 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; Review comment: Should grab the value once by assignment and then compare both legs to the same value, otherwise this is racey and could give the wrong answer, e.g it would wrongly say isOver is true if it changes between lookups from 'NOT_USED' to 'FREE', amongst others. Also wouldnt it be clearer just to say "value == OVER_SIZE || value == OVER_ELEMENTS" Or actually, better yet, "value > FREE" would work with one retrieval and not needing the assignment. ########## 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: Duplicate check ########## 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: Could you simplify and just have an initial if that did 'if(sizeOnly) -> get or inc+get elements' and then another that did the delta checks? It would be close to half the size and possibly more obvious. ########## 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: Given these only looks to be used to call an equivalent method on anther object, could they be combined into one callback and simply pass the boolean as well? Would need to use a custom callback interface though (or a BiConsumer with boxing). Not really a hardship given it would probably just be supplied with a lambda these days and never actually implemented. ########## 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: Seems unnecessary to both have public methods with the boolean and 'alternates' without the 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) { + 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: TLDR: Seems like it needs to account for the other value as well before simply firing 'under' whenever one of them goes under, or it might fire it incorrectly. What if both go over but a later change only brings one of them under? Seems like it cant currently track that properly and so could actually fire the callback early and leave itself in the wrong state. E.g whilst both can be over, the flag state only allows tracking that the size or the elements are over, so will only remember one of those (whichever flag is set first). Just because that remembered one goes under later, doesnt necessarily mean the other one is also under at that point, but currently the flag would just get reset straight to 'free' and the under callback would be fired, both possibly incorrectly. ########## 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: It would be a fine name if it didnt as easily suggest totally different things hehe. If not trying to contort to 'max-size-<foo>' for alignment to the other config, the most obvious thing for me would be just 'max-message-count'. ########## 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 dont see how the other check is moot if its current value at the time is in breach of the threshold that would make _it_ be considered 'over', and this instead would set the flag to FREE and fire 'under' because the one that first hit over has gone under its threshold. If thats expected then I simply dont understand the intent. ########## 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: yeah, I had completely forgot that one was inc and the other was dec so it would obviously need 3 rather than 2 legs -- 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]
