Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
Signed-off-by: Kartik Ganesh <gkart@amazon.com>
  • Loading branch information
kartg committed Dec 15, 2022
1 parent 78681fb commit 58a23b2
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
21 changes: 11 additions & 10 deletions server/src/main/java/org/opensearch/index/shard/IndexShard.java
Original file line number Diff line number Diff line change
Expand Up @@ -2039,16 +2039,7 @@ private void innerOpenEngineAndTranslog(LongSupplier globalCheckpointSupplier) t
}

private boolean assertSequenceNumbersInCommit() throws IOException {
final Map<String, String> userData;
if (indexSettings.isRemoteSnapshot() && indexSettings.getExtendedCompatibilitySnapshotVersion() != null) {
// Inefficient method to support reading old Lucene indexes
userData = Lucene.readSegmentInfosExtendedCompatibility(
store.directory(),
indexSettings.getExtendedCompatibilitySnapshotVersion()
).getUserData();
} else {
userData = SegmentInfos.readLatestCommit(store.directory()).getUserData();
}
final Map<String, String> userData = fetchUserData();
assert userData.containsKey(SequenceNumbers.LOCAL_CHECKPOINT_KEY) : "commit point doesn't contains a local checkpoint";
assert userData.containsKey(MAX_SEQ_NO) : "commit point doesn't contains a maximum sequence number";
assert userData.containsKey(Engine.HISTORY_UUID_KEY) : "commit point doesn't contains a history uuid";
Expand All @@ -2063,6 +2054,16 @@ private boolean assertSequenceNumbersInCommit() throws IOException {
return true;
}

private Map<String, String> fetchUserData() throws IOException {
if (indexSettings.isRemoteSnapshot() && indexSettings.getExtendedCompatibilitySnapshotVersion() != null) {
// Inefficient method to support reading old Lucene indexes
return Lucene.readSegmentInfosExtendedCompatibility(store.directory(), indexSettings.getExtendedCompatibilitySnapshotVersion())
.getUserData();
} else {
return SegmentInfos.readLatestCommit(store.directory()).getUserData();
}
}

private void onNewEngine(Engine newEngine) {
assert Thread.holdsLock(engineMutex);
refreshListeners.setCurrentRefreshLocationSupplier(newEngine::getTranslogLastWriteLocation);
Expand Down
6 changes: 3 additions & 3 deletions server/src/main/java/org/opensearch/index/store/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public SegmentInfos readLastCommittedSegmentsInfo() throws IOException {
failIfCorrupted();
try {
if (indexSettings.isRemoteSnapshot() && indexSettings.getExtendedCompatibilitySnapshotVersion() != null) {
return readSegmentInfosExtendedCompatbility(directory(), indexSettings.getExtendedCompatibilitySnapshotVersion());
return readSegmentInfosExtendedCompatibility(directory(), indexSettings.getExtendedCompatibilitySnapshotVersion());
} else {
return readSegmentsInfo(null, directory());
}
Expand All @@ -235,7 +235,7 @@ public SegmentInfos readLastCommittedSegmentsInfo() throws IOException {
/**
* Returns the segments info for the given commit or for the latest commit if the given commit is <code>null</code>.
* This method will throw an exception if the index is older than the standard backwards compatibility
* policy ( current major - 1). See also {@link #readSegmentInfosExtendedCompatbility(Directory, org.opensearch.Version)}.
* policy ( current major - 1). See also {@link #readSegmentInfosExtendedCompatibility(Directory, org.opensearch.Version)}.
*
* @throws IOException if the index is corrupted or the segments file is not present
*/
Expand All @@ -260,7 +260,7 @@ private static SegmentInfos readSegmentsInfo(IndexCommit commit, Directory direc
*
* @throws IOException if the index is corrupted or the segments file is not present
*/
private static SegmentInfos readSegmentInfosExtendedCompatbility(Directory directory, org.opensearch.Version minimumVersion)
private static SegmentInfos readSegmentInfosExtendedCompatibility(Directory directory, org.opensearch.Version minimumVersion)
throws IOException {
try {
return Lucene.readSegmentInfosExtendedCompatibility(directory, minimumVersion);
Expand Down
53 changes: 53 additions & 0 deletions server/src/test/java/org/opensearch/common/lucene/LuceneTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,59 @@ public void testReadSegmentInfosExtendedCompatibility() throws IOException {
dir.close();
}

/**
* Since the implementation in {@link Lucene#readSegmentInfosExtendedCompatibility(Directory, Version)}
* is a workaround, this test verifies that the response from this method is equivalent to
* {@link Lucene#readSegmentInfos(Directory)} if the version is N-1
*/
public void testReadSegmentInfosExtendedCompatibilityBaseCase() throws IOException {
MockDirectoryWrapper dir = newMockDirectory();
IndexWriterConfig iwc = newIndexWriterConfig();
IndexWriter writer = new IndexWriter(dir, iwc);
Document doc = new Document();
doc.add(new TextField("id", "1", random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
writer.addDocument(doc);
writer.commit();
SegmentInfos expectedSI = Lucene.readSegmentInfos(dir);
SegmentInfos actualSI = Lucene.readSegmentInfosExtendedCompatibility(dir, Version.CURRENT);
assertEquals(Lucene.getNumDocs(expectedSI), Lucene.getNumDocs(actualSI));
assertEquals(expectedSI.getGeneration(), actualSI.getGeneration());
assertEquals(expectedSI.getSegmentsFileName(), actualSI.getSegmentsFileName());
assertEquals(expectedSI.getVersion(), actualSI.getVersion());
assertEquals(expectedSI.getCommitLuceneVersion(), actualSI.getCommitLuceneVersion());
assertEquals(expectedSI.getMinSegmentLuceneVersion(), actualSI.getMinSegmentLuceneVersion());
assertEquals(expectedSI.getIndexCreatedVersionMajor(), actualSI.getIndexCreatedVersionMajor());
assertEquals(expectedSI.getUserData(), actualSI.getUserData());

int numDocsToIndex = randomIntBetween(10, 50);
List<Term> deleteTerms = new ArrayList<>();
for (int i = 0; i < numDocsToIndex; i++) {
doc = new Document();
doc.add(new TextField("id", "doc_" + i, random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
deleteTerms.add(new Term("id", "doc_" + i));
writer.addDocument(doc);
}
int numDocsToDelete = randomIntBetween(0, numDocsToIndex);
Collections.shuffle(deleteTerms, random());
for (int i = 0; i < numDocsToDelete; i++) {
Term remove = deleteTerms.remove(0);
writer.deleteDocuments(remove);
}
writer.commit();
expectedSI = Lucene.readSegmentInfos(dir);
actualSI = Lucene.readSegmentInfosExtendedCompatibility(dir, Version.CURRENT);
assertEquals(Lucene.getNumDocs(expectedSI), Lucene.getNumDocs(actualSI));
assertEquals(expectedSI.getGeneration(), actualSI.getGeneration());
assertEquals(expectedSI.getSegmentsFileName(), actualSI.getSegmentsFileName());
assertEquals(expectedSI.getVersion(), actualSI.getVersion());
assertEquals(expectedSI.getCommitLuceneVersion(), actualSI.getCommitLuceneVersion());
assertEquals(expectedSI.getMinSegmentLuceneVersion(), actualSI.getMinSegmentLuceneVersion());
assertEquals(expectedSI.getIndexCreatedVersionMajor(), actualSI.getIndexCreatedVersionMajor());
assertEquals(expectedSI.getUserData(), actualSI.getUserData());
writer.close();
dir.close();
}

public void testCount() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir);
Expand Down

0 comments on commit 58a23b2

Please sign in to comment.