/* * * Copyright 2013 Netflix, Inc. * * Licensed 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 com.netflix.loadbalancer; import javax.annotation.Nullable; import com.netflix.client.config.IClientConfig; import com.netflix.config.ChainedDynamicProperty; import com.netflix.config.DynamicBooleanProperty; import com.netflix.config.DynamicIntProperty; import com.netflix.config.DynamicPropertyFactory; /** * Predicate with the logic of filtering out circuit breaker tripped servers and servers * with too many concurrent connections from this client. * * @author awang * */ public class AvailabilityPredicate extends AbstractServerPredicate { private static final DynamicBooleanProperty CIRCUIT_BREAKER_FILTERING = DynamicPropertyFactory.getInstance().getBooleanProperty("niws.loadbalancer.availabilityFilteringRule.filterCircuitTripped", true); private static final DynamicIntProperty ACTIVE_CONNECTIONS_LIMIT = DynamicPropertyFactory.getInstance().getIntProperty("niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit", Integer.MAX_VALUE); private ChainedDynamicProperty.IntProperty activeConnectionsLimit = new ChainedDynamicProperty.IntProperty(ACTIVE_CONNECTIONS_LIMIT); public AvailabilityPredicate(IRule rule, IClientConfig clientConfig) { super(rule, clientConfig); initDynamicProperty(clientConfig); } public AvailabilityPredicate(LoadBalancerStats lbStats, IClientConfig clientConfig) { super(lbStats, clientConfig); initDynamicProperty(clientConfig); } AvailabilityPredicate(IRule rule) { super(rule); } private void initDynamicProperty(IClientConfig clientConfig) { String id = "default"; if (clientConfig != null) { id = clientConfig.getClientName(); activeConnectionsLimit = new ChainedDynamicProperty.IntProperty(id + "." + clientConfig.getNameSpace() + ".ActiveConnectionsLimit", ACTIVE_CONNECTIONS_LIMIT); } } @Override public boolean apply(@Nullable PredicateKey input) { LoadBalancerStats stats = getLBStats(); if (stats == null) { return true; } return !shouldSkipServer(stats.getSingleServerStat(input.getServer())); } private boolean shouldSkipServer(ServerStats stats) { if ((CIRCUIT_BREAKER_FILTERING.get() && stats.isCircuitBreakerTripped()) || stats.getActiveRequestsCount() >= activeConnectionsLimit.get()) { return true; } return false; } }