8
8
import static org .opensearch .core .xcontent .XContentParserUtils .ensureExpectedToken ;
9
9
import static org .opensearch .timeseries .constant .CommonMessages .CATEGORICAL_FIELD_TYPE_ERR_MSG ;
10
10
import static org .opensearch .timeseries .constant .CommonMessages .TIMESTAMP_VALIDATION_FAILED ;
11
+ import static org .opensearch .timeseries .indices .IndexManagement .getScripts ;
11
12
import static org .opensearch .timeseries .util .ParseUtils .parseAggregators ;
12
13
import static org .opensearch .timeseries .util .RestHandlerUtils .XCONTENT_WITH_TYPE ;
13
14
import static org .opensearch .timeseries .util .RestHandlerUtils .isExceptionCausedByInvalidQuery ;
25
26
import org .opensearch .action .admin .indices .mapping .get .GetFieldMappingsAction ;
26
27
import org .opensearch .action .admin .indices .mapping .get .GetFieldMappingsRequest ;
27
28
import org .opensearch .action .admin .indices .mapping .get .GetFieldMappingsResponse ;
29
+ import org .opensearch .action .admin .indices .settings .put .UpdateSettingsRequest ;
28
30
import org .opensearch .action .get .GetRequest ;
29
31
import org .opensearch .action .get .GetResponse ;
30
32
import org .opensearch .action .index .IndexRequest ;
31
33
import org .opensearch .action .index .IndexResponse ;
34
+ import org .opensearch .action .ingest .DeletePipelineRequest ;
35
+ import org .opensearch .action .ingest .PutPipelineRequest ;
32
36
import org .opensearch .action .search .SearchRequest ;
33
37
import org .opensearch .action .search .SearchResponse ;
34
38
import org .opensearch .action .support .IndicesOptions ;
35
39
import org .opensearch .action .support .WriteRequest ;
40
+ import org .opensearch .action .support .master .AcknowledgedResponse ;
36
41
import org .opensearch .action .support .replication .ReplicationResponse ;
37
42
import org .opensearch .ad .transport .IndexAnomalyDetectorResponse ;
38
43
import org .opensearch .client .Client ;
41
46
import org .opensearch .common .unit .TimeValue ;
42
47
import org .opensearch .common .util .concurrent .ThreadContext ;
43
48
import org .opensearch .common .xcontent .XContentFactory ;
49
+ import org .opensearch .common .xcontent .XContentType ;
44
50
import org .opensearch .commons .authuser .User ;
45
51
import org .opensearch .core .action .ActionListener ;
46
52
import org .opensearch .core .action .ActionResponse ;
53
+ import org .opensearch .core .common .bytes .BytesReference ;
47
54
import org .opensearch .core .rest .RestStatus ;
48
55
import org .opensearch .core .xcontent .NamedXContentRegistry ;
56
+ import org .opensearch .core .xcontent .XContentBuilder ;
49
57
import org .opensearch .core .xcontent .XContentParser ;
50
58
import org .opensearch .index .query .BoolQueryBuilder ;
51
59
import org .opensearch .index .query .QueryBuilder ;
68
76
import org .opensearch .timeseries .model .TimeSeriesTask ;
69
77
import org .opensearch .timeseries .model .ValidationAspect ;
70
78
import org .opensearch .timeseries .model .ValidationIssueType ;
79
+ import org .opensearch .timeseries .settings .TimeSeriesSettings ;
71
80
import org .opensearch .timeseries .task .TaskCacheManager ;
72
81
import org .opensearch .timeseries .task .TaskManager ;
73
82
import org .opensearch .timeseries .util .*;
@@ -454,39 +463,35 @@ private void handlePutRequest(boolean indexingDryRun, ActionListener<T> listener
454
463
}
455
464
456
465
private void handlePostRequest (boolean indexingDryRun , ActionListener <T > listener ) {
457
- createConfig (indexingDryRun , ActionListener .wrap (
458
- createConfigResponse -> {
459
- if (shouldHandleFlattening (indexingDryRun , createConfigResponse )) {
460
- IndexAnomalyDetectorResponse response = (IndexAnomalyDetectorResponse ) createConfigResponse ;
461
- String detectorId = response .getId ();
462
- String indexName = config .getCustomResultIndexOrAlias () + "_flattened_" + detectorId .toLowerCase ();
463
- String pipelineId = "anomaly_detection_ingest_pipeline_" + detectorId .toLowerCase ();
464
-
465
- timeSeriesIndices .initFlattenedResultIndex (indexName , ActionListener .wrap (
466
- initResponse -> setupIngestPipeline (detectorId , ActionListener .wrap (
467
- pipelineResponse -> {
468
- updateResultIndexSetting (pipelineId , indexName , ActionListener .wrap (
469
- updateResponse -> listener .onResponse (createConfigResponse ),
470
- listener ::onFailure
471
- ));
472
- },
473
- listener ::onFailure
474
- )),
475
- listener ::onFailure
476
- ));
477
- } else {
478
- listener .onResponse (createConfigResponse );
479
- }
480
- },
481
- listener ::onFailure
482
- ));
466
+ createConfig (indexingDryRun , ActionListener .wrap (createConfigResponse -> {
467
+ if (shouldHandleFlattening (indexingDryRun , createConfigResponse )) {
468
+ IndexAnomalyDetectorResponse response = (IndexAnomalyDetectorResponse ) createConfigResponse ;
469
+ String detectorId = response .getId ();
470
+ String indexName = config .getCustomResultIndexOrAlias () + "_flattened_" + detectorId .toLowerCase ();
471
+ String pipelineId = "anomaly_detection_ingest_pipeline_" + detectorId .toLowerCase ();
472
+
473
+ timeSeriesIndices
474
+ .initFlattenedResultIndex (
475
+ indexName ,
476
+ ActionListener .wrap (initResponse -> setupIngestPipeline (detectorId , ActionListener .wrap (pipelineResponse -> {
477
+ updateResultIndexSetting (
478
+ pipelineId ,
479
+ indexName ,
480
+ ActionListener .wrap (updateResponse -> listener .onResponse (createConfigResponse ), listener ::onFailure )
481
+ );
482
+ }, listener ::onFailure )), listener ::onFailure )
483
+ );
484
+ } else {
485
+ listener .onResponse (createConfigResponse );
486
+ }
487
+ }, listener ::onFailure ));
483
488
}
484
489
485
490
private boolean shouldHandleFlattening (boolean indexingDryRun , Object createConfigResponse ) {
486
491
return !indexingDryRun
487
- && config .getCustomResultIndexOrAlias () != null
488
- && config .getFlattenResultIndexMapping ()
489
- && createConfigResponse instanceof IndexAnomalyDetectorResponse ;
492
+ && config .getCustomResultIndexOrAlias () != null
493
+ && config .getFlattenResultIndexMapping ()
494
+ && createConfigResponse instanceof IndexAnomalyDetectorResponse ;
490
495
}
491
496
492
497
protected void setupIngestPipeline (String detectorId , ActionListener <T > listener ) {
@@ -498,22 +503,19 @@ protected void setupIngestPipeline(String detectorId, ActionListener<T> listener
498
503
499
504
PutPipelineRequest putPipelineRequest = new PutPipelineRequest (pipelineId , pipelineSource , XContentType .JSON );
500
505
501
- client .admin ().cluster ().putPipeline (putPipelineRequest , ActionListener .wrap (
502
- response -> {
503
- if (response .isAcknowledged ()) {
504
- logger .info ("Ingest pipeline created successfully for pipelineId: {}" , pipelineId );
505
- listener .onResponse (null );
506
- } else {
507
- String errorMessage = "Ingest pipeline creation was not acknowledged for pipelineId: " + pipelineId ;
508
- logger .error (errorMessage );
509
- listener .onFailure (new OpenSearchStatusException (errorMessage , RestStatus .INTERNAL_SERVER_ERROR ));
510
- }
511
- },
512
- exception -> {
513
- logger .error ("Error while creating ingest pipeline for pipelineId: {}" , pipelineId , exception );
514
- listener .onFailure (exception );
515
- }
516
- ));
506
+ client .admin ().cluster ().putPipeline (putPipelineRequest , ActionListener .wrap (response -> {
507
+ if (response .isAcknowledged ()) {
508
+ logger .info ("Ingest pipeline created successfully for pipelineId: {}" , pipelineId );
509
+ listener .onResponse (null );
510
+ } else {
511
+ String errorMessage = "Ingest pipeline creation was not acknowledged for pipelineId: " + pipelineId ;
512
+ logger .error (errorMessage );
513
+ listener .onFailure (new OpenSearchStatusException (errorMessage , RestStatus .INTERNAL_SERVER_ERROR ));
514
+ }
515
+ }, exception -> {
516
+ logger .error ("Error while creating ingest pipeline for pipelineId: {}" , pipelineId , exception );
517
+ listener .onFailure (exception );
518
+ }));
517
519
518
520
} catch (IOException e ) {
519
521
logger .error ("Exception while building ingest pipeline definition for pipeline ID: {}" , pipelineId , e );
@@ -555,22 +557,19 @@ protected void updateResultIndexSetting(String pipelineId, String flattenedResul
555
557
556
558
updateSettingsRequest .settings (settingsBuilder );
557
559
558
- client .admin ().indices ().updateSettings (updateSettingsRequest , ActionListener .wrap (
559
- response -> {
560
- if (response .isAcknowledged ()) {
561
- logger .info ("Successfully updated settings for index: {} with pipeline: {}" , flattenedResultIndex , pipelineId );
562
- listener .onResponse (null );
563
- } else {
564
- String errorMsg = "Settings update not acknowledged for index: " + flattenedResultIndex ;
565
- logger .error (errorMsg );
566
- listener .onFailure (new OpenSearchStatusException (errorMsg , RestStatus .INTERNAL_SERVER_ERROR ));
567
- }
568
- },
569
- exception -> {
570
- logger .error ("Failed to update settings for index: {} with pipeline: {}" , flattenedResultIndex , pipelineId , exception );
571
- listener .onFailure (exception );
572
- }
573
- ));
560
+ client .admin ().indices ().updateSettings (updateSettingsRequest , ActionListener .wrap (response -> {
561
+ if (response .isAcknowledged ()) {
562
+ logger .info ("Successfully updated settings for index: {} with pipeline: {}" , flattenedResultIndex , pipelineId );
563
+ listener .onResponse (null );
564
+ } else {
565
+ String errorMsg = "Settings update not acknowledged for index: " + flattenedResultIndex ;
566
+ logger .error (errorMsg );
567
+ listener .onFailure (new OpenSearchStatusException (errorMsg , RestStatus .INTERNAL_SERVER_ERROR ));
568
+ }
569
+ }, exception -> {
570
+ logger .error ("Failed to update settings for index: {} with pipeline: {}" , flattenedResultIndex , pipelineId , exception );
571
+ listener .onFailure (exception );
572
+ }));
574
573
}
575
574
576
575
private void handleFlattenResultIndexMappingUpdate (ActionListener <T > listener ) {
@@ -611,7 +610,6 @@ public void onFailure(Exception e) {
611
610
}
612
611
}
613
612
});
614
- >>>>>>> 2 a322387 (add a feature that flattens custom result index when enabled )
615
613
}
616
614
}
617
615
@@ -1018,7 +1016,7 @@ public void onFailure(Exception e) {
1018
1016
});
1019
1017
}
1020
1018
1021
- protected void onCreateMappingsResponse (CreateIndexResponse response , boolean indexingDryRun , ActionListener <T > listener ) throws IOException {
1019
+ protected void onCreateMappingsResponse (CreateIndexResponse response , boolean indexingDryRun , ActionListener <T > listener ) {
1022
1020
if (response .isAcknowledged ()) {
1023
1021
logger .info ("Created {} with mappings." , CommonName .CONFIG_INDEX );
1024
1022
prepareConfigIndexing (indexingDryRun , listener );
0 commit comments