Skip to content

Commit eae85a0

Browse files
committed
ReadHealthStep to set ServerHealth when rest call failed
1 parent a4ff79f commit eae85a0

File tree

5 files changed

+152
-43
lines changed

5 files changed

+152
-43
lines changed

operator/src/main/java/oracle/kubernetes/operator/http/Result.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018, Oracle Corporation and/or its affiliates. All rights reserved.
1+
// Copyright 2018, 2019 Oracle Corporation and/or its affiliates. All rights reserved.
22
// Licensed under the Universal Permissive License v 1.0 as shown at
33
// http://oss.oracle.com/licenses/upl.
44

@@ -38,11 +38,11 @@ public boolean isSuccessful() {
3838
}
3939

4040
/**
41-
* @return true if the HTTP status code from the REST request indicates a server error, ie in the
42-
* 5xx range
41+
* @return true if the HTTP status code from the REST request indicates that the server may be
42+
* overloaded
4343
*/
44-
public boolean isServerError() {
45-
return status >= 500 && status <= 599;
44+
public boolean isServerOverloaded() {
45+
return status == 500 || status == 503;
4646
}
4747

4848
@Override

operator/src/main/java/oracle/kubernetes/operator/steps/ReadHealthStep.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.concurrent.atomic.AtomicInteger;
2020
import oracle.kubernetes.operator.ProcessingConstants;
2121
import oracle.kubernetes.operator.helpers.DomainPresenceInfo;
22-
import oracle.kubernetes.operator.http.HTTPException;
2322
import oracle.kubernetes.operator.http.HttpClient;
2423
import oracle.kubernetes.operator.http.Result;
2524
import oracle.kubernetes.operator.logging.LoggingFacade;
@@ -44,6 +43,10 @@ public class ReadHealthStep extends Step {
4443

4544
private static final LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator");
4645

46+
public static final String OVERALL_HEALTH_NOT_AVAILABLE = "Not available";
47+
public static final String OVERALL_HEALTH_FOR_SERVER_OVERLOADED =
48+
OVERALL_HEALTH_NOT_AVAILABLE + " (possibly overloaded)";
49+
4750
private ReadHealthStep(Step next) {
4851
super(next);
4952
}
@@ -98,7 +101,6 @@ private static String getRetrieveHealthSearchPayload() {
98101
static final class ReadHealthWithHttpClientStep extends Step {
99102
private final V1Service service;
100103
private final V1Pod pod;
101-
static final String OVERALL_HEALTH_FOR_SERVER_ERROR = "Not available";
102104

103105
ReadHealthWithHttpClientStep(V1Service service, V1Pod pod, Step next) {
104106
super(next);
@@ -172,15 +174,15 @@ public NextAction apply(Packet packet) {
172174
}
173175
}
174176

175-
private ServerHealth createServerHealthFromResult(Result restResult)
176-
throws IOException, HTTPException {
177+
private ServerHealth createServerHealthFromResult(Result restResult) throws IOException {
177178
if (restResult.isSuccessful()) {
178179
return parseServerHealthJson(restResult.getResponse());
179180
}
180-
if (restResult.isServerError()) {
181-
return new ServerHealth().withOverallHealth(OVERALL_HEALTH_FOR_SERVER_ERROR);
182-
}
183-
throw new HTTPException(restResult.getStatus());
181+
return new ServerHealth()
182+
.withOverallHealth(
183+
restResult.isServerOverloaded()
184+
? OVERALL_HEALTH_FOR_SERVER_OVERLOADED
185+
: OVERALL_HEALTH_NOT_AVAILABLE);
184186
}
185187

186188
private ServerHealth parseServerHealthJson(String jsonResult) throws IOException {

operator/src/test/java/oracle/kubernetes/operator/http/HttpClientStub.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
public abstract class HttpClientStub extends HttpClient {
88

9-
final String RESPONSE = "{}";
9+
String response = "{}";
10+
int status = 200;
11+
boolean successful = true;
1012

1113
public HttpClientStub() {
1214
super(null, null);
@@ -16,6 +18,21 @@ public HttpClientStub() {
1618
public Result executePostUrlOnServiceClusterIP(
1719
String requestUrl, String serviceURL, String payload, boolean throwOnFailure)
1820
throws HTTPException {
19-
return new Result(RESPONSE, 200, true);
21+
return new Result(response, status, successful);
22+
}
23+
24+
public HttpClientStub withResponse(String response) {
25+
this.response = response;
26+
return this;
27+
}
28+
29+
public HttpClientStub withStatus(int status) {
30+
this.status = status;
31+
return this;
32+
}
33+
34+
public HttpClientStub withSuccessful(boolean successful) {
35+
this.successful = successful;
36+
return this;
2037
}
2138
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2019 Oracle Corporation and/or its affiliates. All rights reserved.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at
3+
// http://oss.oracle.com/licenses/upl.
4+
5+
package oracle.kubernetes.operator.http;
6+
7+
import static org.hamcrest.Matchers.is;
8+
import static org.junit.Assert.*;
9+
10+
import org.junit.Test;
11+
12+
public class ResultTest {
13+
14+
@Test
15+
public void verifyIsServerOverloadedReturnsTrue() {
16+
Result result500 = new Result("response", 500, false);
17+
assertThat(result500.isServerOverloaded(), is(true));
18+
19+
Result result503 = new Result("response", 503, false);
20+
assertThat(result503.isServerOverloaded(), is(true));
21+
}
22+
23+
@Test
24+
public void verifyIsOverloadedReturnsFalseForOtherResponseCode() {
25+
Result result404 = new Result("response", 404, false);
26+
assertThat(result404.isServerOverloaded(), is(false));
27+
28+
Result result200 = new Result("response", 200, false);
29+
assertThat(result200.isServerOverloaded(), is(false));
30+
}
31+
}

operator/src/test/java/oracle/kubernetes/operator/steps/ReadHealthStepTest.java

Lines changed: 87 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.kubernetes.client.models.V1ServiceSpec;
1818
import java.util.ArrayList;
1919
import java.util.List;
20+
import java.util.Map;
2021
import java.util.concurrent.ConcurrentHashMap;
2122
import java.util.concurrent.atomic.AtomicInteger;
2223
import java.util.logging.Level;
@@ -57,13 +58,22 @@ public class ReadHealthStepTest {
5758
.withWlsServer(MANAGED_SERVER1, MANAGED_SERVER1_PORT_NUM)
5859
.withAdminServerName(ADMIN_NAME);
5960

61+
V1Service service;
62+
Step next;
63+
HttpClientStub httpClientStub;
64+
ReadHealthWithHttpClientStep withHttpClientStep;
65+
6066
@Before
6167
public void setup() {
6268
consoleControl =
6369
TestUtils.silenceOperatorLogger()
6470
.collectLogMessages(logRecords, LOG_KEYS)
6571
.ignoringLoggedExceptions(CLASSCAST_EXCEPTION)
6672
.withLogLevel(Level.FINE);
73+
service = Stub.createStub(V1ServiceStub.class);
74+
next = new MockStep(null);
75+
httpClientStub = Stub.createStub(HttpClientStub.class);
76+
withHttpClientStep = new ReadHealthWithHttpClientStep(service, null, next);
6777
}
6878

6979
@After
@@ -73,58 +83,40 @@ public void tearDown() {
7383

7484
@Test
7585
public void withHttpClientStep_Health_logIfFailed() {
76-
V1Service service = Stub.createStub(V1ServiceStub.class);
77-
Step next = new MockStep(null);
78-
final String SERVER_NAME = ADMIN_NAME;
7986
Packet packet =
8087
Stub.createStub(PacketStub.class)
81-
.withServerName(SERVER_NAME)
88+
.withServerName(ADMIN_NAME)
8289
.withGetKeyThrowsException(true);
8390

84-
ReadHealthWithHttpClientStep withHttpClientStep =
85-
new ReadHealthWithHttpClientStep(service, null, next);
8691
withHttpClientStep.apply(packet);
8792

88-
assertThat(logRecords, containsInfo(WLS_HEALTH_READ_FAILED, SERVER_NAME));
93+
assertThat(logRecords, containsInfo(WLS_HEALTH_READ_FAILED, ADMIN_NAME));
8994
}
9095

9196
@Test
9297
public void withHttpClientStep_logIfMissingHTTPClient() {
93-
V1Service service = Stub.createStub(V1ServiceStub.class);
94-
Step next = new MockStep(null);
95-
final String SERVER_NAME = ADMIN_NAME;
9698
Packet packet =
97-
Stub.createStub(PacketStub.class).withServerName(SERVER_NAME).withGetKeyReturnValue(null);
99+
Stub.createStub(PacketStub.class).withServerName(ADMIN_NAME).withGetKeyReturnValue(null);
98100
packet.put(ProcessingConstants.REMAINING_SERVERS_HEALTH_TO_READ, new AtomicInteger(1));
99101

100-
ReadHealthWithHttpClientStep withHttpClientStep =
101-
new ReadHealthWithHttpClientStep(service, null, next);
102102
withHttpClientStep.apply(packet);
103103

104-
assertThat(logRecords, containsInfo(WLS_HEALTH_READ_FAILED_NO_HTTPCLIENT, SERVER_NAME));
104+
assertThat(logRecords, containsInfo(WLS_HEALTH_READ_FAILED_NO_HTTPCLIENT, ADMIN_NAME));
105105
assertThat(
106106
((AtomicInteger) packet.get(ProcessingConstants.REMAINING_SERVERS_HEALTH_TO_READ)).get(),
107107
is(1));
108108
}
109109

110110
@Test
111111
public void withHttpClientStep_decrementRemainingServerHealthReadInPacketIfSucceeded() {
112-
V1Service service = Stub.createStub(V1ServiceStub.class);
113-
Step next = new MockStep(null);
114-
final String SERVER_NAME = ADMIN_NAME;
115-
116-
HttpClientStub httpClientStub = Stub.createStub(HttpClientStub.class);
117-
118112
Packet packet =
119113
Stub.createStub(PacketStub.class)
120-
.withServerName(SERVER_NAME)
114+
.withServerName(ADMIN_NAME)
121115
.withGetKeyReturnValue(httpClientStub);
122116
packet.put(
123117
ProcessingConstants.SERVER_HEALTH_MAP, new ConcurrentHashMap<String, ServerHealth>());
124118
packet.put(ProcessingConstants.REMAINING_SERVERS_HEALTH_TO_READ, new AtomicInteger(1));
125119

126-
ReadHealthWithHttpClientStep withHttpClientStep =
127-
new ReadHealthWithHttpClientStep(service, null, next);
128120
withHttpClientStep.apply(packet);
129121

130122
assertThat(
@@ -134,11 +126,6 @@ public void withHttpClientStep_decrementRemainingServerHealthReadInPacketIfSucce
134126

135127
@Test
136128
public void withHttpClientStep_decrementRemainingServerHealthReadInMultipleClonedPackets() {
137-
V1Service service = Stub.createStub(V1ServiceStub.class);
138-
Step next = new MockStep(null);
139-
140-
HttpClientStub httpClientStub = Stub.createStub(HttpClientStub.class);
141-
142129
Packet packet = new Packet();
143130
packet.put(ProcessingConstants.DOMAIN_TOPOLOGY, configSupport.createDomainConfig());
144131
packet.put(HttpClient.KEY, httpClientStub);
@@ -164,6 +151,78 @@ public void withHttpClientStep_decrementRemainingServerHealthReadInMultipleClone
164151
is(0));
165152
}
166153

154+
@Test
155+
public void withHttpClientStep_verifyOkServerHealthAddedToPacket() {
156+
httpClientStub.withResponse(OK_RESPONSE);
157+
158+
Packet packet =
159+
Stub.createStub(PacketStub.class)
160+
.withServerName(MANAGED_SERVER1)
161+
.withGetKeyReturnValue(httpClientStub);
162+
packet.put(
163+
ProcessingConstants.SERVER_HEALTH_MAP, new ConcurrentHashMap<String, ServerHealth>());
164+
packet.put(ProcessingConstants.REMAINING_SERVERS_HEALTH_TO_READ, new AtomicInteger(1));
165+
166+
withHttpClientStep.apply(packet);
167+
168+
Map<String, ServerHealth> serverHealthMap =
169+
packet.getValue(ProcessingConstants.SERVER_HEALTH_MAP);
170+
ServerHealth serverHealth = serverHealthMap.get(MANAGED_SERVER1);
171+
assertThat(serverHealth.getOverallHealth(), is("ok"));
172+
}
173+
174+
@Test
175+
public void withHttpClientStep_verifyServerHealthForServerOverloadedAddedToPacket() {
176+
httpClientStub.withStatus(500).withSuccessful(false);
177+
178+
Packet packet =
179+
Stub.createStub(PacketStub.class)
180+
.withServerName(MANAGED_SERVER1)
181+
.withGetKeyReturnValue(httpClientStub);
182+
packet.put(
183+
ProcessingConstants.SERVER_HEALTH_MAP, new ConcurrentHashMap<String, ServerHealth>());
184+
packet.put(ProcessingConstants.REMAINING_SERVERS_HEALTH_TO_READ, new AtomicInteger(1));
185+
186+
withHttpClientStep.apply(packet);
187+
188+
Map<String, ServerHealth> serverHealthMap =
189+
packet.getValue(ProcessingConstants.SERVER_HEALTH_MAP);
190+
ServerHealth serverHealth = serverHealthMap.get(MANAGED_SERVER1);
191+
assertThat(
192+
serverHealth.getOverallHealth(), is(ReadHealthStep.OVERALL_HEALTH_FOR_SERVER_OVERLOADED));
193+
}
194+
195+
@Test
196+
public void withHttpClientStep_verifyServerHealthForOtherErrorAddedToPacket() {
197+
httpClientStub.withStatus(404).withSuccessful(false);
198+
199+
Packet packet =
200+
Stub.createStub(PacketStub.class)
201+
.withServerName(MANAGED_SERVER1)
202+
.withGetKeyReturnValue(httpClientStub);
203+
packet.put(
204+
ProcessingConstants.SERVER_HEALTH_MAP, new ConcurrentHashMap<String, ServerHealth>());
205+
packet.put(ProcessingConstants.REMAINING_SERVERS_HEALTH_TO_READ, new AtomicInteger(1));
206+
207+
withHttpClientStep.apply(packet);
208+
209+
Map<String, ServerHealth> serverHealthMap =
210+
packet.getValue(ProcessingConstants.SERVER_HEALTH_MAP);
211+
ServerHealth serverHealth = serverHealthMap.get(MANAGED_SERVER1);
212+
assertThat(serverHealth.getOverallHealth(), is(ReadHealthStep.OVERALL_HEALTH_NOT_AVAILABLE));
213+
}
214+
215+
static final String OK_RESPONSE =
216+
"{\n"
217+
+ " \"overallHealthState\": {\n"
218+
+ " \"state\": \"ok\",\n"
219+
+ " \"subsystemName\": null,\n"
220+
+ " \"partitionName\": null,\n"
221+
+ " \"symptoms\": []\n"
222+
+ " },\n"
223+
+ " \"activationTime\": 1556759105378\n"
224+
+ "}";
225+
167226
abstract static class PacketStub extends Packet {
168227

169228
String serverName;

0 commit comments

Comments
 (0)