Skip to content
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

RPCChainVM fail-fast health RPCs #2123

Merged
merged 6 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 1 addition & 3 deletions vms/rpcchainvm/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/ava-labs/avalanchego/vms/rpcchainvm/grpcutils"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/runtime"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/runtime/subprocess"

vmpb "github.com/ava-labs/avalanchego/proto/pb/vm"
)

var _ vms.Factory = (*factory)(nil)
Expand Down Expand Up @@ -61,7 +59,7 @@ func (f *factory) New(log logging.Logger) (interface{}, error) {
return nil, err
}

vm := NewClient(vmpb.NewVMClient(clientConn))
vm := NewClient(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())
}
4 changes: 1 addition & 3 deletions vms/rpcchainvm/state_syncable_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import (
"github.com/ava-labs/avalanchego/vms/rpcchainvm/grpcutils"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/runtime"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/runtime/subprocess"

vmpb "github.com/ava-labs/avalanchego/proto/pb/vm"
)

var (
Expand Down Expand Up @@ -295,7 +293,7 @@ func buildClientHelper(require *require.Assertions, testKey string) (*VMClient,
clientConn, err := grpcutils.Dial(status.Addr)
require.NoError(err)

return NewClient(vmpb.NewVMClient(clientConn)), stopper
return NewClient(clientConn), stopper
}

func TestStateSyncEnabled(t *testing.T) {
Expand Down
9 changes: 6 additions & 3 deletions vms/rpcchainvm/vm_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ type VMClient struct {
}

// NewClient returns a VM connected to a remote VM
func NewClient(client vmpb.VMClient) *VMClient {
func NewClient(clientConn *grpc.ClientConn) *VMClient {
return &VMClient{
client: client,
client: vmpb.NewVMClient(clientConn),
conns: []*grpc.ClientConn{clientConn},
darioush marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -527,7 +528,9 @@ 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.
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
Loading