This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new 9b6c463598 Up dev issue#10388 (#10414)
9b6c463598 is described below

commit 9b6c463598474e40bf0aec89e9a1c38ae14f83dd
Author: yshuoo <[email protected]>
AuthorDate: Mon Aug 15 09:58:56 2022 +0800

    Up dev issue#10388 (#10414)
    
    * issue#10388
    
    * issue#10388
    
    * 格式化
    
    * 格式化
    
    * 回退对readme的修改
    
    * change to queue
    
    Co-authored-by: keshi.ys <[email protected]>
---
 .../dubbo/common/concurrent/AbortPolicy.java       | 32 +++++++++++
 .../common/concurrent/DiscardOldestPolicy.java     | 31 +++++++++++
 .../dubbo/common/concurrent/DiscardPolicy.java     | 31 +++++++++++
 .../dubbo/common/concurrent/RejectException.java   | 64 ++++++++++++++++++++++
 .../apache/dubbo/common/concurrent/Rejector.java   | 43 +++++++++++++++
 .../threadpool/MemorySafeLinkedBlockingQueue.java  | 31 ++++++++++-
 .../MemorySafeLinkedBlockingQueueTest.java         | 11 ++++
 7 files changed, 241 insertions(+), 2 deletions(-)

diff --git 
a/dubbo-common/src/main/java/org/apache/dubbo/common/concurrent/AbortPolicy.java
 
b/dubbo-common/src/main/java/org/apache/dubbo/common/concurrent/AbortPolicy.java
new file mode 100644
index 0000000000..602ba6470d
--- /dev/null
+++ 
b/dubbo-common/src/main/java/org/apache/dubbo/common/concurrent/AbortPolicy.java
@@ -0,0 +1,32 @@
+/*
+ * 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.dubbo.common.concurrent;
+
+import java.util.Queue;
+
+/**
+ * A handler for rejected element that throws a {@code RejectException}.
+ */
+public class AbortPolicy<E> implements Rejector<E> {
+
+    @Override
+    public void reject(final E e, final Queue<E> queue) {
+        throw new RejectException("no more memory can be used !");
+    }
+
+}
diff --git 
a/dubbo-common/src/main/java/org/apache/dubbo/common/concurrent/DiscardOldestPolicy.java
 
b/dubbo-common/src/main/java/org/apache/dubbo/common/concurrent/DiscardOldestPolicy.java
new file mode 100644
index 0000000000..26b5eaf391
--- /dev/null
+++ 
b/dubbo-common/src/main/java/org/apache/dubbo/common/concurrent/DiscardOldestPolicy.java
@@ -0,0 +1,31 @@
+/*
+ * 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.dubbo.common.concurrent;
+
+import java.util.Queue;
+
+/**
+ * A handler for rejected element that discards the oldest element.
+ */
+public class DiscardOldestPolicy<E> implements Rejector<E> {
+
+    @Override
+    public void reject(final E e, final Queue<E> queue) {
+        queue.poll();
+        queue.offer(e);
+    }
+}
diff --git 
a/dubbo-common/src/main/java/org/apache/dubbo/common/concurrent/DiscardPolicy.java
 
b/dubbo-common/src/main/java/org/apache/dubbo/common/concurrent/DiscardPolicy.java
new file mode 100644
index 0000000000..4830b2aefd
--- /dev/null
+++ 
b/dubbo-common/src/main/java/org/apache/dubbo/common/concurrent/DiscardPolicy.java
@@ -0,0 +1,31 @@
+/*
+ * 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.dubbo.common.concurrent;
+
+
+import java.util.Queue;
+
+/**
+ * A handler for rejected element that silently discards the rejected element.
+ */
+public class DiscardPolicy<E> implements Rejector<E> {
+
+    @Override
+    public void reject(final E e, final Queue<E> queue) {
+
+    }
+}
diff --git 
a/dubbo-common/src/main/java/org/apache/dubbo/common/concurrent/RejectException.java
 
