DynamicConfigurationFactory { public synchronized Optional<DynamicDistributedLogConfiguration> getDynamicConfiguration( String configPath, ConcurrentBaseConfiguration defaultConf) throws ConfigurationException { Preconditions.checkNotNull(configPath); try { if (!dynamicConfigs.containsKey(configPath)) { File configFile = new File(configPath); FileConfigurationBuilder properties = new PropertiesConfigurationBuilder(configFile.toURI().toURL()); DynamicDistributedLogConfiguration dynConf = new DynamicDistributedLogConfiguration(defaultConf); List<FileConfigurationBuilder> fileConfigBuilders = Lists.newArrayList(properties); ConfigurationSubscription subscription = new ConfigurationSubscription( dynConf, fileConfigBuilders, executorService, reloadPeriod, reloadUnit); subscriptions.add(subscription); dynamicConfigs.put(configPath, dynConf); LOG.info("Loaded dynamic configuration at {}", configPath); } return Optional.of(dynamicConfigs.get(configPath)); } catch (MalformedURLException ex) { throw new ConfigurationException(ex); } } DynamicConfigurationFactory(ScheduledExecutorService executorService, int reloadPeriod, TimeUnit reloadUnit); synchronized Optional<DynamicDistributedLogConfiguration> getDynamicConfiguration(
            String configPath,
            ConcurrentBaseConfiguration defaultConf); synchronized Optional<DynamicDistributedLogConfiguration> getDynamicConfiguration(String configPath);  }
@Test(timeout = 60000) public void testGetDynamicConfigBasics() throws Exception { PropertiesWriter writer = new PropertiesWriter(); DynamicConfigurationFactory factory = getConfigFactory(writer.getFile()); Optional<DynamicDistributedLogConfiguration> conf = factory.getDynamicConfiguration(writer.getFile().getPath()); assertEquals(DistributedLogConfiguration.BKDL_RETENTION_PERIOD_IN_HOURS_DEFAULT, conf.get().getRetentionPeriodHours()); writer.setProperty(DistributedLogConfiguration.BKDL_RETENTION_PERIOD_IN_HOURS, "1"); writer.save(); waitForConfig(conf.get(), 1); assertEquals(1, conf.get().getRetentionPeriodHours()); }
@Test(timeout = 60000) public void testGetDynamicConfigIsSingleton() throws Exception { PropertiesWriter writer = new PropertiesWriter(); DynamicConfigurationFactory factory = getConfigFactory(writer.getFile()); String configPath = writer.getFile().getPath(); Optional<DynamicDistributedLogConfiguration> conf1 = factory.getDynamicConfiguration(configPath); Optional<DynamicDistributedLogConfiguration> conf2 = factory.getDynamicConfiguration(configPath); assertEquals(conf1, conf2); }
@Test(timeout = 60000) public void testMissingConfig() throws Exception { PropertiesWriter writer = new PropertiesWriter(); DynamicConfigurationFactory factory = getConfigFactory(writer.getFile()); Optional<DynamicDistributedLogConfiguration> conf = factory.getDynamicConfiguration(writer.getFile().getPath()); writer.setProperty(DistributedLogConfiguration.BKDL_RETENTION_PERIOD_IN_HOURS, "1"); writer.save(); waitForConfig(conf.get(), 1); File configFile = writer.getFile(); configFile.delete(); Thread.sleep(1000); PropertiesWriter writer2 = new PropertiesWriter(writer.getFile()); writer2.setProperty(DistributedLogConfiguration.BKDL_RETENTION_PERIOD_IN_HOURS, "2"); writer2.save(); waitForConfig(conf.get(), 2); }