-
Notifications
You must be signed in to change notification settings - Fork 453
/
topic.go
300 lines (263 loc) · 8.07 KB
/
topic.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package topic
import (
"bytes"
"errors"
"fmt"
"time"
"github.com/m3db/m3/src/cluster/kv"
"github.com/m3db/m3/src/cluster/services"
"github.com/m3db/m3/src/msg/generated/proto/topicpb"
)
var (
errEmptyName = errors.New("invalid topic: empty name")
errZeroShards = errors.New("invalid topic: zero shards")
)
type topic struct {
name string
numOfShards uint32
consumerServices []ConsumerService
version int
}
// NewTopic creates a new topic.
func NewTopic() Topic { return new(topic) }
// NewTopicFromValue creates a topic from a kv.Value.
func NewTopicFromValue(v kv.Value) (Topic, error) {
var topic topicpb.Topic
if err := v.Unmarshal(&topic); err != nil {
return nil, err
}
t, err := NewTopicFromProto(&topic)
if err != nil {
return nil, err
}
return t.SetVersion(v.Version()), nil
}
// NewTopicFromProto creates a topic from a proto.
func NewTopicFromProto(t *topicpb.Topic) (Topic, error) {
css := make([]ConsumerService, len(t.ConsumerServices))
for i, cspb := range t.ConsumerServices {
cs, err := NewConsumerServiceFromProto(cspb)
if err != nil {
return nil, err
}
css[i] = cs
}
return NewTopic().
SetName(t.Name).
SetNumberOfShards(t.NumberOfShards).
SetConsumerServices(css), nil
}
func (t *topic) Name() string {
return t.name
}
func (t *topic) SetName(value string) Topic {
newt := *t
newt.name = value
return &newt
}
func (t *topic) NumberOfShards() uint32 {
return t.numOfShards
}
func (t *topic) SetNumberOfShards(value uint32) Topic {
newt := *t
newt.numOfShards = value
return &newt
}
func (t *topic) ConsumerServices() []ConsumerService {
return t.consumerServices
}
func (t *topic) SetConsumerServices(value []ConsumerService) Topic {
newt := *t
newt.consumerServices = value
return &newt
}
func (t *topic) Version() int {
return t.version
}
func (t *topic) SetVersion(value int) Topic {
newt := *t
newt.version = value
return &newt
}
func (t *topic) AddConsumerService(value ConsumerService) (Topic, error) {
cur := t.ConsumerServices()
for _, cs := range cur {
if cs.ServiceID().Equal(value.ServiceID()) {
return nil, fmt.Errorf("service %s is already consuming the topic", value.ServiceID().String())
}
}
return t.SetConsumerServices(append(cur, value)), nil
}
func (t *topic) RemoveConsumerService(value services.ServiceID) (Topic, error) {
cur := t.ConsumerServices()
for i, cs := range cur {
if cs.ServiceID().Equal(value) {
cur = append(cur[:i], cur[i+1:]...)
return t.SetConsumerServices(cur), nil
}
}
return nil, fmt.Errorf("could not find consumer service %s in the topic", value.String())
}
func (t *topic) UpdateConsumerService(value ConsumerService) (Topic, error) {
css := t.ConsumerServices()
for i, cs := range css {
if !cs.ServiceID().Equal(value.ServiceID()) {
continue
}
if value.ConsumptionType() != cs.ConsumptionType() {
return nil, fmt.Errorf("could not change consumption type for consumer service %s", value.ServiceID().String())
}
css[i] = value
return t.SetConsumerServices(css), nil
}
return nil, fmt.Errorf("could not find consumer service %s in the topic", value.String())
}
func (t *topic) String() string {
var buf bytes.Buffer
buf.WriteString("\n{\n")
buf.WriteString(fmt.Sprintf("\tversion: %d\n", t.version))
buf.WriteString(fmt.Sprintf("\tname: %s\n", t.name))
buf.WriteString(fmt.Sprintf("\tnumOfShards: %d\n", t.numOfShards))
if len(t.consumerServices) > 0 {
buf.WriteString("\tconsumerServices: {\n")
}
for _, cs := range t.consumerServices {
buf.WriteString(fmt.Sprintf("\t\t%s\n", cs.String()))
}
if len(t.consumerServices) > 0 {
buf.WriteString("\t}\n")
}
buf.WriteString("}\n")
return buf.String()
}
func (t *topic) Validate() error {
if t.Name() == "" {
return errEmptyName
}
if t.NumberOfShards() == 0 {
return errZeroShards
}
uniqConsumers := make(map[string]struct{}, len(t.ConsumerServices()))
for _, cs := range t.ConsumerServices() {
_, ok := uniqConsumers[cs.ServiceID().String()]
if ok {
return fmt.Errorf("invalid topic: duplicated consumer %s", cs.ServiceID().String())
}
uniqConsumers[cs.ServiceID().String()] = struct{}{}
}
return nil
}
// ToProto creates proto from a topic.
func ToProto(t Topic) (*topicpb.Topic, error) {
css := t.ConsumerServices()
csspb := make([]*topicpb.ConsumerService, len(css))
for i, cs := range css {
cspb, err := ConsumerServiceToProto(cs)
if err != nil {
return nil, err
}
csspb[i] = cspb
}
return &topicpb.Topic{
Name: t.Name(),
NumberOfShards: t.NumberOfShards(),
ConsumerServices: csspb,
}, nil
}
type consumerService struct {
sid services.ServiceID
ct ConsumptionType
ttlNanos int64
}
// NewConsumerService creates a ConsumerService.
func NewConsumerService() ConsumerService {
return new(consumerService)
}
// NewConsumerServiceFromProto creates a ConsumerService from a proto.
func NewConsumerServiceFromProto(cs *topicpb.ConsumerService) (ConsumerService, error) {
ct, err := NewConsumptionTypeFromProto(cs.ConsumptionType)
if err != nil {
return nil, err
}
return NewConsumerService().
SetServiceID(NewServiceIDFromProto(cs.ServiceId)).
SetConsumptionType(ct).
SetMessageTTLNanos(cs.MessageTtlNanos), nil
}
// ConsumerServiceToProto creates proto from a ConsumerService.
func ConsumerServiceToProto(cs ConsumerService) (*topicpb.ConsumerService, error) {
ct, err := ConsumptionTypeToProto(cs.ConsumptionType())
if err != nil {
return nil, err
}
return &topicpb.ConsumerService{
ConsumptionType: ct,
ServiceId: ServiceIDToProto(cs.ServiceID()),
MessageTtlNanos: cs.MessageTTLNanos(),
}, nil
}
func (cs *consumerService) ServiceID() services.ServiceID {
return cs.sid
}
func (cs *consumerService) SetServiceID(value services.ServiceID) ConsumerService {
newcs := *cs
newcs.sid = value
return &newcs
}
func (cs *consumerService) ConsumptionType() ConsumptionType {
return cs.ct
}
func (cs *consumerService) SetConsumptionType(value ConsumptionType) ConsumerService {
newcs := *cs
newcs.ct = value
return &newcs
}
func (cs *consumerService) MessageTTLNanos() int64 {
return cs.ttlNanos
}
func (cs *consumerService) SetMessageTTLNanos(value int64) ConsumerService {
newcs := *cs
newcs.ttlNanos = value
return &newcs
}
func (cs *consumerService) String() string {
var buf bytes.Buffer
buf.WriteString("{")
buf.WriteString(fmt.Sprintf("service: %s, consumption type: %s", cs.sid.String(), cs.ct.String()))
if cs.ttlNanos != 0 {
buf.WriteString(fmt.Sprintf(", ttl: %v", time.Duration(cs.ttlNanos)))
}
buf.WriteString("}")
return buf.String()
}
// NewServiceIDFromProto creates service id from a proto.
func NewServiceIDFromProto(sid *topicpb.ServiceID) services.ServiceID {
return services.NewServiceID().SetName(sid.Name).SetEnvironment(sid.Environment).SetZone(sid.Zone)
}
// ServiceIDToProto creates proto from a service id.
func ServiceIDToProto(sid services.ServiceID) *topicpb.ServiceID {
return &topicpb.ServiceID{
Name: sid.Name(),
Environment: sid.Environment(),
Zone: sid.Zone(),
}
}