Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4de7b62

Browse files
committed
Make SysClusterHealth/TableHealth.compute synchronous
Having the methods return futures gives the impression that they do something asynchronous, but they don't. There is no disk or network IO involved.
1 parent 08f55b4 commit 4de7b62

4 files changed

Lines changed: 38 additions & 47 deletions

File tree

server/src/main/java/io/crate/metadata/sys/SysClusterHealth.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import java.util.List;
2929
import java.util.Set;
30-
import java.util.concurrent.CompletableFuture;
3130

3231
import org.elasticsearch.cluster.ClusterState;
3332
import org.elasticsearch.cluster.block.ClusterBlock;
@@ -52,7 +51,7 @@ public class SysClusterHealth {
5251
.withRouting((state, ignored, ignored2) -> Routing.forMasterNode(IDENT, state))
5352
.build();
5453

55-
public static CompletableFuture<Iterable<ClusterHealth>> compute(ClusterState clusterState, long numPendingTasks) {
54+
public static Iterable<ClusterHealth> compute(ClusterState clusterState, long numPendingTasks) {
5655
// Following implementation of {@link org.elasticsearch.cluster.health.ClusterStateHealth}
5756
Set<ClusterBlock> blocksRed = clusterState.blocks().global(RestStatus.SERVICE_UNAVAILABLE);
5857
if (!blocksRed.isEmpty()) {
@@ -64,34 +63,31 @@ public static CompletableFuture<Iterable<ClusterHealth>> compute(ClusterState cl
6463
-1,
6564
numPendingTasks
6665
);
67-
return CompletableFuture.completedFuture(List.of(clusterHealth));
66+
return List.of(clusterHealth);
6867
}
6968
Set<ClusterBlock> blocksYellow = clusterState.blocks().global(ClusterBlockLevel.METADATA_WRITE);
7069
final TableHealth.Health clusterHealth = !blocksYellow.isEmpty() ? TableHealth.Health.YELLOW : TableHealth.Health.GREEN;
7170
final String description = !blocksYellow.isEmpty() ? blocksYellow.iterator().next().description() : "";
72-
return TableHealth.compute(clusterState)
73-
.thenApply((it) -> {
74-
long missingShards = 0;
75-
long underreplicatedShards = 0;
76-
TableHealth.Health health = clusterHealth;
77-
String finalDescription = description;
78-
for (var tableHealth : it) {
79-
if (tableHealth.health().severity() > health.severity()) {
80-
health = tableHealth.health();
81-
// shard level health is only set to RED if there are missing primary shards that were
82-
// allocated before see {@link ClusterShardHealth#getInactivePrimaryHealth()}.
83-
// Otherwise, the table health is set to YELLOW.
84-
if (health == TableHealth.Health.RED || tableHealth.getMissingShards() > 0) {
85-
finalDescription = "One or more tables are missing shards";
86-
} else if (health == TableHealth.Health.YELLOW) {
87-
finalDescription = "One or more tables have underreplicated shards";
88-
}
89-
}
90-
missingShards += tableHealth.getMissingShards();
91-
underreplicatedShards += tableHealth.getUnderreplicatedShards();
71+
long missingShards = 0;
72+
long underreplicatedShards = 0;
73+
TableHealth.Health health = clusterHealth;
74+
String finalDescription = description;
75+
for (var tableHealth : TableHealth.compute(clusterState)) {
76+
if (tableHealth.health().severity() > health.severity()) {
77+
health = tableHealth.health();
78+
// shard level health is only set to RED if there are missing primary shards that were
79+
// allocated before see {@link ClusterShardHealth#getInactivePrimaryHealth()}.
80+
// Otherwise, the table health is set to YELLOW.
81+
if (health == TableHealth.Health.RED || tableHealth.getMissingShards() > 0) {
82+
finalDescription = "One or more tables are missing shards";
83+
} else if (health == TableHealth.Health.YELLOW) {
84+
finalDescription = "One or more tables have underreplicated shards";
9285
}
93-
return List.of(new ClusterHealth(health, finalDescription, missingShards, underreplicatedShards, numPendingTasks));
94-
});
86+
}
87+
missingShards += tableHealth.getMissingShards();
88+
underreplicatedShards += tableHealth.getUnderreplicatedShards();
89+
}
90+
return List.of(new ClusterHealth(health, finalDescription, missingShards, underreplicatedShards, numPendingTasks));
9591
}
9692

9793
public record ClusterHealth(TableHealth.Health health,

server/src/main/java/io/crate/metadata/sys/SysTableDefinitions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.elasticsearch.common.inject.Inject;
3636
import org.elasticsearch.repositories.RepositoriesService;
3737

38-
import io.crate.session.Sessions;
3938
import io.crate.execution.engine.collect.files.SummitsIterable;
4039
import io.crate.execution.engine.collect.stats.JobsLogs;
4140
import io.crate.expression.reference.StaticTableDefinition;
@@ -54,6 +53,7 @@
5453
import io.crate.role.metadata.SysPrivilegesTableInfo;
5554
import io.crate.role.metadata.SysRolesTableInfo;
5655
import io.crate.role.metadata.SysUsersTableInfo;
56+
import io.crate.session.Sessions;
5757

5858
public class SysTableDefinitions {
5959

@@ -204,7 +204,7 @@ public SysTableDefinitions(ClusterService clusterService,
204204
Map.entry(
205205
SysHealth.IDENT,
206206
new StaticTableDefinition<>(
207-
() -> TableHealth.compute(clusterService.state()),
207+
() -> completedFuture(TableHealth.compute(clusterService.state())),
208208
SysHealth.INSTANCE.expressions(),
209209
(user, tableHealth) -> roles.hasAnyPrivilege(user, Securable.TABLE, tableHealth.fqn()),
210210
true
@@ -213,7 +213,7 @@ public SysTableDefinitions(ClusterService clusterService,
213213
Map.entry(
214214
SysClusterHealth.IDENT,
215215
new StaticTableDefinition<>(
216-
() -> SysClusterHealth.compute(clusterService.state(), clusterService.getMasterService().numberOfPendingTasks()),
216+
() -> completedFuture(SysClusterHealth.compute(clusterService.state(), clusterService.getMasterService().numberOfPendingTasks())),
217217
SysClusterHealth.INSTANCE.expressions(),
218218
false
219219
)

server/src/main/java/io/crate/metadata/sys/TableHealth.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121

2222
package io.crate.metadata.sys;
2323

24-
import static java.util.concurrent.CompletableFuture.completedFuture;
25-
26-
import java.util.concurrent.CompletableFuture;
2724
import java.util.stream.StreamSupport;
2825

2926
import org.elasticsearch.cluster.ClusterState;
@@ -45,13 +42,11 @@ public short severity() {
4542
}
4643
}
4744

48-
public static CompletableFuture<Iterable<TableHealth>> compute(ClusterState clusterState) {
45+
public static Iterable<TableHealth> compute(ClusterState clusterState) {
4946
var clusterHealth = new ClusterStateHealth(clusterState);
50-
return completedFuture(
51-
StreamSupport.stream(clusterHealth.spliterator(), false)
52-
.filter(i -> IndexName.isDangling(i.getIndex()) == false)
53-
.map(TableHealth::map)::iterator
54-
);
47+
return StreamSupport.stream(clusterHealth.spliterator(), false)
48+
.filter(i -> IndexName.isDangling(i.getIndex()) == false)
49+
.map(TableHealth::map)::iterator;
5550
}
5651

5752
private static TableHealth map(ClusterIndexHealth indexHealth) {

server/src/test/java/io/crate/metadata/sys/SysClusterHealthTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class SysClusterHealthTest extends CrateDummyClusterServiceUnitTest {
4242

4343
@Test
4444
public void test_green_no_tables() {
45-
var healths = SysClusterHealth.compute(clusterService.state(), 0).join();
45+
var healths = SysClusterHealth.compute(clusterService.state(), 0);
4646
var expected = new SysClusterHealth.ClusterHealth(
4747
TableHealth.Health.GREEN,
4848
"",
@@ -59,7 +59,7 @@ public void test_green_with_tables() throws IOException {
5959
.addTable("create table doc.t1 (id int) with (number_of_replicas = 0)");
6060
executor.startShards("doc.t1");
6161

62-
var healths = SysClusterHealth.compute(clusterService.state(), 0).join();
62+
var healths = SysClusterHealth.compute(clusterService.state(), 0);
6363
var expected = new SysClusterHealth.ClusterHealth(
6464
TableHealth.Health.GREEN,
6565
"",
@@ -85,7 +85,7 @@ public void test_cluster_is_yellow() {
8585
var newState = ClusterState.builder(clusterService.state())
8686
.blocks(ClusterBlocks.builder().addGlobalBlock(globalBlock))
8787
.build();
88-
var healths = SysClusterHealth.compute(newState, 0).join();
88+
var healths = SysClusterHealth.compute(newState, 0);
8989
var expected = new SysClusterHealth.ClusterHealth(
9090
TableHealth.Health.YELLOW,
9191
"cannot write metadata",
@@ -101,7 +101,7 @@ public void test_tables_are_yellow() throws Exception {
101101
SQLExecutor executor = SQLExecutor.builder(clusterService).build()
102102
.addTable("create table doc.t1 (id int) with (number_of_replicas = 1)");
103103

104-
var healths = SysClusterHealth.compute(clusterService.state(), 0).join();
104+
var healths = SysClusterHealth.compute(clusterService.state(), 0);
105105
var expected = new SysClusterHealth.ClusterHealth(
106106
TableHealth.Health.YELLOW,
107107
"One or more tables are missing shards",
@@ -113,7 +113,7 @@ public void test_tables_are_yellow() throws Exception {
113113

114114
executor.startShards("doc.t1");
115115

116-
healths = SysClusterHealth.compute(clusterService.state(), 0).join();
116+
healths = SysClusterHealth.compute(clusterService.state(), 0);
117117
expected = new SysClusterHealth.ClusterHealth(
118118
TableHealth.Health.YELLOW,
119119
"One or more tables have underreplicated shards",
@@ -143,7 +143,7 @@ public void test_cluster_and_tables_are_yellow() throws Exception {
143143
var newState = ClusterState.builder(clusterService.state())
144144
.blocks(ClusterBlocks.builder().addGlobalBlock(globalBlock))
145145
.build();
146-
var healths = SysClusterHealth.compute(newState, 0).join();
146+
var healths = SysClusterHealth.compute(newState, 0);
147147
var expected = new SysClusterHealth.ClusterHealth(
148148
TableHealth.Health.YELLOW,
149149
"Cannot write metadata", // cluster level message takes precedence
@@ -172,7 +172,7 @@ public void test_cluster_is_red() throws Exception {
172172
var newState = ClusterState.builder(clusterService.state())
173173
.blocks(ClusterBlocks.builder().addGlobalBlock(globalBlock))
174174
.build();
175-
var healths = SysClusterHealth.compute(newState, 0).join();
175+
var healths = SysClusterHealth.compute(newState, 0);
176176
var expected = new SysClusterHealth.ClusterHealth(
177177
TableHealth.Health.RED,
178178
"recovering", // cluster level message takes precedence
@@ -190,7 +190,7 @@ public void test_tables_are_red() throws Exception {
190190
executor.startShards("doc.t1");
191191
executor.failShards("doc.t1");
192192

193-
var healths = SysClusterHealth.compute(clusterService.state(), 0).join();
193+
var healths = SysClusterHealth.compute(clusterService.state(), 0);
194194
var expected = new SysClusterHealth.ClusterHealth(
195195
TableHealth.Health.RED,
196196
"One or more tables are missing shards",
@@ -203,7 +203,7 @@ public void test_tables_are_red() throws Exception {
203203

204204
@Test
205205
public void test_pending_tasks() throws Exception {
206-
var healths = SysClusterHealth.compute(clusterService.state(), 1).join();
206+
var healths = SysClusterHealth.compute(clusterService.state(), 1);
207207
assertThat(healths).containsExactly(new SysClusterHealth.ClusterHealth(
208208
TableHealth.Health.GREEN,
209209
"",
@@ -212,7 +212,7 @@ public void test_pending_tasks() throws Exception {
212212
1
213213
));
214214

215-
healths = SysClusterHealth.compute(clusterService.state(), 0).join();
215+
healths = SysClusterHealth.compute(clusterService.state(), 0);
216216
assertThat(healths).containsExactly(new SysClusterHealth.ClusterHealth(
217217
TableHealth.Health.GREEN,
218218
"",

0 commit comments

Comments
 (0)