|
| 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.transform; |
| 8 | + |
| 9 | +import org.apache.logging.log4j.LogManager; |
| 10 | +import org.apache.logging.log4j.Logger; |
| 11 | +import org.elasticsearch.action.ActionListener; |
| 12 | +import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; |
| 13 | +import org.elasticsearch.action.support.master.AcknowledgedResponse; |
| 14 | +import org.elasticsearch.client.Client; |
| 15 | +import org.elasticsearch.cluster.ClusterChangedEvent; |
| 16 | +import org.elasticsearch.cluster.ClusterState; |
| 17 | +import org.elasticsearch.cluster.ClusterStateListener; |
| 18 | +import org.elasticsearch.cluster.service.ClusterService; |
| 19 | +import org.elasticsearch.gateway.GatewayService; |
| 20 | +import org.elasticsearch.xpack.core.transform.transforms.persistence.TransformInternalIndexConstants; |
| 21 | + |
| 22 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 23 | + |
| 24 | +import static org.elasticsearch.xpack.core.ClientHelper.TRANSFORM_ORIGIN; |
| 25 | +import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin; |
| 26 | + |
| 27 | +class TransformClusterStateListener implements ClusterStateListener { |
| 28 | + |
| 29 | + private static final Logger logger = LogManager.getLogger(TransformClusterStateListener.class); |
| 30 | + |
| 31 | + private final Client client; |
| 32 | + private final AtomicBoolean isIndexCreationInProgress = new AtomicBoolean(false); |
| 33 | + |
| 34 | + TransformClusterStateListener(ClusterService clusterService, Client client) { |
| 35 | + this.client = client; |
| 36 | + clusterService.addListener(this); |
| 37 | + logger.debug("Created TransformClusterStateListener"); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void clusterChanged(ClusterChangedEvent event) { |
| 42 | + if (event.state().blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK)) { |
| 43 | + // Wait until the gateway has recovered from disk. |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + // The atomic flag prevents multiple simultaneous attempts to run alias creation |
| 48 | + // if there is a flurry of cluster state updates in quick succession |
| 49 | + if (event.localNodeMaster() && isIndexCreationInProgress.compareAndSet(false, true)) { |
| 50 | + createAuditAliasForDataFrameBWC(event.state(), client, ActionListener.wrap( |
| 51 | + r -> { |
| 52 | + isIndexCreationInProgress.set(false); |
| 53 | + if (r) { |
| 54 | + logger.info("Created alias for deprecated data frame notifications index"); |
| 55 | + } else { |
| 56 | + logger.debug("Skipped creating alias for deprecated data frame notifications index"); |
| 57 | + } |
| 58 | + }, |
| 59 | + e -> { |
| 60 | + isIndexCreationInProgress.set(false); |
| 61 | + logger.error("Error creating alias for deprecated data frame notifications index", e); |
| 62 | + })); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static void createAuditAliasForDataFrameBWC(ClusterState state, Client client, final ActionListener<Boolean> finalListener) { |
| 67 | + |
| 68 | + // check if alias already exists |
| 69 | + if (state.getMetaData().getAliasAndIndexLookup().containsKey(TransformInternalIndexConstants.AUDIT_INDEX_READ_ALIAS)) { |
| 70 | + finalListener.onResponse(false); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + // check if old audit index exists, no need to create the alias if it does not |
| 75 | + if (state.getMetaData().getAliasAndIndexLookup().containsKey(TransformInternalIndexConstants.AUDIT_INDEX_DEPRECATED)) { |
| 76 | + finalListener.onResponse(false); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + final IndicesAliasesRequest request = client.admin().indices().prepareAliases() |
| 81 | + .addAlias(TransformInternalIndexConstants.AUDIT_INDEX_DEPRECATED, TransformInternalIndexConstants.AUDIT_INDEX_READ_ALIAS) |
| 82 | + .request(); |
| 83 | + |
| 84 | + executeAsyncWithOrigin(client.threadPool().getThreadContext(), TRANSFORM_ORIGIN, request, |
| 85 | + ActionListener.<AcknowledgedResponse>wrap(r -> finalListener.onResponse(r.isAcknowledged()), finalListener::onFailure), |
| 86 | + client.admin().indices()::aliases); |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments