Skip to content

Commit d0cfaf8

Browse files
committed
1. Rename MetadataParser to MetadataContentParser and MetadataManager to MetadataParser
2. Add TODOs and Deprecate non-generic methods Signed-off-by: Varun Bansal <bansvaru@amazon.com>
1 parent 9b82db0 commit d0cfaf8

9 files changed

Lines changed: 150 additions & 140 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.metadata;
10+
11+
import org.apache.lucene.store.IndexInput;
12+
import org.apache.lucene.store.IndexOutput;
13+
14+
import java.io.IOException;
15+
import java.util.Map;
16+
17+
/**
18+
* Parser interface for Metadata. Holds methods to convert to/from file content streams to metadata object holder - {@link T}
19+
* @param <T> The type of metadata to be parsed
20+
*/
21+
public interface MetadataContentParser<T> {
22+
/**
23+
* Implements logic to read metadata content from metadata file input stream {@code indexInput} and parse into {@link T}
24+
* @param indexInput metadata file input stream
25+
* @return metadata content parsed to {@link T}
26+
*/
27+
T readContent(IndexInput indexInput) throws IOException;
28+
29+
/**
30+
* Implements logic to write metadata content from {@code content} to metadata file output stream {@code indexOutput}
31+
* @param indexOutput metadata file input stream
32+
*/
33+
void writeContent(IndexOutput indexOutput, T content) throws IOException;
34+
35+
/**
36+
* Implements logic to write metadata content from {@code content} to metadata file output stream {@code indexOutput}
37+
* This method only supports metadata content in the form {@code Map<String, String>} to support RemoteSegment store metadata content.
38+
* @param indexOutput metadata file input stream
39+
* @param content metadata content
40+
*
41+
* TODO - This will removed in future releases and only {@link #writeContent(IndexOutput, Object)} should be used.
42+
*/
43+
@Deprecated
44+
void writeContent(IndexOutput indexOutput, Map<String, String> content) throws IOException;
45+
}

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

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

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

Lines changed: 89 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,107 @@
88

99
package org.opensearch.common.metadata;
1010

11-
import org.apache.lucene.store.IndexInput;
12-
import org.apache.lucene.store.IndexOutput;
13-
1411
import java.io.IOException;
1512
import java.util.Map;
1613

