|
1 | 1 | package org.hypertrace.core.attribute.service; |
2 | 2 |
|
3 | | -import static java.util.concurrent.TimeUnit.MINUTES; |
4 | | -import static java.util.concurrent.TimeUnit.SECONDS; |
5 | | - |
6 | | -import io.grpc.Deadline; |
7 | | -import io.grpc.Server; |
8 | | -import io.grpc.ServerBuilder; |
9 | | -import io.grpc.health.v1.HealthCheckRequest; |
10 | | -import io.grpc.health.v1.HealthCheckResponse.ServingStatus; |
11 | | -import io.grpc.health.v1.HealthGrpc; |
12 | | -import io.grpc.health.v1.HealthGrpc.HealthBlockingStub; |
13 | | -import io.grpc.protobuf.services.HealthStatusManager; |
14 | | -import java.io.IOException; |
15 | | -import org.hypertrace.core.grpcutils.client.GrpcChannelRegistry; |
16 | | -import org.hypertrace.core.grpcutils.server.InterceptorUtil; |
17 | | -import org.hypertrace.core.grpcutils.server.ServerManagementUtil; |
18 | | -import org.hypertrace.core.serviceframework.PlatformService; |
19 | 3 | import org.hypertrace.core.serviceframework.config.ConfigClient; |
20 | | -import org.slf4j.Logger; |
21 | | -import org.slf4j.LoggerFactory; |
| 4 | +import org.hypertrace.core.serviceframework.grpc.GrpcPlatformServiceFactory; |
| 5 | +import org.hypertrace.core.serviceframework.grpc.StandAloneGrpcPlatformServiceContainer; |
22 | 6 |
|
23 | | -public class AttributeServiceEntry extends PlatformService { |
24 | | - private static final String SERVICE_NAME_CONFIG = "service.name"; |
25 | | - private static final Logger LOGGER = LoggerFactory.getLogger(AttributeServiceEntry.class); |
| 7 | +public class AttributeServiceEntry extends StandAloneGrpcPlatformServiceContainer { |
26 | 8 | static final String PORT_PATH = "attributes.type.server.port"; |
27 | 9 |
|
28 | | - private String serviceName; |
29 | | - private Server server; |
30 | | - private HealthBlockingStub healthClient; |
31 | | - private final HealthStatusManager healthStatusManager = new HealthStatusManager(); |
32 | | - private final GrpcChannelRegistry grpcChannelRegistry = new GrpcChannelRegistry(); |
33 | | - |
34 | 10 | public AttributeServiceEntry(ConfigClient configClient) { |
35 | 11 | super(configClient); |
36 | 12 | } |
37 | 13 |
|
38 | 14 | @Override |
39 | | - protected void doInit() { |
40 | | - serviceName = getAppConfig().getString(SERVICE_NAME_CONFIG); |
41 | | - int port = getAppConfig().getInt(PORT_PATH); |
42 | | - server = |
43 | | - ServerBuilder.forPort(port) |
44 | | - .addService(InterceptorUtil.wrapInterceptors(new AttributeServiceImpl(getAppConfig()))) |
45 | | - .addService(healthStatusManager.getHealthService()) |
46 | | - .build(); |
47 | | - healthClient = |
48 | | - HealthGrpc.newBlockingStub(this.grpcChannelRegistry.forPlaintextAddress("localhost", port)); |
49 | | - } |
50 | | - |
51 | | - @Override |
52 | | - protected void doStart() { |
53 | | - try { |
54 | | - try { |
55 | | - server.start(); |
56 | | - } catch (IOException e) { |
57 | | - LOGGER.error("Unable to start server"); |
58 | | - throw new RuntimeException(e); |
59 | | - } |
60 | | - server.awaitTermination(); |
61 | | - } catch (InterruptedException e) { |
62 | | - Thread.currentThread().interrupt(); |
63 | | - throw new RuntimeException(e); |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - @Override |
68 | | - protected void doStop() { |
69 | | - healthStatusManager.enterTerminalState(); |
70 | | - grpcChannelRegistry.shutdown(Deadline.after(10, SECONDS)); |
71 | | - ServerManagementUtil.shutdownServer( |
72 | | - this.server, this.getServiceName(), Deadline.after(1, MINUTES)); |
73 | | - } |
74 | | - |
75 | | - @Override |
76 | | - public boolean healthCheck() { |
77 | | - try { |
78 | | - // Intentionally using overly generous deadline to respect health check timeout from config |
79 | | - return healthClient |
80 | | - .withDeadlineAfter(10, SECONDS) |
81 | | - .check(HealthCheckRequest.getDefaultInstance()) |
82 | | - .getStatus() |
83 | | - .equals(ServingStatus.SERVING); |
84 | | - } catch (Exception e) { |
85 | | - LOGGER.debug("health check error", e); |
86 | | - return false; |
87 | | - } |
| 15 | + public GrpcPlatformServiceFactory getServiceFactory() { |
| 16 | + return new AttributeServiceFactory(); |
88 | 17 | } |
89 | 18 |
|
90 | 19 | @Override |
91 | | - public String getServiceName() { |
92 | | - return serviceName; |
| 20 | + protected int getServicePort() { |
| 21 | + return getAppConfig().getInt(PORT_PATH); |
93 | 22 | } |
94 | 23 | } |
0 commit comments