• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.protobuf.services;
18 
19 import static com.google.common.base.Preconditions.checkNotNull;
20 
21 import io.grpc.BindableService;
22 import io.grpc.health.v1.HealthCheckResponse.ServingStatus;
23 
24 /**
25  * A {@code HealthStatusManager} object manages a health check service. A health check service is
26  * created in the constructor of {@code HealthStatusManager}, and it can be retrieved by the
27  * {@link #getHealthService()} method.
28  * The health status manager can update the health statuses of the server.
29  *
30  * <p>The default, empty-string, service name, {@link #SERVICE_NAME_ALL_SERVICES}, is initialized to
31  * {@link ServingStatus#SERVING}.
32  */
33 @io.grpc.ExperimentalApi("https://github.com/grpc/grpc-java/issues/4696")
34 public final class HealthStatusManager {
35   /**
36    * The special "service name" that represent all services on a GRPC server.  It is an empty
37    * string.
38    */
39   public static final String SERVICE_NAME_ALL_SERVICES = "";
40 
41   private final HealthServiceImpl healthService;
42 
43   /**
44    * Creates a new health service instance.
45    */
HealthStatusManager()46   public HealthStatusManager() {
47     healthService = new HealthServiceImpl();
48   }
49 
50   /**
51    * Gets the health check service created in the constructor.
52    */
getHealthService()53   public BindableService getHealthService() {
54     return healthService;
55   }
56 
57   /**
58    * Updates the status of the server.
59    * @param service the name of some aspect of the server that is associated with a health status.
60    *     This name can have no relation with the gRPC services that the server is running with.
61    *     It can also be an empty String {@code ""} per the gRPC specification.
62    * @param status is one of the values {@link ServingStatus#SERVING},
63    *     {@link ServingStatus#NOT_SERVING} and {@link ServingStatus#UNKNOWN}.
64    */
setStatus(String service, ServingStatus status)65   public void setStatus(String service, ServingStatus status) {
66     checkNotNull(status, "status");
67     healthService.setStatus(service, status);
68   }
69 
70   /**
71    * Clears the health status record of a service. The health service will respond with NOT_FOUND
72    * error on checking the status of a cleared service.
73    * @param service the name of some aspect of the server that is associated with a health status.
74    *     This name can have no relation with the gRPC services that the server is running with.
75    *     It can also be an empty String {@code ""} per the gRPC specification.
76    */
clearStatus(String service)77   public void clearStatus(String service) {
78     healthService.clearStatus(service);
79   }
80 
81   /**
82    * enterTerminalState causes the health status manager to mark all services as not serving, and
83    * prevents future updates to services.  This method is meant to be called prior to server
84    * shutdown as a way to indicate that clients should redirect their traffic elsewhere.
85    */
enterTerminalState()86   public void enterTerminalState() {
87     healthService.enterTerminalState();
88   }
89 }
90