-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add MemorySafeLinkedBlockingQueue #1213
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
Open
Codeprh
wants to merge
4
commits into
sofastack:master
Choose a base branch
from
Codeprh:feture/threadpool-plus
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 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
80 changes: 80 additions & 0 deletions
80
core/common/src/main/java/com/alipay/sofa/rpc/common/threadpool/MemoryLimitCalculator.java
This file contains hidden or 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,80 @@ | ||
/* | ||
* 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 com.alipay.sofa.rpc.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); | ||
} | ||
} |
269 changes: 269 additions & 0 deletions
269
core/common/src/main/java/com/alipay/sofa/rpc/common/threadpool/MemoryLimiter.java
This file contains hidden or 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,269 @@ | ||
/* | ||
* 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 com.alipay.sofa.rpc.common.threadpool; | ||
|
||
import java.lang.instrument.Instrumentation; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.LongAdder; | ||
import java.util.concurrent.locks.Condition; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
/** | ||
* memory limiter. | ||
*/ | ||
public class MemoryLimiter { | ||
EvenLjj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private final Instrumentation inst; | ||
|
||
private long memoryLimit; | ||
|
||
private final LongAdder memory = new LongAdder(); | ||
|
||
private final ReentrantLock acquireLock = new ReentrantLock(); | ||
|
||
private final Condition notLimited = acquireLock.newCondition(); | ||
|
||
private final ReentrantLock releaseLock = new ReentrantLock(); | ||
|
||
private final Condition notEmpty = releaseLock.newCondition(); | ||
|
||
public MemoryLimiter(Instrumentation inst) { | ||
this(Integer.MAX_VALUE, inst); | ||
} | ||
|
||
public MemoryLimiter(long memoryLimit, Instrumentation inst) { | ||
if (memoryLimit <= 0) { | ||
throw new IllegalArgumentException(); | ||
} | ||
this.memoryLimit = memoryLimit; | ||
this.inst = inst; | ||
} | ||
|
||
public void setMemoryLimit(long memoryLimit) { | ||
if (memoryLimit <= 0) { | ||
throw new IllegalArgumentException(); | ||
} | ||
this.memoryLimit = memoryLimit; | ||
} | ||
|
||
public long getMemoryLimit() { | ||
return memoryLimit; | ||
} | ||
|
||
public long getCurrentMemory() { | ||
return memory.sum(); | ||
} | ||
|
||
public long getCurrentRemainMemory() { | ||
return getMemoryLimit() - getCurrentMemory(); | ||
} | ||
|
||
private void signalNotEmpty() { | ||
releaseLock.lock(); | ||
try { | ||
notEmpty.signal(); | ||
} finally { | ||
releaseLock.unlock(); | ||
} | ||
} | ||
|
||
private void signalNotLimited() { | ||
acquireLock.lock(); | ||
try { | ||
notLimited.signal(); | ||
} finally { | ||
acquireLock.unlock(); | ||
} | ||
} | ||
|
||
/** | ||
* Locks to prevent both acquires and releases. | ||
*/ | ||
private void fullyLock() { | ||
acquireLock.lock(); | ||
releaseLock.lock(); | ||
} | ||
|
||
/** | ||
* Unlocks to allow both acquires and releases. | ||
*/ | ||
private void fullyUnlock() { | ||
releaseLock.unlock(); | ||
acquireLock.unlock(); | ||
} | ||
|
||
public boolean acquire(Object e) { | ||
if (e == null) { | ||
throw new NullPointerException(); | ||
} | ||
if (memory.sum() >= memoryLimit) { | ||
return false; | ||
} | ||
acquireLock.lock(); | ||
try { | ||
final long sum = memory.sum(); | ||
final long objectSize = inst.getObjectSize(e); | ||
if (sum + objectSize >= memoryLimit) { | ||
return false; | ||
} | ||
memory.add(objectSize); | ||
// see https://github.com/apache/incubator-shenyu/pull/3356 | ||
if (memory.sum() < memoryLimit) { | ||
notLimited.signal(); | ||
} | ||
} finally { | ||
acquireLock.unlock(); | ||
} | ||
if (memory.sum() > 0) { | ||
signalNotEmpty(); | ||
} | ||
return true; | ||
} | ||
|
||
public void acquireInterruptibly(Object e) throws InterruptedException { | ||
if (e == null) { | ||
throw new NullPointerException(); | ||
} | ||
acquireLock.lockInterruptibly(); | ||
try { | ||
final long objectSize = inst.getObjectSize(e); | ||
// see https://github.com/apache/incubator-shenyu/pull/3335 | ||
while (memory.sum() + objectSize >= memoryLimit) { | ||
notLimited.await(); | ||
} | ||
memory.add(objectSize); | ||
if (memory.sum() < memoryLimit) { | ||
notLimited.signal(); | ||
} | ||
} finally { | ||
acquireLock.unlock(); | ||
} | ||
if (memory.sum() > 0) { | ||
signalNotEmpty(); | ||
} | ||
} | ||
|
||
public boolean acquire(Object e, long timeout, TimeUnit unit) throws InterruptedException { | ||
if (e == null) { | ||
throw new NullPointerException(); | ||
} | ||
long nanos = unit.toNanos(timeout); | ||
acquireLock.lockInterruptibly(); | ||
try { | ||
final long objectSize = inst.getObjectSize(e); | ||
while (memory.sum() + objectSize >= memoryLimit) { | ||
if (nanos <= 0) { | ||
return false; | ||
} | ||
nanos = notLimited.awaitNanos(nanos); | ||
} | ||
memory.add(objectSize); | ||
if (memory.sum() < memoryLimit) { | ||
notLimited.signal(); | ||
} | ||
} finally { | ||
acquireLock.unlock(); | ||
} | ||
if (memory.sum() > 0) { | ||
signalNotEmpty(); | ||
} | ||
return true; | ||
} | ||
|
||
public void release(Object e) { | ||
if (null == e) { | ||
return; | ||
} | ||
if (memory.sum() == 0) { | ||
return; | ||
} | ||
releaseLock.lock(); | ||
try { | ||
final long objectSize = inst.getObjectSize(e); | ||
if (memory.sum() > 0) { | ||
memory.add(-objectSize); | ||
if (memory.sum() > 0) { | ||
notEmpty.signal(); | ||
} | ||
} | ||
} finally { | ||
releaseLock.unlock(); | ||
} | ||
if (memory.sum() < memoryLimit) { | ||
signalNotLimited(); | ||
} | ||
} | ||
|
||
public void releaseInterruptibly(Object e) throws InterruptedException { | ||
if (null == e) { | ||
return; | ||
} | ||
releaseLock.lockInterruptibly(); | ||
try { | ||
final long objectSize = inst.getObjectSize(e); | ||
while (memory.sum() == 0) { | ||
notEmpty.await(); | ||
} | ||
memory.add(-objectSize); | ||
if (memory.sum() > 0) { | ||
notEmpty.signal(); | ||
} | ||
} finally { | ||
releaseLock.unlock(); | ||
} | ||
if (memory.sum() < memoryLimit) { | ||
signalNotLimited(); | ||
} | ||
} | ||
|
||
public void releaseInterruptibly(Object e, long timeout, TimeUnit unit) throws InterruptedException { | ||
if (null == e) { | ||
return; | ||
} | ||
long nanos = unit.toNanos(timeout); | ||
releaseLock.lockInterruptibly(); | ||
try { | ||
final long objectSize = inst.getObjectSize(e); | ||
while (memory.sum() == 0) { | ||
if (nanos <= 0) { | ||
return; | ||
} | ||
nanos = notEmpty.awaitNanos(nanos); | ||
} | ||
memory.add(-objectSize); | ||
if (memory.sum() > 0) { | ||
notEmpty.signal(); | ||
} | ||
} finally { | ||
releaseLock.unlock(); | ||
} | ||
if (memory.sum() < memoryLimit) { | ||
signalNotLimited(); | ||
} | ||
} | ||
|
||
public void clear() { | ||
fullyLock(); | ||
try { | ||
if (memory.sumThenReset() < memoryLimit) { | ||
notLimited.signal(); | ||
} | ||
} finally { | ||
fullyUnlock(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.