-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Implement ccr file restore #37130
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
Implement ccr file restore #37130
Changes from all commits
d996d4e
8471837
ee37b25
cce5c99
11ce19c
6af7ec0
f774c28
359ad5b
2031fd2
e18df0a
da5b2e7
2209eac
79a53e0
142800d
6ddaa00
478e25c
0eba9d7
0e56995
8083076
6c011bc
be5f0f9
47d7576
bcef903
483f5c7
d09454c
83a1c65
7835006
c40f242
a201e93
083b9ae
cf35fcd
fa59f49
d872334
92435b0
2bfa3e1
9005245
cb1d455
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.ccr.action.repositories; | ||
|
|
||
| import org.elasticsearch.action.Action; | ||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.action.ActionResponse; | ||
| import org.elasticsearch.action.support.ActionFilters; | ||
| import org.elasticsearch.action.support.HandledTransportAction; | ||
| import org.elasticsearch.common.bytes.BytesReference; | ||
| import org.elasticsearch.common.bytes.ReleasablePagedBytesReference; | ||
| import org.elasticsearch.common.inject.Inject; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.common.io.stream.Writeable; | ||
| import org.elasticsearch.common.util.BigArrays; | ||
| import org.elasticsearch.common.util.ByteArray; | ||
| import org.elasticsearch.common.util.concurrent.AbstractRunnable; | ||
| import org.elasticsearch.tasks.Task; | ||
| import org.elasticsearch.threadpool.ThreadPool; | ||
| import org.elasticsearch.transport.TransportActionProxy; | ||
| import org.elasticsearch.transport.TransportService; | ||
| import org.elasticsearch.xpack.ccr.repository.CcrRestoreSourceService; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class GetCcrRestoreFileChunkAction extends Action<GetCcrRestoreFileChunkAction.GetCcrRestoreFileChunkResponse> { | ||
|
|
||
| public static final GetCcrRestoreFileChunkAction INSTANCE = new GetCcrRestoreFileChunkAction(); | ||
| public static final String NAME = "internal:admin/ccr/restore/file_chunk/get"; | ||
|
|
||
| private GetCcrRestoreFileChunkAction() { | ||
| super(NAME); | ||
| } | ||
|
|
||
| @Override | ||
| public GetCcrRestoreFileChunkResponse newResponse() { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| public Writeable.Reader<GetCcrRestoreFileChunkResponse> getResponseReader() { | ||
| return GetCcrRestoreFileChunkResponse::new; | ||
| } | ||
|
|
||
|
|
||
| public static class TransportGetCcrRestoreFileChunkAction | ||
| extends HandledTransportAction<GetCcrRestoreFileChunkRequest, GetCcrRestoreFileChunkAction.GetCcrRestoreFileChunkResponse> { | ||
|
|
||
| private final CcrRestoreSourceService restoreSourceService; | ||
| private final ThreadPool threadPool; | ||
| private final BigArrays bigArrays; | ||
|
|
||
| @Inject | ||
| public TransportGetCcrRestoreFileChunkAction(BigArrays bigArrays, TransportService transportService, ActionFilters actionFilters, | ||
| CcrRestoreSourceService restoreSourceService) { | ||
| super(NAME, transportService, actionFilters, GetCcrRestoreFileChunkRequest::new); | ||
| TransportActionProxy.registerProxyAction(transportService, NAME, GetCcrRestoreFileChunkResponse::new); | ||
| this.threadPool = transportService.getThreadPool(); | ||
| this.restoreSourceService = restoreSourceService; | ||
| this.bigArrays = bigArrays; | ||
| } | ||
|
|
||
| @Override | ||
| protected void doExecute(Task task, GetCcrRestoreFileChunkRequest request, | ||
| ActionListener<GetCcrRestoreFileChunkResponse> listener) { | ||
| threadPool.generic().execute(new AbstractRunnable() { | ||
| @Override | ||
| public void onFailure(Exception e) { | ||
| listener.onFailure(e); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doRun() throws Exception { | ||
| int bytesRequested = request.getSize(); | ||
| ByteArray array = bigArrays.newByteArray(bytesRequested, false); | ||
| String fileName = request.getFileName(); | ||
| String sessionUUID = request.getSessionUUID(); | ||
| // This is currently safe to do because calling `onResponse` will serialize the bytes to the network layer data | ||
| // structure on the same thread. So the bytes will be copied before the reference is released. | ||
| try (ReleasablePagedBytesReference reference = new ReleasablePagedBytesReference(array, bytesRequested, array)) { | ||
s1monw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try (CcrRestoreSourceService.SessionReader sessionReader = restoreSourceService.getSessionReader(sessionUUID)) { | ||
| long offsetAfterRead = sessionReader.readFileBytes(fileName, reference); | ||
| long offsetBeforeRead = offsetAfterRead - reference.length(); | ||
| listener.onResponse(new GetCcrRestoreFileChunkResponse(offsetBeforeRead, reference)); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| public static class GetCcrRestoreFileChunkResponse extends ActionResponse { | ||
|
|
||
| private final long offset; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. offset is typically the beginning of the range that is sent. Having it different here is a potential source for confusion in the future. I see two options: 1) Name it differently or 2) make it the offset that represents the beginning of the range that is sent. I'm strongly favoring 2), which is a small change to make, and provides the same validation on the receiver.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Went with number 2. |
||
| private final BytesReference chunk; | ||
|
|
||
| GetCcrRestoreFileChunkResponse(StreamInput streamInput) throws IOException { | ||
| super(streamInput); | ||
| offset = streamInput.readVLong(); | ||
| chunk = streamInput.readBytesReference(); | ||
| } | ||
|
|
||
| GetCcrRestoreFileChunkResponse(long offset, BytesReference chunk) { | ||
| this.offset = offset; | ||
| this.chunk = chunk; | ||
| } | ||
|
|
||
| public long getOffset() { | ||
| return offset; | ||
| } | ||
|
|
||
| public BytesReference getChunk() { | ||
| return chunk; | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(StreamOutput out) throws IOException { | ||
| super.writeTo(out); | ||
| out.writeVLong(offset); | ||
| out.writeBytesReference(chunk); | ||
| } | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.ccr.action.repositories; | ||
|
|
||
| import org.elasticsearch.action.ActionRequest; | ||
| import org.elasticsearch.action.ActionRequestValidationException; | ||
| import org.elasticsearch.cluster.node.DiscoveryNode; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.transport.RemoteClusterAwareRequest; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class GetCcrRestoreFileChunkRequest extends ActionRequest implements RemoteClusterAwareRequest { | ||
|
|
||
| private final DiscoveryNode node; | ||
| private final String sessionUUID; | ||
| private final String fileName; | ||
| private final int size; | ||
|
|
||
| @Override | ||
| public ActionRequestValidationException validate() { | ||
| return null; | ||
| } | ||
|
|
||
| public GetCcrRestoreFileChunkRequest(DiscoveryNode node, String sessionUUID, String fileName, int size) { | ||
| this.node = node; | ||
| this.sessionUUID = sessionUUID; | ||
| this.fileName = fileName; | ||
| this.size = size; | ||
| assert size > -1 : "The file chunk request size must be positive. Found: [" + size + "]."; | ||
| } | ||
|
|
||
| GetCcrRestoreFileChunkRequest(StreamInput in) throws IOException { | ||
| super(in); | ||
| node = null; | ||
| sessionUUID = in.readString(); | ||
| fileName = in.readString(); | ||
| size = in.readVInt(); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(StreamOutput out) throws IOException { | ||
| super.writeTo(out); | ||
| out.writeString(sessionUUID); | ||
| out.writeString(fileName); | ||
| out.writeVInt(size); | ||
| } | ||
|
|
||
| @Override | ||
| public void readFrom(StreamInput in) throws IOException { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| String getSessionUUID() { | ||
| return sessionUUID; | ||
| } | ||
|
|
||
| String getFileName() { | ||
| return fileName; | ||
| } | ||
|
|
||
| int getSize() { | ||
| return size; | ||
| } | ||
|
|
||
| @Override | ||
| public DiscoveryNode getPreferredTargetNode() { | ||
| assert node != null : "Target node is null"; | ||
| return node; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe assert here that node is not-null? |
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.