gemmellr commented on a change in pull request #3983: URL: https://github.com/apache/activemq-artemis/pull/3983#discussion_r827924667
########## 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")); + return sizeUpdater.get(this); + } + + flagUpdater.compareAndSet(this, NOT_USED, FREE); + + if (parentSlots != null) { + for (SizeAwareMetric parent : parentSlots) { + if (parent != null) { + parent.addSize(delta, sizeOnly); + } + } + } + + IntConsumer[] slots = sizeOnly ? onSizeOnlySlots : onSizeSlots; + + if (slots != null) { + for (IntConsumer theFunction : slots) { + 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 lowerMark, long maxElements, long lowerMarkElements) { + this.maxSize = maxSize; + this.lowerMark = lowerMark; Review comment: lowerMarkSize for consistency with other args? ########## File path: docs/user-manual/en/paging.md ########## @@ -53,15 +53,15 @@ You can configure the location of the paging folder in `broker.xml`. ## Paging Mode As soon as messages delivered to an address exceed the configured size, -that address alone goes into page mode. If max-size-bytes == 0, an address -will immediately enter into page mode. +that address alone goes into page mode. If max-size-bytes == 0, or max-messages=0, an address +will always use paging to route messages. > **Note:** > -> Paging is done individually per address. If you configure a max-size-bytes +> Paging is done individually per address. If you configure a max-size-bytes or max-messages Review comment: max-messages is inconsistent with the config -- 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]
