-
Notifications
You must be signed in to change notification settings - Fork 25.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CCR] Make auto follow patterns work with security #33501
Merged
martijnvg
merged 16 commits into
elastic:master
from
martijnvg:ccr_auto_follow_security
Sep 17, 2018
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e1a0438
[CCR] Make auto follow patterns work with security
martijnvg f38b019
Merge remote-tracking branch 'es/master' into ccr_auto_follow_security
martijnvg 49b02f1
Merge remote-tracking branch 'es/master' into ccr_auto_follow_security
martijnvg a978f3f
undo removal of assertions
martijnvg 19d18b8
fixed test
martijnvg df144c5
fixed test
martijnvg 6260691
Cleanup, so other test is not affected by it.
martijnvg 345cac6
move wrapping of client to CcrLicenseChecker
martijnvg 797bfb4
jdocs
martijnvg 2c3a276
Merge branch 'master' into pr/33501
jasontedor 3e98c3d
Merge remote-tracking branch 'es/master' into ccr_auto_follow_security
martijnvg 7ac914d
Merge remote-tracking branch 'es/master' into ccr_auto_follow_security
martijnvg 203e762
Merge remote-tracking branch 'es/master' into ccr_auto_follow_security
martijnvg c42d71a
Merge remote-tracking branch 'es/master' into ccr_auto_follow_security
martijnvg 39c8471
Merge remote-tracking branch 'es/master' into ccr_auto_follow_security
martijnvg 2cf4d46
Merge remote-tracking branch 'es/master' into ccr_auto_follow_security
martijnvg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,33 +7,43 @@ | |
package org.elasticsearch.xpack.ccr; | ||
|
||
import org.elasticsearch.ElasticsearchStatusException; | ||
import org.elasticsearch.action.Action; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.ActionRequest; | ||
import org.elasticsearch.action.ActionResponse; | ||
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest; | ||
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; | ||
import org.elasticsearch.action.support.ContextPreservingActionListener; | ||
import org.elasticsearch.action.admin.indices.stats.IndexShardStats; | ||
import org.elasticsearch.action.admin.indices.stats.IndexStats; | ||
import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest; | ||
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse; | ||
import org.elasticsearch.action.admin.indices.stats.ShardStats; | ||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.client.FilterClient; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.metadata.IndexMetaData; | ||
import org.elasticsearch.common.util.concurrent.ThreadContext; | ||
import org.elasticsearch.common.CheckedConsumer; | ||
import org.elasticsearch.index.engine.CommitStats; | ||
import org.elasticsearch.index.engine.Engine; | ||
import org.elasticsearch.index.shard.ShardId; | ||
import org.elasticsearch.license.RemoteClusterLicenseChecker; | ||
import org.elasticsearch.license.XPackLicenseState; | ||
import org.elasticsearch.rest.RestStatus; | ||
import org.elasticsearch.xpack.ccr.action.ShardFollowTask; | ||
import org.elasticsearch.xpack.core.XPackPlugin; | ||
|
||
import java.util.Collections; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.BooleanSupplier; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Encapsulates licensing checking for CCR. | ||
|
@@ -93,6 +103,7 @@ public <T> void checkRemoteClusterLicenseAndFetchLeaderIndexMetadataAndHistoryUU | |
request.indices(leaderIndex); | ||
checkRemoteClusterLicenseAndFetchClusterState( | ||
client, | ||
Collections.emptyMap(), | ||
clusterAlias, | ||
request, | ||
onFailure, | ||
|
@@ -115,19 +126,22 @@ public <T> void checkRemoteClusterLicenseAndFetchLeaderIndexMetadataAndHistoryUU | |
* | ||
* @param client the client | ||
* @param clusterAlias the remote cluster alias | ||
* @param headers the headers to use for leader client | ||
* @param request the cluster state request | ||
* @param onFailure the failure consumer | ||
* @param leaderClusterStateConsumer the leader cluster state consumer | ||
* @param <T> the type of response the listener is waiting for | ||
*/ | ||
public <T> void checkRemoteClusterLicenseAndFetchClusterState( | ||
final Client client, | ||
final Map<String, String> headers, | ||
final String clusterAlias, | ||
final ClusterStateRequest request, | ||
final Consumer<Exception> onFailure, | ||
final Consumer<ClusterState> leaderClusterStateConsumer) { | ||
checkRemoteClusterLicenseAndFetchClusterState( | ||
client, | ||
headers, | ||
clusterAlias, | ||
request, | ||
onFailure, | ||
|
@@ -144,6 +158,7 @@ public <T> void checkRemoteClusterLicenseAndFetchClusterState( | |
* | ||
* @param client the client | ||
* @param clusterAlias the remote cluster alias | ||
* @param headers the headers to use for leader client | ||
* @param request the cluster state request | ||
* @param onFailure the failure consumer | ||
* @param leaderClusterStateConsumer the leader cluster state consumer | ||
|
@@ -153,6 +168,7 @@ public <T> void checkRemoteClusterLicenseAndFetchClusterState( | |
*/ | ||
private <T> void checkRemoteClusterLicenseAndFetchClusterState( | ||
final Client client, | ||
final Map<String, String> headers, | ||
final String clusterAlias, | ||
final ClusterStateRequest request, | ||
final Consumer<Exception> onFailure, | ||
|
@@ -167,7 +183,7 @@ private <T> void checkRemoteClusterLicenseAndFetchClusterState( | |
@Override | ||
public void onResponse(final RemoteClusterLicenseChecker.LicenseCheck licenseCheck) { | ||
if (licenseCheck.isSuccess()) { | ||
final Client leaderClient = client.getRemoteClusterClient(clusterAlias); | ||
final Client leaderClient = wrapClient(client.getRemoteClusterClient(clusterAlias), headers); | ||
final ActionListener<ClusterStateResponse> clusterStateListener = | ||
ActionListener.wrap(s -> leaderClusterStateConsumer.accept(s.getState()), onFailure); | ||
// following an index in remote cluster, so use remote client to fetch leader index metadata | ||
|
@@ -237,6 +253,33 @@ public void fetchLeaderHistoryUUIDs( | |
leaderClient.admin().indices().stats(request, ActionListener.wrap(indicesStatsHandler, onFailure)); | ||
} | ||
|
||
public static Client wrapClient(Client client, Map<String, String> headers) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved it here, because I did not want that this code depends on ccr action code. |
||
if (headers.isEmpty()) { | ||
return client; | ||
} else { | ||
final ThreadContext threadContext = client.threadPool().getThreadContext(); | ||
Map<String, String> filteredHeaders = headers.entrySet().stream() | ||
.filter(e -> ShardFollowTask.HEADER_FILTERS.contains(e.getKey())) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
return new FilterClient(client) { | ||
@Override | ||
protected <Request extends ActionRequest, Response extends ActionResponse> | ||
void doExecute(Action<Response> action, Request request, ActionListener<Response> listener) { | ||
final Supplier<ThreadContext.StoredContext> supplier = threadContext.newRestorableContext(false); | ||
try (ThreadContext.StoredContext ignore = stashWithHeaders(threadContext, filteredHeaders)) { | ||
super.doExecute(action, request, new ContextPreservingActionListener<>(supplier, listener)); | ||
} | ||
} | ||
}; | ||
} | ||
} | ||
|
||
private static ThreadContext.StoredContext stashWithHeaders(ThreadContext threadContext, Map<String, String> headers) { | ||
final ThreadContext.StoredContext storedContext = threadContext.stashContext(); | ||
threadContext.copyHeaders(headers.entrySet()); | ||
return storedContext; | ||
} | ||
|
||
private static ElasticsearchStatusException indexMetadataNonCompliantRemoteLicense( | ||
final String leaderIndex, final RemoteClusterLicenseChecker.LicenseCheck licenseCheck) { | ||
final String clusterAlias = licenseCheck.remoteClusterLicenseInfo().clusterAlias(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not super happy with the changes made to this file. Maybe the caller should provide the leaderClient? So that
headers
andclusterAlias
does not need to be provided?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we discussed offline, this is okay for now, we can always refactor this later. The implementation is fine, we just want to take a step back and look at how ML uses the remote license checker too (multiple clusters) which has kind of forced how we implement this.