forked from grafana/loki
/
storage_client.go
122 lines (107 loc) · 2.76 KB
/
storage_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
package grpc
import (
"context"
"io"
"github.com/pkg/errors"
"google.golang.org/grpc"
"github.com/frelon/loki/v2/pkg/storage/chunk"
)
type StorageClient struct {
schemaCfg chunk.SchemaConfig
client GrpcStoreClient
connection *grpc.ClientConn
}
// NewStorageClient returns a new StorageClient.
func NewStorageClient(cfg Config, schemaCfg chunk.SchemaConfig) (*StorageClient, error) {
grpcClient, conn, err := connectToGrpcServer(cfg.Address)
if err != nil {
return nil, err
}
client := &StorageClient{
schemaCfg: schemaCfg,
client: grpcClient,
connection: conn,
}
return client, nil
}
func (s *StorageClient) Stop() {
s.connection.Close()
}
// PutChunks implements chunk.ObjectClient.
func (s *StorageClient) PutChunks(ctx context.Context, chunks []chunk.Chunk) error {
req := &PutChunksRequest{}
for i := range chunks {
buf, err := chunks[i].Encoded()
if err != nil {
return errors.WithStack(err)
}
key := s.schemaCfg.ExternalKey(chunks[i])
tableName, err := s.schemaCfg.ChunkTableFor(chunks[i].From)
if err != nil {
return errors.WithStack(err)
}
writeChunk := &Chunk{
Encoded: buf,
Key: key,
TableName: tableName,
}
req.Chunks = append(req.Chunks, writeChunk)
}
_, err := s.client.PutChunks(ctx, req)
if err != nil {
return errors.WithStack(err)
}
return nil
}
func (s *StorageClient) DeleteChunk(ctx context.Context, userID, chunkID string) error {
chunkInfo := &ChunkID{ChunkID: chunkID}
_, err := s.client.DeleteChunks(ctx, chunkInfo)
if err != nil {
return errors.WithStack(err)
}
return nil
}
func (s *StorageClient) IsChunkNotFoundErr(_ error) bool {
return false
}
func (s *StorageClient) GetChunks(ctx context.Context, input []chunk.Chunk) ([]chunk.Chunk, error) {
req := &GetChunksRequest{}
req.Chunks = []*Chunk{}
var err error
for _, inputInfo := range input {
chunkInfo := &Chunk{}
// send the table name from upstream gRPC client as gRPC server is unaware of schema
chunkInfo.TableName, err = s.schemaCfg.ChunkTableFor(inputInfo.From)
if err != nil {
return nil, errors.WithStack(err)
}
chunkInfo.Key = s.schemaCfg.ExternalKey(inputInfo)
req.Chunks = append(req.Chunks, chunkInfo)
}
streamer, err := s.client.GetChunks(ctx, req)
if err != nil {
return nil, errors.WithStack(err)
}
var result []chunk.Chunk
decodeContext := chunk.NewDecodeContext()
for {
receivedChunks, err := streamer.Recv()
if err == io.EOF {
break
}
if err != nil {
return nil, errors.WithStack(err)
}
for _, chunkResponse := range receivedChunks.GetChunks() {
var c chunk.Chunk
if chunkResponse != nil {
err = c.Decode(decodeContext, chunkResponse.Encoded)
if err != nil {
return result, err
}
}
result = append(result, c)
}
}
return result, err
}