Skip to content

Commit

Permalink
AOS 2.7 upgrade issue
Browse files Browse the repository at this point in the history
  • Loading branch information
gashutos committed Sep 14, 2023
1 parent eea21c3 commit ea0d57a
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 3 deletions.
6 changes: 5 additions & 1 deletion .idea/runConfigurations/Debug_OpenSearch.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ public IndexService(
// The sort order is validated right after the merge of the mapping later in the process.
this.indexSortSupplier = () -> indexSettings.getIndexSortConfig()
.buildIndexSort(
this.indexSettings.shouldWidenIndexSortType(),
mapperService::fieldType,
(fieldType, searchLookup) -> indexFieldData.getForField(fieldType, indexFieldData.index().getName(), searchLookup)
);
Expand Down
9 changes: 9 additions & 0 deletions server/src/main/java/org/opensearch/index/IndexSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,8 @@ public final class IndexSettings {

private volatile long retentionLeaseMillis;

private final boolean widenIndexSortType;

/**
* The maximum age of a retention lease before it is considered expired.
*
Expand Down Expand Up @@ -821,6 +823,9 @@ public IndexSettings(final IndexMetadata indexMetadata, final Settings nodeSetti
maxFullFlushMergeWaitTime = scopedSettings.get(INDEX_MERGE_ON_FLUSH_MAX_FULL_FLUSH_MERGE_WAIT_TIME);
mergeOnFlushEnabled = scopedSettings.get(INDEX_MERGE_ON_FLUSH_ENABLED);
setMergeOnFlushPolicy(scopedSettings.get(INDEX_MERGE_ON_FLUSH_POLICY));
System.out.println("CHETAN LOGSSSS " +IndexMetadata.SETTING_INDEX_VERSION_CREATED.get(settings));
widenIndexSortType = IndexMetadata.SETTING_INDEX_VERSION_CREATED.get(settings).onOrBefore(LegacyESVersion.V_2_7_1);
System.out.println("CHETAN LOGSSSS " +widenIndexSortType);

scopedSettings.addSettingsUpdateConsumer(MergePolicyConfig.INDEX_COMPOUND_FORMAT_SETTING, mergePolicyConfig::setNoCFSRatio);
scopedSettings.addSettingsUpdateConsumer(
Expand Down Expand Up @@ -1565,4 +1570,8 @@ private void setMergeOnFlushPolicy(String policy) {
public Optional<UnaryOperator<MergePolicy>> getMergeOnFlushPolicy() {
return Optional.ofNullable(mergeOnFlushPolicy);
}

public boolean shouldWidenIndexSortType() {
return this.widenIndexSortType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public boolean hasPrimarySortOnField(String field) {
* or returns null if this index has no sort.
*/
public Sort buildIndexSort(
boolean shouldWidenIndexSortTpe,
Function<String, MappedFieldType> fieldTypeLookup,
BiFunction<MappedFieldType, Supplier<SearchLookup>, IndexFieldData<?>> fieldDataLookup
) {
Expand Down Expand Up @@ -230,7 +231,11 @@ public Sort buildIndexSort(
if (fieldData == null) {
throw new IllegalArgumentException("docvalues not found for index sort field:[" + sortSpec.field + "]");
}
sortFields[i] = fieldData.sortField(sortSpec.missingValue, mode, null, reverse);
if(shouldWidenIndexSortTpe == true) {
sortFields[i] = fieldData.indexSortField(sortSpec.missingValue, mode, null, reverse);
} else {
sortFields[i] = fieldData.sortField(sortSpec.missingValue, mode, null, reverse);
}
validateIndexSortField(sortFields[i]);
}
return new Sort(sortFields);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.index.DirectoryReader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ public interface IndexFieldData<FD extends LeafFieldData> {
*/
SortField sortField(@Nullable Object missingValue, MultiValueMode sortMode, Nested nested, boolean reverse);

/**
* Returns the {@link SortField} to use for index sorting.
*/
default SortField indexSortField(@Nullable Object missingValue, MultiValueMode sortMode, Nested nested, boolean reverse) {
return sortField(missingValue, sortMode, nested, reverse);
}

/**
* Build a sort implementation specialized for aggregations.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,15 @@ public enum NumericType {

private final boolean floatingPoint;
private final ValuesSourceType valuesSourceType;
private final SortField.Type sortFieldType;
private SortField.Type sortFieldType;

private boolean usePointBasedOptimization;

NumericType(boolean floatingPoint, SortField.Type sortFieldType, ValuesSourceType valuesSourceType) {
this.floatingPoint = floatingPoint;
this.sortFieldType = sortFieldType;
this.valuesSourceType = valuesSourceType;
this.usePointBasedOptimization = true;
}

public final boolean isFloatingPoint() {
Expand All @@ -94,6 +97,11 @@ public final boolean isFloatingPoint() {
public final ValuesSourceType getValuesSourceType() {
return valuesSourceType;
}

public void setSortFieldType(SortField.Type type) {
this.sortFieldType = type;
this.usePointBasedOptimization = false; // Disable optimization if we set this
}
}

/**
Expand Down Expand Up @@ -126,6 +134,7 @@ public final SortField sortField(
|| nested != null
|| (sortMode != MultiValueMode.MAX && sortMode != MultiValueMode.MIN)
|| targetNumericType != getNumericType()) {
System.out.println("Custom comparator logic.....");
return new SortField(getFieldName(), source, reverse);
}

Expand All @@ -134,6 +143,9 @@ public final SortField sortField(
: SortedNumericSelector.Type.MIN;
SortField sortField = new SortedNumericSortField(getFieldName(), getNumericType().sortFieldType, reverse, selectorType);
sortField.setMissingValue(source.missingObject(missingValue, reverse));
if(getNumericType().usePointBasedOptimization == false) {
sortField.setOptimizeSortWithPoints(false);
}
return sortField;
}

Expand All @@ -149,6 +161,16 @@ public final SortField sortField(Object missingValue, MultiValueMode sortMode, N
return sortField(getNumericType(), missingValue, sortMode, nested, reverse);
}

@Override
public final SortField indexSortField(Object missingValue, MultiValueMode sortMode, Nested nested, boolean reverse) {
switch(getNumericType().sortFieldType) {
case INT:
getNumericType().setSortFieldType(NumericType.LONG.sortFieldType);
break;
}
return sortField(getNumericType(), missingValue, sortMode, nested, reverse);
}

/**
* Builds a {@linkplain BucketedSort} for the {@code targetNumericType},
* casting the values if their native type doesn't match.
Expand Down Expand Up @@ -207,8 +229,12 @@ private XFieldComparatorSource comparatorSource(
case DATE_NANOSECONDS:
return dateNanosComparatorSource(missingValue, sortMode, nested);
case LONG:
System.out.println("CHETAN logs LONG");
return new LongValuesComparatorSource(this, missingValue, sortMode, nested);
default:
if(getNumericType().sortFieldType == SortField.Type.LONG) {
return new LongValuesComparatorSource(this, missingValue, sortMode, nested);
}
assert !targetNumericType.isFloatingPoint();
return new IntValuesComparatorSource(this, missingValue, sortMode, nested);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ public SortFieldAndFormat build(QueryShardContext context) throws IOException {
}
IndexNumericFieldData numericFieldData = (IndexNumericFieldData) fieldData;
NumericType resolvedType = resolveNumericType(numericType);
System.out.println("CHETAN LOGS : " + resolvedType);
field = numericFieldData.sortField(resolvedType, missing, localSortMode(), nested, reverse);
isNanosecond = resolvedType == NumericType.DATE_NANOSECONDS;
} else {
Expand Down

0 comments on commit ea0d57a

Please sign in to comment.