-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add health check endpoint (#3501)
- Loading branch information
Showing
20 changed files
with
1,123 additions
and
87 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
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
117 changes: 117 additions & 0 deletions
117
ksql-rest-app/src/main/java/io/confluent/ksql/rest/healthcheck/HealthCheckAgent.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,117 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.rest.healthcheck; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import io.confluent.ksql.rest.client.RestResponse; | ||
import io.confluent.ksql.rest.entity.HealthCheckResponse; | ||
import io.confluent.ksql.rest.entity.HealthCheckResponseDetail; | ||
import io.confluent.ksql.rest.entity.KsqlEntityList; | ||
import io.confluent.ksql.rest.server.KsqlRestConfig; | ||
import io.confluent.ksql.services.SimpleKsqlClient; | ||
import io.confluent.rest.RestConfig; | ||
import java.net.URI; | ||
import java.net.URL; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import org.apache.kafka.common.config.ConfigException; | ||
|
||
public class HealthCheckAgent { | ||
|
||
public static final String METASTORE_CHECK_NAME = "metastore"; | ||
public static final String KAFKA_CHECK_NAME = "kafka"; | ||
|
||
private static final List<Check> DEFAULT_CHECKS = ImmutableList.of( | ||
new ExecuteStatementCheck(METASTORE_CHECK_NAME, "list streams; list tables; list queries;"), | ||
new ExecuteStatementCheck(KAFKA_CHECK_NAME, "list topics extended;") | ||
); | ||
|
||
private final SimpleKsqlClient ksqlClient; | ||
private final URI serverEndpoint; | ||
|
||
public HealthCheckAgent( | ||
final SimpleKsqlClient ksqlClient, | ||
final KsqlRestConfig restConfig | ||
) { | ||
this.ksqlClient = Objects.requireNonNull(ksqlClient, "ksqlClient"); | ||
this.serverEndpoint = getServerAddress(restConfig); | ||
} | ||
|
||
public HealthCheckResponse checkHealth() { | ||
final Map<String, HealthCheckResponseDetail> results = DEFAULT_CHECKS.stream() | ||
.collect(Collectors.toMap( | ||
Check::getName, | ||
check -> check.check(ksqlClient, serverEndpoint) | ||
)); | ||
final boolean allHealthy = results.values().stream() | ||
.allMatch(HealthCheckResponseDetail::getIsHealthy); | ||
return new HealthCheckResponse(allHealthy, results); | ||
} | ||
|
||
private static URI getServerAddress(final KsqlRestConfig restConfig) { | ||
final List<String> listeners = restConfig.getList(RestConfig.LISTENERS_CONFIG); | ||
final String address = listeners.stream() | ||
.map(String::trim) | ||
.findFirst() | ||
.orElseThrow(() -> invalidAddressException(listeners, "value cannot be empty")); | ||
|
||
try { | ||
return new URL(address).toURI(); | ||
} catch (final Exception e) { | ||
throw invalidAddressException(listeners, e.getMessage()); | ||
} | ||
} | ||
|
||
private static RuntimeException invalidAddressException( | ||
final List<String> serverAddresses, | ||
final String message | ||
) { | ||
return new ConfigException(RestConfig.LISTENERS_CONFIG, serverAddresses, message); | ||
} | ||
|
||
private interface Check { | ||
String getName(); | ||
|
||
HealthCheckResponseDetail check(SimpleKsqlClient ksqlClient, URI serverEndpoint); | ||
} | ||
|
||
private static class ExecuteStatementCheck implements Check { | ||
private final String name; | ||
private final String ksqlStatement; | ||
|
||
ExecuteStatementCheck(final String name, final String ksqlStatement) { | ||
this.name = Objects.requireNonNull(name, "name"); | ||
this.ksqlStatement = Objects.requireNonNull(ksqlStatement, "ksqlStatement"); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public HealthCheckResponseDetail check( | ||
final SimpleKsqlClient ksqlClient, | ||
final URI serverEndpoint | ||
) { | ||
final RestResponse<KsqlEntityList> response = | ||
ksqlClient.makeKsqlRequest(serverEndpoint, ksqlStatement); | ||
return new HealthCheckResponseDetail(response.isSuccessful()); | ||
} | ||
} | ||
} |
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
97 changes: 97 additions & 0 deletions
97
ksql-rest-app/src/main/java/io/confluent/ksql/rest/server/resources/HealthCheckResource.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,97 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.rest.server.resources; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
import io.confluent.ksql.rest.entity.HealthCheckResponse; | ||
import io.confluent.ksql.rest.entity.Versions; | ||
import io.confluent.ksql.rest.healthcheck.HealthCheckAgent; | ||
import io.confluent.ksql.rest.server.KsqlRestConfig; | ||
import io.confluent.ksql.rest.server.services.ServerInternalKsqlClient; | ||
import io.confluent.ksql.services.ServiceContext; | ||
import java.time.Duration; | ||
import java.util.Objects; | ||
import java.util.concurrent.TimeUnit; | ||
import javax.annotation.Nonnull; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
|
||
@Path("/healthcheck") | ||
@Produces({Versions.KSQL_V1_JSON, MediaType.APPLICATION_JSON}) | ||
public class HealthCheckResource { | ||
|
||
private static final Boolean KEY = Boolean.TRUE; | ||
|
||
private final LoadingCache<Boolean, HealthCheckResponse> responseCache; | ||
|
||
@VisibleForTesting | ||
HealthCheckResource( | ||
final HealthCheckAgent healthCheckAgent, | ||
final Duration healthCheckInterval | ||
) { | ||
Objects.requireNonNull(healthCheckAgent, "healthCheckAgent"); | ||
Objects.requireNonNull(healthCheckInterval, "healthCheckInterval"); | ||
this.responseCache = createResponseCache(healthCheckAgent, healthCheckInterval); | ||
} | ||
|
||
@GET | ||
public Response checkHealth() { | ||
return Response.ok(getResponse()).build(); | ||
} | ||
|
||
private HealthCheckResponse getResponse() { | ||
// This calls healthCheckAgent.checkHealth() if the cached result is expired | ||
return responseCache.getUnchecked(KEY); | ||
} | ||
|
||
public static HealthCheckResource create( | ||
final KsqlResource ksqlResource, | ||
final ServiceContext serviceContext, | ||
final KsqlRestConfig restConfig | ||
) { | ||
return new HealthCheckResource( | ||
new HealthCheckAgent( | ||
new ServerInternalKsqlClient(ksqlResource, serviceContext), | ||
restConfig), | ||
Duration.ofMillis(restConfig.getLong(KsqlRestConfig.KSQL_HEALTHCHECK_INTERVAL_MS_CONFIG)) | ||
); | ||
} | ||
|
||
private static LoadingCache<Boolean, HealthCheckResponse> createResponseCache( | ||
final HealthCheckAgent healthCheckAgent, | ||
final Duration cacheDuration | ||
) { | ||
final CacheLoader<Boolean, HealthCheckResponse> loader = | ||
new CacheLoader<Boolean, HealthCheckResponse>() { | ||
@Override | ||
public HealthCheckResponse load(@Nonnull final Boolean key) { | ||
if (!key.equals(KEY)) { | ||
throw new IllegalArgumentException("Unexpected response cache key: " + key); | ||
} | ||
return healthCheckAgent.checkHealth(); | ||
} | ||
}; | ||
return CacheBuilder.newBuilder() | ||
.expireAfterWrite(cacheDuration.toMillis(), TimeUnit.MILLISECONDS) | ||
.build(loader); | ||
} | ||
} |
Oops, something went wrong.