6
6
package org .opensearch .ml .indices ;
7
7
8
8
import static org .opensearch .ml .common .CommonValue .META ;
9
- import static org .opensearch .ml .common .CommonValue .ML_MODEL_INDEX ;
10
- import static org .opensearch .ml .common .CommonValue .ML_TASK_INDEX ;
11
9
import static org .opensearch .ml .common .CommonValue .SCHEMA_VERSION_FIELD ;
12
10
13
11
import java .util .HashMap ;
17
15
import org .opensearch .action .admin .indices .create .CreateIndexRequest ;
18
16
import org .opensearch .action .admin .indices .create .CreateIndexResponse ;
19
17
import org .opensearch .action .admin .indices .mapping .put .PutMappingRequest ;
18
+ import org .opensearch .action .admin .indices .settings .put .UpdateSettingsRequest ;
20
19
import org .opensearch .client .Client ;
21
20
import org .opensearch .cluster .metadata .IndexMetadata ;
22
21
import org .opensearch .cluster .service .ClusterService ;
@@ -38,11 +37,13 @@ public class MLIndicesHandler {
38
37
39
38
ClusterService clusterService ;
40
39
Client client ;
41
-
40
+ private static final Map < String , Object > indexSettings = Map . of ( "index.auto_expand_replicas" , "0-5" );
42
41
private static final Map <String , AtomicBoolean > indexMappingUpdated = new HashMap <>();
42
+
43
43
static {
44
- indexMappingUpdated .put (ML_MODEL_INDEX , new AtomicBoolean (false ));
45
- indexMappingUpdated .put (ML_TASK_INDEX , new AtomicBoolean (false ));
44
+ for (MLIndex mlIndex : MLIndex .values ()) {
45
+ indexMappingUpdated .put (mlIndex .getIndexName (), new AtomicBoolean (false ));
46
+ }
46
47
}
47
48
48
49
public void initModelGroupIndexIfAbsent (ActionListener <Boolean > listener ) {
@@ -83,7 +84,7 @@ public void initMLIndexIfAbsent(MLIndex index, ActionListener<Boolean> listener)
83
84
log .error ("Failed to create index " + indexName , e );
84
85
internalListener .onFailure (e );
85
86
});
86
- CreateIndexRequest request = new CreateIndexRequest (indexName ).mapping (mapping );
87
+ CreateIndexRequest request = new CreateIndexRequest (indexName ).mapping (mapping ). settings ( indexSettings ) ;
87
88
client .admin ().indices ().create (request , actionListener );
88
89
} else {
89
90
log .debug ("index:{} is already created" , indexName );
@@ -98,8 +99,23 @@ public void initMLIndexIfAbsent(MLIndex index, ActionListener<Boolean> listener)
98
99
new PutMappingRequest ().indices (indexName ).source (mapping , XContentType .JSON ),
99
100
ActionListener .wrap (response -> {
100
101
if (response .isAcknowledged ()) {
101
- indexMappingUpdated .get (indexName ).set (true );
102
- internalListener .onResponse (true );
102
+ UpdateSettingsRequest updateSettingRequest = new UpdateSettingsRequest ();
103
+ updateSettingRequest .indices (indexName ).settings (indexSettings );
104
+ client
105
+ .admin ()
106
+ .indices ()
107
+ .updateSettings (updateSettingRequest , ActionListener .wrap (updateResponse -> {
108
+ if (response .isAcknowledged ()) {
109
+ indexMappingUpdated .get (indexName ).set (true );
110
+ internalListener .onResponse (true );
111
+ } else {
112
+ internalListener
113
+ .onFailure (new MLException ("Failed to update index setting for: " + indexName ));
114
+ }
115
+ }, exception -> {
116
+ log .error ("Failed to update index setting for: " + indexName , exception );
117
+ internalListener .onFailure (exception );
118
+ }));
103
119
} else {
104
120
internalListener .onFailure (new MLException ("Failed to update index: " + indexName ));
105
121
}
0 commit comments