Up dev issue#10388 (#10414)
* issue#10388 * issue#10388 * 格式化 * 格式化 * 回退对readme的修改 * change to queue Co-authored-by: keshi.ys <keshi.ys@alibaba-inc.com>
This commit is contained in:
parent
6b095f1c68
commit
9b6c463598
|
|
@ -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 !");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(() -> {
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue