Skip to content

Commit adae8e8

Browse files
committed
Implement follower rate limiting for file restore
This is related to elastic#35975. This commit implements rate limiting on the follower side using the `RateLimitingInputStream`.
1 parent 36a3b84 commit adae8e8

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/repository/CcrRepository.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.elasticsearch.xpack.ccr.repository;
88

99
import org.apache.lucene.index.IndexCommit;
10+
import org.apache.lucene.store.RateLimiter;
1011
import org.elasticsearch.Version;
1112
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
1213
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
@@ -25,6 +26,7 @@
2526
import org.elasticsearch.common.component.AbstractLifecycleComponent;
2627
import org.elasticsearch.common.io.stream.StreamInput;
2728
import org.elasticsearch.common.settings.Settings;
29+
import org.elasticsearch.common.unit.ByteSizeUnit;
2830
import org.elasticsearch.common.unit.ByteSizeValue;
2931
import org.elasticsearch.index.Index;
3032
import org.elasticsearch.index.IndexSettings;
@@ -35,6 +37,7 @@
3537
import org.elasticsearch.index.snapshots.IndexShardRestoreFailedException;
3638
import org.elasticsearch.index.snapshots.IndexShardSnapshotStatus;
3739
import org.elasticsearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshot;
40+
import org.elasticsearch.index.snapshots.blobstore.RateLimitingInputStream;
3841
import org.elasticsearch.index.snapshots.blobstore.SnapshotFiles;
3942
import org.elasticsearch.index.store.Store;
4043
import org.elasticsearch.index.store.StoreFileMetaData;
@@ -82,6 +85,7 @@ public class CcrRepository extends AbstractLifecycleComponent implements Reposit
8285
private final String remoteClusterAlias;
8386
private final Client client;
8487
private final CcrLicenseChecker ccrLicenseChecker;
88+
private final RateLimiter.SimpleRateLimiter rateLimiter;
8589

8690
public CcrRepository(RepositoryMetaData metadata, Client client, CcrLicenseChecker ccrLicenseChecker, Settings settings) {
8791
super(settings);
@@ -90,6 +94,7 @@ public CcrRepository(RepositoryMetaData metadata, Client client, CcrLicenseCheck
9094
this.remoteClusterAlias = Strings.split(metadata.name(), NAME_PREFIX)[1];
9195
this.ccrLicenseChecker = ccrLicenseChecker;
9296
this.client = client;
97+
this.rateLimiter = new RateLimiter.SimpleRateLimiter(new ByteSizeValue(40, ByteSizeUnit.MB).getMbFrac());
9398
}
9499

95100
@Override
@@ -258,7 +263,7 @@ public void restoreShard(IndexShard indexShard, SnapshotId snapshotId, Version v
258263
// TODO: There should be some local timeout. And if the remote cluster returns an unknown session
259264
// response, we should be able to retry by creating a new session.
260265
String name = metadata.name();
261-
try (RestoreSession restoreSession = RestoreSession.openSession(name, remoteClient, leaderShardId, indexShard, recoveryState)) {
266+
try (RestoreSession restoreSession = openSession(name, remoteClient, leaderShardId, indexShard, recoveryState)) {
262267
restoreSession.restoreFiles();
263268
} catch (Exception e) {
264269
throw new IndexShardRestoreFailedException(indexShard.shardId(), "failed to restore snapshot [" + snapshotId + "]", e);
@@ -286,7 +291,16 @@ private void maybeUpdateMappings(Client localClient, Client remoteClient, Index
286291
}
287292
}
288293

289-
private static class RestoreSession extends FileRestoreContext implements Closeable {
294+
private RestoreSession openSession(String repositoryName, Client remoteClient, ShardId leaderShardId, IndexShard indexShard,
295+
RecoveryState recoveryState) {
296+
String sessionUUID = UUIDs.randomBase64UUID();
297+
PutCcrRestoreSessionAction.PutCcrRestoreSessionResponse response = remoteClient.execute(PutCcrRestoreSessionAction.INSTANCE,
298+
new PutCcrRestoreSessionRequest(sessionUUID, leaderShardId)).actionGet();
299+
return new RestoreSession(repositoryName, remoteClient, sessionUUID, response.getNode(), indexShard, recoveryState,
300+
response.getStoreFileMetaData());
301+
}
302+
303+
private class RestoreSession extends FileRestoreContext implements Closeable {
290304

291305
private static final int BUFFER_SIZE = 1 << 16;
292306

@@ -304,15 +318,6 @@ private static class RestoreSession extends FileRestoreContext implements Closea
304318
this.sourceMetaData = sourceMetaData;
305319
}
306320

307-
static RestoreSession openSession(String repositoryName, Client remoteClient, ShardId leaderShardId, IndexShard indexShard,
308-
RecoveryState recoveryState) {
309-
String sessionUUID = UUIDs.randomBase64UUID();
310-
PutCcrRestoreSessionAction.PutCcrRestoreSessionResponse response = remoteClient.execute(PutCcrRestoreSessionAction.INSTANCE,
311-
new PutCcrRestoreSessionRequest(sessionUUID, leaderShardId)).actionGet();
312-
return new RestoreSession(repositoryName, remoteClient, sessionUUID, response.getNode(), indexShard, recoveryState,
313-
response.getStoreFileMetaData());
314-
}
315-
316321
void restoreFiles() throws IOException {
317322
ArrayList<BlobStoreIndexShardSnapshot.FileInfo> fileInfos = new ArrayList<>();
318323
for (StoreFileMetaData fileMetaData : sourceMetaData) {
@@ -325,7 +330,8 @@ void restoreFiles() throws IOException {
325330

326331
@Override
327332
protected InputStream fileInputStream(BlobStoreIndexShardSnapshot.FileInfo fileInfo) {
328-
return new RestoreFileInputStream(remoteClient, sessionUUID, node, fileInfo.metadata());
333+
RestoreFileInputStream restoreInputStream = new RestoreFileInputStream(remoteClient, sessionUUID, node, fileInfo.metadata());
334+
return new RateLimitingInputStream(restoreInputStream, rateLimiter, (n) -> {});
329335
}
330336

331337
@Override
@@ -336,7 +342,7 @@ public void close() {
336342
}
337343
}
338344

339-
private static class RestoreFileInputStream extends InputStream {
345+
private class RestoreFileInputStream extends InputStream {
340346

341347
private final Client remoteClient;
342348
private final String sessionUUID;

0 commit comments

Comments
 (0)