pkg/rpc: replace methods with direct calls to pb

Change-Id: I8bd015d8d316a2c12c1daceca1d9fd257f6f57bc
This commit is contained in:
Egon Elbre 2019-12-22 17:07:50 +02:00
parent 006baa9ca6
commit d55288cf68
20 changed files with 40 additions and 314 deletions

View File

@ -15,6 +15,7 @@ import (
"github.com/stretchr/testify/require"
"storj.io/storj/certificate/certificateclient"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/peertls/tlsopts"
"storj.io/storj/pkg/rpc"
"storj.io/storj/pkg/storj"
@ -230,7 +231,7 @@ func TestNewClient(t *testing.T) {
defer ctx.Check(conn.Close)
client := certificateclient.NewClientFrom(conn.CertificatesClient())
client := certificateclient.NewClientFrom(pb.NewDRPCCertificatesClient(conn.Raw()))
assert.NoError(t, err)
assert.NotNil(t, client)

View File

@ -41,7 +41,7 @@ func New(ctx context.Context, dialer rpc.Dialer, address string) (_ *Client, err
return &Client{
conn: conn,
client: conn.CertificatesClient(),
client: pb.NewDRPCCertificatesClient(conn.Raw()),
}, nil
}

View File

@ -132,10 +132,10 @@ func NewInspector(address, path string) (*Inspector, error) {
return &Inspector{
conn: conn,
identity: id,
overlayclient: conn.OverlayInspectorClient(),
irrdbclient: conn.IrreparableInspectorClient(),
healthclient: conn.HealthInspectorClient(),
paymentsClient: conn.PaymentsClient(),
overlayclient: pb.NewDRPCOverlayInspectorClient(conn.Raw()),
irrdbclient: pb.NewDRPCIrreparableInspectorClient(conn.Raw()),
healthclient: pb.NewDRPCHealthInspectorClient(conn.Raw()),
paymentsClient: pb.NewDRPCPaymentsClient(conn.Raw()),
}, nil
}

View File

@ -40,7 +40,7 @@ func dialDashboardClient(ctx context.Context, address string) (*dashboardClient,
}
func (dash *dashboardClient) dashboard(ctx context.Context) (*pb.DashboardResponse, error) {
return dash.conn.PieceStoreInspectorClient().Dashboard(ctx, &pb.DashboardRequest{})
return pb.NewDRPCPieceStoreInspectorClient(dash.conn.Raw()).Dashboard(ctx, &pb.DashboardRequest{})
}
func (dash *dashboardClient) close() error {

View File

@ -37,15 +37,15 @@ func dialGracefulExitClient(ctx context.Context, address string) (*gracefulExitC
}
func (client *gracefulExitClient) getNonExitingSatellites(ctx context.Context) (*pb.GetNonExitingSatellitesResponse, error) {
return client.conn.NodeGracefulExitClient().GetNonExitingSatellites(ctx, &pb.GetNonExitingSatellitesRequest{})
return pb.NewDRPCNodeGracefulExitClient(client.conn.Raw()).GetNonExitingSatellites(ctx, &pb.GetNonExitingSatellitesRequest{})
}
func (client *gracefulExitClient) initGracefulExit(ctx context.Context, req *pb.InitiateGracefulExitRequest) (*pb.ExitProgress, error) {
return client.conn.NodeGracefulExitClient().InitiateGracefulExit(ctx, req)
return pb.NewDRPCNodeGracefulExitClient(client.conn.Raw()).InitiateGracefulExit(ctx, req)
}
func (client *gracefulExitClient) getExitProgress(ctx context.Context) (*pb.GetExitProgressResponse, error) {
return client.conn.NodeGracefulExitClient().GetExitProgress(ctx, &pb.GetExitProgressRequest{})
return pb.NewDRPCNodeGracefulExitClient(client.conn.Raw()).GetExitProgress(ctx, &pb.GetExitProgressRequest{})
}
func (client *gracefulExitClient) close() error {

View File

@ -1,92 +0,0 @@
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package rpc
import (
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/rpc/rpcpool"
)
// RawConn is a type alias to a drpc client connection
type RawConn = rpcpool.Conn
// CertificatesClient returns a CertificatesClient for this connection
func (c *Conn) CertificatesClient() pb.DRPCCertificatesClient {
return pb.NewDRPCCertificatesClient(c.raw)
}
// ContactClient returns a ContactClient for this connection
func (c *Conn) ContactClient() pb.DRPCContactClient {
return pb.NewDRPCContactClient(c.raw)
}
// HealthInspectorClient returns a HealthInspectorClient for this connection
func (c *Conn) HealthInspectorClient() pb.DRPCHealthInspectorClient {
return pb.NewDRPCHealthInspectorClient(c.raw)
}
// IrreparableInspectorClient returns a IrreparableInspectorClient for this connection
func (c *Conn) IrreparableInspectorClient() pb.DRPCIrreparableInspectorClient {
return pb.NewDRPCIrreparableInspectorClient(c.raw)
}
// MetainfoClient returns a MetainfoClient for this connection
func (c *Conn) MetainfoClient() pb.DRPCMetainfoClient {
return pb.NewDRPCMetainfoClient(c.raw)
}
// NodeClient returns a NodeClient for this connection
func (c *Conn) NodeClient() pb.DRPCNodeClient {
return pb.NewDRPCNodeClient(c.raw)
}
// NodeGracefulExitClient returns a NodeGracefulExitClient for this connection
func (c *Conn) NodeGracefulExitClient() pb.DRPCNodeGracefulExitClient {
return pb.NewDRPCNodeGracefulExitClient(c.raw)
}
// NodeStatsClient returns a NodeStatsClient for this connection
func (c *Conn) NodeStatsClient() pb.DRPCNodeStatsClient {
return pb.NewDRPCNodeStatsClient(c.raw)
}
// OrdersClient returns a OrdersClient for this connection
func (c *Conn) OrdersClient() pb.DRPCOrdersClient {
return pb.NewDRPCOrdersClient(c.raw)
}
// OverlayInspectorClient returns a OverlayInspectorClient for this connection
func (c *Conn) OverlayInspectorClient() pb.DRPCOverlayInspectorClient {
return pb.NewDRPCOverlayInspectorClient(c.raw)
}
// PaymentsClient returns a PaymentsClient for this connection
func (c *Conn) PaymentsClient() pb.DRPCPaymentsClient {
return pb.NewDRPCPaymentsClient(c.raw)
}
// PieceStoreInspectorClient returns a PieceStoreInspectorClient for this connection
func (c *Conn) PieceStoreInspectorClient() pb.DRPCPieceStoreInspectorClient {
return pb.NewDRPCPieceStoreInspectorClient(c.raw)
}
// PiecestoreClient returns a PiecestoreClient for this connection
func (c *Conn) PiecestoreClient() pb.DRPCPiecestoreClient {
return pb.NewDRPCPiecestoreClient(c.raw)
}
// ReferralManagerClient returns a ReferralManagerClient for this connection
func (c *Conn) ReferralManagerClient() pb.DRPCReferralManagerClient {
return pb.NewDRPCReferralManagerClient(c.raw)
}
// SatelliteGracefulExitClient returns a SatelliteGracefulExitClient for this connection
func (c *Conn) SatelliteGracefulExitClient() pb.DRPCSatelliteGracefulExitClient {
return pb.NewDRPCSatelliteGracefulExitClient(c.raw)
}
// VouchersClient returns a VouchersClient for this connection
func (c *Conn) VouchersClient() pb.DRPCVouchersClient {
return pb.NewDRPCVouchersClient(c.raw)
}

View File

@ -7,8 +7,12 @@ import (
"crypto/tls"
"storj.io/storj/pkg/identity"
"storj.io/storj/pkg/rpc/rpcpool"
)
// RawConn is a type alias to a drpc client connection
type RawConn = rpcpool.Conn
// Conn is a wrapper around a drpc client connection.
type Conn struct {
state tls.ConnectionState
@ -25,3 +29,8 @@ func (c *Conn) ConnectionState() tls.ConnectionState { return c.state }
func (c *Conn) PeerIdentity() (*identity.PeerIdentity, error) {
return identity.PeerIdentityFromChain(c.state.PeerCertificates)
}
// Raw returns the underlying connection.
func (c *Conn) Raw() *RawConn {
return c.raw
}

View File

@ -1,192 +0,0 @@
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
//
// This code generates the compat_drpc and compat_grpc files by reading in
// protobuf definitions. Its purpose is to generate a bunch of type aliases
// and forwarding functions so that a build tag transparently swaps out the
// concrete implementations of the rpcs.
// +build ignore
package main
import (
"bytes"
"fmt"
"go/format"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/zeebo/errs"
)
func main() {
if err := run(); err != nil {
log.Fatalf("%+v", err)
}
}
func usage() error {
return errs.New("usage: %s <dir> drpc <output file>", os.Args[0])
}
func run() error {
if len(os.Args) < 4 {
return usage()
}
clients, err := findClientsInDir(os.Args[1])
if err != nil {
return errs.Wrap(err)
}
info, ok := infos[os.Args[2]]
if !ok {
return usage()
}
return generate(clients, info, os.Args[3])
}
//
// info about the difference between generated files
//
type generateInfo struct {
Name string
Import string
Prefix string
Conn string
}
var infos = map[string]generateInfo{
"drpc": {
Name: "drpc",
Import: "storj.io/storj/pkg/rpc/rpcpool",
Prefix: "DRPC",
Conn: "rpcpool.Conn",
},
}
//
// main code to generate a compatability file
//
func generate(clients []string, info generateInfo, output string) (err error) {
var buf bytes.Buffer
p := printer{w: &buf}
P := p.P
Pf := p.Pf
P("// Copyright (C) 2019 Storj Labs, Inc.")
P("// See LICENSE for copying information.")
P()
P("package rpc")
P()
P("import (")
Pf("%q", info.Import)
if !strings.HasPrefix(info.Import, "storj.io/") {
P()
}
Pf("%q", "storj.io/storj/pkg/pb")
P(")")
P()
P("// RawConn is a type alias to a", info.Name, "client connection")
P("type RawConn =", info.Conn)
P()
P("type (")
for _, client := range clients {
P("//", client, "is an alias to the", info.Name, "client interface")
Pf("%s = pb.%s%s", client, info.Prefix, client)
P()
}
P(")")
for _, client := range clients {
P()
Pf("// New%s returns the %s version of a %s", client, info.Name, client)
Pf("func New%s(rc *RawConn) %s {", client, client)
Pf("return pb.New%s%s(rc)", info.Prefix, client)
P("}")
P()
Pf("// %s returns a %s for this connection", client, client)
Pf("func (c *Conn) %s() %s {", client, client)
Pf("return New%s(c.raw)", client)
P("}")
}
if err := p.Err(); err != nil {
return errs.Wrap(err)
}
fmtd, err := format.Source(buf.Bytes())
if err != nil {
return errs.Wrap(err)
}
return errs.Wrap(ioutil.WriteFile(output, fmtd, 0644))
}
//
// hacky code to find all the rpc clients in a go package
//
var clientRegex = regexp.MustCompile("^type (.*Client) interface {$")
func findClientsInDir(dir string) (clients []string, err error) {
files, err := filepath.Glob(filepath.Join(dir, "*.pb.go"))
if err != nil {
return nil, errs.Wrap(err)
}
for _, file := range files {
fileClients, err := findClientsInFile(file)
if err != nil {
return nil, errs.Wrap(err)
}
clients = append(clients, fileClients...)
}
sort.Strings(clients)
return clients, nil
}
func findClientsInFile(file string) (clients []string, err error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, errs.Wrap(err)
}
for _, line := range bytes.Split(data, []byte("\n")) {
switch client := clientRegex.FindSubmatch(line); {
case client == nil:
case bytes.HasPrefix(client[1], []byte("DRPC")):
case bytes.Contains(client[1], []byte("_")):
default:
clients = append(clients, string(client[1]))
}
}
return clients, nil
}
//
// helper to check errors while printing
//
type printer struct {
w io.Writer
err error
}
func (p *printer) P(args ...interface{}) {
if p.err == nil {
_, p.err = fmt.Fprintln(p.w, args...)
}
}
func (p *printer) Pf(format string, args ...interface{}) {
if p.err == nil {
_, p.err = fmt.Fprintf(p.w, format+"\n", args...)
}
}
func (p *printer) Err() error {
return p.err
}

View File

@ -41,7 +41,7 @@ func TestBasic(t *testing.T) {
conn, err := sn.Dialer.DialNode(ctx, &satellite)
require.NoError(t, err)
defer ctx.Check(conn.Close)
_, err = conn.NodeClient().CheckIn(ctx, &pb.CheckInRequest{
_, err = pb.NewDRPCNodeClient(conn.Raw()).CheckIn(ctx, &pb.CheckInRequest{
Address: node.GetAddress().GetAddress(),
Version: &node.Version,
Capacity: &node.Capacity,

View File

@ -25,7 +25,7 @@ func newClient(ctx context.Context, dialer rpc.Dialer, address string, id storj.
return &client{
conn: conn,
client: conn.ContactClient(),
client: pb.NewDRPCContactClient(conn.Raw()),
}, nil
}

View File

@ -189,7 +189,7 @@ func TestConcurrentConnections(t *testing.T) {
err = errs.Combine(err, conn.Close())
}()
client := conn.SatelliteGracefulExitClient()
client := pb.NewDRPCSatelliteGracefulExitClient(conn.Raw())
// wait for "main" call to begin
wg.Wait()
@ -209,7 +209,7 @@ func TestConcurrentConnections(t *testing.T) {
require.NoError(t, err)
defer ctx.Check(conn.Close)
client := conn.SatelliteGracefulExitClient()
client := pb.NewDRPCSatelliteGracefulExitClient(conn.Raw())
// this connection will immediately return since graceful exit has not been initiated yet
c, err := client.Process(ctx)
require.NoError(t, err)
@ -407,7 +407,7 @@ func TestExitDisqualifiedNodeFailOnStart(t *testing.T) {
require.NoError(t, err)
defer ctx.Check(conn.Close)
client := conn.SatelliteGracefulExitClient()
client := pb.NewDRPCSatelliteGracefulExitClient(conn.Raw())
processClient, err := client.Process(ctx)
require.NoError(t, err)
@ -944,7 +944,7 @@ func TestExitDisabled(t *testing.T) {
require.NoError(t, err)
defer ctx.Check(conn.Close)
client := conn.SatelliteGracefulExitClient()
client := pb.NewDRPCSatelliteGracefulExitClient(conn.Raw())
processClient, err := client.Process(ctx)
require.NoError(t, err)
@ -1025,7 +1025,7 @@ func TestPointerChangedOrDeleted(t *testing.T) {
require.NoError(t, err)
defer ctx.Check(conn.Close)
client := conn.SatelliteGracefulExitClient()
client := pb.NewDRPCSatelliteGracefulExitClient(conn.Raw())
c, err := client.Process(ctx)
require.NoError(t, err)
@ -1260,7 +1260,7 @@ func TestFailureStorageNodeIgnoresTransferMessages(t *testing.T) {
require.NoError(t, err)
defer ctx.Check(conn.Close)
client := conn.SatelliteGracefulExitClient()
client := pb.NewDRPCSatelliteGracefulExitClient(conn.Raw())
c, err := client.Process(ctx)
require.NoError(t, err)
@ -1387,7 +1387,7 @@ func testTransfers(t *testing.T, objects int, verifier func(ctx *testcontext.Con
require.NoError(t, err)
defer ctx.Check(conn.Close)
client := conn.SatelliteGracefulExitClient()
client := pb.NewDRPCSatelliteGracefulExitClient(conn.Raw())
c, err := client.Process(ctx)
require.NoError(t, err)

View File

@ -72,7 +72,7 @@ func (service *Service) GetTokens(ctx context.Context, userID *uuid.UUID) (token
err = conn.Close()
}()
client := conn.ReferralManagerClient()
client := pb.NewDRPCReferralManagerClient(conn.Raw())
response, err := client.GetTokens(ctx, &pb.GetTokensRequest{
OwnerUserId: userID[:],
OwnerSatelliteId: service.signer.ID(),
@ -166,7 +166,7 @@ func (service *Service) redeemToken(ctx context.Context, userID *uuid.UUID, toke
return errs.Wrap(err)
}
client := conn.ReferralManagerClient()
client := pb.NewDRPCReferralManagerClient(conn.Raw())
_, err = client.RedeemToken(ctx, &pb.RedeemTokenRequest{
Token: referralToken[:],
RedeemUserId: userID[:],

View File

@ -23,7 +23,7 @@ func TestVouchers(t *testing.T) {
require.NoError(t, err)
defer ctx.Check(conn.Close)
client := conn.VouchersClient()
client := pb.NewDRPCVouchersClient(conn.Raw())
resp, err := client.Request(ctx, &pb.VoucherRequest{})
require.Nil(t, resp)

View File

@ -159,7 +159,7 @@ func (chore *Chore) pingSatelliteOnce(ctx context.Context, id storj.NodeID) (err
}
defer func() { err = errs.Combine(err, conn.Close()) }()
_, err = conn.NodeClient().CheckIn(ctx, &pb.CheckInRequest{
_, err = pb.NewDRPCNodeClient(conn.Raw()).CheckIn(ctx, &pb.CheckInRequest{
Address: self.Address.GetAddress(),
Version: &self.Version,
Capacity: &self.Capacity,

View File

@ -26,7 +26,7 @@ func TestStoragenodeContactEndpoint(t *testing.T) {
require.NoError(t, err)
defer ctx.Check(conn.Close)
resp, err := conn.ContactClient().PingNode(ctx, &pb.ContactPingRequest{})
resp, err := pb.NewDRPCContactClient(conn.Raw()).PingNode(ctx, &pb.ContactPingRequest{})
require.NotNil(t, resp)
require.NoError(t, err)
@ -34,7 +34,7 @@ func TestStoragenodeContactEndpoint(t *testing.T) {
time.Sleep(time.Second) //HACKFIX: windows has large time granularity
resp, err = conn.ContactClient().PingNode(ctx, &pb.ContactPingRequest{})
resp, err = pb.NewDRPCContactClient(conn.Raw()).PingNode(ctx, &pb.ContactPingRequest{})
require.NotNil(t, resp)
require.NoError(t, err)

View File

@ -72,7 +72,7 @@ func (worker *Worker) Run(ctx context.Context, done func()) (err error) {
err = errs.Combine(err, conn.Close())
}()
client := conn.SatelliteGracefulExitClient()
client := pb.NewDRPCSatelliteGracefulExitClient(conn.Raw())
c, err := client.Process(ctx)
if err != nil {

View File

@ -131,7 +131,7 @@ func (s *Service) dial(ctx context.Context, satelliteID storj.NodeID) (_ *Client
return &Client{
conn: conn,
DRPCNodeStatsClient: conn.NodeStatsClient(),
DRPCNodeStatsClient: pb.NewDRPCNodeStatsClient(conn.Raw()),
}, nil
}

View File

@ -257,7 +257,7 @@ func (service *Service) settle(ctx context.Context, log *zap.Logger, satelliteID
}
defer func() { err = errs.Combine(err, conn.Close()) }()
stream, err := conn.OrdersClient().Settlement(ctx)
stream, err := pb.NewDRPCOrdersClient(conn.Raw()).Settlement(ctx)
if err != nil {
return OrderError.New("failed to start settlement: %v", err)
}

View File

@ -62,7 +62,7 @@ func Dial(ctx context.Context, dialer rpc.Dialer, address string, apiKey *macaro
return &Client{
conn: conn,
client: conn.MetainfoClient(),
client: pb.NewDRPCMetainfoClient(conn.Raw()),
apiKeyRaw: apiKey.SerializeRaw(),
userAgent: userAgent,
}, nil

View File

@ -55,7 +55,7 @@ func Dial(ctx context.Context, dialer rpc.Dialer, target *pb.Node, log *zap.Logg
return &Client{
log: log,
client: conn.PiecestoreClient(),
client: pb.NewDRPCPiecestoreClient(conn.Raw()),
conn: conn,
config: config,
}, nil