-
Notifications
You must be signed in to change notification settings - Fork 143
IGNITE-28305 Add backpressure for partition operations #7950
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
EgorKuts
wants to merge
7
commits into
apache:main
Choose a base branch
from
EgorKuts:ignite-28305
base: main
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 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dd995fd
ignite-28305 backpressure to limit in flight partition operations per…
a943228
ignite-28305 review
f854009
ignite-28305 review
d8c2ab8
ignite-28305 review
9f7fd9f
ignite-28305 review
5521601
ignite-28305 review
48a8a57
ignite-28305 review
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
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
33 changes: 33 additions & 0 deletions
33
modules/core/src/main/java/org/apache/ignite/internal/lang/ReplicaOverloadedException.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,33 @@ | ||
| /* | ||
| * 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.ignite.internal.lang; | ||
|
|
||
| import static org.apache.ignite.lang.ErrorGroups.Replicator.REPLICA_OVERLOADED_ERR; | ||
|
|
||
| /** | ||
| * Thrown when the node has reached the in-flight partition operation byte limit | ||
| * ({@code replication.partitionOperationHeapUsagePercent}) and cannot accept new requests. | ||
| */ | ||
| public class ReplicaOverloadedException extends IgniteInternalException { | ||
| private static final long serialVersionUID = -6023736883539658779L; | ||
|
|
||
| /** Constructor. */ | ||
| public ReplicaOverloadedException() { | ||
| super(REPLICA_OVERLOADED_ERR, "Node is overloaded: in-flight partition operation byte limit reached."); | ||
| } | ||
| } |
134 changes: 134 additions & 0 deletions
134
...core/src/main/java/org/apache/ignite/internal/util/PartitionOperationInflightLimiter.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,134 @@ | ||
| /* | ||
| * 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.ignite.internal.util; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import java.util.function.IntSupplier; | ||
| import org.apache.ignite.internal.hlc.HybridClockImpl; | ||
| import org.apache.ignite.internal.logger.IgniteLogger; | ||
| import org.apache.ignite.internal.logger.Loggers; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| /** | ||
| * Limits the total in-flight bytes of partition operations (queued or executing) across the replica manager and thin-client connector. | ||
| * | ||
| * <p>The byte limit is computed as a percentage of the JVM heap ({@code Runtime.getRuntime().maxMemory()}). | ||
| * When the heap percentage is zero or less, all operations are permitted unconditionally. | ||
| * | ||
| * <p>{@link #tryAcquire(int)} returns {@code false} once adding {@code messageBytes} would exceed the limit. | ||
| * A permit must be released via {@link #release(int)} when the operation completes. | ||
| */ | ||
| public class PartitionOperationInflightLimiter { | ||
|
|
||
| private final IgniteLogger log = Loggers.forClass(HybridClockImpl.class); | ||
|
|
||
| /** Byte limit computed from heap percentage; {@code 0} means unlimited. */ | ||
| private volatile long byteLimit; | ||
|
|
||
| private final @Nullable IntSupplier heapPercentSupplier; | ||
|
|
||
| private volatile boolean initialized; | ||
|
|
||
| /** Running total of in-flight bytes. */ | ||
| private final AtomicLong inFlightBytes = new AtomicLong(); | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
| * @param heapPercent Percentage of max JVM heap to use as the in-flight byte limit. Zero or negative disables the limit. | ||
| */ | ||
| public PartitionOperationInflightLimiter(int heapPercent) { | ||
| this.byteLimit = computeByteLimit(heapPercent); | ||
| this.heapPercentSupplier = null; | ||
| this.initialized = true; | ||
| } | ||
|
|
||
| /** | ||
| * Constructor with a lazy supplier of the heap percentage. | ||
| * | ||
| * @param heapPercentSupplier Supplier of heap percentage (0 or less disables the limit). Called at most once, on first use. | ||
| */ | ||
| public PartitionOperationInflightLimiter(@Nullable IntSupplier heapPercentSupplier) { | ||
| this.heapPercentSupplier = heapPercentSupplier; | ||
| this.initialized = false; | ||
| } | ||
|
|
||
| /** | ||
| * Attempts to reserve {@code messageBytes} in-flight bytes. | ||
| * | ||
| * @param messageBytes Number of bytes to reserve. | ||
| * @return {@code true} if the reservation was made or the limit is disabled; {@code false} if adding the bytes would exceed the limit. | ||
| */ | ||
| public boolean tryAcquire(int messageBytes) { | ||
| long limit = resolvedByteLimit(); | ||
|
|
||
| if (limit <= 0) { | ||
| return true; | ||
| } | ||
|
|
||
| while (true) { | ||
| long current = inFlightBytes.get(); | ||
|
|
||
| if (current + messageBytes > limit) { | ||
| log.error("node is overloaded, cannot permit partition operation requiring {} bytes", messageBytes); | ||
|
EgorKuts marked this conversation as resolved.
Outdated
|
||
| return false; | ||
| } | ||
|
|
||
| if (inFlightBytes.compareAndSet(current, current + messageBytes)) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Releases previously reserved in-flight bytes. | ||
| * Must only be called after a successful {@link #tryAcquire(int)}. | ||
| * | ||
| * @param messageBytes Number of bytes to release. | ||
| */ | ||
| public void release(int messageBytes) { | ||
| long limit = resolvedByteLimit(); | ||
|
|
||
| if (limit > 0) { | ||
| inFlightBytes.addAndGet(-messageBytes); | ||
| } | ||
| } | ||
|
|
||
| private long resolvedByteLimit() { | ||
| if (initialized) { | ||
| return byteLimit; | ||
| } | ||
| synchronized (this) { | ||
| if (initialized) { | ||
| return byteLimit; | ||
| } | ||
| if (heapPercentSupplier != null) { | ||
| byteLimit = computeByteLimit(heapPercentSupplier.getAsInt()); | ||
| } | ||
| initialized = true; | ||
| } | ||
| return byteLimit; | ||
| } | ||
|
|
||
| private static long computeByteLimit(int heapPercent) { | ||
| if (heapPercent <= 0) { | ||
| return 0; | ||
| } | ||
| return (long) (heapPercent / 100.0 * Runtime.getRuntime().maxMemory()); | ||
| } | ||
| } | ||
143 changes: 143 additions & 0 deletions
143
.../src/test/java/org/apache/ignite/internal/util/PartitionOperationInFlightLimiterTest.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,143 @@ | ||
| /* | ||
| * 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.ignite.internal.util; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class PartitionOperationInFlightLimiterTest { | ||
| private static final long MAX_MEMORY = Runtime.getRuntime().maxMemory(); | ||
|
|
||
| @Test | ||
| void zeroHeapPercentAlwaysPermits() { | ||
| var limiter = new PartitionOperationInflightLimiter(0); | ||
|
|
||
| for (int i = 0; i < 100; i++) { | ||
| assertTrue(limiter.tryAcquire(1000)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void negativeHeapPercentAlwaysPermits() { | ||
| var limiter = new PartitionOperationInflightLimiter(-1); | ||
|
|
||
| for (int i = 0; i < 100; i++) { | ||
| assertTrue(limiter.tryAcquire(1000)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void acquireFailsWhenByteLimitExceeded() { | ||
| // Use 10% heap limit. | ||
| var limiter = new PartitionOperationInflightLimiter(10); | ||
| long limit = (long) (0.10 * MAX_MEMORY); | ||
|
|
||
| // A single chunk that exceeds the limit should be rejected. | ||
| assertFalse(limiter.tryAcquire((int) Math.min(limit + 1, Integer.MAX_VALUE))); | ||
| } | ||
|
|
||
| @Test | ||
| void acquireSucceedsUpToLimit() { | ||
| var limiter = new PartitionOperationInflightLimiter(10); | ||
| long limit = (long) (0.10 * MAX_MEMORY); | ||
|
|
||
| // Chunk size that fits within the limit. | ||
| int chunkBytes = (int) Math.min(limit / 2, Integer.MAX_VALUE / 2); | ||
|
|
||
| assertTrue(limiter.tryAcquire(chunkBytes)); | ||
| assertTrue(limiter.tryAcquire(chunkBytes)); | ||
| } | ||
|
|
||
| @Test | ||
| void releaseRestoresBudget() { | ||
| var limiter = new PartitionOperationInflightLimiter(10); | ||
| long limit = (long) (0.10 * MAX_MEMORY); | ||
| int chunkBytes = (int) Math.min(limit / 2, Integer.MAX_VALUE / 2); | ||
|
|
||
| assertTrue(limiter.tryAcquire(chunkBytes)); | ||
| assertTrue(limiter.tryAcquire(chunkBytes)); | ||
| // Now at or near limit; another chunk should fail. | ||
| assertFalse(limiter.tryAcquire(chunkBytes)); | ||
|
|
||
| limiter.release(chunkBytes); | ||
|
|
||
| assertTrue(limiter.tryAcquire(chunkBytes)); | ||
| } | ||
|
|
||
| @Test | ||
| void releaseOnZeroLimitIsNoOp() { | ||
| var limiter = new PartitionOperationInflightLimiter(0); | ||
|
|
||
| // Should not throw. | ||
| limiter.release(1000); | ||
|
|
||
| assertTrue(limiter.tryAcquire(1000)); | ||
| } | ||
|
|
||
| @Test | ||
| void supplierConstructorInitializesLazily() { | ||
| int[] callCount = {0}; | ||
|
|
||
| // 100% heap — effectively unlimited for this test. | ||
| var limiter = new PartitionOperationInflightLimiter(() -> { | ||
| callCount[0]++; | ||
| return 100; | ||
| }); | ||
|
|
||
| assertTrue(callCount[0] == 0, "supplier should not be called at construction time"); | ||
|
|
||
| assertTrue(limiter.tryAcquire(1)); | ||
| assertTrue(callCount[0] == 1, "supplier should be called exactly once"); | ||
|
|
||
| assertTrue(limiter.tryAcquire(1)); | ||
| assertTrue(callCount[0] == 1, "supplier should not be called again"); | ||
| } | ||
|
|
||
| @Test | ||
| void supplierConstructorWithZeroPercentAlwaysPermits() { | ||
| var limiter = new PartitionOperationInflightLimiter(() -> 0); | ||
|
|
||
| for (int i = 0; i < 100; i++) { | ||
| assertTrue(limiter.tryAcquire(1000)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void multipleReleasesRestoreBudget() { | ||
| var limiter = new PartitionOperationInflightLimiter(10); | ||
| long limit = (long) (0.10 * MAX_MEMORY); | ||
| int chunkBytes = (int) Math.min(limit / 4, Integer.MAX_VALUE / 4); | ||
|
|
||
| // Acquire 4 chunks. | ||
| for (int i = 0; i < 4; i++) { | ||
| assertTrue(limiter.tryAcquire(chunkBytes), "acquire " + i + " should succeed"); | ||
| } | ||
|
|
||
| // Release all. | ||
| for (int i = 0; i < 4; i++) { | ||
| limiter.release(chunkBytes); | ||
| } | ||
|
|
||
| // Should be able to acquire again. | ||
| for (int i = 0; i < 4; i++) { | ||
| assertTrue(limiter.tryAcquire(chunkBytes), "re-acquire " + i + " should succeed after release"); | ||
| } | ||
| } | ||
| } |
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
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
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
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.