Search in sources :

Example 1 with ConfigurationChangeInProgressException

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();
    }
}
Also used : ConfigurationNotValidException(org.carapaceproxy.server.config.ConfigurationNotValidException) PropertiesConfigurationStore(org.carapaceproxy.configstore.PropertiesConfigurationStore) ConfigurationStore(org.carapaceproxy.configstore.ConfigurationStore) HerdDBConfigurationStore(org.carapaceproxy.configstore.HerdDBConfigurationStore) EndpointMapper(org.carapaceproxy.server.mapper.EndpointMapper) UserRealm(org.carapaceproxy.user.UserRealm) SimpleUserRealm(org.carapaceproxy.user.SimpleUserRealm) ConfigurationChangeInProgressException(org.carapaceproxy.server.config.ConfigurationChangeInProgressException) BackendConfiguration(org.carapaceproxy.server.config.BackendConfiguration)

Example 2 with ConfigurationChangeInProgressException

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);
    }
}
Also used : ConfigurationNotValidException(org.carapaceproxy.server.config.ConfigurationNotValidException) HerdDBConfigurationStore(org.carapaceproxy.configstore.HerdDBConfigurationStore) ConfigurationChangeInProgressException(org.carapaceproxy.server.config.ConfigurationChangeInProgressException)

Example 3 with ConfigurationChangeInProgressException

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);
    }
}
Also used : HttpProxyServer(org.carapaceproxy.core.HttpProxyServer) ConfigurationNotValidException(org.carapaceproxy.server.config.ConfigurationNotValidException) PropertiesConfigurationStore(org.carapaceproxy.configstore.PropertiesConfigurationStore) ConfigurationChangeInProgressException(org.carapaceproxy.server.config.ConfigurationChangeInProgressException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) POST(javax.ws.rs.POST)

Aggregations

ConfigurationChangeInProgressException (org.carapaceproxy.server.config.ConfigurationChangeInProgressException)3 ConfigurationNotValidException (org.carapaceproxy.server.config.ConfigurationNotValidException)3 HerdDBConfigurationStore (org.carapaceproxy.configstore.HerdDBConfigurationStore)2 PropertiesConfigurationStore (org.carapaceproxy.configstore.PropertiesConfigurationStore)2 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 ConfigurationStore (org.carapaceproxy.configstore.ConfigurationStore)1 HttpProxyServer (org.carapaceproxy.core.HttpProxyServer)1 BackendConfiguration (org.carapaceproxy.server.config.BackendConfiguration)1 EndpointMapper (org.carapaceproxy.server.mapper.EndpointMapper)1 SimpleUserRealm (org.carapaceproxy.user.SimpleUserRealm)1 UserRealm (org.carapaceproxy.user.UserRealm)1