use of org.carapaceproxy.server.config.ConfigurationChangeInProgressException in project carapaceproxy by diennea.
the class HttpProxyServer method applyDynamicConfiguration.
private void applyDynamicConfiguration(ConfigurationStore newConfigurationStore, boolean atBoot) throws InterruptedException, ConfigurationChangeInProgressException {
if (atBoot && newConfigurationStore != null) {
throw new IllegalStateException();
}
if (!atBoot && newConfigurationStore == null) {
throw new IllegalStateException();
}
// at boot we are constructing a configuration from the database
// if the system is already "up" we have to only apply the new config
ConfigurationStore storeWithConfig = atBoot ? dynamicConfigurationStore : newConfigurationStore;
if (!configurationLock.tryLock()) {
throw new ConfigurationChangeInProgressException();
}
try {
RuntimeServerConfiguration newConfiguration = buildValidConfiguration(storeWithConfig);
EndpointMapper newMapper = buildMapper(newConfiguration.getMapperClassname(), storeWithConfig);
newMapper.setParent(this);
UserRealm newRealm = buildRealm(userRealmClassname, storeWithConfig);
this.filters = buildFilters(newConfiguration);
this.backendHealthManager.reloadConfiguration(newConfiguration, newMapper);
this.dynamicCertificatesManager.reloadConfiguration(newConfiguration);
this.ocspStaplingManager.reloadConfiguration(newConfiguration);
this.listeners.reloadConfiguration(newConfiguration);
this.cache.reloadConfiguration(newConfiguration);
this.requestsLogger.reloadConfiguration(newConfiguration);
this.realm = newRealm;
Map<String, BackendConfiguration> currentBackends = mapper != null ? mapper.getBackends() : Collections.emptyMap();
Map<String, BackendConfiguration> newBackends = newMapper.getBackends();
this.mapper = newMapper;
if (atBoot || !newBackends.equals(currentBackends) || isConnectionsConfigurationChanged(newConfiguration)) {
prometheusRegistry.clear();
Metrics.globalRegistry.clear();
proxyRequestsManager.reloadConfiguration(newConfiguration, newBackends.values());
}
if (!atBoot) {
dynamicConfigurationStore.commitConfiguration(newConfigurationStore);
}
this.currentConfiguration = newConfiguration;
} catch (ConfigurationNotValidException err) {
// impossible to have a non valid configuration here
throw new IllegalStateException(err);
} finally {
configurationLock.unlock();
}
}
use of org.carapaceproxy.server.config.ConfigurationChangeInProgressException in project carapaceproxy by diennea.
the class HttpProxyServer method configureAtBoot.
public void configureAtBoot(ConfigurationStore bootConfigurationStore) throws ConfigurationNotValidException, InterruptedException {
if (started) {
throw new IllegalStateException("server already started");
}
// need to be always first thing to do (loads cluster setup)
readClusterConfiguration(bootConfigurationStore);
String dynamicConfigurationType = bootConfigurationStore.getString("config.type", cluster ? "database" : "file");
switch(dynamicConfigurationType) {
case "file":
// configuration is store on the same file
this.dynamicConfigurationStore = bootConfigurationStore;
if (cluster) {
throw new IllegalStateException("Cannot use file based configuration in cluster mode");
}
break;
case "database":
this.dynamicConfigurationStore = new HerdDBConfigurationStore(bootConfigurationStore, cluster, zkAddress, basePath, mainLogger);
break;
default:
throw new ConfigurationNotValidException("invalid config.type='" + dynamicConfigurationType + "', only 'file' and 'database' are supported");
}
this.dynamicCertificatesManager.setConfigurationStore(dynamicConfigurationStore);
// "static" configuration cannot change without a reboot
applyStaticConfiguration(bootConfigurationStore);
// need to be done after static configuration loading in order to know peer info
initGroupMembership();
try {
// apply configuration
// this can cause database configuration to be overwritten with
// configuration from service configuration file
applyDynamicConfiguration(null, true);
} catch (ConfigurationChangeInProgressException impossible) {
throw new IllegalStateException(impossible);
}
}
use of org.carapaceproxy.server.config.ConfigurationChangeInProgressException in project carapaceproxy by diennea.
the class ConfigResource method apply.
@Path("/apply")
@Consumes(value = "text/plain")
@POST
public ConfigurationChangeResult apply(String newConfiguration) {
LOG.info("Apply configuration from API:");
LOG.info(newConfiguration);
LOG.info("**");
HttpProxyServer server = (HttpProxyServer) context.getAttribute("server");
try {
PropertiesConfigurationStore simpleStore = buildStore(newConfiguration);
dumpAndValidateInputConfiguration(simpleStore);
server.applyDynamicConfigurationFromAPI(simpleStore);
return ConfigurationChangeResult.OK;
} catch (ConfigurationNotValidException | ConfigurationChangeInProgressException | RuntimeException err) {
return ConfigurationChangeResult.error(err);
} catch (InterruptedException err) {
Thread.currentThread().interrupt();
return ConfigurationChangeResult.error(err);
}
}
Aggregations