3636import org .apache .logging .log4j .Logger ;
3737import org .opensearch .action .RequestValidators ;
3838import org .opensearch .action .support .ActionFilters ;
39+ import org .opensearch .action .support .TransportIndicesResolvingAction ;
3940import org .opensearch .action .support .clustermanager .AcknowledgedResponse ;
4041import org .opensearch .action .support .clustermanager .TransportClusterManagerNodeAction ;
4142import org .opensearch .cluster .ClusterState ;
4849import org .opensearch .cluster .metadata .IndexNameExpressionResolver ;
4950import org .opensearch .cluster .metadata .Metadata ;
5051import org .opensearch .cluster .metadata .MetadataIndexAliasesService ;
52+ import org .opensearch .cluster .metadata .ResolvedIndices ;
5153import org .opensearch .cluster .service .ClusterService ;
5254import org .opensearch .common .inject .Inject ;
5355import org .opensearch .common .regex .Regex ;
6870import java .util .Objects ;
6971import java .util .Optional ;
7072import java .util .Set ;
73+ import java .util .stream .Collectors ;
7174
7275import static java .util .Collections .unmodifiableList ;
7376
7679 *
7780 * @opensearch.internal
7881 */
79- public class TransportIndicesAliasesAction extends TransportClusterManagerNodeAction <IndicesAliasesRequest , AcknowledgedResponse > {
82+ public class TransportIndicesAliasesAction extends TransportClusterManagerNodeAction <IndicesAliasesRequest , AcknowledgedResponse >
83+ implements
84+ TransportIndicesResolvingAction <IndicesAliasesRequest > {
8085
8186 private static final Logger logger = LogManager .getLogger (TransportIndicesAliasesAction .class );
8287
@@ -131,45 +136,97 @@ protected void clusterManagerOperation(
131136 final IndicesAliasesRequest request ,
132137 final ClusterState state ,
133138 final ActionListener <AcknowledgedResponse > listener
134- ) {
139+ ) throws Exception {
135140
136141 // Expand the indices names
137142 List <IndicesAliasesRequest .AliasActions > actions = request .aliasActions ();
138- List <AliasAction > finalActions = new ArrayList <>();
143+ List <AliasAction > finalActions = resolvedAliasActions (request , state , true );
144+ if (finalActions .isEmpty () && false == actions .isEmpty ()) {
145+ throw new AliasesNotFoundException (
146+ actions .stream ().flatMap (a -> Arrays .stream (a .getOriginalAliases ())).collect (Collectors .toSet ()).toArray (new String [0 ])
147+ );
148+ }
149+ request .aliasActions ().clear ();
150+ IndicesAliasesClusterStateUpdateRequest updateRequest = new IndicesAliasesClusterStateUpdateRequest (unmodifiableList (finalActions ))
151+ .ackTimeout (request .timeout ())
152+ .clusterManagerNodeTimeout (request .clusterManagerNodeTimeout ());
153+
154+ indexAliasesService .indicesAliases (updateRequest , new ActionListener <ClusterStateUpdateResponse >() {
155+ @ Override
156+ public void onResponse (ClusterStateUpdateResponse response ) {
157+ listener .onResponse (new AcknowledgedResponse (response .isAcknowledged ()));
158+ }
159+
160+ @ Override
161+ public void onFailure (Exception t ) {
162+ logger .debug ("failed to perform aliases" , t );
163+ listener .onFailure (t );
164+ }
165+ });
166+ }
167+
168+ @ Override
169+ public ResolvedIndices resolveIndices (IndicesAliasesRequest request ) {
170+ try {
171+ Set <String > indices = new HashSet <>();
172+
173+ for (AliasAction aliasAction : resolvedAliasActions (request , clusterService .state (), false )) {
174+ if (aliasAction instanceof AliasAction .Add addAliasAction ) {
175+ indices .add (addAliasAction .getIndex ());
176+ indices .add (addAliasAction .getAlias ());
177+ } else if (aliasAction instanceof AliasAction .Remove removeAliasAction ) {
178+ indices .add (removeAliasAction .getIndex ());
179+ indices .add (removeAliasAction .getAlias ());
180+ } else if (aliasAction instanceof AliasAction .RemoveIndex removeIndexAction ) {
181+ // TODO special action
182+ indices .add (removeIndexAction .getIndex ());
183+ }
184+ }
185+
186+ return ResolvedIndices .of (indices );
187+ } catch (RuntimeException e ) {
188+ throw e ;
189+ } catch (Exception e ) {
190+ // This should not happen if validate=false is passed to resolvedAliasActions()
191+ throw new RuntimeException (e );
192+ }
193+ }
194+
195+ private List <AliasAction > resolvedAliasActions (IndicesAliasesRequest request , ClusterState state , boolean validate ) throws Exception {
196+ List <AliasAction > result = new ArrayList <>();
139197 // Resolve all the AliasActions into AliasAction instances and gather all the aliases
140- Set <String > aliases = new HashSet <>();
141- for (IndicesAliasesRequest .AliasActions action : actions ) {
198+ for (IndicesAliasesRequest .AliasActions action : request .aliasActions ()) {
142199 final Index [] concreteIndices = indexNameExpressionResolver .concreteIndices (
143200 state ,
144201 request .indicesOptions (),
145202 false ,
146203 action .indices ()
147204 );
148- for (Index concreteIndex : concreteIndices ) {
149- IndexAbstraction indexAbstraction = state .metadata ().getIndicesLookup ().get (concreteIndex .getName ());
150- assert indexAbstraction != null : "invalid cluster metadata. index [" + concreteIndex .getName () + "] was not found" ;
151- if (indexAbstraction .getParentDataStream () != null ) {
152- throw new IllegalArgumentException (
153- "The provided expressions ["
154- + String .join ("," , action .indices ())
155- + "] match a backing index belonging to data stream ["
156- + indexAbstraction .getParentDataStream ().getName ()
157- + "]. Data streams and their backing indices don't support aliases."
158- );
205+ if (validate ) {
206+ for (Index concreteIndex : concreteIndices ) {
207+ IndexAbstraction indexAbstraction = state .metadata ().getIndicesLookup ().get (concreteIndex .getName ());
208+ assert indexAbstraction != null : "invalid cluster metadata. index [" + concreteIndex .getName () + "] was not found" ;
209+ if (indexAbstraction .getParentDataStream () != null ) {
210+ throw new IllegalArgumentException (
211+ "The provided expressions ["
212+ + String .join ("," , action .indices ())
213+ + "] match a backing index belonging to data stream ["
214+ + indexAbstraction .getParentDataStream ().getName ()
215+ + "]. Data streams and their backing indices don't support aliases."
216+ );
217+ }
218+ }
219+ final Optional <Exception > maybeException = requestValidators .validateRequest (request , state , concreteIndices );
220+ if (maybeException .isPresent ()) {
221+ throw maybeException .get ();
159222 }
160- }
161- final Optional <Exception > maybeException = requestValidators .validateRequest (request , state , concreteIndices );
162- if (maybeException .isPresent ()) {
163- listener .onFailure (maybeException .get ());
164- return ;
165223 }
166224
167- Collections .addAll (aliases , action .getOriginalAliases ());
168225 for (final Index index : concreteIndices ) {
169226 switch (action .actionType ()) {
170227 case ADD :
171228 for (String alias : concreteAliases (action , state .metadata (), index .getName ())) {
172- finalActions .add (
229+ result .add (
173230 new AliasAction .Add (
174231 index .getName (),
175232 alias ,
@@ -184,37 +241,19 @@ protected void clusterManagerOperation(
184241 break ;
185242 case REMOVE :
186243 for (String alias : concreteAliases (action , state .metadata (), index .getName ())) {
187- finalActions .add (new AliasAction .Remove (index .getName (), alias , action .mustExist ()));
244+ result .add (new AliasAction .Remove (index .getName (), alias , action .mustExist ()));
188245 }
189246 break ;
190247 case REMOVE_INDEX :
191- finalActions .add (new AliasAction .RemoveIndex (index .getName ()));
248+ result .add (new AliasAction .RemoveIndex (index .getName ()));
192249 break ;
193250 default :
194251 throw new IllegalArgumentException ("Unsupported action [" + action .actionType () + "]" );
195252 }
196253 }
197254 }
198- if (finalActions .isEmpty () && false == actions .isEmpty ()) {
199- throw new AliasesNotFoundException (aliases .toArray (new String [0 ]));
200- }
201- request .aliasActions ().clear ();
202- IndicesAliasesClusterStateUpdateRequest updateRequest = new IndicesAliasesClusterStateUpdateRequest (unmodifiableList (finalActions ))
203- .ackTimeout (request .timeout ())
204- .clusterManagerNodeTimeout (request .clusterManagerNodeTimeout ());
205255
206- indexAliasesService .indicesAliases (updateRequest , new ActionListener <ClusterStateUpdateResponse >() {
207- @ Override
208- public void onResponse (ClusterStateUpdateResponse response ) {
209- listener .onResponse (new AcknowledgedResponse (response .isAcknowledged ()));
210- }
211-
212- @ Override
213- public void onFailure (Exception t ) {
214- logger .debug ("failed to perform aliases" , t );
215- listener .onFailure (t );
216- }
217- });
256+ return result ;
218257 }
219258
220259 private static String [] concreteAliases (IndicesAliasesRequest .AliasActions action , Metadata metadata , String concreteIndex ) {
@@ -255,4 +294,5 @@ private static String[] concreteAliases(IndicesAliasesRequest.AliasActions actio
255294 return action .aliases ();
256295 }
257296 }
297+
258298}
0 commit comments