b/dubbo-common/src/main/java/org/apache/dubbo/common/concurrent/RejectException.java
new file mode 100644
index 0000000000..1b7537b131
--- /dev/null
+++ 
b/dubbo-common/src/main/java/org/apache/dubbo/common/concurrent/RejectException.java
@@ -0,0 +1,64 @@
+/*
+ * 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.dubbo.common.concurrent;
+
+import org.apache.dubbo.common.threadpool.MemorySafeLinkedBlockingQueue;
+
+/**
+ * Exception thrown by an {@link MemorySafeLinkedBlockingQueue} when a element 
cannot be accepted.
+ */
+public class RejectException extends RuntimeException {
+
+    private static final long serialVersionUID = -3240015871717170195L;
+
+    /**
+     * Constructs a {@code RejectException} with no detail message. The cause 
is not initialized, and may subsequently be initialized by a
+     * call to {@link #initCause(Throwable) initCause}.
+     */
+    public RejectException() {
+    }
+
+    /**
+     * Constructs a {@code RejectException} with the specified detail message. 
The cause is not initialized, and may subsequently be
+     * initialized by a call to {@link #initCause(Throwable) initCause}.
+     *
+     * @param message the detail message
+     */
+    public RejectException(final String message) {
+        super(message);
+    }
+
+    /**
+     * Constructs a {@code RejectException} with the specified detail message 
and cause.
+     *
+     * @param message the detail message
+     * @param cause   the cause (which is saved for later retrieval by the 
{@link #getCause()} method)
+     */
+    public RejectException(final String message, final Throwable cause) {
+        super(message, cause);
+    }
+
+    /**
+     * Constructs a {@code RejectException} with the specified cause.  The 
detail message is set to {@code (cause == null ? null :
+     * cause.toString())} (which typically contains the class and detail 
message of {@code cause}).
+     *
+     * @param cause the cause (which is saved for later retrieval by the 
{@link #getCause()} method)
+     */
+    public RejectException(final Throwable cause) {
+        super(cause);
+    }
+}
diff --git 
a/dubbo-common/src/main/java/org/apache/dubbo/common/concurrent/Rejector.java 
b/dubbo-common/src/main/java/org/apache/dubbo/common/concurrent/Rejector.java
new file mode 100644
index 0000000000..1967f43821
--- /dev/null
+++ 
b/dubbo-common/src/main/java/org/apache/dubbo/common/concurrent/Rejector.java
@@ -0,0 +1,43 @@
+/*
+ * 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.dubbo.common.concurrent;
+
+import java.util.Queue;
+
+/**
+ * RejectHandler, it works when you need to custom reject action.
+ *
+ * @see AbortPolicy
+ * @see DiscardPolicy
+ * @see DiscardOldestPolicy
+ */
+public interface Rejector<E> {
+
+    /**
+     * Method that may be invoked by a Queue when Queue has remained memory
+     * return true. This may occur when no more memory are available because 
their bounds would be exceeded.
+     *
+     * <p>In the absence of other alternatives, the method may throw an 
unchecked
+     * {@link RejectException}, which will be propagated to the caller.
+     *
+     * @param e     the element requested to be added
+     * @param queue the queue attempting to add this element
+     *
+     * @throws RejectException if there is no more memory
+     */
+    void reject(E e, Queue<E> queue);
+}
diff --git 
a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/MemorySafeLinkedBlockingQueue.java
 
b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/MemorySafeLinkedBlockingQueue.java
index aae67b618c..3c1ae2482c 100644
--- 
a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/MemorySafeLinkedBlockingQueue.java
+++ 
b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/MemorySafeLinkedBlockingQueue.java
@@ -17,6 +17,9 @@
 
 package org.apache.dubbo.common.threadpool;
 
+import org.apache.dubbo.common.concurrent.DiscardPolicy;
+import org.apache.dubbo.common.concurrent.Rejector;
+
 import java.util.Collection;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.TimeUnit;
