From d74a5ae497db9d919b429861af611a9277e3b888 Mon Sep 17 00:00:00 2001 From: rahulpa Date: Wed, 22 Aug 2018 16:54:42 -0700 Subject: [PATCH] Added GRPC Health service to Ad Service Also added 1. timeout to getAd RPC call in frontend. 2. Async thread for stackdriver init. --- kubernetes-manifests/adservice.yaml | 4 +-- src/adservice/build.gradle | 3 +- .../src/main/java/hipstershop/AdService.java | 31 +++++++++++++++---- src/frontend/rpc.go | 4 +++ 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/kubernetes-manifests/adservice.yaml b/kubernetes-manifests/adservice.yaml index a990219..c6bdc66 100644 --- a/kubernetes-manifests/adservice.yaml +++ b/kubernetes-manifests/adservice.yaml @@ -41,12 +41,12 @@ spec: readinessProbe: tcpSocket: port: 9555 - initialDelaySeconds: 15 + initialDelaySeconds: 5 periodSeconds: 10 livenessProbe: tcpSocket: port: 9555 - initialDelaySeconds: 15 + initialDelaySeconds: 10 periodSeconds: 10 --- apiVersion: v1 diff --git a/src/adservice/build.gradle b/src/adservice/build.gradle index 32d6dbc..47653ac 100644 --- a/src/adservice/build.gradle +++ b/src/adservice/build.gradle @@ -25,7 +25,7 @@ repositories { group = "adservice" version = "0.1.0-SNAPSHOT" // CURRENT_OPENCENSUS_VERSION -def opencensusVersion = "0.14.0" // LATEST_OPENCENSUS_RELEASE_VERSION +def opencensusVersion = "0.15.0" // LATEST_OPENCENSUS_RELEASE_VERSION def grpcVersion = "1.10.1" // CURRENT_GRPC_VERSION def prometheusVersion = "0.3.0" @@ -51,6 +51,7 @@ dependencies { "io.grpc:grpc-protobuf:${grpcVersion}", "io.grpc:grpc-stub:${grpcVersion}", "io.grpc:grpc-netty:${grpcVersion}", + "io.grpc:grpc-services:${grpcVersion}", "io.prometheus:simpleclient_httpserver:${prometheusVersion}" runtime "io.opencensus:opencensus-impl:${opencensusVersion}", diff --git a/src/adservice/src/main/java/hipstershop/AdService.java b/src/adservice/src/main/java/hipstershop/AdService.java index 9a913ce..75ce76f 100644 --- a/src/adservice/src/main/java/hipstershop/AdService.java +++ b/src/adservice/src/main/java/hipstershop/AdService.java @@ -23,7 +23,9 @@ import hipstershop.Demo.AdResponse; import io.grpc.Server; import io.grpc.ServerBuilder; import io.grpc.StatusRuntimeException; +import io.grpc.health.v1.HealthCheckResponse.ServingStatus; import io.grpc.stub.StreamObserver; +import io.grpc.services.*; import io.opencensus.common.Duration; import io.opencensus.common.Scope; import io.opencensus.contrib.grpc.metrics.RpcViews; @@ -55,11 +57,15 @@ public class AdService { private int MAX_ADS_TO_SERVE = 2; private Server server; + private HealthStatusManager healthMgr; static final AdService service = new AdService(); private void start() throws IOException { int port = Integer.parseInt(System.getenv("PORT")); - server = ServerBuilder.forPort(port).addService(new AdServiceImpl()).build().start(); + healthMgr = new HealthStatusManager(); + + server = ServerBuilder.forPort(port).addService(new AdServiceImpl()) + .addService(healthMgr.getHealthService()).build().start(); logger.info("Ad Service started, listening on " + port); Runtime.getRuntime() .addShutdownHook( @@ -72,10 +78,12 @@ public class AdService { System.err.println("*** server shut down"); } }); + healthMgr.setStatus("", ServingStatus.SERVING); } private void stop() { if (server != null) { + healthMgr.clearStatus(""); server.shutdown(); } } @@ -173,11 +181,8 @@ public class AdService { logger.info("Default Ads initialized"); } - /** Main launches the server from the command line. */ - public static void main(String[] args) throws IOException, InterruptedException { - // Add final keyword to pass checkStyle. - - initializeAds(); + public static void initStackdriver() { + logger.info("Initialize StackDriver"); // Registers all RPC views. RpcViews.registerAllViews(); @@ -210,6 +215,20 @@ public class AdService { } } } + logger.info("StackDriver initialization complete."); + } + + /** Main launches the server from the command line. */ + public static void main(String[] args) throws IOException, InterruptedException { + // Add final keyword to pass checkStyle. + + initializeAds(); + + new Thread( new Runnable() { + public void run(){ + initStackdriver(); + } + }).start(); // Register Prometheus exporters and export metrics to a Prometheus HTTPServer. PrometheusStatsCollector.createAndRegister(); diff --git a/src/frontend/rpc.go b/src/frontend/rpc.go index de00deb..05c99b2 100644 --- a/src/frontend/rpc.go +++ b/src/frontend/rpc.go @@ -16,6 +16,7 @@ package main import ( "context" + "time" pb "github.com/GoogleCloudPlatform/microservices-demo/src/frontend/genproto" @@ -116,6 +117,9 @@ func (fe *frontendServer) getRecommendations(ctx context.Context, userID string, } func (fe *frontendServer) getAd(ctx context.Context) ([]*pb.Ad, error) { + ctx, cancel := context.WithTimeout(ctx, time.Millisecond*100) + defer cancel() + resp, err := pb.NewAdServiceClient(fe.adSvcConn).GetAds(ctx, &pb.AdRequest{ ContextKeys: nil, })