Skip to content

Adapt searchable snapshots code to latest master changes #52258

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

Merged
merged 2 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

import org.apache.lucene.store.BufferedIndexInput;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.blobstore.BlobContainer;
import org.elasticsearch.core.internal.io.IOUtils;
import org.elasticsearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshot.FileInfo;

import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.EOFException;
import java.io.IOException;
Expand Down Expand Up @@ -119,8 +121,7 @@ private void readInternalBytes(final int part, long pos, final byte[] b, int off

if (optimizedReadSize < length) {
// we did not read everything in an optimized fashion, so read the remainder directly
try (InputStream inputStream
= blobContainer.readBlob(fileInfo.partName(part), pos + optimizedReadSize, length - optimizedReadSize)) {
try (InputStream inputStream = openBlobStream(part, pos + optimizedReadSize, length - optimizedReadSize)) {
final int directReadSize = inputStream.read(b, offset + optimizedReadSize, length - optimizedReadSize);
assert optimizedReadSize + directReadSize == length : optimizedReadSize + " and " + directReadSize + " vs " + length;
position += directReadSize;
Expand Down Expand Up @@ -192,7 +193,7 @@ private int readFromNewSequentialStream(int part, long pos, byte[] b, int offset

// if we open a stream of length streamLength then it will not be completely consumed by this read, so it is worthwhile to open
// it and keep it open for future reads
final InputStream inputStream = blobContainer.readBlob(fileInfo.partName(part), pos, streamLength);
final InputStream inputStream = openBlobStream(part, pos, streamLength);
streamForSequentialReads = new StreamForSequentialReads(inputStream, part, pos, streamLength);

final int read = streamForSequentialReads.read(b, offset, length);
Expand Down Expand Up @@ -257,6 +258,27 @@ public String toString() {
'}';
}

private InputStream openBlobStream(int part, long pos, long length) throws IOException {
final InputStream stream;
if (fileInfo.metadata().hashEqualsContents() == false) {
stream = blobContainer.readBlob(fileInfo.partName(part), pos, length);
} else {
// extract blob content from metadata hash
final BytesRef data = fileInfo.metadata().hash();
if (part > 0) {
assert fileInfo.numberOfParts() >= part;
for (int i = 0; i < part; i++) {
pos += fileInfo.partBytes(i);
}
}
if ((pos < 0L) || (length < 0L) || (pos + length > data.bytes.length)) {
throw new IllegalArgumentException("Invalid arguments (pos=" + pos + ", length=" + length + ") for hash content");
}
stream = new ByteArrayInputStream(data.bytes, Math.toIntExact(pos), Math.toIntExact(length));
}
return stream;
}

private static class StreamForSequentialReads implements Closeable {
private final InputStream inputStream;
private final int part;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public List<RestHandler> getRestHandlers(Settings settings, RestController restC
IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter,
IndexNameExpressionResolver indexNameExpressionResolver,
Supplier<DiscoveryNodes> nodesInCluster) {
return List.of(new RestSearchableSnapshotsStatsAction(restController));
return List.of(new RestSearchableSnapshotsStatsAction());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestToXContentListener;
import org.elasticsearch.xpack.searchablesnapshots.action.SearchableSnapshotsStatsAction;
import org.elasticsearch.xpack.searchablesnapshots.action.SearchableSnapshotsStatsRequest;

import java.util.List;

public class RestSearchableSnapshotsStatsAction extends BaseRestHandler {

public RestSearchableSnapshotsStatsAction(final RestController controller) {
controller.registerHandler(RestRequest.Method.GET, "/_searchable_snapshots/stats", this);
controller.registerHandler(RestRequest.Method.GET, "/{index}/_searchable_snapshots/stats", this);
@Override
public List<Route> routes() {
return List.of(
new Route(RestRequest.Method.GET, "/_searchable_snapshots/stats"),
new Route(RestRequest.Method.GET, "/{index}/_searchable_snapshots/stats")
);
}

@Override
Expand Down