14+
import org.apache.lucene.codecs.CodecUtil;
15+
import org.apache.lucene.store.BufferedChecksumIndexInput;
16+
import org.apache.lucene.store.ChecksumIndexInput;
17+
import org.apache.lucene.store.IndexInput;
18+
import org.apache.lucene.store.IndexOutput;
19+
1720
/**
18-
* Parser interface for Metadata. Holds methods to convert to/from file content streams to metadata object holder - {@link T}
19-
* @param <T> The type of metadata to be parsed
21+
* Manages Metadata by adding version, codec in header and checksum in footer.
22+
* @param <T> The type of metadata to be read/written
2023
*/
21-
public interface MetadataParser<T> {
24+
public class MetadataParser<T> {
25+
// This can be updated to hold a parserFactory and get relevant parsers based on the metadata versions
26+
private final MetadataContentParser<T> parser;
27+
private final int currentVersion;
28+
private final String codec;
29+
2230
/**
23-
* Implements logic to read metadata content from metadata file input stream {@code indexInput} and parse into {@link T}
24-
* @param indexInput metadata file input stream
25-
* @return metadata content parsed to {@link T}
31+
* @param parser parser to read/write metadata from T
32+
* @param currentVersion latest supported version of the metadata
33+
* @param codec: metadata codec
2634
*/
27-
T readContent(IndexInput indexInput) throws IOException;
35+
public MetadataParser(MetadataContentParser<T> parser, int currentVersion, String codec) {
36+
this.parser = parser;
37+
this.currentVersion = currentVersion;
38+
this.codec = codec;
39+
}
2840

2941
/**
30-
* Implements logic to write metadata content from {@code content} to metadata file output stream {@code indexOutput}
31-
* @param indexOutput metadata file input stream
42+
* Reads metadata content from {@code indexInput} and parses the read content to {@link T}.
43+
* Before reading actual content, verifies the header with relevant codec and version.
44+
* After reading the actual content, verifies the checksum as well
45+
* @param indexInput metadata file input stream
46+
* @return metadata content parsed into {@link T}
3247
*/
33-
void writeContent(IndexOutput indexOutput, T content) throws IOException;
48+
public T readMetadata(IndexInput indexInput) throws IOException {
49+
ChecksumIndexInput checksumIndexInput = new BufferedChecksumIndexInput(indexInput);
50+
checkHeader(checksumIndexInput);
51+
T metadata = this.parser.readContent(checksumIndexInput);
52+
checkFooter(checksumIndexInput);
53+
return metadata;
54+
}
3455

3556
/**
36-
* Implements logic to write metadata content from {@code content} to metadata file output stream {@code indexOutput}
37-
* This method only supports metadata content in the form {@code Map<String, String>} to support RemoteSegment store metadata content.
38-
* This will removed in future releases and only {@link #writeContent(IndexOutput, Object)} )} should be used going fwd.
39-
* @param indexOutput metadata file input stream
40-
* @param content metadata content
57+
* Writes metadata to file output stream {@code indexOutput}
58+
* @param indexOutput file output stream which will store metadata content
59+
* @param metadata metadata content.
60+
*
61+
* TODO - This will removed in future releases and only {@link #writeMetadata(IndexOutput, Object)} should be used.
4162
*/
4263
@Deprecated
43-
void writeContent(IndexOutput indexOutput, Map<String, String> content) throws IOException;
64+
public void writeMetadata(IndexOutput indexOutput, Map<String, String> metadata) throws IOException {
65+
this.writeHeader(indexOutput);
66+
this.parser.writeContent(indexOutput, metadata);
67+
this.writeFooter(indexOutput);
68+
}
69+
70+
/**
71+
* Writes metadata to file output stream {@code indexOutput}
72+
* @param indexOutput file output stream which will store metadata content
73+
* @param metadata metadata content.
74+
*/
75+
public void writeMetadata(IndexOutput indexOutput, T metadata) throws IOException {
76+
this.writeHeader(indexOutput);
77+
this.parser.writeContent(indexOutput, metadata);
78+
this.writeFooter(indexOutput);
79+
}
80+
81+
/**
82+
* Reads header from metadata file input stream containing {@code this.codec} and {@code this.currentVersion}.
83+
* @param indexInput metadata file input stream
84+
* @return header version found in the metadata file
85+
*/
86+
private int checkHeader(IndexInput indexInput) throws IOException {
87+
return CodecUtil.checkHeader(indexInput, this.codec, this.currentVersion, this.currentVersion);
88+
}
89+
90+
/**
91+
* Reads footer from metadata file input stream containing checksum.
92+
* The {@link IndexInput#getFilePointer()} should be at the footer start position.
93+
* @param indexInput metadata file input stream
94+
*/
95+
private void checkFooter(ChecksumIndexInput indexInput) throws IOException {
96+
CodecUtil.checkFooter(indexInput);
97+
}
98+
99+
/**
100+
* Writes header with {@code this.codec} and {@code this.currentVersion} to the metadata file output stream
101+
* @param indexOutput metadata file output stream
102+
*/
103+
private void writeHeader(IndexOutput indexOutput) throws IOException {
104+
CodecUtil.writeHeader(indexOutput, this.codec, this.currentVersion);
105+
}
106+
107+
/**
108+
* Writes footer with checksum of contents of metadata file output stream
109+
* @param indexOutput metadata file output stream
110+
*/
111+
private void writeFooter(IndexOutput indexOutput) throws IOException {
112+
CodecUtil.writeFooter(indexOutput);
113+
}
44114
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
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.MetadataManager;
21+
import org.opensearch.common.metadata.MetadataParser;
2222

2323
import java.io.IOException;
2424
import java.nio.file.NoSuchFileException;
@@ -77,19 +77,19 @@ public final class RemoteSegmentStoreDirectory extends FilterDirectory {
7777
*/
7878
private Map<String, UploadedSegmentMetadata> segmentsUploadedToRemoteStore;
7979

80-
private MetadataManager<RemoteSegmentMetadata> remoteMetadataManager;
80+
private MetadataParser<RemoteSegmentMetadata> remoteMetadataParser;
8181

8282
private static final Logger logger = LogManager.getLogger(RemoteSegmentStoreDirectory.class);
8383

8484
public RemoteSegmentStoreDirectory(
8585
RemoteDirectory remoteDataDirectory,
8686
RemoteDirectory remoteMetadataDirectory,
87-
MetadataManager<RemoteSegmentMetadata> metadataManager
87+
MetadataParser<RemoteSegmentMetadata> metadataParser
8888
) throws IOException {
8989
super(remoteDataDirectory);
9090
this.remoteDataDirectory = remoteDataDirectory;
9191
this.remoteMetadataDirectory = remoteMetadataDirectory;
92-
this.remoteMetadataManager = metadataManager;
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.remoteMetadataManager.readMetadata(indexInput);
138+
RemoteSegmentMetadata metadata = this.remoteMetadataParser.readMetadata(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.remoteMetadataManager.writeMetadata(indexOutput, uploadedSegments);
364+
this.remoteMetadataParser.writeMetadata(indexOutput, 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public Directory newDirectory(String repositoryName, IndexSettings indexSettings
4848
RemoteDirectory dataDirectory = createRemoteDirectory(repository, commonBlobPath, "data");
4949
RemoteDirectory metadataDirectory = createRemoteDirectory(repository, commonBlobPath, "metadata");
5050

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

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.MetadataManager;
14+
import org.opensearch.common.metadata.MetadataParser;
1515
import org.opensearch.index.store.RemoteSegmentStoreDirectory;
1616

1717
/**
@@ -63,10 +63,10 @@ public static RemoteSegmentMetadata fromMapOfStrings(Map<String, String> segment
6363
}
6464

6565
/**
66-
* Constructs {@link MetadataManager} for {@link RemoteSegmentMetadata}
67-
* @return {@link MetadataManager}
66+
* Constructs {@link MetadataParser} for {@link RemoteSegmentMetadata}
67+
* @return {@link MetadataParser}
6868
*/
69-
public static MetadataManager<RemoteSegmentMetadata> createMetadataManager() {
70-
return new MetadataManager<>(new RemoteSegmentMetadataParser(), CURRENT_VERSION, METADATA_CODEC);
69+
public static MetadataParser<RemoteSegmentMetadata> createMetadataParser() {
70+
return new MetadataParser<>(new RemoteSegmentMetadataContentParser(), CURRENT_VERSION, METADATA_CODEC);
7171
}
7272
}

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

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

1414
import org.apache.lucene.store.IndexInput;
1515
import org.apache.lucene.store.IndexOutput;
16-
import org.opensearch.common.metadata.MetadataParser;
16+
import org.opensearch.common.metadata.MetadataContentParser;
1717

1818
/**
1919
* Parser for {@link RemoteSegmentMetadata}
2020
*/
21-
public class RemoteSegmentMetadataParser implements MetadataParser<RemoteSegmentMetadata> {
21+
public class RemoteSegmentMetadataContentParser implements MetadataContentParser<RemoteSegmentMetadata> {
2222
/**
2323
* Reads metadata content from metadata file input stream and parsed into {@link RemoteSegmentMetadata}
2424
* @param indexInput metadata file input stream with {@link IndexInput#getFilePointer()} pointing to metadata content
@@ -41,9 +41,6 @@ public void writeContent(IndexOutput indexOutput, RemoteSegmentMetadata content)
4141
* Writes metadata to file output stream
4242
* @param indexOutput metadata file input stream
4343
* @param content metadata content
44-
*
45-
* This method would be removed in future
46-
* and only {@link #writeContent(IndexOutput, RemoteSegmentMetadata)} would be leveraged in future
4744
*/
4845
@Override
4946
public void writeContent(IndexOutput indexOutput, Map<String, String> content) throws IOException {

server/src/test/java/org/opensearch/index/store/RemoteSegmentStoreDirectoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void setup() throws IOException {
6060
remoteSegmentStoreDirectory = new RemoteSegmentStoreDirectory(
6161
remoteDataDirectory,
6262
remoteMetadataDirectory,
63-
RemoteSegmentMetadata.createMetadataManager()
63+
RemoteSegmentMetadata.createMetadataParser()
6464
);
6565
}
6666

0 commit comments

Comments
 (0)