Skip to content

Commit 58a23b2

Browse files
committed
PR comments
Signed-off-by: Kartik Ganesh <[email protected]>
1 parent 78681fb commit 58a23b2

File tree

3 files changed

+67
-13
lines changed

3 files changed

+67
-13
lines changed

server/src/main/java/org/opensearch/index/shard/IndexShard.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -2039,16 +2039,7 @@ private void innerOpenEngineAndTranslog(LongSupplier globalCheckpointSupplier) t
20392039
}
20402040

20412041
private boolean assertSequenceNumbersInCommit() throws IOException {
2042-
final Map<String, String> userData;
2043-
if (indexSettings.isRemoteSnapshot() && indexSettings.getExtendedCompatibilitySnapshotVersion() != null) {
2044-
// Inefficient method to support reading old Lucene indexes
2045-
userData = Lucene.readSegmentInfosExtendedCompatibility(
2046-
store.directory(),
2047-
indexSettings.getExtendedCompatibilitySnapshotVersion()
2048-
).getUserData();
2049-
} else {
2050-
userData = SegmentInfos.readLatestCommit(store.directory()).getUserData();
2051-
}
2042+
final Map<String, String> userData = fetchUserData();
20522043
assert userData.containsKey(SequenceNumbers.LOCAL_CHECKPOINT_KEY) : "commit point doesn't contains a local checkpoint";
20532044
assert userData.containsKey(MAX_SEQ_NO) : "commit point doesn't contains a maximum sequence number";
20542045
assert userData.containsKey(Engine.HISTORY_UUID_KEY) : "commit point doesn't contains a history uuid";
@@ -2063,6 +2054,16 @@ private boolean assertSequenceNumbersInCommit() throws IOException {
20632054
return true;
20642055
}
20652056

2057+
private Map<String, String> fetchUserData() throws IOException {
2058+
if (indexSettings.isRemoteSnapshot() && indexSettings.getExtendedCompatibilitySnapshotVersion() != null) {
2059+
// Inefficient method to support reading old Lucene indexes
2060+
return Lucene.readSegmentInfosExtendedCompatibility(store.directory(), indexSettings.getExtendedCompatibilitySnapshotVersion())
2061+
.getUserData();
2062+
} else {
2063+
return SegmentInfos.readLatestCommit(store.directory()).getUserData();
2064+
}
2065+
}
2066+
20662067
private void onNewEngine(Engine newEngine) {
20672068
assert Thread.holdsLock(engineMutex);
20682069
refreshListeners.setCurrentRefreshLocationSupplier(newEngine::getTranslogLastWriteLocation);

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public SegmentInfos readLastCommittedSegmentsInfo() throws IOException {
222222
failIfCorrupted();
223223
try {
224224
if (indexSettings.isRemoteSnapshot() && indexSettings.getExtendedCompatibilitySnapshotVersion() != null) {
225-
return readSegmentInfosExtendedCompatbility(directory(), indexSettings.getExtendedCompatibilitySnapshotVersion());
225+
return readSegmentInfosExtendedCompatibility(directory(), indexSettings.getExtendedCompatibilitySnapshotVersion());
226226
} else {
227227
return readSegmentsInfo(null, directory());
228228
}
@@ -235,7 +235,7 @@ public SegmentInfos readLastCommittedSegmentsInfo() throws IOException {
235235
/**
236236
* Returns the segments info for the given commit or for the latest commit if the given commit is <code>null</code>.
237237
* This method will throw an exception if the index is older than the standard backwards compatibility
238-
* policy ( current major - 1). See also {@link #readSegmentInfosExtendedCompatbility(Directory, org.opensearch.Version)}.
238+
* policy ( current major - 1). See also {@link #readSegmentInfosExtendedCompatibility(Directory, org.opensearch.Version)}.
239239
*
240240
* @throws IOException if the index is corrupted or the segments file is not present
241241
*/
@@ -260,7 +260,7 @@ private static SegmentInfos readSegmentsInfo(IndexCommit commit, Directory direc
260260
*
261261
* @throws IOException if the index is corrupted or the segments file is not present
262262
*/
263-
private static SegmentInfos readSegmentInfosExtendedCompatbility(Directory directory, org.opensearch.Version minimumVersion)
263+
private static SegmentInfos readSegmentInfosExtendedCompatibility(Directory directory, org.opensearch.Version minimumVersion)
264264
throws IOException {
265265
try {
266266
return Lucene.readSegmentInfosExtendedCompatibility(directory, minimumVersion);

server/src/test/java/org/opensearch/common/lucene/LuceneTests.java

+53
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,59 @@ public void testReadSegmentInfosExtendedCompatibility() throws IOException {
407407
dir.close();
408408
}
409409

410+
/**
411+
* Since the implementation in {@link Lucene#readSegmentInfosExtendedCompatibility(Directory, Version)}
412+
* is a workaround, this test verifies that the response from this method is equivalent to
413+
* {@link Lucene#readSegmentInfos(Directory)} if the version is N-1
414+
*/
415+
public void testReadSegmentInfosExtendedCompatibilityBaseCase() throws IOException {
416+
MockDirectoryWrapper dir = newMockDirectory();
417+
IndexWriterConfig iwc = newIndexWriterConfig();
418+
IndexWriter writer = new IndexWriter(dir, iwc);
419+
Document doc = new Document();
420+
doc.add(new TextField("id", "1", random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
421+
writer.addDocument(doc);
422+
writer.commit();
423+
SegmentInfos expectedSI = Lucene.readSegmentInfos(dir);
424+
SegmentInfos actualSI = Lucene.readSegmentInfosExtendedCompatibility(dir, Version.CURRENT);
425+
assertEquals(Lucene.getNumDocs(expectedSI), Lucene.getNumDocs(actualSI));
426+
assertEquals(expectedSI.getGeneration(), actualSI.getGeneration());
427+
assertEquals(expectedSI.getSegmentsFileName(), actualSI.getSegmentsFileName());
428+
assertEquals(expectedSI.getVersion(), actualSI.getVersion());
429+
assertEquals(expectedSI.getCommitLuceneVersion(), actualSI.getCommitLuceneVersion());
430+
assertEquals(expectedSI.getMinSegmentLuceneVersion(), actualSI.getMinSegmentLuceneVersion());
431+
assertEquals(expectedSI.getIndexCreatedVersionMajor(), actualSI.getIndexCreatedVersionMajor());
432+
assertEquals(expectedSI.getUserData(), actualSI.getUserData());
433+
434+
int numDocsToIndex = randomIntBetween(10, 50);
435+
List<Term> deleteTerms = new ArrayList<>();
436+
for (int i = 0; i < numDocsToIndex; i++) {
437+
doc = new Document();
438+
doc.add(new TextField("id", "doc_" + i, random().nextBoolean() ? Field.Store.YES : Field.Store.NO));
439+
deleteTerms.add(new Term("id", "doc_" + i));
440+
writer.addDocument(doc);
441+
}
442+
int numDocsToDelete = randomIntBetween(0, numDocsToIndex);
443+
Collections.shuffle(deleteTerms, random());
444+
for (int i = 0; i < numDocsToDelete; i++) {
445+
Term remove = deleteTerms.remove(0);
446+
writer.deleteDocuments(remove);
447+
}
448+
writer.commit();
449+
expectedSI = Lucene.readSegmentInfos(dir);
450+
actualSI = Lucene.readSegmentInfosExtendedCompatibility(dir, Version.CURRENT);
451+
assertEquals(Lucene.getNumDocs(expectedSI), Lucene.getNumDocs(actualSI));
452+
assertEquals(expectedSI.getGeneration(), actualSI.getGeneration());
453+
assertEquals(expectedSI.getSegmentsFileName(), actualSI.getSegmentsFileName());
454+
assertEquals(expectedSI.getVersion(), actualSI.getVersion());
455+
assertEquals(expectedSI.getCommitLuceneVersion(), actualSI.getCommitLuceneVersion());
456+
assertEquals(expectedSI.getMinSegmentLuceneVersion(), actualSI.getMinSegmentLuceneVersion());
457+
assertEquals(expectedSI.getIndexCreatedVersionMajor(), actualSI.getIndexCreatedVersionMajor());
458+
assertEquals(expectedSI.getUserData(), actualSI.getUserData());
459+
writer.close();
460+
dir.close();
461+
}
462+
410463
public void testCount() throws Exception {
411464
Directory dir = newDirectory();
412465
RandomIndexWriter w = new RandomIndexWriter(random(), dir);

0 commit comments

Comments
 (0)