Skip to content

Commit

Permalink
Autoscaling add capacity API logging (#67993)
Browse files Browse the repository at this point in the history
Add debug logging of the autoscaling capacity API reponse.
  • Loading branch information
henningandersen committed Jan 27, 2021
1 parent e76fb4d commit 94dd11b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@

package org.elasticsearch.xpack.autoscaling.action;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.env.NodeEnvironment;
import org.elasticsearch.monitor.os.OsProbe;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.MockLogAppender;
import org.elasticsearch.test.junit.annotations.TestLogging;
import org.elasticsearch.xpack.autoscaling.AutoscalingIntegTestCase;
import org.elasticsearch.xpack.autoscaling.capacity.AutoscalingCapacity;
import org.hamcrest.Matchers;
Expand All @@ -19,6 +25,9 @@
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.hamcrest.Matchers.greaterThan;

@TestLogging(value = "org.elasticsearch.xpack.autoscaling.action.TransportGetAutoscalingCapacityAction:debug",
// spotless hack
reason = "to ensure we log autoscaling capacity response on DEBUG level")
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0)
public class TransportGetAutoscalingCapacityActionIT extends AutoscalingIntegTestCase {

Expand All @@ -40,13 +49,36 @@ public void testCurrentCapacity() throws Exception {
assertBusy(() -> { assertCurrentCapacity(memory, storage, nodes); });
}

public void assertCurrentCapacity(long memory, long storage, int nodes) {
GetAutoscalingCapacityAction.Response capacity = capacity();
AutoscalingCapacity currentCapacity = capacity.results().get("test").currentCapacity();
assertThat(currentCapacity.node().memory().getBytes(), Matchers.equalTo(memory));
assertThat(currentCapacity.total().memory().getBytes(), Matchers.equalTo(memory * nodes));
assertThat(currentCapacity.node().storage().getBytes(), Matchers.equalTo(storage));
assertThat(currentCapacity.total().storage().getBytes(), Matchers.equalTo(storage * nodes));
public void assertCurrentCapacity(long memory, long storage, int nodes) throws IllegalAccessException {
Logger subjectLogger = LogManager.getLogger(TransportGetAutoscalingCapacityAction.class);

MockLogAppender appender = new MockLogAppender();
appender.start();
appender.addExpectation(
new MockLogAppender.SeenEventExpectation(
"autoscaling capacity response message with " + storage,
TransportGetAutoscalingCapacityAction.class.getName(),
Level.DEBUG,
"autoscaling capacity response [*\"policies\"*\"test\"*\"current_capacity\"*\"storage\":"
+ storage
+ "*\"deciders\""
+ "*\"reactive_storage\""
+ "*\"reason_summary\"*\"reason_details\"*]"
)
);
Loggers.addAppender(subjectLogger, appender);
try {
GetAutoscalingCapacityAction.Response capacity = capacity();
AutoscalingCapacity currentCapacity = capacity.results().get("test").currentCapacity();
assertThat(currentCapacity.node().memory().getBytes(), Matchers.equalTo(memory));
assertThat(currentCapacity.total().memory().getBytes(), Matchers.equalTo(memory * nodes));
assertThat(currentCapacity.node().storage().getBytes(), Matchers.equalTo(storage));
assertThat(currentCapacity.total().storage().getBytes(), Matchers.equalTo(storage * nodes));
appender.assertAllExpectationsMatched();
} finally {
appender.stop();
Loggers.removeAppender(subjectLogger, appender);
}
}

public GetAutoscalingCapacityAction.Response capacity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.ActionType;
import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ToXContentObject;
Expand Down Expand Up @@ -119,6 +120,11 @@ public int hashCode() {
public Map<String, AutoscalingDeciderResults> getResults() {
return results;
}

@Override
public String toString() {
return Strings.toString(this);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

package org.elasticsearch.xpack.autoscaling.action;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.master.TransportMasterNodeAction;
Expand All @@ -30,6 +32,8 @@ public class TransportGetAutoscalingCapacityAction extends TransportMasterNodeAc
GetAutoscalingCapacityAction.Request,
GetAutoscalingCapacityAction.Response> {

private static final Logger logger = LogManager.getLogger(TransportGetAutoscalingCapacityAction.class);

private final AutoscalingCalculateCapacityService capacityService;
private final ClusterInfoService clusterInfoService;
private final SnapshotsInfoService snapshotsInfoService;
Expand Down Expand Up @@ -80,16 +84,16 @@ protected void masterOperation(
return;
}

listener.onResponse(
new GetAutoscalingCapacityAction.Response(
capacityService.calculate(
state,
clusterInfoService.getClusterInfo(),
snapshotsInfoService.snapshotShardSizes(),
memoryInfoService.snapshot()
)
GetAutoscalingCapacityAction.Response response = new GetAutoscalingCapacityAction.Response(
capacityService.calculate(
state,
clusterInfoService.getClusterInfo(),
snapshotsInfoService.snapshotShardSizes(),
memoryInfoService.snapshot()
)
);
logger.debug("autoscaling capacity response [{}]", response);
listener.onResponse(response);
}

@Override
Expand Down

0 comments on commit 94dd11b

Please sign in to comment.