@@ -305,14 +305,30 @@ private List<Step> parseStepsFromPhase(String policy, String currentPhase, Strin
305
305
return phaseSteps ;
306
306
}
307
307
308
+ /**
309
+ * Read-only internal helper for getStep that returns a non-null step if one is cached for the provided
310
+ * IndexMetadata and StepKey, and null otherwise.
311
+ */
308
312
@ Nullable
309
- public Step getStep (final IndexMetadata indexMetadata , final Step .StepKey stepKey ) {
313
+ private Step getCachedStep (final IndexMetadata indexMetadata , final Step .StepKey stepKey ) {
310
314
final Tuple <IndexMetadata , Step > cachedStep = cachedSteps .get (indexMetadata .getIndex ());
311
315
// n.b. we're using instance equality here for the IndexMetadata rather than object equality because it's fast,
312
316
// this means that we're erring on the side of cache misses (if the IndexMetadata changed in any way, it'll be
313
317
// a new instance, so we'll miss-and-repopulate the cache for the index in question)
314
- if (cachedStep != null && cachedStep .v1 () == indexMetadata && cachedStep .v2 ().getKey ().equals (stepKey )) {
315
- return cachedStep .v2 ();
318
+ if (cachedStep != null && cachedStep .v1 () == indexMetadata ) {
319
+ assert cachedStep .v2 () != null : "null steps should never be cached in the policy step registry" ;
320
+ if (cachedStep .v2 () != null && cachedStep .v2 ().getKey ().equals (stepKey )) {
321
+ return cachedStep .v2 ();
322
+ }
323
+ }
324
+ return null ;
325
+ }
326
+
327
+ @ Nullable
328
+ public Step getStep (final IndexMetadata indexMetadata , final Step .StepKey stepKey ) {
329
+ final Step cachedStep = getCachedStep (indexMetadata , stepKey );
330
+ if (cachedStep != null ) {
331
+ return cachedStep ;
316
332
}
317
333
318
334
if (ErrorStep .NAME .equals (stepKey .getName ())) {
@@ -354,7 +370,12 @@ public Step getStep(final IndexMetadata indexMetadata, final Step.StepKey stepKe
354
370
355
371
// Return the step that matches the given stepKey or else null if we couldn't find it
356
372
final Step s = phaseSteps .stream ().filter (step -> step .getKey ().equals (stepKey )).findFirst ().orElse (null );
357
- cachedSteps .put (indexMetadata .getIndex (), Tuple .tuple (indexMetadata , s ));
373
+ if (s != null ) {
374
+ cachedSteps .put (indexMetadata .getIndex (), Tuple .tuple (indexMetadata , s ));
375
+ // assert that the cache works as expected -- that is, if we put something into the cache,
376
+ // we should get back the same thing if we were to invoke getStep again with the same arguments
377
+ assert s == getCachedStep (indexMetadata , stepKey ) : "policy step registry cache failed sanity check" ;
378
+ }
358
379
return s ;
359
380
}
360
381
0 commit comments