-
Notifications
You must be signed in to change notification settings - Fork 26.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ISSUE #10020] add MemorySafeLinkedBlockingQueue #10021
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/MemoryLimitCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* 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.threadpool; | ||
|
||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* {@link java.lang.Runtime#freeMemory()} technology is used to calculate the | ||
* memory limit by using the percentage of the current maximum available memory, | ||
* which can be used with {@link MemoryLimiter}. | ||
* | ||
* @see MemoryLimiter | ||
* @see <a href="https://github.com/apache/incubator-shenyu/blob/master/shenyu-common/src/main/java/org/apache/shenyu/common/concurrent/MemoryLimitCalculator.java">MemoryLimitCalculator</a> | ||
*/ | ||
public class MemoryLimitCalculator { | ||
|
||
private static volatile long maxAvailable; | ||
|
||
private static final ScheduledExecutorService SCHEDULER = Executors.newSingleThreadScheduledExecutor(); | ||
|
||
static { | ||
// immediately refresh when this class is loaded to prevent maxAvailable from being 0 | ||
refresh(); | ||
// check every 50 ms to improve performance | ||
SCHEDULER.scheduleWithFixedDelay(MemoryLimitCalculator::refresh, 50, 50, TimeUnit.MILLISECONDS); | ||
Runtime.getRuntime().addShutdownHook(new Thread(SCHEDULER::shutdown)); | ||
} | ||
|
||
private static void refresh() { | ||
maxAvailable = Runtime.getRuntime().freeMemory(); | ||
} | ||
|
||
/** | ||
* Get the maximum available memory of the current JVM. | ||
* | ||
* @return maximum available memory | ||
*/ | ||
public static long maxAvailable() { | ||
return maxAvailable; | ||
} | ||
|
||
/** | ||
* Take the current JVM's maximum available memory | ||
* as a percentage of the result as the limit. | ||
* | ||
* @param percentage percentage | ||
* @return available memory | ||
*/ | ||
public static long calculate(final float percentage) { | ||
if (percentage <= 0 || percentage > 1) { | ||
throw new IllegalArgumentException(); | ||
} | ||
return (long) (maxAvailable() * percentage); | ||
} | ||
|
||
/** | ||
* By default, it takes 80% of the maximum available memory of the current JVM. | ||
* | ||
* @return available memory | ||
*/ | ||
public static long defaultLimit() { | ||
return (long) (maxAvailable() * 0.8); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...ommon/src/main/java/org/apache/dubbo/common/threadpool/MemorySafeLinkedBlockingQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* 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.threadpool; | ||
|
||
import java.util.Collection; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Can completely solve the OOM problem caused by {@link java.util.concurrent.LinkedBlockingQueue}, | ||
* does not depend on {@link java.lang.instrument.Instrumentation} and is easier to use than | ||
* {@link MemoryLimitedLinkedBlockingQueue}. | ||
* | ||
* @see <a href="https://github.com/apache/incubator-shenyu/blob/master/shenyu-common/src/main/java/org/apache/shenyu/common/concurrent/MemorySafeLinkedBlockingQueue.java">MemorySafeLinkedBlockingQueue</a> | ||
*/ | ||
public class MemorySafeLinkedBlockingQueue<E> extends LinkedBlockingQueue<E> { | ||
|
||
private static final long serialVersionUID = 8032578371739960142L; | ||
|
||
public static int THE_256_MB = 256 * 1024 * 1024; | ||
|
||
private int maxFreeMemory; | ||
|
||
public MemorySafeLinkedBlockingQueue() { | ||
this(THE_256_MB); | ||
} | ||
|
||
public MemorySafeLinkedBlockingQueue(final int maxFreeMemory) { | ||
super(Integer.MAX_VALUE); | ||
this.maxFreeMemory = maxFreeMemory; | ||
} | ||
|
||
public MemorySafeLinkedBlockingQueue(final Collection<? extends E> c, | ||
final int maxFreeMemory) { | ||
super(c); | ||
this.maxFreeMemory = maxFreeMemory; | ||
} | ||
|
||
/** | ||
* set the max free memory. | ||
* | ||
* @param maxFreeMemory the max free memory | ||
*/ | ||
public void setMaxFreeMemory(final int maxFreeMemory) { | ||
this.maxFreeMemory = maxFreeMemory; | ||
} | ||
|
||
/** | ||
* get the max free memory. | ||
* | ||
* @return the max free memory limit | ||
*/ | ||
public int getMaxFreeMemory() { | ||
return maxFreeMemory; | ||
} | ||
|
||
/** | ||
* determine if there is any remaining free memory. | ||
* | ||
* @return true if has free memory | ||
*/ | ||
public boolean hasRemainedMemory() { | ||
return MemoryLimitCalculator.maxAvailable() > maxFreeMemory; | ||
} | ||
|
||
@Override | ||
public void put(final E e) throws InterruptedException { | ||
if (hasRemainedMemory()) { | ||
super.put(e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean offer(final E e, final long timeout, final TimeUnit unit) throws InterruptedException { | ||
return hasRemainedMemory() && super.offer(e, timeout, unit); | ||
} | ||
|
||
@Override | ||
public boolean offer(final E e) { | ||
return hasRemainedMemory() && super.offer(e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...n/src/test/java/org/apache/dubbo/common/threadpool/MemorySafeLinkedBlockingQueueTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* 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.threadpool; | ||
|
||
import net.bytebuddy.agent.ByteBuddyAgent; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.lang.instrument.Instrumentation; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
public class MemorySafeLinkedBlockingQueueTest { | ||
@Test | ||
public void test() throws Exception { | ||
ByteBuddyAgent.install(); | ||
final Instrumentation instrumentation = ByteBuddyAgent.getInstrumentation(); | ||
final long objectSize = instrumentation.getObjectSize((Runnable) () -> { | ||
}); | ||
int maxFreeMemory = (int) MemoryLimitCalculator.maxAvailable(); | ||
MemorySafeLinkedBlockingQueue<Runnable> queue = new MemorySafeLinkedBlockingQueue<>(maxFreeMemory); | ||
// all memory is reserved for JVM, so it will fail here | ||
assertThat(queue.offer(() -> { | ||
}), is(false)); | ||
|
||
// maxFreeMemory-objectSize Byte memory is reserved for the JVM, so this will succeed | ||
queue.setMaxFreeMemory((int) (MemoryLimitCalculator.maxAvailable() - objectSize)); | ||
assertThat(queue.offer(() -> { | ||
}), is(true)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This scheduledExecutorService can be managed by Dubbo Framework. I will patch it later. #10178