Search in sources :

Example 1 with ShadowRedisServerDisableEvent

use of com.pamirs.pradar.pressurement.agent.event.impl.ShadowRedisServerDisableEvent in project LinkAgent by shulieTech.

the class RedisShadowServerConfig method compareIsChangeAndSet.

@Override
public Boolean compareIsChangeAndSet(ApplicationConfig applicationConfig, Map<String, ShadowRedisConfig> newConfig) {
    Map<String, ShadowRedisConfig> old = GlobalConfig.getInstance().getShadowRedisConfigs();
    if (old.size() == 0 && newConfig.size() == 0) {
        return Boolean.FALSE;
    } else if (old.size() == 0 && newConfig.size() != 0) {
        GlobalConfig.getInstance().setShadowRedisConfigs(newConfig);
        applicationConfig.setShadowRedisConfigs(newConfig);
        return Boolean.TRUE;
    } else if (old.size() != 0 && newConfig.size() == 0) {
        GlobalConfig.getInstance().setShadowRedisConfigs(newConfig);
        applicationConfig.setShadowRedisConfigs(newConfig);
        for (Map.Entry<String, ShadowRedisConfig> entry : old.entrySet()) {
            ShadowRedisConfig oldValue = entry.getValue();
            ShadowRedisServerDisableEvent event = new ShadowRedisServerDisableEvent(Collections.singletonList(oldValue));
            EventRouter.router().publish(event);
        }
        return Boolean.TRUE;
    } else if (old.size() != 0 && newConfig.size() != 0) {
        Boolean change = false;
        if (old.size() != newConfig.size()) {
            change = true;
        }
        List<ShadowRedisConfig> removeKeys = new ArrayList<ShadowRedisConfig>();
        for (Map.Entry entry : old.entrySet()) {
            String oldKey = String.valueOf(entry.getKey());
            ShadowRedisConfig oldValue = (ShadowRedisConfig) entry.getValue();
            if (!newConfig.containsKey(oldKey)) {
                change = true;
                removeKeys.add(oldValue);
            } else if (newConfig.containsKey(oldKey)) {
                if (!oldValue.equals(newConfig.get(oldKey))) {
                    removeKeys.add(oldValue);
                    change = true;
                }
            }
        }
        if (removeKeys.size() > 0) {
            EventRouter.router().publish(new ShadowRedisServerDisableEvent(removeKeys));
        }
        if (change) {
            GlobalConfig.getInstance().setShadowRedisConfigs(newConfig);
            applicationConfig.setShadowRedisConfigs(newConfig);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
    return Boolean.FALSE;
}
Also used : ShadowRedisServerDisableEvent(com.pamirs.pradar.pressurement.agent.event.impl.ShadowRedisServerDisableEvent) List(java.util.List) ArrayList(java.util.ArrayList) ShadowRedisConfig(com.pamirs.pradar.internal.config.ShadowRedisConfig) Map(java.util.Map)

Aggregations

ShadowRedisConfig (com.pamirs.pradar.internal.config.ShadowRedisConfig)1 ShadowRedisServerDisableEvent (com.pamirs.pradar.pressurement.agent.event.impl.ShadowRedisServerDisableEvent)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1