-
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
- Loading branch information
Showing
6 changed files
with
184 additions
and
39 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
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,51 @@ | ||
/* | ||
* 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; | ||
|
||
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); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...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,72 @@ | ||
/* | ||
* 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; | ||
|
||
public class RateLimitingOffsetRangeInputStream extends OffsetRangeInputStream { | ||
|
||
private StreamLimiter streamLimiter; | ||
|
||
private OffsetRangeInputStream delegate; | ||
|
||
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
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