-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rate Limiter integration for remote transfer (#9448)
* Rate Limiter integration for remote transfer, introduces repository settings to rate limit remote store uploads and downloads Signed-off-by: Bukhtawar Khan <[email protected]>
- Loading branch information
Showing
18 changed files
with
686 additions
and
163 deletions.
There are no files selected for viewing
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
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
56 changes: 56 additions & 0 deletions
56
server/src/main/java/org/opensearch/common/StreamLimiter.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,56 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.common; | ||
|
||
import org.apache.lucene.store.RateLimiter; | ||
|
||
import java.io.IOException; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* The stream limiter that limits the transfer of bytes | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class StreamLimiter { | ||
|
||
private final Supplier<RateLimiter> rateLimiterSupplier; | ||
|
||
private final StreamLimiter.Listener listener; | ||
|
||
private int bytesSinceLastRateLimit; | ||
|
||
public StreamLimiter(Supplier<RateLimiter> rateLimiterSupplier, Listener listener) { | ||
this.rateLimiterSupplier = rateLimiterSupplier; | ||
this.listener = listener; | ||
} | ||
|
||
public void maybePause(int bytes) throws IOException { | ||
bytesSinceLastRateLimit += bytes; | ||
final RateLimiter rateLimiter = rateLimiterSupplier.get(); | ||
if (rateLimiter != null) { | ||
if (bytesSinceLastRateLimit >= rateLimiter.getMinPauseCheckBytes()) { | ||
long pause = rateLimiter.pause(bytesSinceLastRateLimit); | ||
bytesSinceLastRateLimit = 0; | ||
if (pause > 0) { | ||
listener.onPause(pause); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Internal listener | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public interface Listener { | ||
void onPause(long nanos); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...a/org/opensearch/common/blobstore/transfer/stream/RateLimitingOffsetRangeInputStream.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,83 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.common.blobstore.transfer.stream; | ||
|
||
import org.apache.lucene.store.RateLimiter; | ||
import org.opensearch.common.StreamLimiter; | ||
|
||
import java.io.IOException; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Rate Limits an {@link OffsetRangeInputStream} | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class RateLimitingOffsetRangeInputStream extends OffsetRangeInputStream { | ||
|
||
private final StreamLimiter streamLimiter; | ||
|
||
private final OffsetRangeInputStream delegate; | ||
|
||
/** | ||
* The ctor for RateLimitingOffsetRangeInputStream | ||
* @param delegate the underlying {@link OffsetRangeInputStream} | ||
* @param rateLimiterSupplier the supplier for {@link RateLimiter} | ||
* @param listener the listener to be invoked on rate limits | ||
*/ | ||
public RateLimitingOffsetRangeInputStream( | ||
OffsetRangeInputStream delegate, | ||
Supplier<RateLimiter> rateLimiterSupplier, | ||
StreamLimiter.Listener listener | ||
) { | ||
this.streamLimiter = new StreamLimiter(rateLimiterSupplier, listener); | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
int b = delegate.read(); | ||
streamLimiter.maybePause(1); | ||
return b; | ||
} | ||
|
||
@Override | ||
public int read(byte[] b, int off, int len) throws IOException { | ||
int n = delegate.read(b, off, len); | ||
if (n > 0) { | ||
streamLimiter.maybePause(n); | ||
} | ||
return n; | ||
} | ||
|
||
@Override | ||
public synchronized void mark(int readlimit) { | ||
delegate.mark(readlimit); | ||
} | ||
|
||
@Override | ||
public boolean markSupported() { | ||
return delegate.markSupported(); | ||
} | ||
|
||
@Override | ||
public long getFilePointer() throws IOException { | ||
return delegate.getFilePointer(); | ||
} | ||
|
||
@Override | ||
public synchronized void reset() throws IOException { | ||
delegate.reset(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
delegate.close(); | ||
} | ||
} |
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
Oops, something went wrong.