Skip to content

Commit

Permalink
Add min_* conditions to rollover (#83345)
Browse files Browse the repository at this point in the history
  • Loading branch information
probakowski committed Jul 26, 2022
1 parent 55c0d1a commit 539434d
Show file tree
Hide file tree
Showing 50 changed files with 1,892 additions and 179 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/83345.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 83345
summary: Add min_* conditions to rollover
area: ILM+SLM
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
body: { "foo": "hello world" }
# make this doc visible in index stats
refresh: true

- do:
get:
index: logs_search
Expand Down Expand Up @@ -149,3 +149,37 @@
conditions:
max_docs: 1

---
"Will not rollover with only min_* conditions":
- skip:
version: " - 8.3.99"
reason: introduced in 8.4.0

# create index with alias and replica
- do:
indices.create:
index: logs-1
wait_for_active_shards: 1
body:
aliases:
logs_search: {}

# index first document and wait for refresh
- do:
index:
index: logs-1
id: "1"
body: { "foo": "hello world" }
refresh: true

# perform alias rollover with no result
- do:
catch: bad_request
indices.rollover:
alias: "logs_search"
wait_for_active_shards: 1
body:
conditions:
min_age: "0s"
min_docs: 1
- match: { error.reason: "Validation Failed: 1: at least one max_* rollover condition must be set when using min_* conditions;" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
"Rollover with min_age condition":
- skip:
version: " - 8.3.99"
reason: introduced in 8.4.0

# create index with alias and replica
- do:
indices.create:
index: logs-1
wait_for_active_shards: 1
body:
aliases:
logs_search: {}

# index first document and wait for refresh
- do:
index:
index: logs-1
id: "1"
body: { "foo": "hello world" }
refresh: true

# perform alias rollover with no result
- do:
indices.rollover:
alias: "logs_search"
wait_for_active_shards: 1
body:
conditions:
max_docs: 1
min_age: "7d"

- match: { conditions: { "[max_docs: 1]": true, "[min_age: 7d]": false } }
- match: { rolled_over: false }

# perform alias rollover
- do:
indices.rollover:
alias: "logs_search"
wait_for_active_shards: 1
body:
conditions:
max_docs: 1
min_age: "0s"

- match: { conditions: { "[max_docs: 1]": true, "[min_age: 0s]": true } }
- match: { rolled_over: true }
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
"Rollover with min_docs condition":
- skip:
version: " - 8.3.99"
reason: introduced in 8.4.0

# create index with alias and replica
- do:
indices.create:
index: logs-1
wait_for_active_shards: 1
body:
aliases:
logs_search: {}

# index first document and wait for refresh
- do:
index:
index: logs-1
id: "1"
body: { "foo": "hello world" }
refresh: true

# perform alias rollover with no result
- do:
indices.rollover:
alias: "logs_search"
wait_for_active_shards: 1
body:
conditions:
max_docs: 1
min_docs: 10

- match: { conditions: { "[max_docs: 1]": true, "[min_docs: 10]": false } }
- match: { rolled_over: false }

# perform alias rollover
- do:
indices.rollover:
alias: "logs_search"
wait_for_active_shards: 1
body:
conditions:
max_docs: 1
min_docs: 1

- match: { conditions: { "[max_docs: 1]": true, "[min_docs: 1]": true } }
- match: { rolled_over: true }
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
"Rollover with min_primary_shard_docs condition":
- skip:
version: " - 8.3.99"
reason: introduced in 8.4.0

# create index with alias and replica
- do:
indices.create:
index: logs-1
wait_for_active_shards: 1
body:
aliases:
logs_search: {}

# index first document and wait for refresh
- do:
index:
index: logs-1
id: "1"
body: { "foo": "hello world" }
refresh: true

# perform alias rollover with no result
- do:
indices.rollover:
alias: "logs_search"
wait_for_active_shards: 1
body:
conditions:
max_docs: 1
min_primary_shard_docs: 10

- match: { conditions: { "[max_docs: 1]": true, "[min_primary_shard_docs: 10]": false } }
- match: { rolled_over: false }

# perform alias rollover
- do:
indices.rollover:
alias: "logs_search"
wait_for_active_shards: 1
body:
conditions:
max_docs: 1
min_primary_shard_docs: 1

- match: { conditions: { "[max_docs: 1]": true, "[min_primary_shard_docs: 1]": true } }
- match: { rolled_over: true }
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
"Rollover with min_primary_shard_size condition":
- skip:
version: " - 8.3.99"
reason: introduced in 8.4.0

# create index with alias and replica
- do:
indices.create:
index: logs-1
wait_for_active_shards: 1
body:
aliases:
logs_search: {}

# index first document and wait for refresh
- do:
index:
index: logs-1
id: "1"
body: { "foo": "hello world" }
refresh: true

# perform alias rollover with no result
- do:
indices.rollover:
alias: "logs_search"
wait_for_active_shards: 1
body:
conditions:
max_docs: 1
min_primary_shard_size: "50gb"

- match: { conditions: { "[max_docs: 1]": true, "[min_primary_shard_size: 50gb]": false } }
- match: { rolled_over: false }

# perform alias rollover
- do:
indices.rollover:
alias: "logs_search"
wait_for_active_shards: 1
body:
conditions:
max_docs: 1
min_primary_shard_size: "0b"

- match: { conditions: { "[max_docs: 1]": true, "[min_primary_shard_size: 0b]": true } }
- match: { rolled_over: true }
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
"Rollover with min_size condition":
- skip:
version: " - 8.3.99"
reason: introduced in 8.4.0

# create index with alias and replica
- do:
indices.create:
index: logs-1
wait_for_active_shards: 1
body:
aliases:
logs_search: {}

# index first document and wait for refresh
- do:
index:
index: logs-1
id: "1"
body: { "foo": "hello world" }
refresh: true

# perform alias rollover with no result
- do:
indices.rollover:
alias: "logs_search"
wait_for_active_shards: 1
body:
conditions:
max_docs: 1
min_size: "50gb"

- match: { conditions: { "[max_docs: 1]": true, "[min_size: 50gb]": false } }
- match: { rolled_over: false }

# perform alias rollover
- do:
indices.rollover:
alias: "logs_search"
wait_for_active_shards: 1
body:
conditions:
max_docs: 1
min_size: "0b"

- match: { conditions: { "[max_docs: 1]": true, "[min_size: 0b]": true } }
- match: { rolled_over: true }
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ public void testRolloverMaxSize() throws Exception {
.indices()
.prepareRolloverIndex("test_alias")
.addMaxIndexSizeCondition(new ByteSizeValue(randomNonNegativeLong(), ByteSizeUnit.BYTES))
.addMinIndexDocsCondition(1)
.get();
assertThat(response.getOldIndex(), equalTo("test-000002"));
assertThat(response.getNewIndex(), equalTo("test-000003"));
Expand Down Expand Up @@ -532,6 +533,7 @@ public void testRolloverMaxPrimaryShardSize() throws Exception {
.indices()
.prepareRolloverIndex("test_alias")
.addMaxPrimaryShardSizeCondition(new ByteSizeValue(randomNonNegativeLong(), ByteSizeUnit.BYTES))
.addMinIndexDocsCondition(1)
.get();
assertThat(response.getOldIndex(), equalTo("test-000002"));
assertThat(response.getNewIndex(), equalTo("test-000003"));
Expand Down Expand Up @@ -597,6 +599,7 @@ public void testRolloverMaxPrimaryShardDocs() throws Exception {
.indices()
.prepareRolloverIndex("test_alias")
.addMaxPrimaryShardDocsCondition(randomNonNegativeLong())
.addMinIndexDocsCondition(1)
.get();
assertThat(response.getOldIndex(), equalTo("test-000002"));
assertThat(response.getNewIndex(), equalTo("test-000003"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,21 @@
*/
public abstract class Condition<T> implements NamedWriteable, ToXContentFragment {

/**
* Describes the type of condition - a min_* condition (MIN) or max_* condition (MAX).
*/
public enum Type {
MIN,
MAX
}

protected T value;
protected final String name;
protected final Type type;

protected Condition(String name) {
protected Condition(String name, Type type) {
this.name = name;
this.type = type;
}

/**
Expand Down Expand Up @@ -67,6 +77,10 @@ public String name() {
return name;
}

public Type type() {
return type;
}

/**
* Holder for index stats used to evaluate conditions
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public class MaxAgeCondition extends Condition<TimeValue> {
public static final String NAME = "max_age";

public MaxAgeCondition(TimeValue value) {
super(NAME);
super(NAME, Type.MAX);
this.value = value;
}

public MaxAgeCondition(StreamInput in) throws IOException {
super(NAME);
super(NAME, Type.MAX);
this.value = TimeValue.timeValueMillis(in.readLong());
}

Expand Down Expand Up @@ -64,7 +64,7 @@ public static MaxAgeCondition fromXContent(XContentParser parser) throws IOExcep
if (parser.nextToken() == XContentParser.Token.VALUE_STRING) {
return new MaxAgeCondition(TimeValue.parseTimeValue(parser.text(), NAME));
} else {
throw new IllegalArgumentException("invalid token: " + parser.currentToken());
throw new IllegalArgumentException("invalid token when parsing " + NAME + " condition: " + parser.currentToken());
}
}
}

0 comments on commit 539434d

Please sign in to comment.