use of io.pravega.shared.health.HealthContributor in project pravega by pravega.
the class AbstractHealthContributor method getHealthSnapshot.
/**
* Recursively build (in post-order fashion) the {@link Health} result for a given {@link HealthContributor}.
*
* @return The {@link Health} result of the {@link HealthContributor}.
*/
@Override
public final synchronized Health getHealthSnapshot() {
Exceptions.checkNotClosed(isClosed(), this);
Health.HealthBuilder builder = Health.builder().name(getName());
Collection<Status> statuses = new ArrayList<>();
Map<String, Health> children = new HashMap<>();
for (val entry : contributors.entrySet()) {
HealthContributor contributor = entry.getValue();
synchronized (contributor) {
if (!contributor.isClosed()) {
Health health = contributor.getHealthSnapshot();
children.put(entry.getKey(), health);
statuses.add(health.getStatus());
} else {
contributors.remove(name);
}
}
}
Status status = Status.DOWN;
// Perform own health check logic.
try {
status = doHealthCheck(builder);
} catch (Exception ex) {
log.warn("HealthCheck for {} has failed.", this.name, ex);
builder.status(Status.FAILED);
}
// If there are no child statuses, return the Status from its own health check, else
// return the least 'healthy' status between the child aggregate and its own.
status = statuses.isEmpty() ? status : Status.min(StatusAggregator.aggregate(aggregator, statuses), status);
this.status = status;
return builder.name(name).status(status).children(children).build();
}
use of io.pravega.shared.health.HealthContributor in project pravega by pravega.
the class HealthContributorTests method testDynamicContributor.
/**
* Verifies that if the {@link Health} of the child {@link HealthContributor} changes, it properly
* determines the overall health before and after the change.
*/
@Test
public void testDynamicContributor() {
@Cleanup HealthContributor root = new HealthyContributor();
root.register(new HealthyContributor("first"));
Assert.assertEquals("Expecting healthy status.", Status.UP, root.getHealthSnapshot().getStatus());
// Add a failing contributor to the root, which uses the 'UNANIMOUS' aggregation rule.
HealthContributor failing = new FailingContributor();
root.register(failing);
Assert.assertEquals("Expecting failing status.", Status.DOWN, root.getHealthSnapshot().getStatus());
// Remove the failing contributor and now expect it is healthy again.
failing.close();
Assert.assertEquals("Expecting healthy status.", Status.UP, root.getHealthSnapshot().getStatus());
}
use of io.pravega.shared.health.HealthContributor in project pravega by pravega.
the class HealthContributorTests method testChildHealths.
/**
* Tests that children {@link HealthContributor} can be properly registered to another contributor and the {@link Health}
* of the individual children is properly reflected in the overall health of the top most contributor.
*/
@Test
public void testChildHealths() {
@Cleanup HealthContributor contributor = new HealthyContributor();
@Cleanup HealthContributor first = new HealthyContributor("first");
@Cleanup HealthContributor second = new FailingContributor("second");
contributor.register(first, second);
Health health = contributor.getHealthSnapshot();
Assert.assertEquals("Expected 'contributor' to report an unhealthy status.", Status.DOWN, health.getStatus());
Assert.assertEquals("Expected to see two children registered to 'contributor'.", 2, health.getChildren().size());
}
use of io.pravega.shared.health.HealthContributor in project pravega by pravega.
the class HealthContributorTests method testClosedContributor.
/**
* Verifies that a closed {@link HealthContributor} can no longer be acted upon.
*/
@Test
public void testClosedContributor() {
@Cleanup HealthContributor root = new HealthyContributor();
HealthContributor contributor = new HealthyContributor();
contributor.close();
AssertExtensions.assertThrows("Expected an exception requesting the health of a closed contributor.", () -> contributor.getHealthSnapshot(), ex -> ex instanceof ObjectClosedException);
AssertExtensions.assertThrows("Expected an exception adding a child to a closed contributor.", () -> contributor.register(new HealthyContributor("")), ex -> ex instanceof ObjectClosedException);
HealthContributor parent = new HealthyContributor("parent");
root.register(parent);
@Cleanup HealthContributor child = new FailingContributor("child");
parent.register(child);
parent.close();
Assert.assertEquals("Expecting child contributor to be unreachable after closing parent", 0, root.getHealthSnapshot().getChildren().size());
Assert.assertEquals("Expecting default Status (UP) from empty HealthContributor.", Status.UP, root.getHealthSnapshot().getStatus());
}
Aggregations