From e899aebf9809b254e344269a49aa0da1cec93e43 Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Wed, 1 Jul 2015 10:33:11 -0700 Subject: [PATCH 1/6] Added health check service implementation --- src/node/health_check/health.js | 61 ++++++++++++++++++++++++++++++ src/node/health_check/health.proto | 49 ++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 src/node/health_check/health.js create mode 100644 src/node/health_check/health.proto diff --git a/src/node/health_check/health.js b/src/node/health_check/health.js new file mode 100644 index 00000000000..935795b91bd --- /dev/null +++ b/src/node/health_check/health.js @@ -0,0 +1,61 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +'use strict'; + +var grpc = require('../'); + +var _ = require('lodash'); + +var health_proto = grpc.load(__dirname + '/health.proto'); + +var HealthClient = health_proto.grpc.health.v1alpha.Health; + +function HealthImplementation(statusMap) { + this.statusMap = _.clone(statusMap); +} + +HealthImplementation.prototype.setStatus = function(service, status) { + this.statusMap[service] = status; +}; + +HealthImplementation.prototype.check = function(call, callback){ + var service = call.request.service; + callback(null, {status: _.get(this.statusMap, service, 'UNSPECIFIED')}); +}; + +module.exports = { + Client: HealthClient, + Service: HealthClient.service, + Implementation: HealthImplementation +}; diff --git a/src/node/health_check/health.proto b/src/node/health_check/health.proto new file mode 100644 index 00000000000..224d954faa3 --- /dev/null +++ b/src/node/health_check/health.proto @@ -0,0 +1,49 @@ +// Copyright 2015, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +syntax = "proto3"; + +package grpc.health.v1alpha; + +message HealthCheckRequest { + string service = 1; +} + +message HealthCheckResponse { + enum ServingStatus { + UNSPECIFIED = 0; + SERVING = 1; + NOT_SERVING = 2; + } + ServingStatus status = 1; +} + +service Health { + rpc Check(HealthCheckRequest) returns (HealthCheckResponse); +} \ No newline at end of file From f0c6f96e26df8cb48989dabb9308003eb7f2b962 Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Wed, 1 Jul 2015 12:48:44 -0700 Subject: [PATCH 2/6] Added tests for health checking --- src/node/health_check/health.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/node/health_check/health.js b/src/node/health_check/health.js index 935795b91bd..a8b6ed8546d 100644 --- a/src/node/health_check/health.js +++ b/src/node/health_check/health.js @@ -51,11 +51,12 @@ HealthImplementation.prototype.setStatus = function(service, status) { HealthImplementation.prototype.check = function(call, callback){ var service = call.request.service; + debugger; callback(null, {status: _.get(this.statusMap, service, 'UNSPECIFIED')}); }; module.exports = { Client: HealthClient, - Service: HealthClient.service, + service: HealthClient.service, Implementation: HealthImplementation }; From 87e74fc8a186884a53912c956d0e752c416930df Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Wed, 1 Jul 2015 12:56:10 -0700 Subject: [PATCH 3/6] Stopped binding service handler functions to server --- src/node/health_check/health.js | 1 - src/node/src/server.js | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/node/health_check/health.js b/src/node/health_check/health.js index a8b6ed8546d..632cb24127d 100644 --- a/src/node/health_check/health.js +++ b/src/node/health_check/health.js @@ -51,7 +51,6 @@ HealthImplementation.prototype.setStatus = function(service, status) { HealthImplementation.prototype.check = function(call, callback){ var service = call.request.service; - debugger; callback(null, {status: _.get(this.statusMap, service, 'UNSPECIFIED')}); }; diff --git a/src/node/src/server.js b/src/node/src/server.js index c6cf9e7eb80..9ac428f3ee2 100644 --- a/src/node/src/server.js +++ b/src/node/src/server.js @@ -634,7 +634,8 @@ function makeServerConstructor(service_attr_map) { } var serialize = attrs.responseSerialize; var deserialize = attrs.requestDeserialize; - server.register(attrs.path, service_handlers[service_name][name], + server.register(attrs.path, _.bind(service_handlers[service_name][name], + service_handlers[service_name]), serialize, deserialize, method_type); }); }, this); From d046cc6de52e3bf0a742efd19597b680d689f373 Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Mon, 6 Jul 2015 12:12:27 -0700 Subject: [PATCH 4/6] Added some tests for the health check service --- src/node/test/health_test.js | 91 ++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/node/test/health_test.js diff --git a/src/node/test/health_test.js b/src/node/test/health_test.js new file mode 100644 index 00000000000..b4ff7538771 --- /dev/null +++ b/src/node/test/health_test.js @@ -0,0 +1,91 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +'use strict'; + +var assert = require('assert'); + +var health = require('../health_check/health.js'); + +var grpc = require('../'); + +describe('Health Checking', function() { + var HealthServer = grpc.buildServer([health.service]); + var healthServer = new HealthServer({ + 'grpc.health.v1alpha.Health': new health.Implementation({ + '': 'SERVING', + 'grpc.health.v1alpha.Health': 'SERVING', + 'not.serving.Service': 'NOT_SERVING' + }) + }); + var healthClient; + before(function() { + var port_num = healthServer.bind('0.0.0.0:0'); + healthServer.listen(); + healthClient = new health.Client('localhost:' + port_num); + }); + after(function() { + healthServer.shutdown(); + }); + it('should respond with SERVING with no service specified', function(done) { + healthClient.check({}, function(err, response) { + assert.ifError(err); + assert.strictEqual(response.status, 'SERVING'); + done(); + }); + }); + it('should respond that the health check service is SERVING', function(done) { + healthClient.check({service: 'grpc.health.v1alpha.Health'}, + function(err, response) { + assert.ifError(err); + assert.strictEqual(response.status, 'SERVING'); + done(); + }); + }); + it('should respond that a disabled service is NOT_SERVING', function(done) { + healthClient.check({service: 'not.serving.Service'}, + function(err, response) { + assert.ifError(err); + assert.strictEqual(response.status, 'NOT_SERVING'); + done(); + }); + }); + it('should respond with UNSPECIFIED for an unknown service', function(done) { + healthClient.check({service: 'unknown.service.Name'}, + function(err, response) { + assert.ifError(err); + assert.strictEqual(response.status, 'UNSPECIFIED'); + done(); + }); + }); +}); From 2be6ac0dc14901b7f8728fcc0627592c0d4e37c1 Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Tue, 7 Jul 2015 16:11:47 -0700 Subject: [PATCH 5/6] Minor changes to match recent design changes --- src/node/health_check/health.js | 2 +- src/node/health_check/health.proto | 2 +- src/node/test/health_test.js | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/node/health_check/health.js b/src/node/health_check/health.js index 632cb24127d..09a57d38bd2 100644 --- a/src/node/health_check/health.js +++ b/src/node/health_check/health.js @@ -51,7 +51,7 @@ HealthImplementation.prototype.setStatus = function(service, status) { HealthImplementation.prototype.check = function(call, callback){ var service = call.request.service; - callback(null, {status: _.get(this.statusMap, service, 'UNSPECIFIED')}); + callback(null, {status: _.get(this.statusMap, service, 'UNKNOWN')}); }; module.exports = { diff --git a/src/node/health_check/health.proto b/src/node/health_check/health.proto index 224d954faa3..1f6eb6a80de 100644 --- a/src/node/health_check/health.proto +++ b/src/node/health_check/health.proto @@ -37,7 +37,7 @@ message HealthCheckRequest { message HealthCheckResponse { enum ServingStatus { - UNSPECIFIED = 0; + UNKNOWN = 0; SERVING = 1; NOT_SERVING = 2; } diff --git a/src/node/test/health_test.js b/src/node/test/health_test.js index b4ff7538771..456b686b9b0 100644 --- a/src/node/test/health_test.js +++ b/src/node/test/health_test.js @@ -80,11 +80,11 @@ describe('Health Checking', function() { done(); }); }); - it('should respond with UNSPECIFIED for an unknown service', function(done) { + it('should respond with UNKNOWN for an unknown service', function(done) { healthClient.check({service: 'unknown.service.Name'}, function(err, response) { assert.ifError(err); - assert.strictEqual(response.status, 'UNSPECIFIED'); + assert.strictEqual(response.status, 'UNKNOWN'); done(); }); }); From a5b482a8ec1ce77d881024c0de28bd9a02f97959 Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Wed, 8 Jul 2015 15:24:42 -0700 Subject: [PATCH 6/6] Updated health check service with new changes --- src/node/health_check/health.js | 15 ++++++++-- src/node/health_check/health.proto | 3 +- src/node/test/health_test.js | 46 +++++++++++++++++++----------- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/node/health_check/health.js b/src/node/health_check/health.js index 09a57d38bd2..87e00197fe5 100644 --- a/src/node/health_check/health.js +++ b/src/node/health_check/health.js @@ -45,13 +45,22 @@ function HealthImplementation(statusMap) { this.statusMap = _.clone(statusMap); } -HealthImplementation.prototype.setStatus = function(service, status) { - this.statusMap[service] = status; +HealthImplementation.prototype.setStatus = function(host, service, status) { + if (!this.statusMap[host]) { + this.statusMap[host] = {}; + } + this.statusMap[host][service] = status; }; HealthImplementation.prototype.check = function(call, callback){ + var host = call.request.host; var service = call.request.service; - callback(null, {status: _.get(this.statusMap, service, 'UNKNOWN')}); + var status = _.get(this.statusMap, [host, service], null); + if (status === null) { + callback({code:grpc.status.NOT_FOUND}); + } else { + callback(null, {status: status}); + } }; module.exports = { diff --git a/src/node/health_check/health.proto b/src/node/health_check/health.proto index 1f6eb6a80de..d31df1e0a7c 100644 --- a/src/node/health_check/health.proto +++ b/src/node/health_check/health.proto @@ -32,7 +32,8 @@ syntax = "proto3"; package grpc.health.v1alpha; message HealthCheckRequest { - string service = 1; + string host = 1; + string service = 2; } message HealthCheckResponse { diff --git a/src/node/test/health_test.js b/src/node/test/health_test.js index 456b686b9b0..4d1a5082e04 100644 --- a/src/node/test/health_test.js +++ b/src/node/test/health_test.js @@ -40,13 +40,18 @@ var health = require('../health_check/health.js'); var grpc = require('../'); describe('Health Checking', function() { + var statusMap = { + '': { + '': 'SERVING', + 'grpc.test.TestService': 'NOT_SERVING', + }, + virtual_host: { + 'grpc.test.TestService': 'SERVING' + } + }; var HealthServer = grpc.buildServer([health.service]); var healthServer = new HealthServer({ - 'grpc.health.v1alpha.Health': new health.Implementation({ - '': 'SERVING', - 'grpc.health.v1alpha.Health': 'SERVING', - 'not.serving.Service': 'NOT_SERVING' - }) + 'grpc.health.v1alpha.Health': new health.Implementation(statusMap) }); var healthClient; before(function() { @@ -57,34 +62,41 @@ describe('Health Checking', function() { after(function() { healthServer.shutdown(); }); - it('should respond with SERVING with no service specified', function(done) { - healthClient.check({}, function(err, response) { + it('should say an enabled service is SERVING', function(done) { + healthClient.check({service: ''}, function(err, response) { assert.ifError(err); assert.strictEqual(response.status, 'SERVING'); done(); }); }); - it('should respond that the health check service is SERVING', function(done) { - healthClient.check({service: 'grpc.health.v1alpha.Health'}, + it('should say that a disabled service is NOT_SERVING', function(done) { + healthClient.check({service: 'grpc.test.TestService'}, function(err, response) { assert.ifError(err); - assert.strictEqual(response.status, 'SERVING'); + assert.strictEqual(response.status, 'NOT_SERVING'); done(); }); }); - it('should respond that a disabled service is NOT_SERVING', function(done) { - healthClient.check({service: 'not.serving.Service'}, + it('should say that a service on another host is SERVING', function(done) { + healthClient.check({host: 'virtual_host', service: 'grpc.test.TestService'}, function(err, response) { assert.ifError(err); - assert.strictEqual(response.status, 'NOT_SERVING'); + assert.strictEqual(response.status, 'SERVING'); done(); }); }); - it('should respond with UNKNOWN for an unknown service', function(done) { - healthClient.check({service: 'unknown.service.Name'}, + it('should get NOT_FOUND if the service is not registered', function(done) { + healthClient.check({service: 'not_registered'}, function(err, response) { + assert(err); + assert.strictEqual(err.code, grpc.status.NOT_FOUND); + done(); + }); + }); + it('should get NOT_FOUND if the host is not registered', function(done) { + healthClient.check({host: 'wrong_host', service: 'grpc.test.TestService'}, function(err, response) { - assert.ifError(err); - assert.strictEqual(response.status, 'UNKNOWN'); + assert(err); + assert.strictEqual(err.code, grpc.status.NOT_FOUND); done(); }); });