Skip to content

health: shutdown server to set NOT_SERVING and disallow future updates #2513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions health/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ import (
"google.golang.org/grpc/codes"
healthgrpc "google.golang.org/grpc/health/grpc_health_v1"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/internal/grpcsync"
"google.golang.org/grpc/status"
)

// Server implements `service Health`.
type Server struct {
// If shutdownEvent has fired, it's expected all serving status is
// NOT_SERVING, and will stay in NOT_SERVING.
shutdownEvent *grpcsync.Event

mu sync.Mutex
// statusMap stores the serving status of the services this Server monitors.
statusMap map[string]healthpb.HealthCheckResponse_ServingStatus
Expand All @@ -43,6 +48,8 @@ type Server struct {
// NewServer returns a new Server.
func NewServer() *Server {
return &Server{
shutdownEvent: grpcsync.NewEvent(),

statusMap: map[string]healthpb.HealthCheckResponse_ServingStatus{"": healthpb.HealthCheckResponse_SERVING},
updates: make(map[string]map[healthgrpc.Health_WatchServer]chan healthpb.HealthCheckResponse_ServingStatus),
}
Expand Down Expand Up @@ -110,7 +117,14 @@ func (s *Server) Watch(in *healthpb.HealthCheckRequest, stream healthgrpc.Health
func (s *Server) SetServingStatus(service string, servingStatus healthpb.HealthCheckResponse_ServingStatus) {
s.mu.Lock()
defer s.mu.Unlock()
if s.shutdownEvent.HasFired() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could go above the lock. (?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There can be a race if this is not in the lock

goroutine-1: HasFired()=false
goroutine-2: Fire()
goroutine-2: take lock, set Not_serving, release lock
goroutine-1: take lock, set status (to Serving)

return
}

s.setServingStatusLocked(service, servingStatus)
}

func (s *Server) setServingStatusLocked(service string, servingStatus healthpb.HealthCheckResponse_ServingStatus) {
s.statusMap[service] = servingStatus
for _, update := range s.updates[service] {
// Clears previous updates, that are not sent to the client, from the channel.
Expand All @@ -123,3 +137,14 @@ func (s *Server) SetServingStatus(service string, servingStatus healthpb.HealthC
update <- servingStatus
}
}

// Shutdown sets all serving status to NOT_SERVING, and configures the server to
// ignore all future status changes.
func (s *Server) Shutdown() {
s.mu.Lock()
defer s.mu.Unlock()
s.shutdownEvent.Fire()
for service := range s.statusMap {
s.setServingStatusLocked(service, healthpb.HealthCheckResponse_NOT_SERVING)
}
}
64 changes: 64 additions & 0 deletions health/server_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
*
* Copyright 2018 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package health

import (
"sync"
"testing"
"time"

healthpb "google.golang.org/grpc/health/grpc_health_v1"
)

func TestShutdown(t *testing.T) {
const testService = "tteesstt"
s := NewServer()
s.SetServingStatus(testService, healthpb.HealthCheckResponse_SERVING)

s.mu.Lock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is nothing asynchronous happening, so I don't think this needs a lock. Remove the status local entirely maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

status := s.statusMap[testService]
s.mu.Unlock()
if status != healthpb.HealthCheckResponse_SERVING {
t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_SERVING)
}

var wg sync.WaitGroup
wg.Add(2)
// Run SetServingStatus and Shutdown in parallel.
go func() {
for i := 0; i < 1000; i++ {
s.SetServingStatus(testService, healthpb.HealthCheckResponse_SERVING)
time.Sleep(time.Microsecond)
}
wg.Done()
}()
go func() {
time.Sleep(300 * time.Microsecond)
s.Shutdown()
wg.Done()
}()
wg.Wait()

s.mu.Lock()
status = s.statusMap[testService]
s.mu.Unlock()
if status != healthpb.HealthCheckResponse_NOT_SERVING {
t.Fatalf("status for %s is %v, want %v", testService, status, healthpb.HealthCheckResponse_NOT_SERVING)
}
}