/
client.go
210 lines (197 loc) · 4.89 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package s3
import (
"bytes"
"context"
"io"
"math"
"time"
"github.com/DeBankDeFi/nodex/pkg/metrics"
"github.com/DeBankDeFi/nodex/pkg/pb"
"github.com/DeBankDeFi/nodex/pkg/utils"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/proto"
)
type Client struct {
s3client pb.S3ProxyClient
conn *grpc.ClientConn
cache *utils.Cache
addr string
s3Metric *metrics.S3Metrics
}
func NewClient(addr string) (*Client, error) {
conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(math.MaxInt32),
grpc.MaxCallSendMsgSize(math.MaxInt32)))
if err != nil {
return nil, err
}
return &Client{
conn: conn,
s3client: pb.NewS3ProxyClient(conn),
cache: utils.NewCache(MaxCacheSize),
addr: addr,
s3Metric: metrics.NewS3Metrics(),
}, nil
}
func (c *Client) GetConn() *grpc.ClientConn {
return c.conn
}
func (c *Client) ResetConn() error {
if c.conn != nil {
if utils.CheckConnState(c.conn) == nil {
return nil
}
c.conn.Close()
}
conn, err := grpc.Dial(c.addr, grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(math.MaxInt32),
grpc.MaxCallSendMsgSize(math.MaxInt32)))
if err != nil {
return err
}
c.s3client = pb.NewS3ProxyClient(conn)
return nil
}
func (c *Client) GetBlock(ctx context.Context, info *pb.BlockInfo, noCache bool) (header *pb.Block, err error) {
err = c.ResetConn()
if err != nil {
return nil, err
}
commonPrefix := utils.CommonPrefix(info.Env, info.ChainId, info.Role, info.BlockType)
lru := c.cache.GetOrCreatePrefixCache(commonPrefix)
key := utils.InfoToPrefix(info)
startTime := time.Now()
if noCache {
val, err := c.getBlock(ctx, info, noCache)
if err != nil {
return nil, err
}
header = val
lru.Insert(key, header, info.BlockNum)
} else {
val, err := lru.Get(key, info.BlockNum, func() (interface{}, error) { return c.getBlock(ctx, info, noCache) })
if err != nil {
return nil, err
}
header = proto.Clone(val.(*pb.Block)).(*pb.Block)
}
c.s3Metric.ObserveReadLatency("s3-proxy", float64(time.Since(startTime).Milliseconds()))
c.s3Metric.IncreaseReadSize("s3-proxy", int64(proto.Size(header)))
return header, nil
}
func (c *Client) getBlock(ctx context.Context, info *pb.BlockInfo, noCache bool) (header *pb.Block, err error) {
client, err := c.s3client.GetBlock(ctx, &pb.GetBlockRequest{
Info: info,
NoCache: noCache,
})
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
for {
chunk, err := client.Recv()
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
if len(chunk.Chunk) > 0 {
buf.Write(chunk.Chunk)
}
}
client.CloseSend()
if buf.Len() == 0 {
return nil, nil
}
header = &pb.Block{}
err = proto.Unmarshal(buf.Bytes(), header)
if err != nil {
return nil, err
}
return header, nil
}
func (c *Client) PutBlock(ctx context.Context, block *pb.Block) (err error) {
startTime := time.Now()
err = c.ResetConn()
if err != nil {
return err
}
data, err := proto.Marshal(block)
if err != nil {
return err
}
client, err := c.s3client.PutBlock(ctx)
if err != nil {
return err
}
block.Info.BlockSize = int64(len(data))
err = client.Send(&pb.BlockChunk{
Info: block.Info,
})
if err != nil {
return err
}
chunk := make([]byte, ChunkSize)
reader := bytes.NewReader(data)
for {
n, err := reader.Read(chunk)
if err != nil {
if err == io.EOF {
break
}
return err
}
if err := client.Send(&pb.BlockChunk{Chunk: chunk[:n]}); err != nil {
return err
}
}
_, err = client.CloseAndRecv()
if err != nil {
return err
}
c.s3Metric.ObserveWriteLatency("s3-proxy", float64(time.Since(startTime).Milliseconds()))
c.s3Metric.IncreaseWriteSize("s3-proxy", int64(proto.Size(block)))
return nil
}
func (c *Client) ListHeaderStartAt(ctx context.Context, chainId, env, role string, blockNum int64, count int64, after int64) (info []*pb.BlockInfo, err error) {
err = c.ResetConn()
if err != nil {
return nil, err
}
rsp, err := c.s3client.ListHeaderStartAt(ctx, &pb.ListHeaderStartAtRequest{
ChainId: chainId,
Env: env,
Role: role,
BlockNum: blockNum,
CountNum: count,
AfterMsgOffset: after,
})
if err != nil {
return nil, err
}
return rsp.Infos, nil
}
func (c *Client) RemoveFiles(ctx context.Context, infos []*pb.BlockInfo) error {
_, err := c.s3client.RemoveFiles(ctx, &pb.RemoveFilesRequest{
Infos: infos,
})
return err
}
func (c *Client) PutFile(ctx context.Context, key string, buf []byte) error {
_, err := c.s3client.PutFile(ctx, &pb.PutFileRequest{
Path: key,
Data: buf,
})
return err
}
func (c *Client) GetFile(ctx context.Context, key string) ([]byte, error) {
rsp, err := c.s3client.GetFile(ctx, &pb.GetFileRequest{
Path: key,
})
if err != nil {
return nil, err
}
return rsp.Data, nil
}