-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[health] bootstrap HealthObserver from agent to API (#16141)
* [health] bootstrap HealthObserver from agent to API * specs: mocked agent needs health observer * add license headers
- Loading branch information
Showing
6 changed files
with
208 additions
and
2 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
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
39 changes: 39 additions & 0 deletions
39
logstash-core/src/main/java/org/logstash/health/HealthObserver.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,39 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.logstash.health; | ||
|
||
import com.google.common.collect.Iterables; | ||
|
||
import java.util.EnumSet; | ||
|
||
public class HealthObserver { | ||
public final Status getStatus() { | ||
// INTERNAL-ONLY Proof-of-concept to show flow-through to API results | ||
switch (System.getProperty("logstash.apiStatus", "green")) { | ||
case "green": return Status.GREEN; | ||
case "yellow": return Status.YELLOW; | ||
case "red": return Status.RED; | ||
case "random": | ||
final EnumSet<Status> statuses = EnumSet.allOf(Status.class); | ||
return Iterables.get(statuses, new java.util.Random().nextInt(statuses.size())); | ||
default: | ||
return Status.UNKNOWN; | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
logstash-core/src/main/java/org/logstash/health/Status.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,50 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.logstash.health; | ||
|
||
import com.fasterxml.jackson.annotation.JsonValue; | ||
|
||
public enum Status { | ||
UNKNOWN, | ||
GREEN, | ||
YELLOW, | ||
RED, | ||
; | ||
|
||
private final String externalValue = name().toLowerCase(); | ||
|
||
@JsonValue | ||
public String externalValue() { | ||
return externalValue; | ||
} | ||
|
||
/** | ||
* Combine this status with another status. | ||
* This method is commutative. | ||
* @param status the other status | ||
* @return the more-degraded of the two statuses. | ||
*/ | ||
public Status reduce(Status status) { | ||
if (compareTo(status) >= 0) { | ||
return this; | ||
} else { | ||
return status; | ||
} | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
logstash-core/src/test/java/org/logstash/health/StatusTest.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,106 @@ | ||
package org.logstash.health; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.Test; | ||
import org.junit.experimental.runners.Enclosed; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameter; | ||
import org.junit.runners.Parameterized.Parameters; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.EnumSet; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.google.common.collect.Collections2.orderedPermutations; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.logstash.health.Status.*; | ||
|
||
@RunWith(Enclosed.class) | ||
public class StatusTest { | ||
|
||
public static class Tests { | ||
@Test | ||
public void testReduceUnknown() { | ||
assertThat(UNKNOWN.reduce(UNKNOWN), is(UNKNOWN)); | ||
assertThat(UNKNOWN.reduce(GREEN), is(GREEN)); | ||
assertThat(UNKNOWN.reduce(YELLOW), is(YELLOW)); | ||
assertThat(UNKNOWN.reduce(RED), is(RED)); | ||
} | ||
|
||
@Test | ||
public void testReduceGreen() { | ||
assertThat(GREEN.reduce(UNKNOWN), is(GREEN)); | ||
assertThat(GREEN.reduce(GREEN), is(GREEN)); | ||
assertThat(GREEN.reduce(YELLOW), is(YELLOW)); | ||
assertThat(GREEN.reduce(RED), is(RED)); | ||
} | ||
|
||
@Test | ||
public void testReduceYellow() { | ||
assertThat(YELLOW.reduce(UNKNOWN), is(YELLOW)); | ||
assertThat(YELLOW.reduce(GREEN), is(YELLOW)); | ||
assertThat(YELLOW.reduce(YELLOW), is(YELLOW)); | ||
assertThat(YELLOW.reduce(RED), is(RED)); | ||
} | ||
|
||
@Test | ||
public void testReduceRed() { | ||
assertThat(RED.reduce(UNKNOWN), is(RED)); | ||
assertThat(RED.reduce(GREEN), is(RED)); | ||
assertThat(RED.reduce(YELLOW), is(RED)); | ||
assertThat(RED.reduce(RED), is(RED)); | ||
} | ||
} | ||
|
||
@RunWith(Parameterized.class) | ||
public static class JacksonSerialization { | ||
@Parameters(name = "{0}") | ||
public static Iterable<?> data() { | ||
return EnumSet.allOf(Status.class); | ||
} | ||
|
||
@Parameter | ||
public Status status; | ||
|
||
private final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
@Test | ||
public void testSerialization() throws Exception { | ||
assertThat(mapper.writeValueAsString(status), is(equalTo('"' + status.name().toLowerCase() + '"'))); | ||
} | ||
} | ||
|
||
@RunWith(Parameterized.class) | ||
public static class ReduceCommutativeSpecification { | ||
@Parameters(name = "{0}<=>{1}") | ||
public static Collection<Object[]> data() { | ||
return getCombinations(EnumSet.allOf(Status.class), 2); | ||
} | ||
|
||
@Parameter(0) | ||
public Status statusA; | ||
@Parameter(1) | ||
public Status statusB; | ||
|
||
@Test | ||
public void testReduceCommutative() { | ||
assertThat(statusA.reduce(statusB), is(statusB.reduce(statusA))); | ||
} | ||
|
||
private static <T extends Comparable<T>> List<Object[]> getCombinations(Collection<T> source, int count) { | ||
return orderedPermutations(source).stream() | ||
.map((l) -> l.subList(0, count)) | ||
.map(ArrayList::new).peek(Collections::sort) | ||
.collect(Collectors.toSet()) | ||
.stream() | ||
.map(List::toArray) | ||
.collect(Collectors.toList()); | ||
} | ||
} | ||
} |