-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopics.go
More file actions
353 lines (333 loc) · 13.3 KB
/
Copy pathtopics.go
File metadata and controls
353 lines (333 loc) · 13.3 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package events
import (
"context"
"math/rand"
"strconv"
"time"
"github.com/lestrrat-go/backoff/v2"
"github.com/memsql/errors"
"github.com/segmentio/kafka-go"
"github.com/singlestore-labs/events/internal/pwork"
"github.com/singlestore-labs/generic"
)
type topicsWhy struct {
why string
errorCategory string
}
// This file handles the creation of topics. Topic creation is done on-the-fly as
// messages are sent or consumers are started. The topic configuration can be
// overridden before the topic is created. It is expected that the same topic can
// be requested to be created from multiple go routines at once. Only one go routine
// will actually create the topic. All other will wait for the one that is doing the
// work to complete.
const (
topicCreateSleepTime = time.Second
topicCreationDeadline = time.Second * 30
defaultNumPartitions = 2
defaultReplicationFactor = 3
debugLogTopicsMissingPrefix = false
)
var topicListingBackoffPolicy = backoff.Exponential(
backoff.WithMinInterval(time.Second),
backoff.WithMaxInterval(time.Second*30),
backoff.WithJitterFactor(0.05),
backoff.WithMaxRetries(0),
)
// UnregisteredTopicError is the base error when attempting to create a
// topic that isn't pre-preregistered when pre-registration is required.
const UnregisteredTopicError errors.String = "topic is not pre-registered"
// SetTopicConfig can be used to override the configuration parameters
// for new topics. If no override has been set, then the default configuration
// for new topics is simply: 2 partitions. High volume topics should use 10
// or even 20 partitions.
//
// Topics will be auto-created when a message is sent. Topics will be auto-created
// on startup for all topics that are consumed.
func (lib *LibraryNoDB) SetTopicConfig(topicConfig kafka.TopicConfig) {
lib.lock.Lock()
defer lib.lock.Unlock()
if topicConfig.Topic == "" {
panic(errors.Alertf("attempt to register event library topic configuration with an empty topic name"))
}
lib.topicConfig[topicConfig.Topic] = topicConfig
}
func (lib *LibraryNoDB) getTopicConfig(unprefixedTopic string) (kafka.TopicConfig, bool) {
lib.lock.Lock()
defer lib.lock.Unlock()
c, ok := lib.topicConfig[unprefixedTopic]
return c, ok
}
// ValidateTopics will be fast whenever it can be fast. Sometimes it will
// wait for topics to be listed. ValidateTopics topics can only be used after Configure.
func (lib *Library[ID, TX, DB]) ValidateTopics(ctx context.Context, unprefixedTopics []string) error {
err := lib.start(ctx, "validate topics")
if err != nil {
return err
}
if !lib.mustRegisterTopics {
return nil
}
for _, unprefixedTopic := range unprefixedTopics {
if _, ok := lib.getTopicConfig(unprefixedTopic); ok {
continue
}
if unprefixedTopic == heartbeatTopic.Topic() {
continue
}
if err := lib.waitForTopicsListing(ctx); err != nil {
return err
}
switch lib.topicsWork.GetState(unprefixedTopic) {
case pwork.ItemDone:
continue
case pwork.ItemDoesNotExist:
return errors.Errorf("topic (%s) is invalid", unprefixedTopic)
default:
return errors.Errorf("topic (%s) is invalid, or at least not created yet", unprefixedTopic)
}
}
return nil
}
func (lib *LibraryNoDB) precreateTopicsForConsuming(ctx context.Context, consumerGroup consumerGroupName, unprefixedTopics []string) error {
return lib.topicsWork.WorkUntilDone(ctx, unprefixedTopics, topicsWhy{
why: "consume with " + consumerGroup.String(),
errorCategory: "preCreateTopicsForConsume",
})
}
func (lib *LibraryNoDB) configureTopicsPrework() {
lib.topicsWork.MaxSimultaneous = 20
lib.topicsWork.BackoffPolicy = backoffPolicy
lib.topicsWork.ThreadContext = lib.threadContext
lib.topicsWork.WorkDeadline = topicCreationDeadline
lib.topicsWork.ItemRetryDelay = 5 * time.Second
lib.topicsWork.ErrorReporter = func(ctx context.Context, err error, why topicsWhy) {
_ = lib.RecordErrorNoWait(ctx, why.errorCategory, err)
}
lib.topicsWork.IsFatalError = func(err error) bool {
return errors.Is(err, UnregisteredTopicError)
}
lib.topicsWork.ClearedUp = func(ctx context.Context, _ error, why topicsWhy, unprefixedTopics []string) {
lib.logf(ctx, "[events] prior error creating topics %v, preventing %s, has cleared up", unprefixedTopics, why.why)
}
lib.topicsWork.FirstWorkMessage = func(ctx context.Context, _ topicsWhy, unprefixedTopic string) {
lib.logf(ctx, "done waiting for topic listing to complete (%s needs to be created)", unprefixedTopic)
}
lib.topicsWork.NotRetryingError = func(ctx context.Context, unprefixedTopic string, why topicsWhy, err error) error {
err = errors.Errorf("event library topic (%s) creation failed (%s): %w", unprefixedTopic, why.why, err)
lib.logf(ctx, "[events] %s: %+v", why.why, err)
return err
}
lib.topicsWork.RetryingOrNot = func(ctx context.Context, doCreate bool, unprefixedTopic string, why topicsWhy) {
if doCreate {
lib.logf(ctx, "[events] %s: will re-attempt creation of topic %s, previous attempt failed", why.why, unprefixedTopic)
} else {
lib.logf(ctx, "[events] %s: will NOT re-attempt creation of topic %s yet, previous attempt failed", why.why, unprefixedTopic)
}
}
lib.topicsWork.ItemPreWork = func(ctx context.Context, unprefixedTopic string, why topicsWhy) error {
_, ok := lib.getTopicConfig(unprefixedTopic)
if lib.mustRegisterTopics && !ok && unprefixedTopic != heartbeatTopic.Topic() {
lib.logf(ctx, "[events] %s: requested topic, %s, not pre-registered", why.why, unprefixedTopic)
return UnregisteredTopicError.Errorf("event library attempt to create topic (%s) that was not pre-registered (%s)", unprefixedTopic, why.why)
}
return nil
}
lib.topicsWork.ItemWork = func(ctx context.Context, unprefixedTopic string, why topicsWhy) error {
tc, _ := lib.getTopicConfig(unprefixedTopic)
prefixedTopic := lib.addPrefix(unprefixedTopic)
tc.Topic = prefixedTopic
if tc.NumPartitions == 0 {
tc.NumPartitions = defaultNumPartitions
}
if tc.ReplicationFactor == 0 {
tc.ReplicationFactor = defaultReplicationFactor
}
if tc.ReplicationFactor > len(lib.brokers) {
tc.ReplicationFactor = len(lib.brokers)
}
mir := getIntConfigValue(tc, "min.insync.replicas")
if mir <= 0 || mir >= int64(tc.ReplicationFactor) {
mir = int64(tc.ReplicationFactor) - 1
if mir == 0 {
mir = 1
}
tc.ConfigEntries = setIntConfigValue(tc, "min.insync.replicas", mir)
}
tsti := generic.FirstMatchIndex(tc.ConfigEntries, func(e kafka.ConfigEntry) bool { return e.ConfigName == "message.timestamp.type" })
if tsti < 0 {
tc.ConfigEntries = append(tc.ConfigEntries, kafka.ConfigEntry{
ConfigName: "message.timestamp.type",
ConfigValue: "LogAppendTime",
})
}
mir = getIntConfigValue(tc, "min.insync.replicas")
var ctr kafka.CreateTopicsRequest
ctr.Topics = append(ctr.Topics, tc)
lib.logf(ctx, "[events] %s: attempting creation of topic %s with replicas %d and min.insync %d", why.why, prefixedTopic, tc.ReplicationFactor, mir)
client, err := lib.getController(ctx)
if err == nil {
lib.logf(ctx, "[events] %s: making topic creation request for %v", why.why, prefixedTopic)
var resp *kafka.CreateTopicsResponse
resp, err = client.CreateTopics(ctx, &ctr)
if err == nil {
err = resp.Errors[prefixedTopic]
switch {
case err == nil:
lib.logf(ctx, "[events] %s: topic %s no error when creating", why.why, prefixedTopic)
case errors.Is(err, kafka.TopicAlreadyExists):
lib.logf(ctx, "[events] %s: topic %s already exists", why.why, prefixedTopic)
err = nil
default:
// uh, oh. Handled later
}
for tpc, topicErr := range resp.Errors {
if tpc != prefixedTopic {
lib.logf(ctx, "[event] received create topic response for topic (%s) not in request (%s %s): %s", tpc, why.why, prefixedTopic, topicErr)
}
}
}
if resp.Throttle != 0 {
lib.logf(ctx, "[events] %s: topic creation request was throttled for %s", why.why, resp.Throttle)
}
}
return err
}
lib.topicsWork.ItemDone = func(ctx context.Context, unprefixedTopic string, why topicsWhy) {
lib.logf(ctx, "[events] %s: topic %s should now exist", why.why, unprefixedTopic)
}
lib.topicsWork.ItemFailed = func(ctx context.Context, unprefixedTopic string, why topicsWhy, err error, primary bool) error {
err = errors.Errorf("event library error creating topic (%s) (%s): %w", unprefixedTopic, why.why, err)
if primary {
err = errors.Alert(err)
}
lib.logf(ctx, "[events] %+v", err)
return err
}
lib.topicsWork.ItemTimeoutError = func(_ context.Context, unprefixedTopic string, why topicsWhy, _ error) error {
return errors.Errorf("event library could not create kafka topic (%s) (%s): %w", unprefixedTopic, why.why, ErrTopicCreationTimeout)
}
lib.topicsWork.ItemPending = func(ctx context.Context, unprefixedTopic string, why topicsWhy) {
lib.logf(ctx, "[events] %s: will wait for creation attempt of topic %s to complete", why.why, unprefixedTopic)
}
lib.topicsWork.PreWork = func(ctx context.Context, why topicsWhy, unprefixedTopics []string) error {
if lib.ready.Load() == isNotConfigured {
err := errors.Alertf("attempt to create topics before library configuration (%s)", why.why)
lib.logf(ctx, "[events] %s: %+v", why.why, err)
panic(err)
}
for _, unprefixedTopic := range unprefixedTopics {
if unprefixedTopic == "" {
err := errors.Errorf("cannot create an empty topic (%s) in event library", why.why)
lib.logf(ctx, "[events] %s: %+v", why.why, err)
return err
}
}
if err := lib.waitForTopicsListing(ctx); err != nil {
return err
}
return nil
}
lib.topicsWork.SpanMapItem = func(_ context.Context, topic string, why topicsWhy) map[string]string {
return map[string]string{
"action": "thread",
"thread": "create topic " + topic + " for " + why.why,
}
}
}
func (lib *LibraryNoDB) listAvailableTopics(ctx context.Context) error {
dialer := lib.dialer()
b := topicListingBackoffPolicy.Start(ctx)
for backoff.Continue(b) {
lib.logf(ctx, "[events] starting over on listing topics")
for _, i := range rand.Perm(len(lib.brokers)) {
broker := lib.brokers[i]
lib.logf(ctx, "[events] connecting to %s to list topics", broker)
conn, err := dialer.DialContext(ctx, "tcp", broker)
if err != nil {
lib.logf(ctx, "[events] could not connect to broker %s, was going to list topics: %v", broker, err)
continue
}
partitions, err := conn.ReadPartitions()
_ = conn.Close()
if err != nil {
lib.logf(ctx, "[events] could not list partitions on broker %s: %v", broker, err)
continue
}
lib.logf(ctx, "[events] listing existing topics...")
seen := make(map[string]bool)
for _, p := range partitions {
if seen[p.Topic] {
continue
}
seen[p.Topic] = true
unprefixedTopic := lib.removePrefix(p.Topic)
if lib.prefix != "" && unprefixedTopic == p.Topic {
if debugLogTopicsMissingPrefix {
lib.logf(ctx, "[events] topic %s found in partition, IGNORING (not prefixed)", p.Topic)
}
continue
}
lib.logf(ctx, "[events] topic %s found in partition", unprefixedTopic)
lib.topicsWork.SetDone(unprefixedTopic)
}
lib.logf(ctx, "[events] done listing existing topics")
return nil
}
lib.logf(ctx, "[events] waiting before making another attempt to list topics")
}
if err := ctx.Err(); err != nil {
return errors.Errorf("event library could not list kafka topics from any broker: %w", err)
}
return errors.Errorf("event library could not list kafka topics from any broker")
}
func (lib *LibraryNoDB) waitForTopicsListing(ctx context.Context) error {
lib.topicListingStarted.Do(func() {
// The listing thread is library-owned. Individual callers may stop waiting
// via ctx, but must not cancel the one shared listing attempt.
threadCtx, threadDone := lib.threadContext(lib.shutdownCtx, map[string]string{
"action": "thread",
"thread": "list available topics",
})
go func() {
defer threadDone()
lib.topicsListingErr = lib.listAvailableTopics(threadCtx)
close(lib.topicsHaveBeenListed)
}()
})
select {
case <-lib.topicsHaveBeenListed:
return lib.topicsListingErr
case <-ctx.Done():
select {
case <-lib.topicsHaveBeenListed:
return lib.topicsListingErr
default:
return ctx.Err()
}
}
}
// CreateTopics orechestrates the creation of topics that have not already been successfully
// created. The set of created topics is in lib.topicsSeen. It is expected that createTopics
// will be called simultaneously from multiple threads. Its behavior is optimized to do
// minimal work and to return almost instantly if there are no topics that need creating.
func (lib *LibraryNoDB) CreateTopics(ctx context.Context, why string, unprefixedTopics []string) error {
return lib.topicsWork.Work(ctx, unprefixedTopics, topicsWhy{
why: why,
errorCategory: "createTopics",
})
}
var ErrTopicCreationTimeout errors.String = "event library topic creation deadline exceeded"
func getIntConfigValue(tc kafka.TopicConfig, configName string) int64 {
i := generic.FirstMatchIndex(tc.ConfigEntries, func(e kafka.ConfigEntry) bool { return e.ConfigName == configName })
if i >= 0 {
v, _ := strconv.ParseInt(tc.ConfigEntries[i].ConfigValue, 10, 64)
return v
}
return 0
}
func setIntConfigValue(tc kafka.TopicConfig, configName string, value int64) []kafka.ConfigEntry {
return generic.ReplaceOrAppend(tc.ConfigEntries, kafka.ConfigEntry{
ConfigName: configName,
ConfigValue: strconv.FormatInt(value, 10),
}, func(e kafka.ConfigEntry) bool { return e.ConfigName == configName })
}