Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.flink.kubernetes.operator.health;

import org.apache.flink.annotation.Experimental;
import org.apache.flink.kubernetes.operator.observer.ClusterHealthResult;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -47,15 +48,15 @@ public class ClusterHealthInfo {
private long numCompletedCheckpointsIncreasedTimeStamp;

/** Calculated field whether the cluster is healthy or not. */
private boolean healthy;
private ClusterHealthResult healthResult;

public ClusterHealthInfo() {
this(Clock.systemDefaultZone());
}

public ClusterHealthInfo(Clock clock) {
timeStamp = clock.millis();
healthy = true;
healthResult = ClusterHealthResult.healthy();
}

public static boolean isValid(ClusterHealthInfo clusterHealthInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,28 @@ public void evaluate(
LOG.debug("Last valid health info: {}", lastValidClusterHealthInfo);
LOG.debug("Observed health info: {}", observedClusterHealthInfo);

boolean isHealthy =
var jobHealthResult =
evaluateRestarts(
configuration,
clusterInfo,
lastValidClusterHealthInfo,
observedClusterHealthInfo)
&& evaluateCheckpoints(
configuration,
lastValidClusterHealthInfo,
observedClusterHealthInfo);
configuration,
clusterInfo,
lastValidClusterHealthInfo,
observedClusterHealthInfo);

var checkpointHealthResult =
evaluateCheckpoints(
configuration,
lastValidClusterHealthInfo,
observedClusterHealthInfo);

lastValidClusterHealthInfo.setTimeStamp(observedClusterHealthInfo.getTimeStamp());
lastValidClusterHealthInfo.setHealthy(isHealthy);
lastValidClusterHealthInfo.setHealthResult(
jobHealthResult.join(checkpointHealthResult));
setLastValidClusterHealthInfo(clusterInfo, lastValidClusterHealthInfo);
}
}
}

