-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kie-kogito-apps-2069: Improve Jobs Service liveness check to limit th…
…e amount of time to get the leader status (#2096) * kie-kogito-apps-2069: Improve Jobs Service liveness check to limit the amount of time to get the leader status * Update jobs-service/jobs-service-common/src/test/java/org/kie/kogito/jobs/service/management/JobServiceLeaderLivenessHealthCheckTest.java Co-authored-by: Gonzalo Muñoz <[email protected]> * Apply formatting * Tests improvements * JPAReactiveJobServiceManagementRepository updates --------- Co-authored-by: Gonzalo Muñoz <[email protected]>
- Loading branch information
Showing
11 changed files
with
236 additions
and
10 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
76 changes: 76 additions & 0 deletions
76
...main/java/org/kie/kogito/jobs/service/management/JobServiceLeaderLivenessHealthCheck.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,76 @@ | ||
/* | ||
* 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.kie.kogito.jobs.service.management; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.eclipse.microprofile.health.HealthCheck; | ||
import org.eclipse.microprofile.health.HealthCheckResponse; | ||
import org.eclipse.microprofile.health.HealthCheckResponseBuilder; | ||
import org.eclipse.microprofile.health.Liveness; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.event.Observes; | ||
|
||
@Liveness | ||
@ApplicationScoped | ||
public class JobServiceLeaderLivenessHealthCheck implements HealthCheck { | ||
|
||
private final AtomicBoolean enabled = new AtomicBoolean(false); | ||
|
||
private final AtomicLong startTime = new AtomicLong(); | ||
|
||
private static final String EXPIRATION_IN_SECONDS = "kogito.jobs-service.management.leader-check.expiration-in-seconds"; | ||
|
||
@ConfigProperty(name = EXPIRATION_IN_SECONDS, defaultValue = "-1") | ||
long expirationInSeconds; | ||
|
||
@PostConstruct | ||
void init() { | ||
startTime.set(getCurrentTimeMillis()); | ||
} | ||
|
||
@Override | ||
public HealthCheckResponse call() { | ||
final HealthCheckResponseBuilder responseBuilder = HealthCheckResponse.named("Get Leader Instance Timeout"); | ||
if (hasExpired() && !enabled.get()) { | ||
return responseBuilder.down().build(); | ||
} | ||
return responseBuilder.up().build(); | ||
} | ||
|
||
boolean hasExpired() { | ||
return (expirationInSeconds > 0) && (getCurrentTimeMillis() - startTime.get()) > (expirationInSeconds * 1000); | ||
} | ||
|
||
protected void onMessagingStatusChange(@Observes MessagingChangeEvent event) { | ||
this.enabled.set(event.isEnabled()); | ||
startTime.set(getCurrentTimeMillis()); | ||
} | ||
|
||
/** | ||
* Facilitates testing | ||
*/ | ||
long getCurrentTimeMillis() { | ||
return System.currentTimeMillis(); | ||
} | ||
} |
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
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
77 changes: 77 additions & 0 deletions
77
.../java/org/kie/kogito/jobs/service/management/JobServiceLeaderLivenessHealthCheckTest.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,77 @@ | ||
/* | ||
* 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.kie.kogito.jobs.service.management; | ||
|
||
import org.eclipse.microprofile.health.HealthCheckResponse; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.spy; | ||
|
||
class JobServiceLeaderLivenessHealthCheckTest { | ||
|
||
private static final long START_TIME = 1234; | ||
|
||
private JobServiceLeaderLivenessHealthCheck healthCheck; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
healthCheck = spy(new JobServiceLeaderLivenessHealthCheck()); | ||
doReturn(START_TIME).when(healthCheck).getCurrentTimeMillis(); | ||
healthCheck.init(); | ||
} | ||
|
||
@Test | ||
void timeoutNotSet() { | ||
doReturn(START_TIME + 1000 * 50).when(healthCheck).getCurrentTimeMillis(); | ||
assertThat(healthCheck.call().getStatus()) | ||
.isNotNull() | ||
.isEqualTo(HealthCheckResponse.Status.UP); | ||
} | ||
|
||
@Test | ||
void timeoutSetButNotReached() { | ||
healthCheck.expirationInSeconds = 60; | ||
doReturn(START_TIME + 1000 * 10).when(healthCheck).getCurrentTimeMillis(); | ||
assertThat(healthCheck.call().getStatus()) | ||
.isNotNull() | ||
.isEqualTo(HealthCheckResponse.Status.UP); | ||
} | ||
|
||
@Test | ||
void timeoutSetAndReached() { | ||
healthCheck.expirationInSeconds = 60; | ||
doReturn(START_TIME + 1000 * 60 + 1).when(healthCheck).getCurrentTimeMillis(); | ||
assertThat(healthCheck.call().getStatus()) | ||
.isNotNull() | ||
.isEqualTo(HealthCheckResponse.Status.DOWN); | ||
} | ||
|
||
@Test | ||
void statusChanged() { | ||
healthCheck.onMessagingStatusChange(new MessagingChangeEvent(true)); | ||
doReturn(START_TIME + 1000 * 10).when(healthCheck).getCurrentTimeMillis(); | ||
HealthCheckResponse response = healthCheck.call(); | ||
assertThat(response.getStatus()) | ||
.isNotNull() | ||
.isEqualTo(HealthCheckResponse.Status.UP); | ||
} | ||
} |
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
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