1010import org .elasticsearch .action .index .IndexRequest ;
1111import org .elasticsearch .action .support .PlainActionFuture ;
1212import org .elasticsearch .client .internal .OriginSettingClient ;
13+ import org .elasticsearch .common .settings .Settings ;
1314import org .elasticsearch .common .unit .ByteSizeValue ;
1415import org .elasticsearch .indices .TestIndexNameExpressionResolver ;
1516import org .elasticsearch .tasks .TaskId ;
@@ -64,8 +65,53 @@ public void createComponents() {
6465
6566 public void testRemoveUnusedStats () throws Exception {
6667
67- prepareIndex ("foo" ).setId ("some-empty-doc" ).setSource ("{}" , XContentType .JSON ).get ();
68+ String modelId = "model-with-stats" ;
69+ putDFA (modelId );
70+
71+
72+
73+ indexStatDocument (new DataCounts ("analytics-with-stats" , 1 , 1 , 1 ), DataCounts .documentId ("analytics-with-stats" ));
74+ indexStatDocument (new DataCounts ("missing-analytics-with-stats" , 1 , 1 , 1 ), DataCounts .documentId ("missing-analytics-with-stats" ));
75+ indexStatDocument (
76+ new InferenceStats (1 , 1 , 1 , 1 , TrainedModelProvider .MODELS_STORED_AS_RESOURCE .iterator ().next (), "test" , Instant .now ()),
77+ InferenceStats .docId (TrainedModelProvider .MODELS_STORED_AS_RESOURCE .iterator ().next (), "test" )
78+ );
79+ indexStatDocument (
80+ new InferenceStats (1 , 1 , 1 , 1 , "missing-model" , "test" , Instant .now ()),
81+ InferenceStats .docId ("missing-model" , "test" )
82+ );
83+ indexStatDocument (
84+ new InferenceStats (1 , 1 , 1 , 1 , modelId , "test" , Instant .now ()),
85+ InferenceStats .docId (modelId , "test" )
86+ );
87+ client ().admin ().indices ().prepareRefresh (MlStatsIndex .indexPattern ()).get ();
88+
89+ PlainActionFuture <Boolean > deletionListener = new PlainActionFuture <>();
90+ UnusedStatsRemover statsRemover = new UnusedStatsRemover (client , new TaskId ("test" , 0L ));
91+ statsRemover .remove (10000.0f , deletionListener , () -> false );
92+ deletionListener .actionGet ();
93+
94+ client ().admin ().indices ().prepareRefresh (MlStatsIndex .indexPattern ()).get ();
95+
96+ final String initialStateIndex = MlStatsIndex .TEMPLATE_NAME + "-000001" ;
97+
98+ // Make sure that stats that should exist still exist
99+ assertTrue (client ().prepareGet (initialStateIndex , InferenceStats .docId (modelId , "test" )).get ().isExists ());
100+ assertTrue (
101+ client ().prepareGet (
102+ initialStateIndex ,
103+ InferenceStats .docId (TrainedModelProvider .MODELS_STORED_AS_RESOURCE .iterator ().next (), "test" )
104+ ).get ().isExists ()
105+ );
106+ assertTrue (client ().prepareGet (initialStateIndex , DataCounts .documentId ("analytics-with-stats" )).get ().isExists ());
68107
108+ // make sure that unused stats were deleted
109+ assertFalse (client ().prepareGet (initialStateIndex , DataCounts .documentId ("missing-analytics-with-stats" )).get ().isExists ());
110+ assertFalse (client ().prepareGet (initialStateIndex , InferenceStats .docId ("missing-model" , "test" )).get ().isExists ());
111+ }
112+
113+ private void putDFA (String modelId ) {
114+ prepareIndex ("foo" ).setId ("some-empty-doc" ).setSource ("{}" , XContentType .JSON ).get ();
69115 PutDataFrameAnalyticsAction .Request request = new PutDataFrameAnalyticsAction .Request (
70116 new DataFrameAnalyticsConfig .Builder ().setId ("analytics-with-stats" )
71117 .setModelMemoryLimit (ByteSizeValue .ofGb (1 ))
@@ -80,7 +126,7 @@ public void testRemoveUnusedStats() throws Exception {
80126 PutTrainedModelAction .INSTANCE ,
81127 new PutTrainedModelAction .Request (
82128 TrainedModelConfig .builder ()
83- .setModelId ("model-with-stats" )
129+ .setModelId (modelId )
84130 .setInferenceConfig (RegressionConfig .EMPTY_PARAMS )
85131 .setInput (new TrainedModelInput (Arrays .asList ("foo" , "bar" )))
86132 .setParsedDefinition (
@@ -97,21 +143,21 @@ public void testRemoveUnusedStats() throws Exception {
97143 false
98144 )
99145 ).actionGet ();
146+ }
147+
148+ public void testRemovingUnusedStatsFromReadOnlyIndexShouldFailSilently () throws Exception {
149+
150+ String modelId = "model-with-stats" ;
151+ putDFA (modelId );
100152
101- indexStatDocument (new DataCounts ("analytics-with-stats" , 1 , 1 , 1 ), DataCounts .documentId ("analytics-with-stats" ));
102- indexStatDocument (new DataCounts ("missing-analytics-with-stats" , 1 , 1 , 1 ), DataCounts .documentId ("missing-analytics-with-stats" ));
103- indexStatDocument (
104- new InferenceStats (1 , 1 , 1 , 1 , TrainedModelProvider .MODELS_STORED_AS_RESOURCE .iterator ().next (), "test" , Instant .now ()),
105- InferenceStats .docId (TrainedModelProvider .MODELS_STORED_AS_RESOURCE .iterator ().next (), "test" )
106- );
107153 indexStatDocument (
108154 new InferenceStats (1 , 1 , 1 , 1 , "missing-model" , "test" , Instant .now ()),
109155 InferenceStats .docId ("missing-model" , "test" )
110156 );
111- indexStatDocument (
112- new InferenceStats ( 1 , 1 , 1 , 1 , "model-with-stats" , "test" , Instant . now ()),
113- InferenceStats . docId ( "model-with-stats " , "test" )
114- );
157+
158+ // set index to read-only
159+ client (). admin (). indices (). prepareUpdateSettings ( MlStatsIndex . indexPattern ()). setSettings ( Settings . builder (). put ( "index.blocks.write " , true )). get ();
160+
115161 client ().admin ().indices ().prepareRefresh (MlStatsIndex .indexPattern ()).get ();
116162
117163 PlainActionFuture <Boolean > deletionListener = new PlainActionFuture <>();
@@ -120,22 +166,9 @@ public void testRemoveUnusedStats() throws Exception {
120166 deletionListener .actionGet ();
121167
122168 client ().admin ().indices ().prepareRefresh (MlStatsIndex .indexPattern ()).get ();
123-
169+ // make sure that unused stats are still there
124170 final String initialStateIndex = MlStatsIndex .TEMPLATE_NAME + "-000001" ;
125-
126- // Make sure that stats that should exist still exist
127- assertTrue (client ().prepareGet (initialStateIndex , InferenceStats .docId ("model-with-stats" , "test" )).get ().isExists ());
128- assertTrue (
129- client ().prepareGet (
130- initialStateIndex ,
131- InferenceStats .docId (TrainedModelProvider .MODELS_STORED_AS_RESOURCE .iterator ().next (), "test" )
132- ).get ().isExists ()
133- );
134- assertTrue (client ().prepareGet (initialStateIndex , DataCounts .documentId ("analytics-with-stats" )).get ().isExists ());
135-
136- // make sure that unused stats were deleted
137- assertFalse (client ().prepareGet (initialStateIndex , DataCounts .documentId ("missing-analytics-with-stats" )).get ().isExists ());
138- assertFalse (client ().prepareGet (initialStateIndex , InferenceStats .docId ("missing-model" , "test" )).get ().isExists ());
171+ assertTrue (client ().prepareGet (initialStateIndex , InferenceStats .docId ("missing-model" , "test" )).get ().isExists ());
139172 }
140173
141174 private void indexStatDocument (ToXContentObject object , String docId ) throws Exception {
0 commit comments