Skip to content

Commit 26e922e

Browse files
committed
Rename new constructs and minor refactoring around version specific reading contract
Signed-off-by: Varun Bansal <bansvaru@amazon.com>
1 parent f945417 commit 26e922e

11 files changed

Lines changed: 161 additions & 172 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.common.io;
10+
11+
import org.apache.lucene.store.IndexInput;
12+
import org.apache.lucene.store.IndexOutput;
13+
14+
import java.io.IOException;
15+
16+
/**
17+
* Interface for reading/writing content streams to/from - {@link T}
18+
* @param <T> The type of content to be read/written to stream
19+
*/
20+
public interface StreamReadWriteHandler<T> {
21+
/**
22+
* Implements logic to read content from file input stream {@code indexInput} and parse into {@link T}
23+
* @param indexInput file input stream
24+
* @return content parsed to {@link T}
25+
*/
26+
T readContent(IndexInput indexInput) throws IOException;
27+
28+
/**
29+
* Implements logic to write content from {@code content} to file output stream {@code indexOutput}
30+
* @param indexOutput file input stream
31+
*/
32+
void writeContent(IndexOutput indexOutput, T content) throws IOException;
33+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.common.io;
10+
11+
import java.io.IOException;
12+
13+
import org.apache.lucene.codecs.CodecUtil;
14+
import org.apache.lucene.store.BufferedChecksumIndexInput;
15+
import org.apache.lucene.store.ChecksumIndexInput;
16+
import org.apache.lucene.store.IndexInput;
17+
import org.apache.lucene.store.IndexOutput;
18+
19+
/**
20+
* Manages versioning and checksum for a stream of content.
21+
* @param <T> Type of content to be read/written
22+
*/
23+
public class VersionedCodecStreamWrapper<T> {
24+
// This can be updated to hold a parserFactory and get relevant parsers based on the stream versions
25+
private final StreamReadWriteHandler<T> parser;
26+
private final int currentVersion;
27+
private final String codec;
28+
29+
/**
30+
* @param parser parser to read/write stream from T
31+
* @param currentVersion latest supported version of the stream
32+
* @param codec: stream codec
33+
*/
34+
public VersionedCodecStreamWrapper(StreamReadWriteHandler<T> parser, int currentVersion, String codec) {
35+
this.parser = parser;
36+
this.currentVersion = currentVersion;
37+
this.codec = codec;
38+
}
39+
40+
/**
41+
* Reads stream content from {@code indexInput} and parses the read content to {@link T}.
42+
* Before reading actual content, verifies the header with relevant codec and version.
43+
* After reading the actual content, verifies the checksum as well
44+
* @param indexInput file input stream
45+
* @return stream content parsed into {@link T}
46+
*/
47+
public T readStream(IndexInput indexInput) throws IOException {
48+
ChecksumIndexInput checksumIndexInput = new BufferedChecksumIndexInput(indexInput);
49+
int readStreamVersion = checkHeader(checksumIndexInput);
50+
T content = getParserForVersion(readStreamVersion).readContent(checksumIndexInput);
51+
checkFooter(checksumIndexInput);
52+
return content;
53+
}
54+
55+
/**
56+
* Writes to file output stream {@code indexOutput}
57+
* @param indexOutput file output stream which will store stream content
58+
* @param content stream content.
59+
*/
60+
public void writeStream(IndexOutput indexOutput, T content) throws IOException {
61+
this.writeHeader(indexOutput);
62+
getParserForVersion(this.currentVersion).writeContent(indexOutput, content);
63+
this.writeFooter(indexOutput);
64+
}
65+
66+
/**
67+
* Reads header from file input stream containing {@code this.codec} and {@code this.currentVersion}.
68+
* @param indexInput file input stream
69+
* @return header version found in the input stream
70+
*/
71+
private int checkHeader(IndexInput indexInput) throws IOException {
72+
return CodecUtil.checkHeader(indexInput, this.codec, this.currentVersion, this.currentVersion);
73+
}
74+
75+
/**
76+
* Reads footer from file input stream containing checksum.
77+
* The {@link IndexInput#getFilePointer()} should be at the footer start position.
78+
* @param indexInput file input stream
79+
*/
80+
private void checkFooter(ChecksumIndexInput indexInput) throws IOException {
81+
CodecUtil.checkFooter(indexInput);
82+
}
83+
84+
/**
85+
* Writes header with {@code this.codec} and {@code this.currentVersion} to the file output stream
86+
* @param indexOutput file output stream
87+
*/
88+
private void writeHeader(IndexOutput indexOutput) throws IOException {
89+
CodecUtil.writeHeader(indexOutput, this.codec, this.currentVersion);
90+
}
91+
92+
/**
93+
* Writes footer with checksum of contents of file output stream
94+
* @param indexOutput file output stream
95+
*/
96+
private void writeFooter(IndexOutput indexOutput) throws IOException {
97+
CodecUtil.writeFooter(indexOutput);
98+
}
99+
100+
/**
101+
* Returns relevant parser for the version
102+
* @param version stream content version
103+
*/
104+
private StreamReadWriteHandler<T> getParserForVersion(int version) {
105+
// TODO implement parser factory and pick relevant parser based on version
106+
return this.parser;
107+
}
108+
}

server/src/main/java/org/opensearch/common/metadata/MetadataContentParser.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

server/src/main/java/org/opensearch/common/metadata/MetadataParser.java

Lines changed: 0 additions & 99 deletions
This file was deleted.

server/src/main/java/org/opensearch/common/metadata/package-info.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

server/src/main/java/org/opensearch/index/store/RemoteSegmentStoreDirectory.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
import org.apache.lucene.store.IndexOutput;
1919
import org.opensearch.common.UUIDs;
2020
import org.opensearch.index.store.remote.metadata.RemoteSegmentMetadata;
21-
import org.opensearch.common.metadata.MetadataParser;
21+
import org.opensearch.common.io.VersionedCodecStreamWrapper;
22+
import org.opensearch.index.store.remote.metadata.RemoteSegmentMetadataHandler;
2223

2324
import java.io.IOException;
2425
import java.nio.file.NoSuchFileException;
@@ -77,19 +78,18 @@ public final class RemoteSegmentStoreDirectory extends FilterDirectory {
7778
*/
7879
private Map<String, UploadedSegmentMetadata> segmentsUploadedToRemoteStore;
7980

80-
private MetadataParser<RemoteSegmentMetadata> remoteMetadataParser;
81+
private static final VersionedCodecStreamWrapper<RemoteSegmentMetadata> metadataStreamWrapper = new VersionedCodecStreamWrapper<>(
82+
new RemoteSegmentMetadataHandler(),
83+
RemoteSegmentMetadata.CURRENT_VERSION,
84+
RemoteSegmentMetadata.METADATA_CODEC
85+
);
8186

8287
private static final Logger logger = LogManager.getLogger(RemoteSegmentStoreDirectory.class);
8388

84-
public RemoteSegmentStoreDirectory(
85-
RemoteDirectory remoteDataDirectory,
86-
RemoteDirectory remoteMetadataDirectory,
87-
MetadataParser<RemoteSegmentMetadata> metadataParser
88-
) throws IOException {
89+
public RemoteSegmentStoreDirectory(RemoteDirectory remoteDataDirectory, RemoteDirectory remoteMetadataDirectory) throws IOException {
8990
super(remoteDataDirectory);
9091
this.remoteDataDirectory = remoteDataDirectory;
9192
this.remoteMetadataDirectory = remoteMetadataDirectory;
92-
this.remoteMetadataParser = metadataParser;
9393
init();
9494
}
9595

@@ -135,7 +135,7 @@ private Map<String, UploadedSegmentMetadata> readLatestMetadataFile() throws IOE
135135

136136
private Map<String, UploadedSegmentMetadata> readMetadataFile(String metadataFilename) throws IOException {
137137
try (IndexInput indexInput = remoteMetadataDirectory.openInput(metadataFilename, IOContext.DEFAULT)) {
138-
RemoteSegmentMetadata metadata = this.remoteMetadataParser.readMetadata(indexInput);
138+
RemoteSegmentMetadata metadata = metadataStreamWrapper.readStream(indexInput);
139139
return metadata.getMetadata();
140140
}
141141
}
@@ -361,7 +361,7 @@ public void uploadMetadata(Collection<String> segmentFiles, Directory storeDirec
361361
throw new NoSuchFileException(file);
362362
}
363363
}
364-
this.remoteMetadataParser.writeMetadata(indexOutput, RemoteSegmentMetadata.fromMapOfStrings(uploadedSegments));
364+
metadataStreamWrapper.writeStream(indexOutput, RemoteSegmentMetadata.fromMapOfStrings(uploadedSegments));
365365
indexOutput.close();
366366
storeDirectory.sync(Collections.singleton(metadataFilename));
367367
remoteMetadataDirectory.copyFrom(storeDirectory, metadataFilename, metadataFilename, IOContext.DEFAULT);

server/src/main/java/org/opensearch/index/store/RemoteSegmentStoreDirectoryFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.opensearch.common.blobstore.BlobPath;
1414
import org.opensearch.index.IndexSettings;
1515
import org.opensearch.index.shard.ShardPath;
16-
import org.opensearch.index.store.remote.metadata.RemoteSegmentMetadata;
1716
import org.opensearch.plugins.IndexStorePlugin;
1817
import org.opensearch.repositories.RepositoriesService;
1918
import org.opensearch.repositories.Repository;
@@ -48,7 +47,7 @@ public Directory newDirectory(String repositoryName, IndexSettings indexSettings
4847
RemoteDirectory dataDirectory = createRemoteDirectory(repository, commonBlobPath, "data");
4948
RemoteDirectory metadataDirectory = createRemoteDirectory(repository, commonBlobPath, "metadata");
5049

51-
return new RemoteSegmentStoreDirectory(dataDirectory, metadataDirectory, RemoteSegmentMetadata.createMetadataParser());
50+
return new RemoteSegmentStoreDirectory(dataDirectory, metadataDirectory);
5251
} catch (RepositoryMissingException e) {
5352
throw new IllegalArgumentException("Repository should be created before creating index with remote_store enabled setting", e);
5453
}

server/src/main/java/org/opensearch/index/store/remote/metadata/RemoteSegmentMetadata.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import java.util.Map;
1212
import java.util.stream.Collectors;
1313

14-
import org.opensearch.common.metadata.MetadataParser;
14+
import org.opensearch.common.io.VersionedCodecStreamWrapper;
1515
import org.opensearch.index.store.RemoteSegmentStoreDirectory;
1616

1717
/**
@@ -71,10 +71,10 @@ public static RemoteSegmentMetadata fromMapOfStrings(Map<String, String> segment
7171
}
7272

7373
/**
74-
* Constructs {@link MetadataParser} for {@link RemoteSegmentMetadata}
75-
* @return {@link MetadataParser}
74+
* Constructs {@link VersionedCodecStreamWrapper} for {@link RemoteSegmentMetadata}
75+
* @return {@link VersionedCodecStreamWrapper}
7676
*/
77-
public static MetadataParser<RemoteSegmentMetadata> createMetadataParser() {
78-
return new MetadataParser<>(new RemoteSegmentMetadataContentParser(), CURRENT_VERSION, METADATA_CODEC);
77+
public static VersionedCodecStreamWrapper<RemoteSegmentMetadata> createMetadataHandler() {
78+
return new VersionedCodecStreamWrapper<>(new RemoteSegmentMetadataHandler(), CURRENT_VERSION, METADATA_CODEC);
7979
}
8080
}

server/src/main/java/org/opensearch/index/store/remote/metadata/RemoteSegmentMetadataContentParser.java renamed to server/src/main/java/org/opensearch/index/store/remote/metadata/RemoteSegmentMetadataHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212

1313
import org.apache.lucene.store.IndexInput;
1414
import org.apache.lucene.store.IndexOutput;
15-
import org.opensearch.common.metadata.MetadataContentParser;
15+
import org.opensearch.common.io.StreamReadWriteHandler;
1616

1717
/**
1818
* Parser for {@link RemoteSegmentMetadata}
1919
*/
20-
public class RemoteSegmentMetadataContentParser implements MetadataContentParser<RemoteSegmentMetadata> {
20+
public class RemoteSegmentMetadataHandler implements StreamReadWriteHandler<RemoteSegmentMetadata> {
2121
/**
2222
* Reads metadata content from metadata file input stream and parsed into {@link RemoteSegmentMetadata}
2323
* @param indexInput metadata file input stream with {@link IndexInput#getFilePointer()} pointing to metadata content

0 commit comments

Comments
 (0)