This repository has been archived by the owner on Jan 24, 2020. It is now read-only.
/
broker_fetch.go
147 lines (122 loc) · 3.34 KB
/
broker_fetch.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
package broker
import (
"bytes"
"context"
"io"
"google.golang.org/grpc/codes"
"github.com/celrenheit/sandflake"
"github.com/celrenheit/sandglass-grpc/go/sgproto"
"github.com/celrenheit/sandglass/storage"
"github.com/celrenheit/sandglass/topic"
"github.com/grpc/grpc-go/status"
)
func (b *Broker) FetchRange(ctx context.Context, req *sgproto.FetchRangeRequest, fn func(msg *sgproto.Message) error) error {
topic := b.getTopic(req.Topic)
if topic == nil {
return ErrTopicNotFound
}
if req.Partition == "" {
return ErrNoPartitionSet
}
leader := b.getPartitionLeader(topic.Name, req.Partition)
if leader.Name != b.Name() {
stream, err := leader.FetchRange(ctx, req)
if err != nil {
return err
}
for {
msg, err := stream.Recv()
if err == io.EOF {
break
} else if err != nil {
return err
}
err = fn(msg)
if err != nil {
return err
}
}
return nil
}
p := topic.GetPartition(req.Partition)
return p.ForRange(req.From, req.To, fn)
}
func (b *Broker) FetchFromSync(topicName, partition string, from []byte, fn func(msg *sgproto.Message) error) error {
topic := b.getTopic(topicName)
if topic == nil {
return ErrTopicNotFound
}
if partition == "" {
return ErrNoPartitionSet
}
p := topic.GetPartition(partition)
return p.RangeFromWAL(from, fn)
}
func (b *Broker) Get(ctx context.Context, topicName string, partition string, key []byte) (*sgproto.Message, error) {
t := b.getTopic(topicName)
var p *topic.Partition
if partition != "" {
p = t.GetPartition(partition)
} else {
p = t.ChoosePartitionForKey(key)
}
return b.getFromPartition(ctx, topicName, p, key)
}
func (b *Broker) HasKey(ctx context.Context, topicName string, partition string, key, clusterKey []byte) (bool, error) {
t := b.getTopic(topicName)
var p *topic.Partition
if partition != "" {
p = t.GetPartition(partition)
} else {
p = t.ChoosePartitionForKey(key)
}
return b.hasKeyInPartition(ctx, topicName, p, key, clusterKey)
}
func (b *Broker) getFromPartition(ctx context.Context, topic string, p *topic.Partition, key []byte) (*sgproto.Message, error) {
leader := b.getPartitionLeader(topic, p.Id)
if leader == nil {
return nil, ErrNoLeaderFound
}
if leader.Name != b.Name() {
b.Debug("fetch key remotely '%v' from %v", string(key), leader.Name)
return leader.GetByKey(ctx, &sgproto.GetRequest{
Topic: topic,
Partition: p.Id,
Key: key,
})
}
msg, err := p.GetMessage(sandflake.Nil, key, nil)
if err != nil {
return nil, err
}
if msg == nil {
return nil, status.Error(codes.NotFound, "message not found")
}
return msg, nil
}
func (b *Broker) hasKeyInPartition(ctx context.Context, topic string, p *topic.Partition, key, clusterKey []byte) (bool, error) {
leader := b.getPartitionLeader(topic, p.Id)
if leader == nil {
return false, ErrNoLeaderFound
}
if leader.Name != b.Name() {
b.Debug("fetch key remotely '%v' from %v", string(key), leader.Name)
resp, err := leader.HasKey(ctx, &sgproto.GetRequest{
Topic: topic,
Partition: p.Id,
Key: key,
ClusteringKey: clusterKey,
})
if err != nil {
return false, err
}
return resp.Exists, nil
}
return p.HasKey(key, clusterKey)
}
func generatePrefixConsumerOffsetKey(partitionKey []byte, offset sandflake.ID) []byte {
return bytes.Join([][]byte{
partitionKey,
offset.Bytes(),
}, storage.Separator)
}