2323import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
2424import org .elasticsearch .common .xcontent .XContentParser ;
2525import org .elasticsearch .common .xcontent .json .JsonXContent ;
26+ import org .elasticsearch .core .Nullable ;
2627import org .elasticsearch .tasks .Task ;
2728import org .elasticsearch .threadpool .ThreadPool ;
2829import org .elasticsearch .transport .TransportService ;
@@ -52,8 +53,8 @@ public class TransportExplainLifecycleAction
5253 public TransportExplainLifecycleAction (TransportService transportService , ClusterService clusterService , ThreadPool threadPool ,
5354 ActionFilters actionFilters , IndexNameExpressionResolver indexNameExpressionResolver ,
5455 NamedXContentRegistry xContentRegistry , IndexLifecycleService indexLifecycleService ) {
55- super (ExplainLifecycleAction .NAME , transportService , clusterService , threadPool , actionFilters ,
56- ExplainLifecycleRequest :: new , indexNameExpressionResolver , ExplainLifecycleResponse ::new );
56+ super (ExplainLifecycleAction .NAME , transportService , clusterService , threadPool , actionFilters , ExplainLifecycleRequest :: new ,
57+ indexNameExpressionResolver , ExplainLifecycleResponse ::new );
5758 this .xContentRegistry = xContentRegistry ;
5859 this .indexLifecycleService = indexLifecycleService ;
5960 }
@@ -64,57 +65,13 @@ protected void doMasterOperation(Task task, ExplainLifecycleRequest request, Str
6465 Map <String , IndexLifecycleExplainResponse > indexResponses = new HashMap <>();
6566 for (String index : concreteIndices ) {
6667 IndexMetadata idxMetadata = state .metadata ().index (index );
67- Settings idxSettings = idxMetadata .getSettings ();
68- LifecycleExecutionState lifecycleState = LifecycleExecutionState .fromIndexMetadata (idxMetadata );
69- String policyName = LifecycleSettings .LIFECYCLE_NAME_SETTING .get (idxSettings );
70- String currentPhase = lifecycleState .getPhase ();
71- String stepInfo = lifecycleState .getStepInfo ();
72- BytesArray stepInfoBytes = null ;
73- if (stepInfo != null ) {
74- stepInfoBytes = new BytesArray (stepInfo );
75- }
76- // parse existing phase steps from the phase definition in the index settings
77- String phaseDef = lifecycleState .getPhaseDefinition ();
78- PhaseExecutionInfo phaseExecutionInfo = null ;
79- if (Strings .isNullOrEmpty (phaseDef ) == false ) {
80- try (XContentParser parser = JsonXContent .jsonXContent .createParser (xContentRegistry ,
81- DeprecationHandler .THROW_UNSUPPORTED_OPERATION , phaseDef )) {
82- phaseExecutionInfo = PhaseExecutionInfo .parse (parser , currentPhase );
83- } catch (IOException e ) {
84- listener .onFailure (new ElasticsearchParseException (
85- "failed to parse phase definition for index [" + index + "]" , e ));
86- return ;
87- }
88- }
8968 final IndexLifecycleExplainResponse indexResponse ;
90- if (Strings .hasLength (policyName )) {
91- // If this is requesting only errors, only include indices in the error step or which are using a nonexistent policy
92- if (request .onlyErrors () == false
93- || (ErrorStep .NAME .equals (lifecycleState .getStep ()) || indexLifecycleService .policyExists (policyName ) == false )) {
94- Long originationDate = idxSettings .getAsLong (LIFECYCLE_ORIGINATION_DATE , -1L );
95- indexResponse = IndexLifecycleExplainResponse .newManagedIndexResponse (index , policyName ,
96- originationDate != -1L ? originationDate : lifecycleState .getLifecycleDate (),
97- lifecycleState .getPhase (),
98- lifecycleState .getAction (),
99- lifecycleState .getStep (),
100- lifecycleState .getFailedStep (),
101- lifecycleState .isAutoRetryableError (),
102- lifecycleState .getFailedStepRetryCount (),
103- lifecycleState .getPhaseTime (),
104- lifecycleState .getActionTime (),
105- lifecycleState .getStepTime (),
106- lifecycleState .getSnapshotRepository (),
107- lifecycleState .getSnapshotName (),
108- lifecycleState .getShrinkIndexName (),
109- stepInfoBytes ,
110- phaseExecutionInfo );
111- } else {
112- indexResponse = null ;
113- }
114- } else if (request .onlyManaged () == false && request .onlyErrors () == false ) {
115- indexResponse = IndexLifecycleExplainResponse .newUnmanagedIndexResponse (index );
116- } else {
117- indexResponse = null ;
69+ try {
70+ indexResponse = getIndexLifecycleExplainResponse (idxMetadata , request .onlyErrors (), request .onlyManaged (),
71+ indexLifecycleService , xContentRegistry );
72+ } catch (IOException e ) {
73+ listener .onFailure (new ElasticsearchParseException ("failed to parse phase definition for index [" + index + "]" , e ));
74+ return ;
11875 }
11976
12077 if (indexResponse != null ) {
@@ -124,4 +81,61 @@ protected void doMasterOperation(Task task, ExplainLifecycleRequest request, Str
12481 listener .onResponse (new ExplainLifecycleResponse (indexResponses ));
12582 }
12683
84+ @ Nullable
85+ static IndexLifecycleExplainResponse getIndexLifecycleExplainResponse (IndexMetadata indexMetadata , boolean onlyErrors ,
86+ boolean onlyManaged , IndexLifecycleService indexLifecycleService ,
87+ NamedXContentRegistry xContentRegistry ) throws IOException {
88+ Settings idxSettings = indexMetadata .getSettings ();
89+ LifecycleExecutionState lifecycleState = LifecycleExecutionState .fromIndexMetadata (indexMetadata );
90+ String policyName = LifecycleSettings .LIFECYCLE_NAME_SETTING .get (idxSettings );
91+ String currentPhase = lifecycleState .getPhase ();
92+ String stepInfo = lifecycleState .getStepInfo ();
93+ BytesArray stepInfoBytes = null ;
94+ if (stepInfo != null ) {
95+ stepInfoBytes = new BytesArray (stepInfo );
96+ }
97+ String indexName = indexMetadata .getIndex ().getName ();
98+
99+ // parse existing phase steps from the phase definition in the index settings
100+ String phaseDef = lifecycleState .getPhaseDefinition ();
101+ PhaseExecutionInfo phaseExecutionInfo = null ;
102+ if (Strings .isNullOrEmpty (phaseDef ) == false ) {
103+ try (XContentParser parser = JsonXContent .jsonXContent .createParser (xContentRegistry ,
104+ DeprecationHandler .THROW_UNSUPPORTED_OPERATION , phaseDef )) {
105+ phaseExecutionInfo = PhaseExecutionInfo .parse (parser , currentPhase );
106+ }
107+ }
108+
109+ final IndexLifecycleExplainResponse indexResponse ;
110+ if (Strings .hasLength (policyName )) {
111+ // If this is requesting only errors, only include indices in the error step or which are using a nonexistent policy
112+ if (onlyErrors == false
113+ || (ErrorStep .NAME .equals (lifecycleState .getStep ()) || indexLifecycleService .policyExists (policyName ) == false )) {
114+ Long originationDate = idxSettings .getAsLong (LIFECYCLE_ORIGINATION_DATE , -1L );
115+ indexResponse = IndexLifecycleExplainResponse .newManagedIndexResponse (indexName , policyName ,
116+ originationDate != -1L ? originationDate : lifecycleState .getLifecycleDate (),
117+ lifecycleState .getPhase (),
118+ lifecycleState .getAction (),
119+ lifecycleState .getStep (),
120+ lifecycleState .getFailedStep (),
121+ lifecycleState .isAutoRetryableError (),
122+ lifecycleState .getFailedStepRetryCount (),
123+ lifecycleState .getPhaseTime (),
124+ lifecycleState .getActionTime (),
125+ lifecycleState .getStepTime (),
126+ lifecycleState .getSnapshotRepository (),
127+ lifecycleState .getSnapshotName (),
128+ lifecycleState .getShrinkIndexName (),
129+ stepInfoBytes ,
130+ phaseExecutionInfo );
131+ } else {
132+ indexResponse = null ;
133+ }
134+ } else if (onlyManaged == false && onlyErrors == false ) {
135+ indexResponse = IndexLifecycleExplainResponse .newUnmanagedIndexResponse (indexName );
136+ } else {
137+ indexResponse = null ;
138+ }
139+ return indexResponse ;
140+ }
127141}
0 commit comments