|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.ccr; |
| 8 | + |
| 9 | +import org.elasticsearch.ElasticsearchStatusException; |
| 10 | +import org.elasticsearch.action.ActionListener; |
| 11 | +import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest; |
| 12 | +import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse; |
| 13 | +import org.elasticsearch.client.Client; |
| 14 | +import org.elasticsearch.cluster.ClusterState; |
| 15 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 16 | +import org.elasticsearch.license.RemoteClusterLicenseChecker; |
| 17 | +import org.elasticsearch.license.XPackLicenseState; |
| 18 | +import org.elasticsearch.rest.RestStatus; |
| 19 | +import org.elasticsearch.xpack.core.XPackPlugin; |
| 20 | + |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.Locale; |
| 23 | +import java.util.Objects; |
| 24 | +import java.util.function.BooleanSupplier; |
| 25 | +import java.util.function.Consumer; |
| 26 | + |
| 27 | +/** |
| 28 | + * Encapsulates licensing checking for CCR. |
| 29 | + */ |
| 30 | +public final class CcrLicenseChecker { |
| 31 | + |
| 32 | + private final BooleanSupplier isCcrAllowed; |
| 33 | + |
| 34 | + /** |
| 35 | + * Constructs a CCR license checker with the default rule based on the license state for checking if CCR is allowed. |
| 36 | + */ |
| 37 | + CcrLicenseChecker() { |
| 38 | + this(XPackPlugin.getSharedLicenseState()::isCcrAllowed); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Constructs a CCR license checker with the specified boolean supplier. |
| 43 | + * |
| 44 | + * @param isCcrAllowed a boolean supplier that should return true if CCR is allowed and false otherwise |
| 45 | + */ |
| 46 | + CcrLicenseChecker(final BooleanSupplier isCcrAllowed) { |
| 47 | + this.isCcrAllowed = Objects.requireNonNull(isCcrAllowed); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Returns whether or not CCR is allowed. |
| 52 | + * |
| 53 | + * @return true if CCR is allowed, otherwise false |
| 54 | + */ |
| 55 | + public boolean isCcrAllowed() { |
| 56 | + return isCcrAllowed.getAsBoolean(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Fetches the leader index metadata from the remote cluster. Before fetching the index metadata, the remote cluster is checked for |
| 61 | + * license compatibility with CCR. If the remote cluster is not licensed for CCR, the {@link ActionListener#onFailure(Exception)} method |
| 62 | + * of the specified listener is invoked. Otherwise, the specified consumer is invoked with the leader index metadata fetched from the |
| 63 | + * remote cluster. |
| 64 | + * |
| 65 | + * @param client the client |
| 66 | + * @param clusterAlias the remote cluster alias |
| 67 | + * @param leaderIndex the name of the leader index |
| 68 | + * @param listener the listener |
| 69 | + * @param leaderIndexMetadataConsumer the leader index metadata consumer |
| 70 | + * @param <T> the type of response the listener is waiting for |
| 71 | + */ |
| 72 | + public <T> void checkRemoteClusterLicenseAndFetchLeaderIndexMetadata( |
| 73 | + final Client client, |
| 74 | + final String clusterAlias, |
| 75 | + final String leaderIndex, |
| 76 | + final ActionListener<T> listener, |
| 77 | + final Consumer<IndexMetaData> leaderIndexMetadataConsumer) { |
| 78 | + // we have to check the license on the remote cluster |
| 79 | + new RemoteClusterLicenseChecker(client, XPackLicenseState::isCcrAllowedForOperationMode).checkRemoteClusterLicenses( |
| 80 | + Collections.singletonList(clusterAlias), |
| 81 | + new ActionListener<RemoteClusterLicenseChecker.LicenseCheck>() { |
| 82 | + |
| 83 | + @Override |
| 84 | + public void onResponse(final RemoteClusterLicenseChecker.LicenseCheck licenseCheck) { |
| 85 | + if (licenseCheck.isSuccess()) { |
| 86 | + final Client remoteClient = client.getRemoteClusterClient(clusterAlias); |
| 87 | + final ClusterStateRequest clusterStateRequest = new ClusterStateRequest(); |
| 88 | + clusterStateRequest.clear(); |
| 89 | + clusterStateRequest.metaData(true); |
| 90 | + clusterStateRequest.indices(leaderIndex); |
| 91 | + final ActionListener<ClusterStateResponse> clusterStateListener = ActionListener.wrap( |
| 92 | + r -> { |
| 93 | + final ClusterState remoteClusterState = r.getState(); |
| 94 | + final IndexMetaData leaderIndexMetadata = |
| 95 | + remoteClusterState.getMetaData().index(leaderIndex); |
| 96 | + leaderIndexMetadataConsumer.accept(leaderIndexMetadata); |
| 97 | + }, |
| 98 | + listener::onFailure); |
| 99 | + // following an index in remote cluster, so use remote client to fetch leader index metadata |
| 100 | + remoteClient.admin().cluster().state(clusterStateRequest, clusterStateListener); |
| 101 | + } else { |
| 102 | + listener.onFailure(incompatibleRemoteLicense(leaderIndex, licenseCheck)); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void onFailure(final Exception e) { |
| 108 | + listener.onFailure(unknownRemoteLicense(leaderIndex, clusterAlias, e)); |
| 109 | + } |
| 110 | + |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + private static ElasticsearchStatusException incompatibleRemoteLicense( |
| 115 | + final String leaderIndex, final RemoteClusterLicenseChecker.LicenseCheck licenseCheck) { |
| 116 | + final String clusterAlias = licenseCheck.remoteClusterLicenseInfo().clusterAlias(); |
| 117 | + final String message = String.format( |
| 118 | + Locale.ROOT, |
| 119 | + "can not fetch remote index [%s:%s] metadata as the remote cluster [%s] is not licensed for [ccr]; %s", |
| 120 | + clusterAlias, |
| 121 | + leaderIndex, |
| 122 | + clusterAlias, |
| 123 | + RemoteClusterLicenseChecker.buildErrorMessage( |
| 124 | + "ccr", |
| 125 | + licenseCheck.remoteClusterLicenseInfo(), |
| 126 | + RemoteClusterLicenseChecker::isLicensePlatinumOrTrial)); |
| 127 | + return new ElasticsearchStatusException(message, RestStatus.BAD_REQUEST); |
| 128 | + } |
| 129 | + |
| 130 | + private static ElasticsearchStatusException unknownRemoteLicense( |
| 131 | + final String leaderIndex, final String clusterAlias, final Exception cause) { |
| 132 | + final String message = String.format( |
| 133 | + Locale.ROOT, |
| 134 | + "can not fetch remote index [%s:%s] metadata as the license state of the remote cluster [%s] could not be determined", |
| 135 | + clusterAlias, |
| 136 | + leaderIndex, |
| 137 | + clusterAlias); |
| 138 | + return new ElasticsearchStatusException(message, RestStatus.BAD_REQUEST, cause); |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments