Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
2232f7d8
Commit
2232f7d8
authored
Oct 29, 2018
by
Brian Clozel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish
Closes gh-14914
parent
3e6a4eb6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
26 deletions
+41
-26
ElasticsearchJestHealthIndicator.java
...tuate/elasticsearch/ElasticsearchJestHealthIndicator.java
+12
-6
ElasticsearchJestHealthIndicatorTests.java
.../elasticsearch/ElasticsearchJestHealthIndicatorTests.java
+29
-20
No files found.
spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchJestHealthIndicator.java
View file @
2232f7d8
...
@@ -30,6 +30,7 @@ import org.springframework.boot.actuate.health.HealthIndicator;
...
@@ -30,6 +30,7 @@ import org.springframework.boot.actuate.health.HealthIndicator;
*
*
* @author Stephane Nicoll
* @author Stephane Nicoll
* @author Julian Devia Serna
* @author Julian Devia Serna
* @author Brian Clozel
* @since 2.0.0
* @since 2.0.0
*/
*/
public
class
ElasticsearchJestHealthIndicator
extends
AbstractHealthIndicator
{
public
class
ElasticsearchJestHealthIndicator
extends
AbstractHealthIndicator
{
...
@@ -47,14 +48,19 @@ public class ElasticsearchJestHealthIndicator extends AbstractHealthIndicator {
...
@@ -47,14 +48,19 @@ public class ElasticsearchJestHealthIndicator extends AbstractHealthIndicator {
protected
void
doHealthCheck
(
Health
.
Builder
builder
)
throws
Exception
{
protected
void
doHealthCheck
(
Health
.
Builder
builder
)
throws
Exception
{
JestResult
healthResult
=
this
.
jestClient
JestResult
healthResult
=
this
.
jestClient
.
execute
(
new
io
.
searchbox
.
cluster
.
Health
.
Builder
().
build
());
.
execute
(
new
io
.
searchbox
.
cluster
.
Health
.
Builder
().
build
());
JsonElement
root
=
this
.
jsonParser
.
parse
(
healthResult
.
getJsonString
());
if
(
healthResult
.
getResponseCode
()
!=
200
||
!
healthResult
.
isSucceeded
())
{
JsonElement
status
=
root
.
getAsJsonObject
().
get
(
"status"
);
builder
.
down
();
if
(!
healthResult
.
isSucceeded
()
||
healthResult
.
getResponseCode
()
!=
200
||
status
.
getAsString
().
equals
(
io
.
searchbox
.
cluster
.
Health
.
Status
.
RED
.
getKey
()))
{
builder
.
outOfService
();
}
}
else
{
else
{
builder
.
up
();
JsonElement
root
=
this
.
jsonParser
.
parse
(
healthResult
.
getJsonString
());
JsonElement
status
=
root
.
getAsJsonObject
().
get
(
"status"
);
if
(
status
.
getAsString
()
.
equals
(
io
.
searchbox
.
cluster
.
Health
.
Status
.
RED
.
getKey
()))
{
builder
.
outOfService
();
}
else
{
builder
.
up
();
}
}
}
}
}
...
...
spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchJestHealthIndicatorTests.java
View file @
2232f7d8
/*
/*
* Copyright 2012-201
7
the original author or authors.
* Copyright 2012-201
8
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -40,6 +40,7 @@ import static org.mockito.Mockito.mock;
...
@@ -40,6 +40,7 @@ import static org.mockito.Mockito.mock;
*
*
* @author Stephane Nicoll
* @author Stephane Nicoll
* @author Julian Devia Serna
* @author Julian Devia Serna
* @author Brian Clozel
*/
*/
public
class
ElasticsearchJestHealthIndicatorTests
{
public
class
ElasticsearchJestHealthIndicatorTests
{
...
@@ -52,7 +53,7 @@ public class ElasticsearchJestHealthIndicatorTests {
...
@@ -52,7 +53,7 @@ public class ElasticsearchJestHealthIndicatorTests {
@Test
@Test
public
void
elasticsearchIsUp
()
throws
IOException
{
public
void
elasticsearchIsUp
()
throws
IOException
{
given
(
this
.
jestClient
.
execute
(
any
(
Action
.
class
)))
given
(
this
.
jestClient
.
execute
(
any
(
Action
.
class
)))
.
willReturn
(
createJestResult
(
"green"
,
200
,
true
));
.
willReturn
(
createJestResult
(
200
,
true
,
"green"
));
Health
health
=
this
.
healthIndicator
.
health
();
Health
health
=
this
.
healthIndicator
.
health
();
assertThat
(
health
.
getStatus
()).
isEqualTo
(
Status
.
UP
);
assertThat
(
health
.
getStatus
()).
isEqualTo
(
Status
.
UP
);
}
}
...
@@ -68,42 +69,50 @@ public class ElasticsearchJestHealthIndicatorTests {
...
@@ -68,42 +69,50 @@ public class ElasticsearchJestHealthIndicatorTests {
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
@Test
@Test
public
void
elasticsearchIs
OutOfServiceByStatus
()
throws
IOException
{
public
void
elasticsearchIs
DownWhenQueryDidNotSucceed
()
throws
IOException
{
given
(
this
.
jestClient
.
execute
(
any
(
Action
.
class
)))
given
(
this
.
jestClient
.
execute
(
any
(
Action
.
class
)))
.
willReturn
(
createJestResult
(
"red"
,
200
,
true
));
.
willReturn
(
createJestResult
(
200
,
false
,
""
));
Health
health
=
this
.
healthIndicator
.
health
();
Health
health
=
this
.
healthIndicator
.
health
();
assertThat
(
health
.
getStatus
()).
isEqualTo
(
Status
.
OUT_OF_SERVICE
);
assertThat
(
health
.
getStatus
()).
isEqualTo
(
Status
.
DOWN
);
}
}
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
@Test
@Test
public
void
elasticsearchIs
OutOfService
ByResponseCode
()
throws
IOException
{
public
void
elasticsearchIs
Down
ByResponseCode
()
throws
IOException
{
given
(
this
.
jestClient
.
execute
(
any
(
Action
.
class
)))
given
(
this
.
jestClient
.
execute
(
any
(
Action
.
class
)))
.
willReturn
(
createJestResult
(
""
,
500
,
true
));
.
willReturn
(
createJestResult
(
500
,
false
,
""
));
Health
health
=
this
.
healthIndicator
.
health
();
Health
health
=
this
.
healthIndicator
.
health
();
assertThat
(
health
.
getStatus
()).
isEqualTo
(
Status
.
OUT_OF_SERVICE
);
assertThat
(
health
.
getStatus
()).
isEqualTo
(
Status
.
DOWN
);
}
}
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
@Test
@Test
public
void
elasticsearchIsOutOfServiceByS
ucceeded
()
throws
IOException
{
public
void
elasticsearchIsOutOfServiceByS
tatus
()
throws
IOException
{
given
(
this
.
jestClient
.
execute
(
any
(
Action
.
class
)))
given
(
this
.
jestClient
.
execute
(
any
(
Action
.
class
)))
.
willReturn
(
createJestResult
(
"red"
,
500
,
false
));
.
willReturn
(
createJestResult
(
200
,
true
,
"red"
));
Health
health
=
this
.
healthIndicator
.
health
();
Health
health
=
this
.
healthIndicator
.
health
();
assertThat
(
health
.
getStatus
()).
isEqualTo
(
Status
.
OUT_OF_SERVICE
);
assertThat
(
health
.
getStatus
()).
isEqualTo
(
Status
.
OUT_OF_SERVICE
);
}
}
private
static
JestResult
createJestResult
(
String
status
,
int
responseCode
,
private
static
JestResult
createJestResult
(
int
responseCode
,
boolean
succeeded
,
boolean
succeeded
)
{
String
status
)
{
String
json
=
String
.
format
(
"{\"cluster_name\":\"docker-cluster\","
+
"\"status\":\"%s\",\"timed_out\":false,\"number_of_nodes\":1,"
+
"\"number_of_data_nodes\":1,\"active_primary_shards\":0,"
+
"\"active_shards\":0,\"relocating_shards\":0,\"initializing_shards\":0,"
+
"\"unassigned_shards\":0,\"delayed_unassigned_shards\":0,"
+
"\"number_of_pending_tasks\":0,\"number_of_in_flight_fetch\":0,"
+
"\"task_max_waiting_in_queue_millis\":0,\"active_shards_percent_as_number\":100.0}"
,
status
);
SearchResult
searchResult
=
new
SearchResult
(
new
Gson
());
SearchResult
searchResult
=
new
SearchResult
(
new
Gson
());
String
json
;
if
(
responseCode
==
200
)
{
json
=
String
.
format
(
"{\"cluster_name\":\"elasticsearch\","
+
"\"status\":\"%s\",\"timed_out\":false,\"number_of_nodes\":1,"
+
"\"number_of_data_nodes\":1,\"active_primary_shards\":0,"
+
"\"active_shards\":0,\"relocating_shards\":0,\"initializing_shards\":0,"
+
"\"unassigned_shards\":0,\"delayed_unassigned_shards\":0,"
+
"\"number_of_pending_tasks\":0,\"number_of_in_flight_fetch\":0,"
+
"\"task_max_waiting_in_queue_millis\":0,\"active_shards_percent_as_number\":100.0}"
,
status
);
}
else
{
json
=
"{\n"
+
" \"error\": \"Server Error\",\n"
+
" \"status\": "
+
responseCode
+
"\n"
+
"}"
;
}
searchResult
.
setJsonString
(
json
);
searchResult
.
setJsonString
(
json
);
searchResult
.
setJsonObject
(
new
JsonParser
().
parse
(
json
).
getAsJsonObject
());
searchResult
.
setJsonObject
(
new
JsonParser
().
parse
(
json
).
getAsJsonObject
());
searchResult
.
setResponseCode
(
responseCode
);
searchResult
.
setResponseCode
(
responseCode
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment