@@ -104,9 +104,8 @@ public boolean isCcrAllowed() {
104104 * @param leaderIndex the name of the leader index
105105 * @param onFailure the failure consumer
106106 * @param consumer the consumer for supplying the leader index metadata and historyUUIDs of all leader shards
107- * @param <T> the type of response the listener is waiting for
108107 */
109- public < T > void checkRemoteClusterLicenseAndFetchLeaderIndexMetadataAndHistoryUUIDs (
108+ public void checkRemoteClusterLicenseAndFetchLeaderIndexMetadataAndHistoryUUIDs (
110109 final Client client ,
111110 final String clusterAlias ,
112111 final String leaderIndex ,
@@ -119,8 +118,8 @@ public <T> void checkRemoteClusterLicenseAndFetchLeaderIndexMetadataAndHistoryUU
119118 request .indices (leaderIndex );
120119 checkRemoteClusterLicenseAndFetchClusterState (
121120 client ,
122- Collections .emptyMap (),
123121 clusterAlias ,
122+ client .getRemoteClusterClient (clusterAlias ),
124123 request ,
125124 onFailure ,
126125 leaderClusterState -> {
@@ -152,22 +151,20 @@ public <T> void checkRemoteClusterLicenseAndFetchLeaderIndexMetadataAndHistoryUU
152151 *
153152 * @param client the client
154153 * @param clusterAlias the remote cluster alias
155- * @param headers the headers to use for leader client
156154 * @param request the cluster state request
157155 * @param onFailure the failure consumer
158156 * @param leaderClusterStateConsumer the leader cluster state consumer
159157 */
160158 public void checkRemoteClusterLicenseAndFetchClusterState (
161159 final Client client ,
162- final Map <String , String > headers ,
163160 final String clusterAlias ,
164161 final ClusterStateRequest request ,
165162 final Consumer <Exception > onFailure ,
166163 final Consumer <ClusterState > leaderClusterStateConsumer ) {
167164 checkRemoteClusterLicenseAndFetchClusterState (
168165 client ,
169- headers ,
170166 clusterAlias ,
167+ systemClient (client .getRemoteClusterClient (clusterAlias )),
171168 request ,
172169 onFailure ,
173170 leaderClusterStateConsumer ,
@@ -183,18 +180,17 @@ public void checkRemoteClusterLicenseAndFetchClusterState(
183180 *
184181 * @param client the client
185182 * @param clusterAlias the remote cluster alias
186- * @param headers the headers to use for leader client
183+ * @param leaderClient the leader client to use to execute cluster state API
187184 * @param request the cluster state request
188185 * @param onFailure the failure consumer
189186 * @param leaderClusterStateConsumer the leader cluster state consumer
190187 * @param nonCompliantLicense the supplier for when the license state of the remote cluster is non-compliant
191188 * @param unknownLicense the supplier for when the license state of the remote cluster is unknown due to failure
192- * @param <T> the type of response the listener is waiting for
193189 */
194- private < T > void checkRemoteClusterLicenseAndFetchClusterState (
190+ private void checkRemoteClusterLicenseAndFetchClusterState (
195191 final Client client ,
196- final Map <String , String > headers ,
197192 final String clusterAlias ,
193+ final Client leaderClient ,
198194 final ClusterStateRequest request ,
199195 final Consumer <Exception > onFailure ,
200196 final Consumer <ClusterState > leaderClusterStateConsumer ,
@@ -208,7 +204,6 @@ private <T> void checkRemoteClusterLicenseAndFetchClusterState(
208204 @ Override
209205 public void onResponse (final RemoteClusterLicenseChecker .LicenseCheck licenseCheck ) {
210206 if (licenseCheck .isSuccess ()) {
211- final Client leaderClient = wrapClient (client .getRemoteClusterClient (clusterAlias ), headers );
212207 final ActionListener <ClusterStateResponse > clusterStateListener =
213208 ActionListener .wrap (s -> leaderClusterStateConsumer .accept (s .getState ()), onFailure );
214209 // following an index in remote cluster, so use remote client to fetch leader index metadata
@@ -363,6 +358,22 @@ void doExecute(Action<Request, Response, RequestBuilder> action, Request request
363358 }
364359 }
365360
361+ private static Client systemClient (Client client ) {
362+ final ThreadContext threadContext = client .threadPool ().getThreadContext ();
363+ return new FilterClient (client ) {
364+ @ Override
365+ protected <Request extends ActionRequest , Response extends ActionResponse ,
366+ RequestBuilder extends ActionRequestBuilder <Request , Response , RequestBuilder >>
367+ void doExecute (Action <Request , Response , RequestBuilder > action , Request request , ActionListener <Response > listener ) {
368+ final Supplier <ThreadContext .StoredContext > supplier = threadContext .newRestorableContext (false );
369+ try (ThreadContext .StoredContext ignore = threadContext .stashContext ()) {
370+ threadContext .markAsSystemContext ();
371+ super .doExecute (action , request , new ContextPreservingActionListener <>(supplier , listener ));
372+ }
373+ }
374+ };
375+ }
376+
366377 private static ThreadContext .StoredContext stashWithHeaders (ThreadContext threadContext , Map <String , String > headers ) {
367378 final ThreadContext .StoredContext storedContext = threadContext .stashContext ();
368379 threadContext .copyHeaders (headers .entrySet ());
0 commit comments