Skip to content

Commit aa50d4f

Browse files
author
Tianli Feng
committed
Merge remote-tracking branch 'upstream/main' into add-cluster-manager-timeout
Signed-off-by: Tianli Feng <ftianli@amazon.com>
2 parents 3f46f4e + 5c0f9bc commit aa50d4f

33 files changed

+360
-252
lines changed

plugins/repository-azure/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ dependencies {
6767
api "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:${versions.jackson}"
6868
api "com.fasterxml.jackson.module:jackson-module-jaxb-annotations:${versions.jackson}"
6969
api 'org.codehaus.woodstox:stax2-api:4.2.1'
70-
implementation 'com.fasterxml.woodstox:woodstox-core:6.1.1'
70+
implementation 'com.fasterxml.woodstox:woodstox-core:6.2.8'
7171
runtimeOnly 'com.google.guava:guava:31.1-jre'
72-
api 'org.apache.commons:commons-lang3:3.4'
72+
api 'org.apache.commons:commons-lang3:3.12.0'
7373
testImplementation project(':test:fixtures:azure-fixture')
7474
}
7575

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c6842c86792ff03b9f1d1fe2aab8dc23aa6c6f0e

plugins/repository-azure/licenses/commons-lang3-3.4.jar.sha1

-1
This file was deleted.

plugins/repository-azure/licenses/woodstox-core-6.1.1.jar.sha1

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
670748292899c53b1963730d9eb7f8ab71314e90

server/src/internalClusterTest/java/org/opensearch/validate/SimpleValidateQueryIT.java

+97
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262

6363
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS;
6464
import static org.opensearch.index.query.QueryBuilders.queryStringQuery;
65+
import static org.opensearch.index.query.QueryBuilders.rangeQuery;
6566
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked;
6667
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertNoFailures;
6768
import static org.hamcrest.Matchers.allOf;
@@ -500,4 +501,100 @@ public void testExplainTermsQueryWithLookup() throws Exception {
500501
.actionGet();
501502
assertThat(response.isValid(), is(true));
502503
}
504+
505+
// Issue: https://github.com/opensearch-project/OpenSearch/issues/2036
506+
public void testValidateDateRangeInQueryString() throws IOException {
507+
assertAcked(prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put("index.number_of_shards", 1)));
508+
509+
assertAcked(
510+
client().admin()
511+
.indices()
512+
.preparePutMapping("test")
513+
.setSource(
514+
XContentFactory.jsonBuilder()
515+
.startObject()
516+
.startObject(MapperService.SINGLE_MAPPING_NAME)
517+
.startObject("properties")
518+
.startObject("name")
519+
.field("type", "keyword")
520+
.endObject()
521+
.startObject("timestamp")
522+
.field("type", "date")
523+
.endObject()
524+
.endObject()
525+
.endObject()
526+
.endObject()
527+
)
528+
);
529+
530+
client().prepareIndex("test").setId("1").setSource("name", "username", "timestamp", 200).get();
531+
refresh();
532+
533+
ValidateQueryResponse response = client().admin()
534+
.indices()
535+
.prepareValidateQuery()
536+
.setQuery(
537+
QueryBuilders.boolQuery()
538+
.must(rangeQuery("timestamp").gte(0).lte(100))
539+
.must(queryStringQuery("username").allowLeadingWildcard(false))
540+
)
541+
.setRewrite(true)
542+
.get();
543+
544+
assertNoFailures(response);
545+
assertThat(response.isValid(), is(true));
546+
547+
// Use wildcard and date outside the range
548+
response = client().admin()
549+
.indices()
550+
.prepareValidateQuery()
551+
.setQuery(
552+
QueryBuilders.boolQuery()
553+
.must(rangeQuery("timestamp").gte(0).lte(100))
554+
.must(queryStringQuery("*erna*").allowLeadingWildcard(false))
555+
)
556+
.setRewrite(true)
557+
.get();
558+
559+
assertNoFailures(response);
560+
assertThat(response.isValid(), is(false));
561+
562+
// Use wildcard and date inside the range
563+
response = client().admin()
564+
.indices()
565+
.prepareValidateQuery()
566+
.setQuery(
567+
QueryBuilders.boolQuery()
568+
.must(rangeQuery("timestamp").gte(0).lte(1000))
569+
.must(queryStringQuery("*erna*").allowLeadingWildcard(false))
570+
)
571+
.setRewrite(true)
572+
.get();
573+
574+
assertNoFailures(response);
575+
assertThat(response.isValid(), is(false));
576+
577+
// Use wildcard and date inside the range (allow leading wildcard)
578+
response = client().admin()
579+
.indices()
580+
.prepareValidateQuery()
581+
.setQuery(QueryBuilders.boolQuery().must(rangeQuery("timestamp").gte(0).lte(1000)).must(queryStringQuery("*erna*")))
582+
.setRewrite(true)
583+
.get();
584+
585+
assertNoFailures(response);
586+
assertThat(response.isValid(), is(true));
587+
588+
// Use invalid date range
589+
response = client().admin()
590+
.indices()
591+
.prepareValidateQuery()
592+
.setQuery(QueryBuilders.boolQuery().must(rangeQuery("timestamp").gte("aaa").lte(100)))
593+
.setRewrite(true)
594+
.get();
595+
596+
assertNoFailures(response);
597+
assertThat(response.isValid(), is(false));
598+
599+
}
503600
}

