-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
client.go
135 lines (115 loc) · 3.46 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Copyright 2019 The Vitess 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 grpcmysqlctlclient contains the gRPC1 version of the mysqlctl
// client protocol.
package grpcmysqlctlclient
import (
"fmt"
"net"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"context"
"vitess.io/vitess/go/vt/grpcclient"
"vitess.io/vitess/go/vt/mysqlctl/mysqlctlclient"
mysqlctlpb "vitess.io/vitess/go/vt/proto/mysqlctl"
)
type client struct {
cc *grpc.ClientConn
c mysqlctlpb.MysqlCtlClient
}
func factory(network, addr string) (mysqlctlclient.MysqlctlClient, error) {
// create the RPC client
cc, err := grpcclient.Dial(addr, grpcclient.FailFast(false), grpc.WithInsecure(), grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) { //nolint:staticcheck
return net.DialTimeout(network, addr, timeout)
}))
if err != nil {
return nil, err
}
c := mysqlctlpb.NewMysqlCtlClient(cc)
return &client{
cc: cc,
c: c,
}, nil
}
// Start is part of the MysqlctlClient interface.
func (c *client) Start(ctx context.Context, mysqldArgs ...string) error {
return c.withRetry(ctx, func() error {
_, err := c.c.Start(ctx, &mysqlctlpb.StartRequest{
MysqldArgs: mysqldArgs,
})
return err
})
}
// Shutdown is part of the MysqlctlClient interface.
func (c *client) Shutdown(ctx context.Context, waitForMysqld bool) error {
return c.withRetry(ctx, func() error {
_, err := c.c.Shutdown(ctx, &mysqlctlpb.ShutdownRequest{
WaitForMysqld: waitForMysqld,
})
return err
})
}
// RunMysqlUpgrade is part of the MysqlctlClient interface.
func (c *client) RunMysqlUpgrade(ctx context.Context) error {
return c.withRetry(ctx, func() error {
_, err := c.c.RunMysqlUpgrade(ctx, &mysqlctlpb.RunMysqlUpgradeRequest{})
return err
})
}
// ReinitConfig is part of the MysqlctlClient interface.
func (c *client) ReinitConfig(ctx context.Context) error {
return c.withRetry(ctx, func() error {
_, err := c.c.ReinitConfig(ctx, &mysqlctlpb.ReinitConfigRequest{})
return err
})
}
// RefreshConfig is part of the MysqlctlClient interface.
func (c *client) RefreshConfig(ctx context.Context) error {
return c.withRetry(ctx, func() error {
_, err := c.c.RefreshConfig(ctx, &mysqlctlpb.RefreshConfigRequest{})
return err
})
}
// Close is part of the MysqlctlClient interface.
func (c *client) Close() {
c.cc.Close()
}
// withRetry is needed because grpc doesn't handle some transient errors
// correctly (like EAGAIN) when sockets are used.
func (c *client) withRetry(ctx context.Context, f func() error) error {
var lastError error
for {
select {
case <-ctx.Done():
return fmt.Errorf("%v: %v", ctx.Err(), lastError)
default:
}
if err := f(); err != nil {
if st, ok := status.FromError(err); ok {
code := st.Code()
if code == codes.Unavailable {
lastError = err
time.Sleep(100 * time.Millisecond)
continue
}
}
return err
}
return nil
}
}
func init() {
mysqlctlclient.RegisterFactory("grpc", factory)
}