@@ -57,9 +57,11 @@ func (s *SamplerFactory) updatePeerCounts() {
5757 // Update goal throughput for all throughput-based dynsamplers
5858 for dynsamplerKey , dynsamplerInstance := range s .sharedDynsamplers {
5959 if hasThroughput , ok := dynsamplerInstance .(CanSetGoalThroughputPerSec ); ok {
60- // Calculate new throughput based on cluster size
61- newThroughput := max (s .goalThroughputConfigs [dynsamplerKey ]/ s .peerCount , 1 )
62- hasThroughput .SetGoalThroughputPerSec (newThroughput )
60+ if cfg , ok := s .goalThroughputConfigs [dynsamplerKey ]; ok {
61+ // Calculate new throughput based on cluster size
62+ newThroughput := max (cfg / s .peerCount , 1 )
63+ hasThroughput .SetGoalThroughputPerSec (newThroughput )
64+ }
6365 }
6466 }
6567}
@@ -92,7 +94,12 @@ func getSharedDynsampler[ST any, CT any](
9294 return dynsamplerInstance
9395}
9496
95- // createSampler creates a sampler with shared dynsamplers based on the config type
97+ // createSampler creates a sampler with shared dynsamplers based on the config type.
98+ // A unique dynsampler is created based on a composite key that includes the keyPrefix
99+ // (dataset/environment), sampler type, and configuration parameters (e.g., sample rate
100+ // and field list). This ensures that samplers with identical configurations share the
101+ // same underlying dynsampler instance, guaranteeing consistent sampling decisions across
102+ // parallel collector workers within a single Refinery instance.
96103func (s * SamplerFactory ) createSampler (c any , keyPrefix string ) Sampler {
97104 var sampler Sampler
98105
@@ -112,24 +119,32 @@ func (s *SamplerFactory) createSampler(c any, keyPrefix string) Sampler {
112119 case * config.TotalThroughputSamplerConfig :
113120 dynsamplerKey := fmt .Sprintf ("%s:totalthroughput:%d:%v" , keyPrefix , c .GoalThroughputPerSec , c .FieldList )
114121 dynsamplerInstance := getSharedDynsampler (s , dynsamplerKey , c , createDynForTotalThroughputSampler )
115- // Store goal throughput config under mutex protection
116- s .mutex .Lock ()
117- s .goalThroughputConfigs [dynsamplerKey ] = c .GoalThroughputPerSec
118- s .mutex .Unlock ()
122+ // only track goal throughput config if we need to recalculate it later based on cluster size
123+ if c .UseClusterSize {
124+ s .mutex .Lock ()
125+ s .goalThroughputConfigs [dynsamplerKey ] = c .GoalThroughputPerSec
126+ s .mutex .Unlock ()
127+ }
119128 sampler = & TotalThroughputSampler {Config : c , Logger : s .Logger , Metrics : s .Metrics , dynsampler : dynsamplerInstance }
120129 case * config.EMAThroughputSamplerConfig :
121130 dynsamplerKey := fmt .Sprintf ("%s:emathroughput:%d:%v" , keyPrefix , c .GoalThroughputPerSec , c .FieldList )
122131 dynsamplerInstance := getSharedDynsampler (s , dynsamplerKey , c , createDynForEMAThroughputSampler )
123- s .mutex .Lock ()
124- s .goalThroughputConfigs [dynsamplerKey ] = c .GoalThroughputPerSec
125- s .mutex .Unlock ()
132+ // only track goal throughput config if we need to recalculate it later based on cluster size
133+ if c .UseClusterSize {
134+ s .mutex .Lock ()
135+ s .goalThroughputConfigs [dynsamplerKey ] = c .GoalThroughputPerSec
136+ s .mutex .Unlock ()
137+ }
126138 sampler = & EMAThroughputSampler {Config : c , Logger : s .Logger , Metrics : s .Metrics , dynsampler : dynsamplerInstance }
127139 case * config.WindowedThroughputSamplerConfig :
128140 dynsamplerKey := fmt .Sprintf ("%s:windowedthroughput:%d:%v" , keyPrefix , c .GoalThroughputPerSec , c .FieldList )
129141 dynsamplerInstance := getSharedDynsampler (s , dynsamplerKey , c , createDynForWindowedThroughputSampler )
130- s .mutex .Lock ()
131- s .goalThroughputConfigs [dynsamplerKey ] = c .GoalThroughputPerSec
132- s .mutex .Unlock ()
142+ // only track goal throughput config if we need to recalculate it later based on cluster size
143+ if c .UseClusterSize {
144+ s .mutex .Lock ()
145+ s .goalThroughputConfigs [dynsamplerKey ] = c .GoalThroughputPerSec
146+ s .mutex .Unlock ()
147+ }
133148 sampler = & WindowedThroughputSampler {Config : c , Logger : s .Logger , Metrics : s .Metrics , dynsampler : dynsamplerInstance }
134149 default :
135150 s .Logger .Error ().Logf ("unknown sampler type %T. Exiting." , c )
0 commit comments