/* * Licensed to ElasticSearch and Shay Banon under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. ElasticSearch licenses this * file to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.elasticsearch.cluster.routing.allocation.decider; import com.google.common.collect.ImmutableMap; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.node.DiscoveryNodeFilters; import org.elasticsearch.cluster.routing.RoutingNode; import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.cluster.routing.allocation.RoutingAllocation; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.node.settings.NodeSettingsService; /** */ public class FilterAllocationDecider extends ServerAllocationDecider { static { MetaData.addDynamicSettings( "cluster.routing.allocation.include.*", "cluster.routing.allocation.exclude.*" ); IndexMetaData.addDynamicSettings( "index.routing.allocation.include.*", "index.routing.allocation.exclude.*" ); } private volatile DiscoveryNodeFilters clusterIncludeFilters; private volatile DiscoveryNodeFilters clusterExcludeFilters; public FilterAllocationDecider() { } @Inject public FilterAllocationDecider(Settings settings, NodeSettingsService nodeSettingsService) { super(settings); ImmutableMap<String, String> includeMap = settings.getByPrefix("cluster.routing.allocation.include.").getAsMap(); if (includeMap.isEmpty()) { clusterIncludeFilters = null; } else { clusterIncludeFilters = DiscoveryNodeFilters.buildFromKeyValue(includeMap); } ImmutableMap<String, String> excludeMap = settings.getByPrefix("cluster.routing.allocation.exclude.").getAsMap(); if (excludeMap.isEmpty()) { clusterExcludeFilters = null; } else { clusterExcludeFilters = DiscoveryNodeFilters.buildFromKeyValue(excludeMap); } nodeSettingsService.addListener(new ApplySettings()); } @Override public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { return shouldFilter(shardRouting, node, allocation) ? Decision.NO : Decision.YES; } @Override public boolean canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { return !shouldFilter(shardRouting, node, allocation); } private boolean shouldFilter(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { if (clusterIncludeFilters != null) { if (!clusterIncludeFilters.match(node.node())) { return true; } } if (clusterExcludeFilters != null) { if (clusterExcludeFilters.match(node.node())) { return true; } } IndexMetaData indexMd = allocation.routingNodes().metaData().index(shardRouting.index()); if (indexMd.includeFilters() != null) { if (!indexMd.includeFilters().match(node.node())) { return true; } } if (indexMd.excludeFilters() != null) { if (indexMd.excludeFilters().match(node.node())) { return true; } } return false; } class ApplySettings implements NodeSettingsService.Listener { @Override public void onRefreshSettings(Settings settings) { ImmutableMap<String, String> includeMap = settings.getByPrefix("cluster.routing.allocation.include.").getAsMap(); if (!includeMap.isEmpty()) { clusterIncludeFilters = DiscoveryNodeFilters.buildFromKeyValue(includeMap); } ImmutableMap<String, String> excludeMap = settings.getByPrefix("cluster.routing.allocation.exclude.").getAsMap(); if (!excludeMap.isEmpty()) { clusterExcludeFilters = DiscoveryNodeFilters.buildFromKeyValue(excludeMap); } } } }