@@ -36,6 +39,8 @@ public class MemorySafeLinkedBlockingQueue<E> extends 
LinkedBlockingQueue<E> {
 
     private int maxFreeMemory;
 
+    private Rejector<E> rejector;
+
     public MemorySafeLinkedBlockingQueue() {
         this(THE_256_MB);
     }
@@ -43,12 +48,16 @@ public class MemorySafeLinkedBlockingQueue<E> extends 
LinkedBlockingQueue<E> {
     public MemorySafeLinkedBlockingQueue(final int maxFreeMemory) {
         super(Integer.MAX_VALUE);
         this.maxFreeMemory = maxFreeMemory;
+        //default as DiscardPolicy to ensure compatibility with the old version
+        this.rejector = new DiscardPolicy<>();
     }
 
     public MemorySafeLinkedBlockingQueue(final Collection<? extends E> c,
                                          final int maxFreeMemory) {
         super(c);
         this.maxFreeMemory = maxFreeMemory;
+        //default as DiscardPolicy to ensure compatibility with the old version
+        this.rejector = new DiscardPolicy<>();
     }
 
     /**
@@ -69,6 +78,15 @@ public class MemorySafeLinkedBlockingQueue<E> extends 
LinkedBlockingQueue<E> {
         return maxFreeMemory;
     }
 
+    /**
+     * set the rejector.
+     *
+     * @param rejector the rejector
+     */
+    public void setRejector(final Rejector<E> rejector) {
+        this.rejector = rejector;
+    }
+
     /**
      * determine if there is any remaining free memory.
      *
@@ -83,15 +101,24 @@ public class MemorySafeLinkedBlockingQueue<E> extends 
LinkedBlockingQueue<E> {
         if (hasRemainedMemory()) {
             super.put(e);
         }
+        rejector.reject(e, this);
     }
 
     @Override
     public boolean offer(final E e, final long timeout, final TimeUnit unit) 
throws InterruptedException {
-        return hasRemainedMemory() && super.offer(e, timeout, unit);
+        if (!hasRemainedMemory()) {
+            rejector.reject(e, this);
+            return false;
+        }
+        return super.offer(e, timeout, unit);
     }
 
     @Override
     public boolean offer(final E e) {
-        return hasRemainedMemory() && super.offer(e);
+        if (!hasRemainedMemory()) {
+            rejector.reject(e, this);
+            return false;
+        }
+        return super.offer(e);
     }
 }
diff --git 
a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/MemorySafeLinkedBlockingQueueTest.java
 
b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/MemorySafeLinkedBlockingQueueTest.java
index dd7f095b88..cb8b31b021 100644
--- 
a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/MemorySafeLinkedBlockingQueueTest.java
+++ 
b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/MemorySafeLinkedBlockingQueueTest.java
@@ -18,12 +18,15 @@
 package org.apache.dubbo.common.threadpool;
 
 import net.bytebuddy.agent.ByteBuddyAgent;
+import org.apache.dubbo.common.concurrent.AbortPolicy;
+import org.apache.dubbo.common.concurrent.RejectException;
 import org.junit.jupiter.api.Test;
 
 import java.lang.instrument.Instrumentation;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 public class MemorySafeLinkedBlockingQueueTest {
     @Test
@@ -43,4 +46,12 @@ public class MemorySafeLinkedBlockingQueueTest {
         assertThat(queue.offer(() -> {
         }), is(true));
     }
+
+    @Test
+    public void testCustomReject() throws Exception {
+        MemorySafeLinkedBlockingQueue<Runnable> queue = new 
MemorySafeLinkedBlockingQueue<>(Integer.MAX_VALUE);
+        queue.setRejector(new AbortPolicy<>());
+        assertThrows(RejectException.class, () -> queue.offer(() -> {
+        }));
+    }
 }

Reply via email to