private boolean evaluateRestarts(
private ClusterHealthResult evaluateRestarts(
Configuration configuration,
Map<String, String> clusterInfo,
ClusterHealthInfo lastValidClusterHealthInfo,
Expand All @@ -128,7 +131,7 @@ private boolean evaluateRestarts(
lastValidClusterHealthInfo.setNumRestarts(observedClusterHealthInfo.getNumRestarts());
lastValidClusterHealthInfo.setNumRestartsEvaluationTimeStamp(
observedClusterHealthInfo.getTimeStamp());
return true;
return ClusterHealthResult.healthy();
}

var timestampDiffMs =
Expand All @@ -153,9 +156,15 @@ private boolean evaluateRestarts(
LOG.debug("Calculated restart count for {} window: {}", restartCheckWindow, numRestarts);

var restartThreshold = configuration.get(OPERATOR_CLUSTER_HEALTH_CHECK_RESTARTS_THRESHOLD);

var healthResult = ClusterHealthResult.healthy();

boolean isHealthy = numRestarts <= restartThreshold;
if (!isHealthy) {
LOG.info("Restart count hit threshold: {}", restartThreshold);
healthResult =
ClusterHealthResult.error(
String.format("Restart count hit threshold: %s", restartThreshold));
}

if (lastValidClusterHealthInfo.getNumRestartsEvaluationTimeStamp()
Expand All @@ -167,15 +176,15 @@ private boolean evaluateRestarts(
observedClusterHealthInfo.getTimeStamp());
}

return isHealthy;
return healthResult;
}

private boolean evaluateCheckpoints(
private ClusterHealthResult evaluateCheckpoints(
Configuration configuration,
ClusterHealthInfo lastValidClusterHealthInfo,
ClusterHealthInfo observedClusterHealthInfo) {
if (!configuration.getBoolean(OPERATOR_CLUSTER_HEALTH_CHECK_CHECKPOINT_PROGRESS_ENABLED)) {
return true;
return ClusterHealthResult.healthy();
}

var windowOpt =
Expand All @@ -195,7 +204,7 @@ private boolean evaluateCheckpoints(
if (windowOpt.isEmpty() && !checkpointConfig.isCheckpointingEnabled()) {
// If no explicit checkpoint check window is specified and checkpointing is disabled
// based on the config, we don't do anything
return true;
return ClusterHealthResult.healthy();
}

var completedCheckpointsCheckWindow =
Expand Down Expand Up @@ -226,15 +235,14 @@ private boolean evaluateCheckpoints(
observedClusterHealthInfo.getNumCompletedCheckpoints());
lastValidClusterHealthInfo.setNumCompletedCheckpointsIncreasedTimeStamp(
observedClusterHealthInfo.getTimeStamp());
return true;
return ClusterHealthResult.healthy();
}

var timestampDiffMs =
observedClusterHealthInfo.getTimeStamp()
- lastValidClusterHealthInfo.getNumCompletedCheckpointsIncreasedTimeStamp();
LOG.debug("Time difference between health infos: {}", Duration.ofMillis(timestampDiffMs));

boolean isHealthy = true;
var completedCheckpointsCheckWindowMs = completedCheckpointsCheckWindow.toMillis();

if (observedClusterHealthInfo.getNumCompletedCheckpoints()
Expand All @@ -248,9 +256,9 @@ private boolean evaluateCheckpoints(
+ completedCheckpointsCheckWindowMs
< clock.millis()) {
LOG.info("Cluster is not able to complete checkpoints");
isHealthy = false;
return ClusterHealthResult.error("Cluster is not able to complete checkpoints");
}

return isHealthy;
return ClusterHealthResult.healthy();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.apache.flink.kubernetes.operator.observer;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Value;

import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/** Cluster Health Result. */
@Value
public class ClusterHealthResult {
boolean healthy;
String error;

@JsonCreator
public ClusterHealthResult(
@JsonProperty("healthy") boolean healthy, @JsonProperty("error") String error) {
this.healthy = healthy;
this.error = error;
}

public static ClusterHealthResult error(String error) {
return new ClusterHealthResult(false, error);
}

public static ClusterHealthResult healthy() {
return new ClusterHealthResult(true, null);
}

public ClusterHealthResult join(ClusterHealthResult clusterHealthResult) {
boolean isHealthy = this.healthy && clusterHealthResult.healthy;
String error =
Stream.of(this.error, clusterHealthResult.getError())
.filter(Objects::nonNull)
.collect(Collectors.joining("|"));

return new ClusterHealthResult(isHealthy, error);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.flink.kubernetes.operator.exception.UpgradeFailureException;
import org.apache.flink.kubernetes.operator.health.ClusterHealthInfo;
import org.apache.flink.kubernetes.operator.observer.ClusterHealthEvaluator;
import org.apache.flink.kubernetes.operator.observer.ClusterHealthResult;
import org.apache.flink.kubernetes.operator.reconciler.ReconciliationUtils;
import org.apache.flink.kubernetes.operator.service.FlinkService;
import org.apache.flink.kubernetes.operator.service.SuspendMode;
Expand Down Expand Up @@ -268,6 +269,9 @@ public boolean reconcileOtherChanges(FlinkResourceContext<FlinkDeployment> ctx)

var deployment = ctx.getResource();
var observeConfig = ctx.getObserveConfig();
var clusterHealthInfo =
ClusterHealthEvaluator.getLastValidClusterHealthInfo(
deployment.getStatus().getClusterInfo());
boolean shouldRestartJobBecauseUnhealthy =
shouldRestartJobBecauseUnhealthy(deployment, observeConfig);
boolean shouldRecoverDeployment = shouldRecoverDeployment(observeConfig, deployment);
Expand All @@ -288,7 +292,10 @@ public boolean reconcileOtherChanges(FlinkResourceContext<FlinkDeployment> ctx)
EventRecorder.Type.Warning,
EventRecorder.Reason.RestartUnhealthyJob,
EventRecorder.Component.Job,
MSG_RESTART_UNHEALTHY,
Optional.ofNullable(clusterHealthInfo)
.map(ClusterHealthInfo::getHealthResult)
.map(ClusterHealthResult::getError)
.orElse(MSG_RESTART_UNHEALTHY),
ctx.getKubernetesClient());
cleanupAfterFailedJob(ctx);
}
Expand All @@ -312,7 +319,7 @@ private boolean shouldRestartJobBecauseUnhealthy(
ClusterHealthEvaluator.getLastValidClusterHealthInfo(clusterInfo);
if (clusterHealthInfo != null) {
LOG.debug("Cluster info contains job health info");
if (!clusterHealthInfo.isHealthy()) {
if (!clusterHealthInfo.getHealthResult().isHealthy()) {
if (deployment.getSpec().getJob().getUpgradeMode() == UpgradeMode.STATELESS) {
LOG.debug("Stateless job, recovering unhealthy jobmanager deployment");
restartNeeded = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.flink.kubernetes.operator.health;

import org.apache.flink.kubernetes.operator.observer.ClusterHealthResult;

import org.junit.jupiter.api.Test;

import java.time.Clock;
Expand All @@ -43,11 +45,13 @@ public void isValidShouldReturnTrueWhenTimestampIsNonzero() {

@Test
public void deserializeWithOldVersionShouldDeserializeCorrectly() {
var clusterHealthInfoJson = "{\"timeStamp\":1,\"numRestarts\":2,\"healthy\":true}";
var clusterHealthInfoJson =
"{\"timeStamp\":1,\"numRestarts\":2,\"healthResult\": {\"healthy\":false, \"error\":\"test-error\"}}}}";
var clusterHealthInfoFromJson = ClusterHealthInfo.deserialize(clusterHealthInfoJson);
assertEquals(1, clusterHealthInfoFromJson.getTimeStamp());
assertEquals(2, clusterHealthInfoFromJson.getNumRestarts());
assertTrue(clusterHealthInfoFromJson.isHealthy());
assertFalse(clusterHealthInfoFromJson.getHealthResult().isHealthy());
assertEquals("test-error", clusterHealthInfoFromJson.getHealthResult().getError());
}

@Test
Expand All @@ -58,7 +62,7 @@ public void serializationRoundTrip() {
clusterHealthInfo.setNumRestartsEvaluationTimeStamp(3);
clusterHealthInfo.setNumCompletedCheckpoints(4);
clusterHealthInfo.setNumCompletedCheckpointsIncreasedTimeStamp(5);
clusterHealthInfo.setHealthy(false);
clusterHealthInfo.setHealthResult(ClusterHealthResult.error("error"));
var clusterHealthInfoJson = ClusterHealthInfo.serialize(clusterHealthInfo);

var clusterHealthInfoFromJson = ClusterHealthInfo.deserialize(clusterHealthInfoJson);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,6 @@ private void assertClusterHealthIs(boolean healthy) {
var lastValidClusterHealthInfo =
ClusterHealthEvaluator.getLastValidClusterHealthInfo(clusterInfo);
assertNotNull(lastValidClusterHealthInfo);
assertEquals(healthy, lastValidClusterHealthInfo.isHealthy());
assertEquals(healthy, lastValidClusterHealthInfo.getHealthResult().isHealthy());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.apache.flink.kubernetes.operator.observer;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ClusterHealthResultTest {

@Test
void error() {
ClusterHealthResult clusterHealthResult = ClusterHealthResult.error("test-error");

assertFalse(clusterHealthResult.isHealthy());
assertEquals("test-error", clusterHealthResult.getError());
}

@Test
void healthy() {
ClusterHealthResult clusterHealthResult = ClusterHealthResult.healthy();

assertTrue(clusterHealthResult.isHealthy());
assertNull(clusterHealthResult.getError());
}

@Test
void join() {
ClusterHealthResult clusterHealthResult =
ClusterHealthResult.healthy()
.join(ClusterHealthResult.error("test-error-1"))
.join(ClusterHealthResult.error("test-error-2"));

assertFalse(clusterHealthResult.isHealthy());
assertEquals("test-error-1|test-error-2", clusterHealthResult.getError());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.apache.flink.kubernetes.operator.exception.UpgradeFailureException;
import org.apache.flink.kubernetes.operator.health.ClusterHealthInfo;
import org.apache.flink.kubernetes.operator.observer.ClusterHealthEvaluator;
import org.apache.flink.kubernetes.operator.observer.ClusterHealthResult;
import org.apache.flink.kubernetes.operator.reconciler.ReconciliationUtils;
import org.apache.flink.kubernetes.operator.reconciler.SnapshotType;
import org.apache.flink.kubernetes.operator.reconciler.TestReconcilerAdapter;
Expand Down Expand Up @@ -128,7 +129,6 @@
import static org.apache.flink.kubernetes.operator.reconciler.SnapshotType.SAVEPOINT;
import static org.apache.flink.kubernetes.operator.reconciler.deployment.AbstractFlinkResourceReconciler.MSG_SUBMIT;
import static org.apache.flink.kubernetes.operator.reconciler.deployment.ApplicationReconciler.MSG_RECOVERY;
import static org.apache.flink.kubernetes.operator.reconciler.deployment.ApplicationReconciler.MSG_RESTART_UNHEALTHY;
import static org.apache.flink.kubernetes.operator.utils.SnapshotUtils.getLastSnapshotStatus;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -1223,12 +1223,11 @@ public void testRestartUnhealthyEvent() throws Exception {
var clusterHealthInfo = new ClusterHealthInfo();
clusterHealthInfo.setTimeStamp(System.currentTimeMillis());
clusterHealthInfo.setNumRestarts(2);
clusterHealthInfo.setHealthy(false);
clusterHealthInfo.setHealthResult(ClusterHealthResult.error("error"));
ClusterHealthEvaluator.setLastValidClusterHealthInfo(
deployment.getStatus().getClusterInfo(), clusterHealthInfo);
reconciler.reconcile(deployment, context);
Assertions.assertEquals(
MSG_RESTART_UNHEALTHY, flinkResourceEventCollector.events.remove().getMessage());
Assertions.assertEquals("error", flinkResourceEventCollector.events.remove().getMessage());
}

@Test
Expand Down