Skip to content

Commit 20803a4

Browse files
authored
Merge pull request #19 from scalableminds/grpc-health
support standard grpc health check
2 parents 9a6cf01 + 0e436ad commit 20803a4

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

build.sbt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ libraryDependencies ++= Seq(
2323
"ch.qos.logback" % "logback-classic" % "1.2.3",
2424
"com.typesafe.scala-logging" %% "scala-logging" % "3.7.2",
2525
"org.scalatest" % "scalatest_2.12" % "3.0.4" % "test",
26-
"io.grpc" % "grpc-netty" % com.trueaccord.scalapb.compiler.Version.grpcJavaVersion,
27-
"com.trueaccord.scalapb" %% "scalapb-runtime-grpc" % com.trueaccord.scalapb.compiler.Version.scalapbVersion,
26+
"io.grpc" % "grpc-netty" % scalapb.compiler.Version.grpcJavaVersion,
27+
"io.grpc" % "grpc-services" % scalapb.compiler.Version.grpcJavaVersion,
28+
"com.thesamet.scalapb" %% "scalapb-runtime-grpc" % scalapb.compiler.Version.scalapbVersion,
2829
"org.rocksdb" % "rocksdbjni" % "5.1.2",
2930
"com.github.scopt" %% "scopt" % "3.7.0"
3031
)

project/scalapb.sbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.12")
1+
addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.18")
22

3-
libraryDependencies += "com.trueaccord.scalapb" %% "compilerplugin" % "0.6.6"
3+
libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.7.4"

src/main/scala/com/scalableminds/fossildb/FossilDBGrpcImpl.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import com.typesafe.scalalogging.LazyLogging
1313

1414
import scala.concurrent.Future
1515

16-
class FossilDBGrpcImpl(storeManager: StoreManager) extends FossilDBGrpc.FossilDB with LazyLogging {
16+
class FossilDBGrpcImpl(storeManager: StoreManager)
17+
extends FossilDBGrpc.FossilDB
18+
with LazyLogging {
1719

1820
override def health(req: HealthRequest) = withExceptionHandler(req) {
1921
HealthReply(true)

src/main/scala/com/scalableminds/fossildb/FossilDBServer.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,26 @@ package com.scalableminds.fossildb
55

66
import com.scalableminds.fossildb.db.StoreManager
77
import com.scalableminds.fossildb.proto.fossildbapi.FossilDBGrpc
8+
import io.grpc.health.v1.HealthCheckResponse
89
import com.typesafe.scalalogging.LazyLogging
910
import io.grpc.Server
1011
import io.grpc.netty.NettyServerBuilder
12+
import io.grpc.services.HealthStatusManager
1113

1214
import scala.concurrent.ExecutionContext
1315

1416
class FossilDBServer(storeManager: StoreManager, port: Int, executionContext: ExecutionContext) extends LazyLogging
1517
{ self =>
1618
private[this] var server: Server = null
19+
private[this] var healthStatusManager: HealthStatusManager = null
1720

1821
def start(): Unit = {
19-
server = NettyServerBuilder.forPort(port).maxMessageSize(Int.MaxValue).addService(FossilDBGrpc.bindService(new FossilDBGrpcImpl(storeManager), executionContext)).build.start
22+
healthStatusManager = new HealthStatusManager()
23+
server = NettyServerBuilder.forPort(port).maxMessageSize(Int.MaxValue)
24+
.addService(FossilDBGrpc.bindService(new FossilDBGrpcImpl(storeManager), executionContext))
25+
.addService(healthStatusManager.getHealthService())
26+
.build.start
27+
healthStatusManager.setStatus("", HealthCheckResponse.ServingStatus.SERVING)
2028
logger.info("Server started, listening on " + port)
2129
sys.addShutdownHook {
2230
logger.info("Shutting down gRPC server since JVM is shutting down")
@@ -29,6 +37,7 @@ class FossilDBServer(storeManager: StoreManager, port: Int, executionContext: Ex
2937
if (server != null) {
3038
server.shutdown()
3139
storeManager.close
40+
healthStatusManager.setStatus("", HealthCheckResponse.ServingStatus.NOT_SERVING)
3241
}
3342
}
3443

src/test/scala/com/scalableminds/fossildb/FossilDBSuite.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import java.nio.file.Paths
99
import com.google.protobuf.ByteString
1010
import com.scalableminds.fossildb.db.StoreManager
1111
import com.scalableminds.fossildb.proto.fossildbapi._
12+
import io.grpc.health.v1._
1213
import io.grpc.netty.NettyChannelBuilder
1314
import org.scalatest.{BeforeAndAfterEach, FlatSpec}
1415

@@ -21,7 +22,9 @@ class FossilDBSuite extends FlatSpec with BeforeAndAfterEach {
2122

2223
val port = 21505
2324
var serverOpt: Option[FossilDBServer] = None
24-
val client = FossilDBGrpc.blockingStub(NettyChannelBuilder.forAddress("127.0.0.1", port).maxInboundMessageSize(Int.MaxValue).usePlaintext(true).build)
25+
val channel = NettyChannelBuilder.forAddress("127.0.0.1", port).maxInboundMessageSize(Int.MaxValue).usePlaintext().build
26+
val client = FossilDBGrpc.blockingStub(channel)
27+
val healthClient = HealthGrpc.newBlockingStub(channel)
2528

2629
val collectionA = "collectionA"
2730
val collectionB = "collectionB"
@@ -65,6 +68,11 @@ class FossilDBSuite extends FlatSpec with BeforeAndAfterEach {
6568
assert(reply.success)
6669
}
6770

71+
"GRPC Standard Health Check" should "report SERVING" in {
72+
val reply = healthClient.check(HealthCheckRequest.getDefaultInstance())
73+
assert(reply.getStatus.toString == "SERVING")
74+
}
75+
6876
"Put" should "overwrite old value" in {
6977
client.put(PutRequest(collectionA, aKey, Some(0), testData1))
7078
client.put(PutRequest(collectionA, aKey, Some(0), testData2))

0 commit comments

Comments
 (0)