generated from Jille/raft-grpc-example
/
client.go
190 lines (173 loc) · 4.9 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
package client
import (
"context"
proto "github.com/Kapperchino/jet-stream/application/proto/proto"
clusterPb "github.com/Kapperchino/jet-stream/cluster/proto/proto"
"github.com/Kapperchino/jet-stream/util"
"github.com/buraksezer/consistent"
"github.com/rs/zerolog/log"
"github.com/spaolacci/murmur3"
"math/rand"
"strconv"
)
type JetClient struct {
info *clusterPb.ClusterInfo
shardClients *util.Map[string, *ShardClient]
metaData *Meta
}
type ConsumerGroup struct {
group *proto.ConsumerGroup
}
type Meta struct {
topics *util.Map[string, *TopicMeta]
consumerGroups *util.Map[string, *ConsumerGroup]
}
type TopicMeta struct {
partitions *util.Map[uint64, *PartitionMeta]
hash *consistent.Consistent
}
type PartitionMeta struct {
partitionNum uint64
topic string
shardId string
}
type ShardClient struct {
leader string
shardId string
memberclients *util.Map[string, *MemberClient]
roundRobinIndex int32
partitions *util.Map[uint64, *PartitionMeta]
buffer [100]*proto.KeyVal
}
type MemberClient struct {
nodeId string
clusterClient clusterPb.ClusterMetaServiceClient
messageClient proto.MessageServiceClient
}
type hasher struct{}
func (h hasher) Sum64(data []byte) uint64 {
// you should use a proper hash function for uniformity.
return murmur3.Sum64(data)
}
func New(address string) (*JetClient, error) {
con, err := newClientConnection(address)
if err != nil {
log.Error().Msgf("Error creating connection with client")
return nil, err
}
ctx := context.Background()
rootClient := newClusterClient(con)
defer con.Close()
clusterInfo, err := rootClient.GetClusterInfo(ctx, &clusterPb.GetClusterInfoRequest{})
if err != nil {
log.Err(err).Msgf("Error getting cluster info")
return nil, err
}
shardClients := util.NewMap[string, *ShardClient]()
for shardId, shard := range clusterInfo.Info.ShardMap {
shardClient := ShardClient{
leader: shard.LeaderId,
memberclients: util.NewMap[string, *MemberClient](),
partitions: util.NewMap[uint64, *PartitionMeta](),
shardId: shardId,
}
shardClients.Set(shardId, &shardClient)
for nodeId, member := range shard.MemberAddressMap {
con, err = newClientConnection(member.Address)
if err != nil {
log.Err(err).Msgf("Error creating connection")
return nil, err
}
memberClient := MemberClient{
nodeId: nodeId,
clusterClient: newClusterClient(con),
messageClient: newMessageClient(con),
}
shardClient.memberclients.Set(nodeId, &memberClient)
}
}
metaData := Meta{topics: util.NewMap[string, *TopicMeta](), consumerGroups: util.NewMap[string, *ConsumerGroup]()}
shardClients.ForEach(func(shardId string, client *ShardClient) bool {
meta, err := client.GetNextMember().messageClient.GetMeta(ctx, &proto.GetMetaRequest{})
if err != nil {
log.Err(err).Msgf("Error while getting meta from shard %s", shardId)
return false
}
for key, topic := range meta.Topics {
//updating the map
var item = metaData.topics.Get(key)
if item == nil {
topicMeta := &TopicMeta{
partitions: util.NewMap[uint64, *PartitionMeta](),
hash: newHashRing(),
}
for _, partition := range topic.Partitions {
topicMeta.partitions.Set(partition.Num, &PartitionMeta{
partitionNum: partition.Num,
topic: topic.Name,
shardId: shardId,
})
}
metaData.topics.Set(key, topicMeta)
item = topicMeta
}
partitions := item.partitions
for num, partition := range topic.Partitions {
partitionMeta := &PartitionMeta{
partitionNum: num,
topic: partition.Topic,
shardId: shardId,
}
item.hash.Add(partitionMeta)
partitions.Set(num, partitionMeta)
client.partitions.Set(num, partitionMeta)
}
}
for id, group := range meta.ConsumerGroups {
var partitions []uint64
for _, consumer := range group.Consumers {
partitions = append(partitions, consumer.Partition)
}
metaData.consumerGroups.Set(id, &ConsumerGroup{
group: group,
})
}
return true
},
)
return &JetClient{
info: clusterInfo.Info,
shardClients: shardClients,
metaData: &metaData,
}, nil
}
type KeyValuePair struct {
id string
count int
}
func (s *ShardClient) GetLeader() *MemberClient {
res := s.memberclients.Get(s.leader)
return res
}
func (s *ShardClient) GetNextMember() *MemberClient {
if s.memberclients.Len() == 1 {
var res *MemberClient
s.memberclients.ForEach(func(s string, client *MemberClient) bool {
res = client
return true
})
return res
}
var memberClients []*MemberClient
s.memberclients.ForEach(func(id string, client *MemberClient) bool {
if id != s.leader {
memberClients = append(memberClients, client)
}
return true
})
index := rand.Int() % len(memberClients)
return memberClients[index]
}
func (p *PartitionMeta) String() string {
return strconv.FormatUint(p.partitionNum, 10)
}