-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add MinDocsCondition to Rollover API #7243
Closed
shinbay-almaz
wants to merge
9
commits into
opensearch-project:main
from
shinbay-almaz:min_docs_condition
+192
−12
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6aa1367
add MinDocsCondition and tests
dc76574
remove bug in testEvaluateWithoutDocStats
8e5471c
change javadoc and revise testEvaluateWithoutDocStats
6b21fdb
resolve conflicts
2e375a3
Merge branch 'opensearch-project:main' into min_docs_condition
shinbay-almaz 2108191
specify descriptions
9b12976
specify descriptions
8329048
Merge branch 'main' into min_docs_condition and resolve conflicts
shinbay-almaz 156a367
Fix imports in MinDocsCondition.java
shinbay-almaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
server/src/main/java/org/opensearch/action/admin/indices/rollover/MinDocsCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.action.admin.indices.rollover; | ||
|
||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Condition for minimum index docs. | ||
* Evaluates to <code>true</code> when the index has at least {@link #value} docs. | ||
* If it evaluates to <code>false</code> then it prevents rollover | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class MinDocsCondition extends Condition<Long> { | ||
public static final String NAME = "min_docs"; | ||
|
||
public MinDocsCondition(Long value) { | ||
super(NAME); | ||
this.value = value; | ||
} | ||
|
||
public MinDocsCondition(StreamInput in) throws IOException { | ||
super(NAME); | ||
this.value = in.readLong(); | ||
} | ||
|
||
@Override | ||
public Result evaluate(final Stats stats) { | ||
return new Result(this, this.value <= stats.numDocs); | ||
} | ||
|
||
@Override | ||
public String getWriteableName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeLong(value); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
return builder.field(NAME, value); | ||
} | ||
|
||
public static MinDocsCondition fromXContent(XContentParser parser) throws IOException { | ||
if (parser.nextToken() == XContentParser.Token.VALUE_NUMBER) { | ||
return new MinDocsCondition(parser.longValue()); | ||
} else { | ||
throw new IllegalArgumentException("invalid token: " + parser.currentToken()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,9 +128,12 @@ public void testEvaluateConditions() { | |
MaxDocsCondition maxDocsCondition = new MaxDocsCondition(100L); | ||
MaxAgeCondition maxAgeCondition = new MaxAgeCondition(TimeValue.timeValueHours(2)); | ||
MaxSizeCondition maxSizeCondition = new MaxSizeCondition(new ByteSizeValue(randomIntBetween(10, 100), ByteSizeUnit.MB)); | ||
MinDocsCondition minDocsCondition = new MinDocsCondition(10L); | ||
|
||
long matchMaxDocsAndMatchMinDocs = randomIntBetween(100, 1000); | ||
long notMatchMaxDocsAndMatchMinDocs = randomIntBetween(10, 99); | ||
long notMatchMaxDocsAndNotMatchMinDocs = randomIntBetween(0, 9); | ||
|
||
long matchMaxDocs = randomIntBetween(100, 1000); | ||
long notMatchMaxDocs = randomIntBetween(0, 99); | ||
ByteSizeValue notMatchMaxSize = new ByteSizeValue(randomIntBetween(0, 9), ByteSizeUnit.MB); | ||
final Settings settings = Settings.builder() | ||
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) | ||
|
@@ -142,26 +145,44 @@ public void testEvaluateConditions() { | |
.creationDate(System.currentTimeMillis() - TimeValue.timeValueHours(3).getMillis()) | ||
.settings(settings) | ||
.build(); | ||
final Set<Condition<?>> conditions = Sets.newHashSet(maxDocsCondition, maxAgeCondition, maxSizeCondition); | ||
final Set<Condition<?>> conditions = Sets.newHashSet(maxDocsCondition, maxAgeCondition, maxSizeCondition, minDocsCondition); | ||
Map<String, Boolean> results = evaluateConditions( | ||
conditions, | ||
new DocsStats(matchMaxDocs, 0L, ByteSizeUnit.MB.toBytes(120)), | ||
new DocsStats(matchMaxDocsAndMatchMinDocs, 0L, ByteSizeUnit.MB.toBytes(120)), | ||
metadata | ||
); | ||
assertThat(results.size(), equalTo(3)); | ||
assertThat(results.size(), equalTo(4)); | ||
for (Boolean matched : results.values()) { | ||
assertThat(matched, equalTo(true)); | ||
} | ||
|
||
results = evaluateConditions(conditions, new DocsStats(notMatchMaxDocs, 0, notMatchMaxSize.getBytes()), metadata); | ||
assertThat(results.size(), equalTo(3)); | ||
results = evaluateConditions(conditions, new DocsStats(notMatchMaxDocsAndMatchMinDocs, 0, notMatchMaxSize.getBytes()), metadata); | ||
assertThat(results.size(), equalTo(4)); | ||
for (Map.Entry<String, Boolean> entry : results.entrySet()) { | ||
if (entry.getKey().equals(maxAgeCondition.toString())) { | ||
assertThat(entry.getValue(), equalTo(true)); | ||
} else if (entry.getKey().equals(maxDocsCondition.toString())) { | ||
assertThat(entry.getValue(), equalTo(false)); | ||
} else if (entry.getKey().equals(maxSizeCondition.toString())) { | ||
assertThat(entry.getValue(), equalTo(false)); | ||
} else if (entry.getKey().equals(minDocsCondition.toString())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a test, it makes more sense to have deterministic expected outcomes. We could set the minDocsCondition value to 10, in which case the expected value will always be false. |
||
assertThat(entry.getValue(), equalTo(true)); | ||
} else { | ||
fail("unknown condition result found " + entry.getKey()); | ||
} | ||
} | ||
|
||
results = evaluateConditions(conditions, new DocsStats(notMatchMaxDocsAndNotMatchMinDocs, 0, notMatchMaxSize.getBytes()), metadata); | ||
assertThat(results.size(), equalTo(4)); | ||
for (Map.Entry<String, Boolean> entry : results.entrySet()) { | ||
if (entry.getKey().equals(maxAgeCondition.toString())) { | ||
assertThat(entry.getValue(), equalTo(true)); | ||
} else if (entry.getKey().equals(maxSizeCondition.toString())) { | ||
assertThat(entry.getValue(), equalTo(false)); | ||
} else if (entry.getKey().equals(maxDocsCondition.toString())) { | ||
assertThat(entry.getValue(), equalTo(false)); | ||
} else if (entry.getKey().equals(minDocsCondition.toString())) { | ||
assertThat(entry.getValue(), equalTo(false)); | ||
} else { | ||
fail("unknown condition result found " + entry.getKey()); | ||
} | ||
|
@@ -172,8 +193,9 @@ public void testEvaluateWithoutDocStats() { | |
MaxDocsCondition maxDocsCondition = new MaxDocsCondition(randomNonNegativeLong()); | ||
MaxAgeCondition maxAgeCondition = new MaxAgeCondition(TimeValue.timeValueHours(randomIntBetween(1, 3))); | ||
MaxSizeCondition maxSizeCondition = new MaxSizeCondition(new ByteSizeValue(randomNonNegativeLong())); | ||
MinDocsCondition minDocsCondition = new MinDocsCondition(randomNonNegativeLong()); | ||
|
||
Set<Condition<?>> conditions = Sets.newHashSet(maxDocsCondition, maxAgeCondition, maxSizeCondition); | ||
Set<Condition<?>> conditions = Sets.newHashSet(maxDocsCondition, maxAgeCondition, maxSizeCondition, minDocsCondition); | ||
final Settings settings = Settings.builder() | ||
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) | ||
.put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()) | ||
|
@@ -186,7 +208,7 @@ public void testEvaluateWithoutDocStats() { | |
.settings(settings) | ||
.build(); | ||
Map<String, Boolean> results = evaluateConditions(conditions, null, metadata); | ||
assertThat(results.size(), equalTo(3)); | ||
assertThat(results.size(), equalTo(4)); | ||
|
||
for (Map.Entry<String, Boolean> entry : results.entrySet()) { | ||
if (entry.getKey().equals(maxAgeCondition.toString())) { | ||
|
@@ -195,6 +217,8 @@ public void testEvaluateWithoutDocStats() { | |
assertThat(entry.getValue(), equalTo(false)); | ||
} else if (entry.getKey().equals(maxSizeCondition.toString())) { | ||
assertThat(entry.getValue(), equalTo(false)); | ||
} else if (entry.getKey().equals(minDocsCondition.toString())) { | ||
assertThat(entry.getValue(), equalTo(false)); | ||
} else { | ||
fail("unknown condition result found " + entry.getKey()); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same description as addMaxIndexDocsCondition. Specify what makes them different.