38
38
import org .elasticsearch .common .Priority ;
39
39
import org .elasticsearch .common .bytes .BytesReference ;
40
40
import org .elasticsearch .common .inject .Inject ;
41
+ import org .elasticsearch .common .settings .IndexScopedSettings ;
42
+ import org .elasticsearch .common .settings .Setting ;
41
43
import org .elasticsearch .common .settings .Settings ;
42
44
import org .elasticsearch .common .xcontent .XContentHelper ;
43
45
import org .elasticsearch .index .Index ;
61
63
import org .elasticsearch .xpack .core .rollup .action .RollupIndexerAction ;
62
64
63
65
import java .io .IOException ;
64
- import java .time .Instant ;
65
66
import java .util .HashMap ;
66
67
import java .util .List ;
67
68
import java .util .Map ;
@@ -79,6 +80,7 @@ public class TransportRollupAction extends AcknowledgedTransportMasterNodeAction
79
80
private final Client client ;
80
81
private final ClusterService clusterService ;
81
82
private final MetadataCreateIndexService metadataCreateIndexService ;
83
+ private final IndexScopedSettings indexScopedSettings ;
82
84
83
85
/**
84
86
* This is the cluster state task executor for cluster state update actions.
@@ -107,7 +109,8 @@ public TransportRollupAction(
107
109
ThreadPool threadPool ,
108
110
MetadataCreateIndexService metadataCreateIndexService ,
109
111
ActionFilters actionFilters ,
110
- IndexNameExpressionResolver indexNameExpressionResolver
112
+ IndexNameExpressionResolver indexNameExpressionResolver ,
113
+ IndexScopedSettings indexScopedSettings
111
114
) {
112
115
super (
113
116
RollupAction .NAME ,
@@ -122,6 +125,7 @@ public TransportRollupAction(
122
125
this .client = new OriginSettingClient (client , ClientHelper .ROLLUP_ORIGIN );
123
126
this .clusterService = clusterService ;
124
127
this .metadataCreateIndexService = metadataCreateIndexService ;
128
+ this .indexScopedSettings = indexScopedSettings ;
125
129
}
126
130
127
131
@ Override
@@ -242,19 +246,20 @@ protected void masterOperation(
242
246
client .execute (RollupIndexerAction .INSTANCE , rollupIndexerRequest , ActionListener .wrap (indexerResp -> {
243
247
if (indexerResp .isCreated ()) {
244
248
// 4. Make rollup index read-only and set the correct number of replicas
245
- final Settings settings = Settings .builder ()
246
- .put (IndexMetadata .SETTING_BLOCKS_WRITE , true )
247
- .put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , sourceIndexMetadata .getNumberOfReplicas ())
248
- .build ();
249
- UpdateSettingsRequest updateSettingsReq = new UpdateSettingsRequest (settings , rollupIndexName );
249
+ final Settings .Builder settings = Settings .builder ().put (IndexMetadata .SETTING_BLOCKS_WRITE , true );
250
+ // Number of replicas had been previously set to 0 to speed up index population
251
+ if (sourceIndexMetadata .getNumberOfReplicas () > 0 ) {
252
+ settings .put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , sourceIndexMetadata .getNumberOfReplicas ());
253
+ }
254
+ UpdateSettingsRequest updateSettingsReq = new UpdateSettingsRequest (settings .build (), rollupIndexName );
250
255
updateSettingsReq .setParentTask (parentTask );
251
256
client .admin ().indices ().updateSettings (updateSettingsReq , ActionListener .wrap (updateSettingsResponse -> {
252
257
if (updateSettingsResponse .isAcknowledged ()) {
253
258
// 5. Refresh rollup index
254
259
refreshIndex (rollupIndexName , parentTask , ActionListener .wrap (refreshIndexResponse -> {
255
260
if (refreshIndexResponse .getFailedShards () == 0 ) {
256
261
// 6. Mark rollup index as "completed successfully"
257
- updateRollupMetadata (sourceIndexName , rollupIndexName , request , ActionListener .wrap (resp -> {
262
+ updateRollupMetadata (rollupIndexName , request , ActionListener .wrap (resp -> {
258
263
if (resp .isAcknowledged ()) {
259
264
// 7. Force-merge the rollup index to a single segment
260
265
forceMergeIndex (
@@ -427,42 +432,42 @@ public static String createRollupIndexMapping(
427
432
}
428
433
429
434
/**
430
- * Copy index metadata from the source index to the rollup index.
435
+ * Copy index settings from the source index to the rollup index. Settings that
436
+ * have already been set in the rollup index will not be overridden.
431
437
*/
432
438
private IndexMetadata .Builder copyIndexMetadata (IndexMetadata sourceIndexMetadata , IndexMetadata rollupIndexMetadata ) {
433
- String sourceIndexName = sourceIndexMetadata .getIndex ().getName ();
439
+ // Copy index settings from the source index, but do not override the settings
440
+ // that already have been set in the rollup index
441
+ final Settings .Builder targetSettings = Settings .builder ().put (rollupIndexMetadata .getSettings ());
442
+ for (final String key : sourceIndexMetadata .getSettings ().keySet ()) {
443
+ final Setting <?> setting = indexScopedSettings .get (key );
444
+ if (setting == null ) {
445
+ assert indexScopedSettings .isPrivateSetting (key ) : "expected [" + key + "] to be private but it was not" ;
446
+ } else if (setting .getProperties ().contains (Setting .Property .NotCopyableOnResize )) {
447
+ // we leverage the NotCopyableOnResize setting property for rollup, because
448
+ // the same rules with resize apply
449
+ continue ;
450
+ }
451
+ // do not override settings that have already been set in the rollup index
452
+ if (targetSettings .keys ().contains (key )) {
453
+ continue ;
454
+ }
455
+ targetSettings .copy (key , sourceIndexMetadata .getSettings ());
456
+ }
434
457
435
458
/*
436
459
* Add the source index name and UUID to the rollup index metadata.
437
460
* If the source index is a rollup index, we will add the name and UUID
438
461
* of the first index that we initially rolled up.
439
462
*/
440
- String originalIndexName = IndexMetadata .INDEX_ROLLUP_SOURCE_NAME .exists (sourceIndexMetadata .getSettings ())
441
- ? IndexMetadata .INDEX_ROLLUP_SOURCE_NAME .get (sourceIndexMetadata .getSettings ())
442
- : sourceIndexName ;
443
- String originalIndexUuid = IndexMetadata .INDEX_ROLLUP_SOURCE_UUID .exists (sourceIndexMetadata .getSettings ())
444
- ? IndexMetadata .INDEX_ROLLUP_SOURCE_UUID .get (sourceIndexMetadata .getSettings ())
445
- : sourceIndexMetadata .getIndexUUID ();
446
-
447
- // Copy time series index settings from original index
448
- List <String > indexRoutingPath = sourceIndexMetadata .getRoutingPaths ();
449
- Instant startTime = IndexSettings .TIME_SERIES_START_TIME .get (sourceIndexMetadata .getSettings ());
450
- Instant endTime = IndexSettings .TIME_SERIES_END_TIME .get (sourceIndexMetadata .getSettings ());
451
- IndexMode indexMode = IndexSettings .MODE .get (sourceIndexMetadata .getSettings ());
452
-
453
- return IndexMetadata .builder (rollupIndexMetadata )
454
- .settings (
455
- Settings .builder ()
456
- .put (rollupIndexMetadata .getSettings ())
457
- .put (IndexMetadata .INDEX_ROLLUP_SOURCE_NAME .getKey (), originalIndexName )
458
- .put (IndexMetadata .INDEX_ROLLUP_SOURCE_UUID .getKey (), originalIndexUuid )
459
- .put (IndexMetadata .INDEX_HIDDEN_SETTING .getKey (), sourceIndexMetadata .isHidden ())
460
- // Add the time series index settings
461
- .put (IndexSettings .MODE .getKey (), indexMode )
462
- .putList (IndexMetadata .INDEX_ROUTING_PATH .getKey (), indexRoutingPath )
463
- .put (IndexSettings .TIME_SERIES_START_TIME .getKey (), startTime .toString ())
464
- .put (IndexSettings .TIME_SERIES_END_TIME .getKey (), endTime .toString ())
465
- );
463
+ if (IndexMetadata .INDEX_ROLLUP_SOURCE_UUID .exists (sourceIndexMetadata .getSettings ()) == false
464
+ || IndexMetadata .INDEX_ROLLUP_SOURCE_NAME .exists (sourceIndexMetadata .getSettings ()) == false ) {
465
+ Index sourceIndex = sourceIndexMetadata .getIndex ();
466
+ targetSettings .put (IndexMetadata .INDEX_ROLLUP_SOURCE_NAME .getKey (), sourceIndex .getName ())
467
+ .put (IndexMetadata .INDEX_ROLLUP_SOURCE_UUID .getKey (), sourceIndex .getUUID ());
468
+ }
469
+
470
+ return IndexMetadata .builder (rollupIndexMetadata ).settings (targetSettings );
466
471
}
467
472
468
473
/**
@@ -524,12 +529,7 @@ public ClusterState execute(ClusterState currentState) throws Exception {
524
529
}, ClusterStateTaskConfig .build (Priority .URGENT , request .masterNodeTimeout ()), STATE_UPDATE_TASK_EXECUTOR );
525
530
}
526
531
527
- private void updateRollupMetadata (
528
- String sourceIndexName ,
529
- String rollupIndexName ,
530
- RollupAction .Request request ,
531
- ActionListener <AcknowledgedResponse > listener
532
- ) {
532
+ private void updateRollupMetadata (String rollupIndexName , RollupAction .Request request , ActionListener <AcknowledgedResponse > listener ) {
533
533
// 6. Mark rollup index as "completed successfully" ("index.rollup.status": "success")
534
534
clusterService .submitStateUpdateTask (
535
535
"update-rollup-metadata [" + rollupIndexName + "]" ,
0 commit comments