1919import org .elasticsearch .common .settings .Setting ;
2020import org .elasticsearch .common .settings .Settings ;
2121import org .elasticsearch .core .TimeValue ;
22+ import org .elasticsearch .features .FeatureService ;
2223import org .elasticsearch .inference .InferenceServiceRegistry ;
2324import org .elasticsearch .inference .Model ;
25+ import org .elasticsearch .xpack .inference .InferenceFeatures ;
2426
2527import java .util .Collection ;
2628import java .util .List ;
29+ import java .util .function .Supplier ;
2730
2831/**
2932 * A registry that assembles and caches Inference Endpoints, {@link Model}, for reuse.
@@ -64,14 +67,16 @@ public static Collection<? extends Setting<?>> getSettingsDefinitions() {
6467 private final InferenceServiceRegistry serviceRegistry ;
6568 private final ProjectResolver projectResolver ;
6669 private final Cache <InferenceIdAndProject , Model > cache ;
67- private volatile boolean cacheEnabled ;
70+ private final Supplier <Boolean > cacheEnabledViaFeature ;
71+ private volatile boolean cacheEnabledViaSetting ;
6872
6973 public InferenceEndpointRegistry (
7074 ClusterService clusterService ,
7175 Settings settings ,
7276 ModelRegistry modelRegistry ,
7377 InferenceServiceRegistry serviceRegistry ,
74- ProjectResolver projectResolver
78+ ProjectResolver projectResolver ,
79+ FeatureService featureService
7580 ) {
7681 this .modelRegistry = modelRegistry ;
7782 this .serviceRegistry = serviceRegistry ;
@@ -80,15 +85,19 @@ public InferenceEndpointRegistry(
8085 .setMaximumWeight (INFERENCE_ENDPOINT_CACHE_WEIGHT .get (settings ))
8186 .setExpireAfterWrite (INFERENCE_ENDPOINT_CACHE_EXPIRY .get (settings ))
8287 .build ();
83- this .cacheEnabled = INFERENCE_ENDPOINT_CACHE_ENABLED .get (settings );
88+ this .cacheEnabledViaFeature = () -> {
89+ var state = clusterService .state ();
90+ return state .clusterRecovered () && featureService .clusterHasFeature (state , InferenceFeatures .INFERENCE_ENDPOINT_CACHE );
91+ };
92+ this .cacheEnabledViaSetting = INFERENCE_ENDPOINT_CACHE_ENABLED .get (settings );
8493
8594 clusterService .getClusterSettings ()
86- .addSettingsUpdateConsumer (INFERENCE_ENDPOINT_CACHE_ENABLED , enabled -> this .cacheEnabled = enabled );
95+ .addSettingsUpdateConsumer (INFERENCE_ENDPOINT_CACHE_ENABLED , enabled -> this .cacheEnabledViaSetting = enabled );
8796 }
8897
8998 public void getEndpoint (String inferenceEntityId , ActionListener <Model > listener ) {
9099 var key = new InferenceIdAndProject (inferenceEntityId , projectResolver .getProjectId ());
91- var cachedModel = cacheEnabled ? cache .get (key ) : null ;
100+ var cachedModel = cacheEnabled () ? cache .get (key ) : null ;
92101 if (cachedModel != null ) {
93102 log .trace ("Retrieved [{}] from cache." , inferenceEntityId );
94103 listener .onResponse (cachedModel );
@@ -98,7 +107,7 @@ public void getEndpoint(String inferenceEntityId, ActionListener<Model> listener
98107 }
99108
100109 void invalidateAll (ProjectId projectId ) {
101- if (cacheEnabled ) {
110+ if (cacheEnabled () ) {
102111 var cacheKeys = cache .keys ().iterator ();
103112 while (cacheKeys .hasNext ()) {
104113 if (cacheKeys .next ().projectId .equals (projectId )) {
@@ -126,23 +135,23 @@ private void loadFromIndex(InferenceIdAndProject idAndProject, ActionListener<Mo
126135 unparsedModel .secrets ()
127136 );
128137
129- if (cacheEnabled ) {
138+ if (cacheEnabled () ) {
130139 cache .put (idAndProject , model );
131140 }
132141 l .onResponse (model );
133142 }));
134143 }
135144
136145 public Cache .Stats stats () {
137- return cacheEnabled ? cache .stats () : EMPTY ;
146+ return cacheEnabled () ? cache .stats () : EMPTY ;
138147 }
139148
140149 public int cacheCount () {
141- return cacheEnabled ? cache .count () : 0 ;
150+ return cacheEnabled () ? cache .count () : 0 ;
142151 }
143152
144153 public boolean cacheEnabled () {
145- return cacheEnabled ;
154+ return cacheEnabledViaSetting && cacheEnabledViaFeature . get () ;
146155 }
147156
148157 private record InferenceIdAndProject (String inferenceEntityId , ProjectId projectId ) {}
0 commit comments