Skip to content

Commit

Permalink
Add Lucene version to UploadedSegmentMetadata
Browse files Browse the repository at this point in the history
Signed-off-by: Bhumika Saini <[email protected]>
  • Loading branch information
Bhumika Saini committed Jun 15, 2023
1 parent 87e34a3 commit 72fb904
Showing 1 changed file with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.codecs.CodecUtil;
import org.apache.lucene.index.SegmentCommitInfo;
import org.apache.lucene.index.SegmentInfo;
import org.apache.lucene.index.SegmentInfos;
import org.apache.lucene.store.ByteBuffersDataOutput;
import org.apache.lucene.store.ByteBuffersIndexOutput;
Expand All @@ -19,8 +21,10 @@
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.Version;
import org.opensearch.common.UUIDs;
import org.opensearch.common.io.VersionedCodecStreamWrapper;
import org.opensearch.common.lucene.Lucene;
import org.opensearch.common.lucene.store.ByteArrayIndexInput;
import org.opensearch.index.store.lockmanager.FileLockInfo;
import org.opensearch.index.store.lockmanager.RemoteStoreCommitLevelLockManager;
Expand Down Expand Up @@ -174,6 +178,7 @@ public static class UploadedSegmentMetadata {
private final String uploadedFilename;
private final String checksum;
private final long length;
private Version writtenBy;

UploadedSegmentMetadata(String originalFilename, String uploadedFilename, String checksum, long length) {
this.originalFilename = originalFilename;
Expand All @@ -184,7 +189,12 @@ public static class UploadedSegmentMetadata {

@Override
public String toString() {
return String.join(SEPARATOR, originalFilename, uploadedFilename, checksum, String.valueOf(length));
String metadataStr = String.join(SEPARATOR, originalFilename, uploadedFilename, checksum, String.valueOf(length));
if (writtenBy != null) {
metadataStr += SEPARATOR + writtenBy;
}

return metadataStr;
}

public String getChecksum() {
Expand All @@ -197,12 +207,25 @@ public long getLength() {

public static UploadedSegmentMetadata fromString(String uploadedFilename) {
String[] values = uploadedFilename.split(SEPARATOR);
return new UploadedSegmentMetadata(values[0], values[1], values[2], Long.parseLong(values[3]));
UploadedSegmentMetadata metadata = new UploadedSegmentMetadata(values[0], values[1], values[2], Long.parseLong(values[3]));
if (values.length == 5) {
metadata.setWrittenBy(values[4]);
}

return metadata;
}

public String getOriginalFilename() {
return originalFilename;
}

public void setWrittenBy(String writtenBy) {
this.writtenBy = Lucene.parseVersionLenient(writtenBy, Version.LATEST);
}

public void setWrittenBy(Version writtenBy) {
this.writtenBy = writtenBy;
}
}

/**
Expand Down Expand Up @@ -467,10 +490,27 @@ public void uploadMetadata(
);
try {
IndexOutput indexOutput = storeDirectory.createOutput(metadataFilename, IOContext.DEFAULT);
List<SegmentCommitInfo> infos = segmentInfosSnapshot.asList();
Map<String, Version> segmentToLuceneVersion = new HashMap<>();
for (SegmentCommitInfo segmentCommitInfo : infos) {
SegmentInfo info = segmentCommitInfo.info;
Set<String> segFiles = info.files();
for (String file : segFiles) {
segmentToLuceneVersion.put(file, info.getVersion());
}
}

Map<String, String> uploadedSegments = new HashMap<>();
for (String file : segmentFiles) {
if (segmentsUploadedToRemoteStore.containsKey(file)) {
uploadedSegments.put(file, segmentsUploadedToRemoteStore.get(file).toString());
UploadedSegmentMetadata metadata = segmentsUploadedToRemoteStore.get(file);
if (segmentToLuceneVersion.containsKey(metadata.originalFilename)) {
metadata.setWrittenBy(segmentToLuceneVersion.get(metadata.originalFilename));
} else if (metadata.originalFilename.equals(segmentInfosSnapshot.getSegmentsFileName())) {
metadata.setWrittenBy(segmentInfosSnapshot.getCommitLuceneVersion());
}

uploadedSegments.put(file, metadata.toString());
} else {
throw new NoSuchFileException(file);
}
Expand Down

0 comments on commit 72fb904

Please sign in to comment.