Skip to content

Commit

Permalink
Allow Shutdown and HealthCheck RPCs to fail-fast
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Batschelet <[email protected]>
  • Loading branch information
hexfusion committed Oct 3, 2023
1 parent f79d609 commit be951f7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions vms/rpcchainvm/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (f *factory) New(log logging.Logger) (interface{}, error) {
}

vm := NewClient(vmpb.NewVMClient(clientConn))
vm.conns = append(vm.conns, clientConn)
vm.SetProcess(stopper, status.Pid, f.processTracker)

f.runtimeTracker.TrackRuntime(stopper)
Expand Down
23 changes: 21 additions & 2 deletions vms/rpcchainvm/grpcutils/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package grpcutils

import (
"context"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -55,12 +57,11 @@ func TestWaitForReady(t *testing.T) {
Serve(listener, server)
}()

// The default includes grpc.WaitForReady(true).
// The default is WaitForReady = true.
conn, err := Dial(listener.Addr().String())
require.NoError(err)

db := rpcdb.NewClient(pb.NewDatabaseClient(conn))

require.NoError(db.Put([]byte("foo"), []byte("bar")))

noWaitListener, err := NewListener()
Expand All @@ -84,3 +85,21 @@ func TestWaitForReady(t *testing.T) {
require.True(ok)
require.Equal(codes.Unavailable, status.Code())
}

func TestWaitForReadyCallOption(t *testing.T) {
require := require.New(t)

listener, err := NewListener()
require.NoError(err)
conn, err := Dial(listener.Addr().String())
require.NoError(err)
// close listener causes RPC to fail fast.
listener.Close()

db := pb.NewDatabaseClient(conn)
_, err = db.Put(context.Background(), &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}, grpc.WaitForReady(false))
s, ok := status.FromError(err)
fmt.Printf("status: %v\n", s)
require.True(ok)
require.Equal(codes.Unavailable, s.Code())
}
10 changes: 8 additions & 2 deletions vms/rpcchainvm/vm_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,10 @@ func (vm *VMClient) SetState(ctx context.Context, state snow.State) error {

func (vm *VMClient) Shutdown(ctx context.Context) error {
errs := wrappers.Errs{}
_, err := vm.client.Shutdown(ctx, &emptypb.Empty{})
// Shutdown is a special case, where we want to fail fast instead of block
// here.
failFast := grpc.WaitForReady(false)
_, err := vm.client.Shutdown(ctx, &emptypb.Empty{}, failFast)
errs.Add(err)

vm.serverCloser.Stop()
Expand Down Expand Up @@ -527,7 +530,10 @@ func (vm *VMClient) SetPreference(ctx context.Context, blkID ids.ID) error {
}

func (vm *VMClient) HealthCheck(ctx context.Context) (interface{}, error) {
health, err := vm.client.Health(ctx, &emptypb.Empty{})
// HealthCheck is a special case, where we want to fail fast instead of block
// here.
failFast := grpc.WaitForReady(false)
health, err := vm.client.Health(ctx, &emptypb.Empty{}, failFast)
if err != nil {
return nil, fmt.Errorf("health check failed: %w", err)
}
Expand Down

0 comments on commit be951f7

Please sign in to comment.