Search in sources :

Example 1 with UpdateSettingsClusterStateUpdateRequest

use of org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsClusterStateUpdateRequest in project elasticsearch by elastic.

the class MetaDataUpdateSettingsService method clusterChanged.

@Override
public void clusterChanged(ClusterChangedEvent event) {
    // update an index with number of replicas based on data nodes if possible
    if (!event.state().nodes().isLocalNodeElectedMaster()) {
        return;
    }
    // we will want to know this for translating "all" to a number
    final int dataNodeCount = event.state().nodes().getDataNodes().size();
    Map<Integer, List<Index>> nrReplicasChanged = new HashMap<>();
    // we need to do this each time in case it was changed by update settings
    for (final IndexMetaData indexMetaData : event.state().metaData()) {
        AutoExpandReplicas autoExpandReplicas = IndexMetaData.INDEX_AUTO_EXPAND_REPLICAS_SETTING.get(indexMetaData.getSettings());
        if (autoExpandReplicas.isEnabled()) {
            /*
                 * we have to expand the number of replicas for this index to at least min and at most max nodes here
                 * so we are bumping it up if we have to or reduce it depending on min/max and the number of datanodes.
                 * If we change the number of replicas we just let the shard allocator do it's thing once we updated it
                 * since it goes through the index metadata to figure out if something needs to be done anyway. Do do that
                 * we issue a cluster settings update command below and kicks off a reroute.
                 */
            final int min = autoExpandReplicas.getMinReplicas();
            final int max = autoExpandReplicas.getMaxReplicas(dataNodeCount);
            int numberOfReplicas = dataNodeCount - 1;
            if (numberOfReplicas < min) {
                numberOfReplicas = min;
            } else if (numberOfReplicas > max) {
                numberOfReplicas = max;
            }
            // same value, nothing to do there
            if (numberOfReplicas == indexMetaData.getNumberOfReplicas()) {
                continue;
            }
            if (numberOfReplicas >= min && numberOfReplicas <= max) {
                if (!nrReplicasChanged.containsKey(numberOfReplicas)) {
                    nrReplicasChanged.put(numberOfReplicas, new ArrayList<>());
                }
                nrReplicasChanged.get(numberOfReplicas).add(indexMetaData.getIndex());
            }
        }
    }
    if (nrReplicasChanged.size() > 0) {
        // update settings and kick of a reroute (implicit) for them to take effect
        for (final Integer fNumberOfReplicas : nrReplicasChanged.keySet()) {
            Settings settings = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, fNumberOfReplicas).build();
            final List<Index> indices = nrReplicasChanged.get(fNumberOfReplicas);
            UpdateSettingsClusterStateUpdateRequest updateRequest = new UpdateSettingsClusterStateUpdateRequest().indices(indices.toArray(new Index[indices.size()])).settings(settings).ackTimeout(//no need to wait for ack here
            TimeValue.timeValueMillis(0)).masterNodeTimeout(TimeValue.timeValueMinutes(10));
            updateSettings(updateRequest, new ActionListener<ClusterStateUpdateResponse>() {

                @Override
                public void onResponse(ClusterStateUpdateResponse response) {
                    for (Index index : indices) {
                        logger.info("{} auto expanded replicas to [{}]", index, fNumberOfReplicas);
                    }
                }

                @Override
                public void onFailure(Exception t) {
                    for (Index index : indices) {
                        logger.warn("{} fail to auto expand replicas to [{}]", index, fNumberOfReplicas);
                    }
                }
            });
        }
    }
}
Also used : HashMap(java.util.HashMap) Index(org.elasticsearch.index.Index) UpdateSettingsClusterStateUpdateRequest(org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsClusterStateUpdateRequest) IOException(java.io.IOException) ArrayList(java.util.ArrayList) List(java.util.List) ClusterStateUpdateResponse(org.elasticsearch.cluster.ack.ClusterStateUpdateResponse) Settings(org.elasticsearch.common.settings.Settings) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 UpdateSettingsClusterStateUpdateRequest (org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsClusterStateUpdateRequest)1 ClusterStateUpdateResponse (org.elasticsearch.cluster.ack.ClusterStateUpdateResponse)1 IndexScopedSettings (org.elasticsearch.common.settings.IndexScopedSettings)1 Settings (org.elasticsearch.common.settings.Settings)1 Index (org.elasticsearch.index.Index)1