【源码品读】负载均衡策略解析-2

/**
 * This class essentially contains the RoundRobinRule class defined in the
 * loadbalancer package
 * 
 * @author stonse
 * 
 */
public class ClientConfigEnabledRoundRobinRule extends AbstractLoadBalancerRule {

    RoundRobinRule roundRobinRule = new RoundRobinRule();

    @Override
    public void initWithNiwsConfig(IClientConfig clientConfig) {
        roundRobinRule = new RoundRobinRule();
    }

    @Override
    public void setLoadBalancer(ILoadBalancer lb) {
    	super.setLoadBalancer(lb);
    	roundRobinRule.setLoadBalancer(lb);
    }
    
    @Override
    public Server choose(Object key) {
        if (roundRobinRule != null) {
            return roundRobinRule.choose(key);
        } else {
            throw new IllegalArgumentException(
                    "This class has not been initialized with the RoundRobinRule class");
        }
    }

}
public ServerStats getSingleServerStat(Server server) {
	return getServerStats(server);
}
protected ServerStats getServerStats(Server server) {
	try {
		return serverStatsCache.get(server);
	} catch (ExecutionException e) {
		ServerStats stats = createServerStats(server);
		serverStatsCache.asMap().putIfAbsent(server, stats);
		return serverStatsCache.asMap().get(server);
	}
}
protected ServerStats createServerStats(Server server) {
	ServerStats ss = new ServerStats(this);
	//configure custom settings
	ss.setBufferSize(1000);
	ss.setPublishInterval(1000);                    
	ss.initialize(server);
	return ss;        
}
public boolean isCircuitBreakerTripped(long currentTime) {
	long circuitBreakerTimeout = getCircuitBreakerTimeout();
	if (circuitBreakerTimeout <= 0) {
		return false;
	}
	return circuitBreakerTimeout > currentTime;
}
private long getCircuitBreakerTimeout() {
	long blackOutPeriod = getCircuitBreakerBlackoutPeriod();
	if (blackOutPeriod <= 0) {
		return 0;
	}
	return lastConnectionFailedTimestamp + blackOutPeriod;
}
private long getCircuitBreakerBlackoutPeriod() {
	int failureCount = successiveConnectionFailureCount.get();
	int threshold = connectionFailureThreshold.get();
	if (failureCount < threshold) {
		return 0;
	}
	int diff = (failureCount - threshold) > 16 ? 16 : (failureCount - threshold);
	int blackOutSeconds = (1 << diff) * circuitTrippedTimeoutFactor.get();
	if (blackOutSeconds > maxCircuitTrippedTimeout.get()) {
		blackOutSeconds = maxCircuitTrippedTimeout.get();
	}
	return blackOutSeconds * 1000L;
}
private final CachedDynamicIntProperty connectionFailureThreshold;

com.netflix.loadbalancer.RetryRule

IRule subRule = new RoundRobinRule();
public InterruptTask(long millis) {
		target = Thread.currentThread();
		timer.schedule(this, millis);
}
public Server choose(ILoadBalancer lb, Object key) {
	long requestTime = System.currentTimeMillis();
	long deadline = requestTime + this.maxRetryMillis;
	Server answer = null;
	answer = this.subRule.choose(key);
	if ((answer == null || !answer.isAlive()) && System.currentTimeMillis() < deadline) {
		InterruptTask task = new InterruptTask(deadline - System.currentTimeMillis());

		while(!Thread.interrupted()) {
			answer = this.subRule.choose(key);
			if (answer != null && answer.isAlive() || System.currentTimeMillis() >= deadline) {
				break;
			}

			Thread.yield();
		}

		task.cancel();
	}

	return answer != null && answer.isAlive() ? answer : null;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值