server/src/main/java/org/opensearch/action/admin/indices/validate/query/TransportValidateQueryAction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ protected void doExecute(Task task, ValidateQueryRequest request, ActionListener
131131
if (request.query() == null) {
132132
rewriteListener.onResponse(request.query());
133133
} else {
134-
Rewriteable.rewriteAndFetch(request.query(), searchService.getRewriteContext(timeProvider), rewriteListener);
134+
Rewriteable.rewriteAndFetch(request.query(), searchService.getValidationRewriteContext(timeProvider), rewriteListener);
135135
}
136136
}
137137

@@ -225,7 +225,7 @@ protected ShardValidateQueryResponse shardOperation(ShardValidateQueryRequest re
225225
request.nowInMillis(),
226226
request.filteringAliases()
227227
);
228-
SearchContext searchContext = searchService.createSearchContext(shardSearchLocalRequest, SearchService.NO_TIMEOUT);
228+
SearchContext searchContext = searchService.createValidationContext(shardSearchLocalRequest, SearchService.NO_TIMEOUT);
229229
try {
230230
ParsedQuery parsedQuery = searchContext.getQueryShardContext().toQuery(request.query());
231231
searchContext.parsedQuery(parsedQuery);

server/src/main/java/org/opensearch/index/IndexService.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,22 @@ public IndexSettings getIndexSettings() {
630630
* {@link IndexReader}-specific optimizations, such as rewriting containing range queries.
631631
*/
632632
public QueryShardContext newQueryShardContext(int shardId, IndexSearcher searcher, LongSupplier nowInMillis, String clusterAlias) {
633+
return newQueryShardContext(shardId, searcher, nowInMillis, clusterAlias, false);
634+
}
635+
636+
/**
637+
* Creates a new QueryShardContext.
638+
*
639+
* Passing a {@code null} {@link IndexSearcher} will return a valid context, however it won't be able to make
640+
* {@link IndexReader}-specific optimizations, such as rewriting containing range queries.
641+
*/
642+
public QueryShardContext newQueryShardContext(
643+
int shardId,
644+
IndexSearcher searcher,
645+
LongSupplier nowInMillis,
646+
String clusterAlias,
647+
boolean validate
648+
) {
633649
final SearchIndexNameMatcher indexNameMatcher = new SearchIndexNameMatcher(
634650
index().getName(),
635651
clusterAlias,
@@ -653,7 +669,8 @@ public QueryShardContext newQueryShardContext(int shardId, IndexSearcher searche
653669
clusterAlias,
654670
indexNameMatcher,
655671
allowExpensiveQueries,
656-
valuesSourceRegistry
672+
valuesSourceRegistry,
673+
validate
657674
);
658675
}
659676

server/src/main/java/org/opensearch/index/engine/Engine.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
import org.opensearch.common.unit.TimeValue;
7373
import org.opensearch.common.util.concurrent.ReleasableLock;
7474
import org.opensearch.index.VersionType;
75-
import org.opensearch.index.mapper.MapperService;
7675
import org.opensearch.index.mapper.Mapping;
7776
import org.opensearch.index.mapper.ParseContext.Document;
7877
import org.opensearch.index.mapper.ParsedDocument;
@@ -736,13 +735,8 @@ public enum SearcherScope {
736735
* Creates a new history snapshot from Lucene for reading operations whose seqno in the requesting seqno range (both inclusive).
737736
* This feature requires soft-deletes enabled. If soft-deletes are disabled, this method will throw an {@link IllegalStateException}.
738737
*/
739-
public abstract Translog.Snapshot newChangesSnapshot(
740-
String source,
741-
MapperService mapperService,
742-
long fromSeqNo,
743-
long toSeqNo,
744-
boolean requiredFullRange
745-
) throws IOException;
738+
public abstract Translog.Snapshot newChangesSnapshot(String source, long fromSeqNo, long toSeqNo, boolean requiredFullRange)
739+
throws IOException;
746740

747741
public abstract boolean hasCompleteOperationHistory(String reason, long startingSeqNo);
748742

server/src/main/java/org/opensearch/index/engine/InternalEngine.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
import org.opensearch.index.VersionType;
9292
import org.opensearch.index.fieldvisitor.IdOnlyFieldVisitor;
9393
import org.opensearch.index.mapper.IdFieldMapper;
94-
import org.opensearch.index.mapper.MapperService;
9594
import org.opensearch.index.mapper.ParseContext;
9695
import org.opensearch.index.mapper.ParsedDocument;
9796
import org.opensearch.index.mapper.SeqNoFieldMapper;
@@ -2773,20 +2772,13 @@ long getNumDocUpdates() {
27732772
}
27742773

27752774
@Override
2776-
public Translog.Snapshot newChangesSnapshot(
2777-
String source,
2778-
MapperService mapperService,
2779-
long fromSeqNo,
2780-
long toSeqNo,
2781-
boolean requiredFullRange
2782-
) throws IOException {
2775+
public Translog.Snapshot newChangesSnapshot(String source, long fromSeqNo, long toSeqNo, boolean requiredFullRange) throws IOException {
27832776
ensureOpen();
27842777
refreshIfNeeded(source, toSeqNo);
27852778
Searcher searcher = acquireSearcher(source, SearcherScope.INTERNAL);
27862779
try {
27872780
LuceneChangesSnapshot snapshot = new LuceneChangesSnapshot(
27882781
searcher,
2789-
mapperService,
27902782
LuceneChangesSnapshot.DEFAULT_BATCH_SIZE,
27912783
fromSeqNo,
27922784
toSeqNo,

server/src/main/java/org/opensearch/index/engine/LuceneChangesSnapshot.java

+5-20
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.apache.lucene.index.LeafReader;
3737
import org.apache.lucene.index.LeafReaderContext;
3838
import org.apache.lucene.index.NumericDocValues;
39-
import org.apache.lucene.index.Term;
4039
import org.apache.lucene.search.BooleanClause;
4140
import org.apache.lucene.search.BooleanQuery;
4241
import org.apache.lucene.search.DocValuesFieldExistsQuery;
@@ -51,11 +50,8 @@
5150
import org.opensearch.common.lucene.Lucene;
5251
import org.opensearch.core.internal.io.IOUtils;
5352
import org.opensearch.index.fieldvisitor.FieldsVisitor;
54-
import org.opensearch.index.mapper.IdFieldMapper;
55-
import org.opensearch.index.mapper.MapperService;
5653
import org.opensearch.index.mapper.SeqNoFieldMapper;
5754
import org.opensearch.index.mapper.SourceFieldMapper;
58-
import org.opensearch.index.mapper.Uid;
5955
import org.opensearch.index.translog.Translog;
6056

6157
import java.io.Closeable;
@@ -77,7 +73,6 @@ final class LuceneChangesSnapshot implements Translog.Snapshot {
7773
private final boolean requiredFullRange;
7874

7975
private final IndexSearcher indexSearcher;
80-
private final MapperService mapperService;
8176
private int docIndex = 0;
8277
private final int totalHits;
8378
private ScoreDoc[] scoreDocs;
@@ -88,20 +83,13 @@ final class LuceneChangesSnapshot implements Translog.Snapshot {
8883
* Creates a new "translog" snapshot from Lucene for reading operations whose seq# in the specified range.
8984
*
9085
* @param engineSearcher the internal engine searcher which will be taken over if the snapshot is opened successfully
91-
* @param mapperService the mapper service which will be mainly used to resolve the document's type and uid
9286
* @param searchBatchSize the number of documents should be returned by each search
9387
* @param fromSeqNo the min requesting seq# - inclusive
9488
* @param toSeqNo the maximum requesting seq# - inclusive
9589
* @param requiredFullRange if true, the snapshot will strictly check for the existence of operations between fromSeqNo and toSeqNo
9690
*/
97-
LuceneChangesSnapshot(
98-
Engine.Searcher engineSearcher,
99-
MapperService mapperService,
100-
int searchBatchSize,
101-
long fromSeqNo,
102-
long toSeqNo,
103-
boolean requiredFullRange
104-
) throws IOException {
91+
LuceneChangesSnapshot(Engine.Searcher engineSearcher, int searchBatchSize, long fromSeqNo, long toSeqNo, boolean requiredFullRange)
92+
throws IOException {
10593
if (fromSeqNo < 0 || toSeqNo < 0 || fromSeqNo > toSeqNo) {
10694
throw new IllegalArgumentException("Invalid range; from_seqno [" + fromSeqNo + "], to_seqno [" + toSeqNo + "]");
10795
}
@@ -114,7 +102,6 @@ final class LuceneChangesSnapshot implements Translog.Snapshot {
114102
IOUtils.close(engineSearcher);
115103
}
116104
};
117-
this.mapperService = mapperService;
118105
final long requestingSize = (toSeqNo - fromSeqNo) == Long.MAX_VALUE ? Long.MAX_VALUE : (toSeqNo - fromSeqNo + 1L);
119106
this.searchBatchSize = requestingSize < searchBatchSize ? Math.toIntExact(requestingSize) : searchBatchSize;
120107
this.fromSeqNo = fromSeqNo;
@@ -278,19 +265,17 @@ private Translog.Operation readDocAsOp(int docIndex) throws IOException {
278265
: SourceFieldMapper.NAME;
279266
final FieldsVisitor fields = new FieldsVisitor(true, sourceField);
280267
leaf.reader().document(segmentDocID, fields);
281-
fields.postProcess(mapperService);
282268

283269
final Translog.Operation op;
284270
final boolean isTombstone = parallelArray.isTombStone[docIndex];
285-
if (isTombstone && fields.uid() == null) {
271+
if (isTombstone && fields.id() == null) {
286272
op = new Translog.NoOp(seqNo, primaryTerm, fields.source().utf8ToString());
287273
assert version == 1L : "Noop tombstone should have version 1L; actual version [" + version + "]";
288274
assert assertDocSoftDeleted(leaf.reader(), segmentDocID) : "Noop but soft_deletes field is not set [" + op + "]";
289275
} else {
290-
final String id = fields.uid().id();
291-
final Term uid = new Term(IdFieldMapper.NAME, Uid.encodeId(id));
276+
final String id = fields.id();
292277
if (isTombstone) {
293-
op = new Translog.Delete(id, uid, seqNo, primaryTerm, version);
278+
op = new Translog.Delete(id, seqNo, primaryTerm, version);
294279
assert assertDocSoftDeleted(leaf.reader(), segmentDocID) : "Delete op but soft_deletes field is not set [" + op + "]";
295280
} else {
296281
final BytesReference source = fields.source();

server/src/main/java/org/opensearch/index/engine/ReadOnlyEngine.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import org.opensearch.common.lucene.index.OpenSearchDirectoryReader;
4747
import org.opensearch.common.util.concurrent.ReleasableLock;
4848
import org.opensearch.core.internal.io.IOUtils;
49-
import org.opensearch.index.mapper.MapperService;
5049
import org.opensearch.index.seqno.SeqNoStats;
5150
import org.opensearch.index.seqno.SequenceNumbers;
5251
import org.opensearch.index.store.Store;
@@ -326,13 +325,7 @@ public Closeable acquireHistoryRetentionLock() {
326325
}
327326

328327
@Override
329-
public Translog.Snapshot newChangesSnapshot(
330-
String source,
331-
MapperService mapperService,
332-
long fromSeqNo,
333-
long toSeqNo,
334-
boolean requiredFullRange
335-
) {
328+
public Translog.Snapshot newChangesSnapshot(String source, long fromSeqNo, long toSeqNo, boolean requiredFullRange) {
336329
return newEmptySnapshot();
337330
}
338331

server/src/main/java/org/opensearch/index/fieldvisitor/FieldsVisitor.java

+3-14
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.apache.lucene.util.BytesRef;
3737
import org.opensearch.common.bytes.BytesArray;
3838
import org.opensearch.common.bytes.BytesReference;
39-
import org.opensearch.index.mapper.DocumentMapper;
4039
import org.opensearch.index.mapper.IdFieldMapper;
4140
import org.opensearch.index.mapper.IgnoredFieldMapper;
4241
import org.opensearch.index.mapper.MappedFieldType;
@@ -67,7 +66,7 @@ public class FieldsVisitor extends StoredFieldVisitor {
6766
private final String sourceFieldName;
6867
private final Set<String> requiredFields;
6968
protected BytesReference source;
70-
protected String type, id;
69+
protected String id;
7170
protected Map<String, List<Object>> fieldsValues;
7271

7372
public FieldsVisitor(boolean loadSource) {
@@ -98,10 +97,6 @@ public Status needsField(FieldInfo fieldInfo) {
9897
}
9998

10099
public void postProcess(MapperService mapperService) {
101-
final DocumentMapper mapper = mapperService.documentMapper();
102-
if (mapper != null) {
103-
type = mapper.type();
104-
}
105100
for (Map.Entry<String, List<Object>> entry : fields().entrySet()) {
106101
MappedFieldType fieldType = mapperService.fieldType(entry.getKey());
107102
if (fieldType == null) {
@@ -167,13 +162,8 @@ public BytesReference source() {
167162
return source;
168163
}
169164

170-
public Uid uid() {
171-
if (id == null) {
172-
return null;
173-
} else if (type == null) {
174-
throw new IllegalStateException("Call postProcess before getting the uid");
175-
}
176-
return new Uid(type, id);
165+
public String id() {
166+
return id;
177167
}
178168

179169
public String routing() {
@@ -195,7 +185,6 @@ public Map<String, List<Object>> fields() {
195185
public void reset() {
196186
if (fieldsValues != null) fieldsValues.clear();
197187
source = null;
198-
type = null;
199188
id = null;
200189

201190
requiredFields.addAll(BASE_REQUIRED_FIELDS);

0 commit comments

Comments
 (0)