franz1981 commented on a change in pull request #3876:
URL: https://github.com/apache/activemq-artemis/pull/3876#discussion_r766331944



##########
File path: 
artemis-protocols/artemis-openwire-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/openwire/OpenWireConnection.java
##########
@@ -765,11 +810,19 @@ public AMQConnectionContext initContext(ConnectionInfo 
info) throws Exception {
       createInternalSession(info);
 
       // the actor can only be used after the WireFormat has been initialized 
with versioning
-      this.openWireActor = new Actor<>(executor, this::act);
+      this.openWireActor = new BoundActor<>(executor, this::act, maxActorSize, 
this::getSize, this::disableAutoRead, this::flushedActor);
 
       return context;
    }
 
+   private int getSize(Command command) {

Review comment:
       ```suggestion
      private static int getSize(Command command) {
   ```

##########
File path: 
artemis-commons/src/main/java/org/apache/activemq/artemis/utils/actors/BoundActor.java
##########
@@ -0,0 +1,79 @@
+/**
+ * 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.actors;
+
+import java.util.concurrent.Executor;
+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
+import java.util.function.ToIntFunction;
+
+public class BoundActor<T> extends ProcessorBase<Object> {
+
+   private static final AtomicIntegerFieldUpdater<BoundActor> SIZE_UPDATER = 
AtomicIntegerFieldUpdater.newUpdater(BoundActor.class, "size");
+   private volatile int size = 0;
+
+   private static final AtomicIntegerFieldUpdater<BoundActor> 
SCHEDULED_FUSH_UPDATER = AtomicIntegerFieldUpdater.newUpdater(BoundActor.class, 
"scheduledFlush");
+   private volatile int scheduledFlush = 0;
+
+   private static final Object FLUSH = new Object();
+
+
+   final int maxSize;
+   final ToIntFunction<T> sizeGetter;

Review comment:
       `private final` on the members
   

##########
File path: 
artemis-commons/src/main/java/org/apache/activemq/artemis/utils/actors/BoundActor.java
##########
@@ -0,0 +1,79 @@
+/**
+ * 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.actors;
+
+import java.util.concurrent.Executor;
+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
+import java.util.function.ToIntFunction;
+
+public class BoundActor<T> extends ProcessorBase<Object> {
+
+   private static final AtomicIntegerFieldUpdater<BoundActor> SIZE_UPDATER = 
AtomicIntegerFieldUpdater.newUpdater(BoundActor.class, "size");
+   private volatile int size = 0;
+
+   private static final AtomicIntegerFieldUpdater<BoundActor> 
SCHEDULED_FUSH_UPDATER = AtomicIntegerFieldUpdater.newUpdater(BoundActor.class, 
"scheduledFlush");
+   private volatile int scheduledFlush = 0;
+
+   private static final Object FLUSH = new Object();
+
+
+   final int maxSize;
+   final ToIntFunction<T> sizeGetter;
+   ActorListener<T> listener;
+
+   Runnable overLimit;
+   Runnable clearLimit;
+
+   public BoundActor(Executor parent, ActorListener<T> listener, int maxSize, 
ToIntFunction<T> sizeGetter, Runnable overLimit, Runnable clearLimit) {
+      super(parent);
+      this.listener = listener;
+      this.maxSize = maxSize;
+      this.sizeGetter = sizeGetter;
+      this.overLimit = overLimit;
+      this.clearLimit = clearLimit;
+   }
+
+   @Override
+   protected final void doTask(Object task) {
+      if (task == FLUSH) {
+         this.scheduledFlush = 0;

Review comment:
       `scheduledFlush` must be `0` after `clearLimit::run` or a racy `act` 
after this would call `overLimit::run` before it

##########
File path: 
artemis-protocols/artemis-openwire-protocol/src/main/java/org/apache/activemq/artemis/core/protocol/openwire/OpenWireConnection.java
##########
@@ -765,11 +810,19 @@ public AMQConnectionContext initContext(ConnectionInfo 
info) throws Exception {
       createInternalSession(info);
 
       // the actor can only be used after the WireFormat has been initialized 
with versioning
-      this.openWireActor = new Actor<>(executor, this::act);
+      this.openWireActor = new BoundActor<>(executor, this::act, maxActorSize, 
this::getSize, this::disableAutoRead, this::flushedActor);
 
       return context;
    }
 
+   private int getSize(Command command) {
+      if (command instanceof ActiveMQMessage) {
+         return ((ActiveMQMessage) command).getSize();
+      } else {
+         return 1024; // we return 1k for anything else

Review comment:
       Pack it into some static final constant with a meaningful name

##########
File path: 
artemis-commons/src/main/java/org/apache/activemq/artemis/utils/actors/BoundActor.java
##########
@@ -0,0 +1,79 @@
+/**
+ * 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.actors;
+
+import java.util.concurrent.Executor;
+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
+import java.util.function.ToIntFunction;
+
+public class BoundActor<T> extends ProcessorBase<Object> {
+
+   private static final AtomicIntegerFieldUpdater<BoundActor> SIZE_UPDATER = 
AtomicIntegerFieldUpdater.newUpdater(BoundActor.class, "size");
+   private volatile int size = 0;
+
+   private static final AtomicIntegerFieldUpdater<BoundActor> 
SCHEDULED_FUSH_UPDATER = AtomicIntegerFieldUpdater.newUpdater(BoundActor.class, 
"scheduledFlush");
+   private volatile int scheduledFlush = 0;
+
+   private static final Object FLUSH = new Object();
+
+
+   final int maxSize;
+   final ToIntFunction<T> sizeGetter;
+   ActorListener<T> listener;
+
+   Runnable overLimit;
+   Runnable clearLimit;
+
+   public BoundActor(Executor parent, ActorListener<T> listener, int maxSize, 
ToIntFunction<T> sizeGetter, Runnable overLimit, Runnable clearLimit) {
+      super(parent);
+      this.listener = listener;
+      this.maxSize = maxSize;
+      this.sizeGetter = sizeGetter;
+      this.overLimit = overLimit;
+      this.clearLimit = clearLimit;
+   }
+
+   @Override
+   protected final void doTask(Object task) {
+      if (task == FLUSH) {
+         this.scheduledFlush = 0;
+         clearLimit.run();
+         return;
+      }
+      try {
+         listener.onMessage((T)task);
+      } finally {
+         SIZE_UPDATER.getAndAdd(this, -sizeGetter.applyAsInt((T)task));
+      }
+   }
+
+   public void act(T message) {
+      int size = SIZE_UPDATER.addAndGet(this, 
(sizeGetter.applyAsInt((T)message)));

Review comment:
       `sizeGetter.applyAsInt((T)message)` should check if the estimated size 
is > 0




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