Skip to content

Commit 1a1ca73

Browse files
committed
decouple BuildWriters from ConfigureSharding
Signed-off-by: Walther Lee <[email protected]>
1 parent 8195dbe commit 1a1ca73

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

internal/discovery/discovery.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func (r *CRDiscoverer) PollForCacheUpdates(
236236
r.WasUpdated = false
237237
})
238238
// Update metric handler with the new configs.
239-
m.ReconfigureSharding(ctx)
239+
m.BuildWriters(ctx)
240240
}
241241
go func() {
242242
for range t.C {

pkg/metricshandler/metrics_handler.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,34 +71,32 @@ func New(opts *options.Options, kubeClient kubernetes.Interface, storeBuilder ks
7171

7272
// ReconfigureSharding reconfigures sharding with the current shard and totalShards, and
7373
// it's a no-op if both values are 0.
74-
func (m *MetricsHandler) ReconfigureSharding(ctx context.Context) {
75-
m.mtx.RLock()
76-
hasShardsSet := m.curShard != 0 || m.curTotalShards != 0
77-
m.mtx.RUnlock()
78-
if !hasShardsSet {
79-
return
74+
func (m *MetricsHandler) BuildWriters(ctx context.Context) {
75+
m.mtx.Lock()
76+
defer m.mtx.Unlock()
77+
78+
if m.cancel != nil {
79+
m.cancel()
8080
}
81-
m.ConfigureSharding(ctx, m.curShard, m.curTotalShards)
81+
ctx, m.cancel = context.WithCancel(ctx)
82+
m.storeBuilder.WithContext(ctx)
83+
m.metricsWriters = m.storeBuilder.Build()
8284
}
8385

8486
// ConfigureSharding configures sharding. Configuration can be used mutlitple times and
8587
// concurrently.
8688
func (m *MetricsHandler) ConfigureSharding(ctx context.Context, shard int32, totalShards int) {
8789
m.mtx.Lock()
88-
defer m.mtx.Unlock()
8990

90-
if m.cancel != nil {
91-
m.cancel()
92-
}
9391
if totalShards != 1 {
9492
klog.InfoS("Configuring sharding of this instance to be shard index (zero-indexed) out of total shards", "shard", shard, "totalShards", totalShards)
9593
}
96-
ctx, m.cancel = context.WithCancel(ctx)
97-
m.storeBuilder.WithSharding(shard, totalShards)
98-
m.storeBuilder.WithContext(ctx)
99-
m.metricsWriters = m.storeBuilder.Build()
10094
m.curShard = shard
10195
m.curTotalShards = totalShards
96+
m.storeBuilder.WithSharding(shard, totalShards)
97+
m.mtx.Unlock()
98+
99+
m.BuildWriters(ctx)
102100
}
103101

104102
// Run configures the MetricsHandler's sharding and if autosharding is enabled

0 commit comments

Comments
 (0)