/** * We should update if we have no prior state, or the state's generation id is older than the * current cluster generation, or the state was last updated more than _updateIntervalMs ago. * * <p>Now requiring shouldUpdate to take a DegraderLoadBalancerState because we must have a static * view of the state, and we don't want the state to change between checking if we should update * and actually updating. * * @param clusterGenerationId The cluster generation for a set of tracker clients * @param currentState Current DegraderLoadBalancerState * @param config Current DegraderLoadBalancerStrategyConfig * @param updateEnabled Whether updates to the strategy state is allowed. * @return True if we should update, and false otherwise. */ protected static boolean shouldUpdate( long clusterGenerationId, DegraderLoadBalancerState currentState, DegraderLoadBalancerStrategyConfig config, boolean updateEnabled) { return updateEnabled && (((currentState.getClusterGenerationId() != clusterGenerationId || config.getClock().currentTimeMillis() - currentState.getLastUpdated() >= config.getUpdateIntervalMs() || currentState.getClusterGenerationId() == -1))); }
// for unit testing, this allows the strategy to be forced for the next time updateState // is called. This is not to be used in prod code. void setStrategy(DegraderLoadBalancerState.Strategy strategy) { DegraderLoadBalancerState newState; newState = new DegraderLoadBalancerState( _state.getUpdateIntervalMs(), _state.getClusterGenerationId(), _state.getPointsMap(), _state.getLastUpdated(), strategy, _state.getCurrentOverrideDropRate(), _state.getCurrentAvgClusterLatency(), _state.isInitialized(), _state.getRecoveryMap(), _state.getServiceName(), _state.getDegraderProperties(), _state.getCurrentClusterCallCount()); _state = newState; }