Skip to content

Commit

Permalink
single flights topic creation; makes duplicate async
Browse files Browse the repository at this point in the history
  • Loading branch information
owen-d committed Jan 29, 2025
1 parent 904fa68 commit 3f36579
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions pkg/distributor/tenant_topic_tee.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/twmb/franz-go/pkg/kadm"
"github.com/twmb/franz-go/pkg/kerr"
"github.com/twmb/franz-go/pkg/kgo"
"golang.org/x/sync/singleflight"

"github.com/grafana/loki/v3/pkg/kafka"
"github.com/grafana/loki/v3/pkg/kafka/client"
Expand Down Expand Up @@ -156,6 +157,7 @@ type ShardedPartitionResolver struct {
admin *kadm.Client
topicPrefix string

sflight singleflight.Group // for topic creation
// tenantShards maps tenant IDs to their active shards
// map[tenant]map[shard]struct{}
tenantShards map[string]map[int32]struct{}
Expand Down Expand Up @@ -204,15 +206,15 @@ func (r *ShardedPartitionResolver) createShard(ctx context.Context, tenant strin
topic := r.topicName(tenant, shard)
replicationFactor := 2 // TODO: expose RF

// TODO(owen-d): ensure this only runs once
// at a time.
_, err := r.admin.CreateTopic(
ctx,
1,
int16(replicationFactor),
nil,
topic,
)
_, err, _ := r.sflight.Do(topic, func() (interface{}, error) {
return r.admin.CreateTopic(
ctx,
1,
int16(replicationFactor),
nil,
topic,
)
})

// Topic creation errors are returned in the response
if err != nil && !errors.Is(err, kerr.TopicAlreadyExists) {
Expand Down Expand Up @@ -320,6 +322,10 @@ func (t *TenantTopicWriter) partitionsForRateLimit(bytesRateLimit float64) uint3

// Duplicate implements the Tee interface
func (t *TenantTopicWriter) Duplicate(tenant string, streams []KeyedStream) {
go t.write(tenant, streams)
}

func (t *TenantTopicWriter) write(tenant string, streams []KeyedStream) {
if len(streams) == 0 {
return
}
Expand Down

0 comments on commit 3f36579

Please sign in to comment.