@@ -138,74 +138,76 @@ private void maybeSample(
138138 }
139139 SamplingConfiguration samplingConfig = samplingMetadata .getIndexToSamplingConfigMap ().get (indexName );
140140 ProjectId projectId = projectMetadata .id ();
141- if (samplingConfig != null ) {
142- SoftReference <SampleInfo > sampleInfoReference = samples .compute (
143- new ProjectIndex (projectId , indexName ),
144- (k , v ) -> v == null || v .get () == null
145- ? new SoftReference <>(
146- new SampleInfo (samplingConfig .maxSamples (), samplingConfig .timeToLive (), relativeMillisTimeSupplier .getAsLong ())
147- )
148- : v
149- );
150- SampleInfo sampleInfo = sampleInfoReference .get ();
151- if (sampleInfo != null ) {
152- SampleStats stats = sampleInfo .stats ;
153- stats .potentialSamples .increment ();
154- try {
155- if (sampleInfo .hasCapacity ()) {
156- if (random .nextDouble () < samplingConfig .rate ()) {
157- String condition = samplingConfig .condition ();
158- if (condition != null ) {
159- if (sampleInfo .script == null || sampleInfo .factory == null ) {
160- // We don't want to pay for synchronization because worst case, we compile the script twice
161- long compileScriptStartTime = statsTimeSupplier .getAsLong ();
162- try {
163- if (sampleInfo .compilationFailed ) {
164- // we don't want to waste time -- if the script failed to compile once it will just fail again
165- stats .samplesRejectedForException .increment ();
166- return ;
167- } else {
168- Script script = getScript (condition );
169- sampleInfo .setScript (script , scriptService .compile (script , IngestConditionalScript .CONTEXT ));
170- }
171- } catch (Exception e ) {
172- sampleInfo .compilationFailed = true ;
173- throw e ;
174- } finally {
175- stats .timeCompilingCondition .add ((statsTimeSupplier .getAsLong () - compileScriptStartTime ));
176- }
177- }
178- }
179- if (condition == null
180- || evaluateCondition (ingestDocumentSupplier , sampleInfo .script , sampleInfo .factory , sampleInfo .stats )) {
181- RawDocument sample = getRawDocumentForIndexRequest (projectId , indexName , indexRequest );
182- if (sampleInfo .offer (sample )) {
183- stats .samples .increment ();
184- logger .trace ("Sampling " + indexRequest );
185- } else {
186- stats .samplesRejectedForMaxSamplesExceeded .increment ();
187- }
188- } else {
189- stats .samplesRejectedForCondition .increment ();
190- }
141+ if (samplingConfig == null ) {
142+ return ;
143+ }
144+ SoftReference <SampleInfo > sampleInfoReference = samples .compute (
145+ new ProjectIndex (projectId , indexName ),
146+ (k , v ) -> v == null || v .get () == null
147+ ? new SoftReference <>(
148+ new SampleInfo (samplingConfig .maxSamples (), samplingConfig .timeToLive (), relativeMillisTimeSupplier .getAsLong ())
149+ )
150+ : v
151+ );
152+ SampleInfo sampleInfo = sampleInfoReference .get ();
153+ if (sampleInfo == null ) {
154+ return ;
155+ }
156+ SampleStats stats = sampleInfo .stats ;
157+ stats .potentialSamples .increment ();
158+ try {
159+ if (sampleInfo .hasCapacity () == false ) {
160+ stats .samplesRejectedForMaxSamplesExceeded .increment ();
161+ return ;
162+ }
163+ if (random .nextDouble () >= samplingConfig .rate ()) {
164+ stats .samplesRejectedForRate .increment ();
165+ return ;
166+ }
167+ String condition = samplingConfig .condition ();
168+ if (condition != null ) {
169+ if (sampleInfo .script == null || sampleInfo .factory == null ) {
170+ // We don't want to pay for synchronization because worst case, we compile the script twice
171+ long compileScriptStartTime = statsTimeSupplier .getAsLong ();
172+ try {
173+ if (sampleInfo .compilationFailed ) {
174+ // we don't want to waste time -- if the script failed to compile once it will just fail again
175+ stats .samplesRejectedForException .increment ();
176+ return ;
191177 } else {
192- stats .samplesRejectedForRate .increment ();
178+ Script script = getScript (condition );
179+ sampleInfo .setScript (script , scriptService .compile (script , IngestConditionalScript .CONTEXT ));
193180 }
194- } else {
195- stats .samplesRejectedForMaxSamplesExceeded .increment ();
181+ } catch (Exception e ) {
182+ sampleInfo .compilationFailed = true ;
183+ throw e ;
184+ } finally {
185+ stats .timeCompilingCondition .add ((statsTimeSupplier .getAsLong () - compileScriptStartTime ));
196186 }
197- } catch (Exception e ) {
198- stats .samplesRejectedForException .increment ();
199- /*
200- * We potentially overwrite a previous exception here. But the thinking is that the user will pretty rapidly iterate on
201- * exceptions as they come up, and this avoids the overhead and complexity of keeping track of multiple exceptions.
202- */
203- stats .lastException = e ;
204- logger .debug ("Error performing sampling for " + indexName , e );
205- } finally {
206- stats .timeSampling .add ((statsTimeSupplier .getAsLong () - startTime ));
207187 }
208188 }
189+ if (condition != null
190+ && evaluateCondition (ingestDocumentSupplier , sampleInfo .script , sampleInfo .factory , sampleInfo .stats ) == false ) {
191+ stats .samplesRejectedForCondition .increment ();
192+ return ;
193+ }
194+ RawDocument sample = getRawDocumentForIndexRequest (projectId , indexName , indexRequest );
195+ if (sampleInfo .offer (sample )) {
196+ stats .samples .increment ();
197+ logger .trace ("Sampling " + indexRequest );
198+ } else {
199+ stats .samplesRejectedForMaxSamplesExceeded .increment ();
200+ }
201+ } catch (Exception e ) {
202+ stats .samplesRejectedForException .increment ();
203+ /*
204+ * We potentially overwrite a previous exception here. But the thinking is that the user will pretty rapidly iterate on
205+ * exceptions as they come up, and this avoids the overhead and complexity of keeping track of multiple exceptions.
206+ */
207+ stats .lastException = e ;
208+ logger .debug ("Error performing sampling for " + indexName , e );
209+ } finally {
210+ stats .timeSampling .add ((statsTimeSupplier .getAsLong () - startTime ));
209211 }
210212 }
211213
0 commit comments