Skip to content

Commit 4c1a7f3

Browse files
committed
Replace working with exceptions with custom comparator
Signed-off-by: Aleksandr Tuliakov <tulyakov@yandex-team.ru>
1 parent bfe6dab commit 4c1a7f3

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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.apache.lucene.codecs.lucene90;
10+
11+
/**
12+
* This class is a custom comparator for Lucene90StoredFieldsFormat
13+
*/
14+
public class Lucene90StoredFieldsFormatComparator {
15+
public static boolean equal(Lucene90StoredFieldsFormat one, Lucene90StoredFieldsFormat two) {
16+
return one.mode == two.mode;
17+
}
18+
}

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,13 @@
3535
import org.apache.logging.log4j.LogManager;
3636
import org.apache.logging.log4j.Logger;
3737
import org.apache.lucene.codecs.Codec;
38-
import org.apache.lucene.codecs.StoredFieldsWriter;
38+
import org.apache.lucene.codecs.lucene90.Lucene90StoredFieldsFormat;
39+
import org.apache.lucene.codecs.lucene90.Lucene90StoredFieldsFormatComparator;
3940
import org.apache.lucene.index.FilterMergePolicy;
4041
import org.apache.lucene.index.IndexWriter;
4142
import org.apache.lucene.index.MergePolicy;
4243
import org.apache.lucene.index.SegmentCommitInfo;
4344
import org.apache.lucene.index.SegmentInfos;
44-
import org.apache.lucene.store.ByteBuffersDirectory;
45-
import org.apache.lucene.store.IOContext;
4645
import org.opensearch.Version;
4746

4847
import java.io.IOException;
@@ -112,18 +111,13 @@ private boolean shouldUpgrade(SegmentCommitInfo info) {
112111
}
113112

114113
if (info.info.getAttributes() != null) {
115-
// Only way to compare Codec.mode(BEST_SPEED or BEST_COMPRESSION) is try to create fields writer
116-
// and catch an IllegalStateException if they are different, and ignore others exceptions
117-
try {
118-
StoredFieldsWriter writer = actualCodec.storedFieldsFormat().fieldsWriter(new ByteBuffersDirectory(), info.info, IOContext.DEFAULT);
119-
writer.close();
120-
} catch (IllegalStateException e) {
121-
// fieldsWriter throw an exception if previous mode not equal to current
122-
logger.debug("Received expected IllegalStateException", e);
123-
return true;
124-
} catch (Exception e) {
125-
// Most of the time it will be java.nio.file.FileAlreadyExistsException, which means codec mode same as should be,
126-
// otherwise it is something wrong and will be processed somewhere else
114+
if (info.info.getCodec().storedFieldsFormat() instanceof Lucene90StoredFieldsFormat
115+
&& actualCodec.storedFieldsFormat() instanceof Lucene90StoredFieldsFormat) {
116+
var current = (Lucene90StoredFieldsFormat) info.info.getCodec().storedFieldsFormat();
117+
var target = (Lucene90StoredFieldsFormat) actualCodec.storedFieldsFormat();
118+
if (!Lucene90StoredFieldsFormatComparator.equal(current, target)) {
119+
return true;
120+
}
127121
}
128122
}
129123

0 commit comments

Comments
 (0)