private void monitorDnsChange(final SingleServerConfig cfg) { monitorFuture = GlobalEventExecutor.INSTANCE.scheduleWithFixedDelay( new Runnable() { @Override public void run() { try { InetAddress master = currentMaster.get(); InetAddress now = InetAddress.getByName(cfg.getAddress().getHost()); if (!now.getHostAddress().equals(master.getHostAddress())) { log.info( "Detected DNS change. {} has changed from {} to {}", cfg.getAddress().getHost(), master.getHostAddress(), now.getHostAddress()); if (currentMaster.compareAndSet(master, now)) { changeMaster( MAX_SLOT, cfg.getAddress().getHost(), cfg.getAddress().getPort()); log.info("Master has been changed"); } } } catch (Exception e) { log.error(e.getMessage(), e); } } }, cfg.getDnsMonitoringInterval(), cfg.getDnsMonitoringInterval(), TimeUnit.MILLISECONDS); }
public SingleConnectionManager(SingleServerConfig cfg, Config config) { MasterSlaveServersConfig newconfig = new MasterSlaveServersConfig(); String addr = cfg.getAddress().getHost() + ":" + cfg.getAddress().getPort(); newconfig.setRetryAttempts(cfg.getRetryAttempts()); newconfig.setRetryInterval(cfg.getRetryInterval()); newconfig.setTimeout(cfg.getTimeout()); newconfig.setPingTimeout(cfg.getPingTimeout()); newconfig.setPassword(cfg.getPassword()); newconfig.setDatabase(cfg.getDatabase()); newconfig.setClientName(cfg.getClientName()); newconfig.setMasterAddress(addr); newconfig.setMasterConnectionPoolSize(cfg.getConnectionPoolSize()); newconfig.setSubscriptionsPerConnection(cfg.getSubscriptionsPerConnection()); newconfig.setSlaveSubscriptionConnectionPoolSize(cfg.getSubscriptionConnectionPoolSize()); init(newconfig, config); if (cfg.isDnsMonitoring()) { try { this.currentMaster.set(InetAddress.getByName(cfg.getAddress().getHost())); } catch (UnknownHostException e) { throw new RedisConnectionException("Unknown host", e); } log.debug("DNS monitoring enabled; Current master set to {}", currentMaster.get()); monitorDnsChange(cfg); } }