forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce autoscaling decisions (elastic#53934)
This is the first in a series of commits that will introduce the autoscaling deciders framework. This commit introduces the basic framework for representing autoscaling decisions.
- Loading branch information
1 parent
f39aa75
commit 94e81b0
Showing
12 changed files
with
485 additions
and
6 deletions.
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
85 changes: 85 additions & 0 deletions
85
...in/autoscaling/src/main/java/org/elasticsearch/xpack/autoscaling/AutoscalingDecision.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,85 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.autoscaling; | ||
|
||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.common.xcontent.ToXContent; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Represents an autoscaling decision. | ||
*/ | ||
public class AutoscalingDecision implements ToXContent, Writeable { | ||
|
||
private final String name; | ||
|
||
public String name() { | ||
return name; | ||
} | ||
|
||
private final AutoscalingDecisionType type; | ||
|
||
public AutoscalingDecisionType type() { | ||
return type; | ||
} | ||
|
||
private final String reason; | ||
|
||
public String reason() { | ||
return reason; | ||
} | ||
|
||
public AutoscalingDecision(final String name, final AutoscalingDecisionType type, final String reason) { | ||
this.name = Objects.requireNonNull(name); | ||
this.type = Objects.requireNonNull(type); | ||
this.reason = Objects.requireNonNull(reason); | ||
} | ||
|
||
public AutoscalingDecision(final StreamInput in) throws IOException { | ||
this.name = in.readString(); | ||
this.type = AutoscalingDecisionType.readFrom(in); | ||
this.reason = in.readString(); | ||
} | ||
|
||
@Override | ||
public void writeTo(final StreamOutput out) throws IOException { | ||
out.writeString(name); | ||
type.writeTo(out); | ||
out.writeString(reason); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(final XContentBuilder builder, final ToXContent.Params params) throws IOException { | ||
builder.startObject(); | ||
{ | ||
builder.field("name", name); | ||
builder.field("type", type); | ||
builder.field("reason", reason); | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
final AutoscalingDecision that = (AutoscalingDecision) o; | ||
return name.equals(that.name) && type == that.type && reason.equals(that.reason); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(name, type, reason); | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
...utoscaling/src/main/java/org/elasticsearch/xpack/autoscaling/AutoscalingDecisionType.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,73 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.autoscaling; | ||
|
||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.common.xcontent.ToXContentFragment; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.Locale; | ||
|
||
/** | ||
* Represents the type of an autoscaling decision: to indicating if a scale down, no scaling event, or a scale up is needed. | ||
*/ | ||
public enum AutoscalingDecisionType implements Writeable, ToXContentFragment { | ||
|
||
/** | ||
* Indicates that a scale down event is needed. | ||
*/ | ||
SCALE_DOWN((byte) 0), | ||
|
||
/** | ||
* Indicates that no scaling event is needed. | ||
*/ | ||
NO_SCALE((byte) 1), | ||
|
||
/** | ||
* Indicates that a scale up event is needed. | ||
*/ | ||
SCALE_UP((byte) 2); | ||
|
||
private final byte id; | ||
|
||
byte id() { | ||
return id; | ||
} | ||
|
||
AutoscalingDecisionType(final byte id) { | ||
this.id = id; | ||
} | ||
|
||
public static AutoscalingDecisionType readFrom(final StreamInput in) throws IOException { | ||
final byte id = in.readByte(); | ||
switch (id) { | ||
case 0: | ||
return SCALE_DOWN; | ||
case 1: | ||
return NO_SCALE; | ||
case 2: | ||
return SCALE_UP; | ||
default: | ||
throw new IllegalArgumentException("unexpected value [" + id + "] for autoscaling decision type"); | ||
} | ||
} | ||
|
||
@Override | ||
public void writeTo(final StreamOutput out) throws IOException { | ||
out.writeByte(id); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException { | ||
builder.value(name().toLowerCase(Locale.ROOT)); | ||
return builder; | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
...n/autoscaling/src/main/java/org/elasticsearch/xpack/autoscaling/AutoscalingDecisions.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,77 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.autoscaling; | ||
|
||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.common.xcontent.ToXContent; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Represents a collection of individual autoscaling decisions that can be aggregated into a single autoscaling decision. | ||
*/ | ||
public class AutoscalingDecisions implements ToXContent, Writeable { | ||
|
||
private final Collection<AutoscalingDecision> decisions; | ||
|
||
public AutoscalingDecisions(final Collection<AutoscalingDecision> decisions) { | ||
Objects.requireNonNull(decisions); | ||
if (decisions.isEmpty()) { | ||
throw new IllegalArgumentException("decisions can not be empty"); | ||
} | ||
this.decisions = decisions; | ||
} | ||
|
||
public AutoscalingDecisions(final StreamInput in) throws IOException { | ||
this.decisions = in.readList(AutoscalingDecision::new); | ||
} | ||
|
||
@Override | ||
public void writeTo(final StreamOutput out) throws IOException { | ||
out.writeCollection(decisions); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException { | ||
for (final AutoscalingDecision decision : decisions) { | ||
decision.toXContent(builder, params); | ||
} | ||
return builder; | ||
} | ||
|
||
public AutoscalingDecisionType type() { | ||
if (decisions.stream().anyMatch(p -> p.type() == AutoscalingDecisionType.SCALE_UP)) { | ||
// if any deciders say to scale up | ||
return AutoscalingDecisionType.SCALE_UP; | ||
} else if (decisions.stream().allMatch(p -> p.type() == AutoscalingDecisionType.SCALE_DOWN)) { | ||
// if all deciders say to scale down | ||
return AutoscalingDecisionType.SCALE_DOWN; | ||
} else { | ||
// otherwise, do not scale | ||
return AutoscalingDecisionType.NO_SCALE; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
final org.elasticsearch.xpack.autoscaling.AutoscalingDecisions that = (org.elasticsearch.xpack.autoscaling.AutoscalingDecisions) o; | ||
return decisions.equals(that.decisions); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(decisions); | ||
} | ||
|
||
} |
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
31 changes: 31 additions & 0 deletions
31
...toscaling/src/test/java/org/elasticsearch/xpack/autoscaling/AutoscalingDecisionTests.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,31 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.autoscaling; | ||
|
||
import org.elasticsearch.common.io.stream.BytesStreamOutput; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class AutoscalingDecisionTests extends AutoscalingTestCase { | ||
|
||
public void testAutoscalingDecisionType() { | ||
final AutoscalingDecisionType type = randomFrom(AutoscalingDecisionType.values()); | ||
final AutoscalingDecision decision = randomAutoscalingDecisionOfType(type); | ||
assertThat(decision.type(), equalTo(type)); | ||
} | ||
|
||
public void testAutoscalingDecisionTypeSerialization() throws IOException { | ||
final AutoscalingDecisionType before = randomFrom(AutoscalingDecisionType.values()); | ||
final BytesStreamOutput out = new BytesStreamOutput(); | ||
before.writeTo(out); | ||
final AutoscalingDecisionType after = AutoscalingDecisionType.readFrom(out.bytes().streamInput()); | ||
assertThat(after, equalTo(before)); | ||
} | ||
|
||
} |
Oops, something went wrong.