Skip to content

Commit

Permalink
Add a few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fcofdez committed May 5, 2022
1 parent 39f3603 commit b2b4f26
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ private void validate(DesiredNode node) {
// we create a new setting just to run the validations using the desired node
// number of available processors
if (settings.hasValue(NODE_PROCESSORS_SETTING.getKey())) {
int processors = node.minProcessors();
Setting.intSetting(NODE_PROCESSORS_SETTING.getKey(), processors, 1, processors, Setting.Property.NodeScope).get(settings);
int processors = node.roundedMinProcessors();
int maxProcessors = node.roundedMaxProcessors();
Setting.intSetting(NODE_PROCESSORS_SETTING.getKey(), processors, 1, maxProcessors, Setting.Property.NodeScope).get(settings);
final Settings.Builder updatedSettings = Settings.builder().put(settings);
updatedSettings.remove(NODE_PROCESSORS_SETTING.getKey());
settings = updatedSettings.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,20 @@ public Settings settings() {
return settings;
}

public float processors() {
public float minProcessors() {
return processors.min();
}

public int minProcessors() {
return processors.minCeil();
public int roundedMinProcessors() {
return Math.max(1, processors.roundedMin());
}

public float maxProcessors() {
return processors.max();
}

public int roundedMaxProcessors() {
return Math.max(1, processors.roundedMax());
}

public ByteSizeValue memory() {
Expand Down Expand Up @@ -306,12 +314,16 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeFloat(min);
out.writeFloat(max);
} else {
out.writeInt(minCeil());
out.writeInt(roundedMin());
}
}

public int minCeil() {
return (int) Math.ceil(min);
public int roundedMin() {
return Math.round(min);
}

public int roundedMax() {
return Math.round(max);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,21 @@ public void testGetRoles() {
assertThat(desiredNode.getRoles(), contains(NODE_ROLES_SETTING.get(Settings.EMPTY).toArray()));
}
}

public void testNodeCPUsRoundUp() {
final var settings = Settings.builder().put(NODE_NAME_SETTING.getKey(), randomAlphaOfLength(10));

final var desiredNode = new DesiredNode(
settings.build(),
new DesiredNode.Processors((float) 0.4, (float) 1.2),
ByteSizeValue.ofGb(1),
ByteSizeValue.ofGb(1),
Version.CURRENT
);

assertThat(desiredNode.minProcessors(), is(equalTo((float) 0.4)));
assertThat(desiredNode.roundedMinProcessors(), is(equalTo(1)));
assertThat(desiredNode.maxProcessors(), is(equalTo((float) 1.2)));
assertThat(desiredNode.roundedMinProcessors(), is(equalTo(1)));
}
}

0 comments on commit b2b4f26

Please sign in to comment.