Search in sources :

Example 1 with ConfigListener

use of com.netflix.archaius.api.ConfigListener in project titus-control-plane by Netflix.

the class JobRuntimePredictionSelectors method reloadedOnConfigurationUpdate.

/**
 * A helper selector which listens to configuration changes, and reloads downstream selectors when that happens.
 */
public static CloseableReference<JobRuntimePredictionSelector> reloadedOnConfigurationUpdate(Config config, Function<Config, JobRuntimePredictionSelector> factory) {
    AtomicReference<JobRuntimePredictionSelector> last = new AtomicReference<>(factory.apply(config));
    ConfigListener listener = new ConfigListener() {

        @Override
        public void onConfigAdded(Config config) {
            tryReload(config);
        }

        @Override
        public void onConfigRemoved(Config config) {
            tryReload(config);
        }

        @Override
        public void onConfigUpdated(Config config) {
            tryReload(config);
        }

        @Override
        public void onError(Throwable error, Config config) {
            tryReload(config);
        }

        private void tryReload(Config updated) {
            try {
                // Use the past config, in case it is prefixed view
                last.set(factory.apply(config));
            } catch (Exception e) {
                logger.info("Failed to reload job runtime prediction selectors", e);
            }
        }
    };
    config.addListener(listener);
    JobRuntimePredictionSelector wrapper = (jobDescriptor, predictions) -> last.get().apply(jobDescriptor, predictions);
    return CloseableReference.<JobRuntimePredictionSelector>newBuilder().withResource(wrapper).withCloseAction(() -> config.removeListener(listener)).withSwallowException(true).withSerialize(true).build();
}
Also used : DateTimeExt(com.netflix.titus.common.util.DateTimeExt) Arrays(java.util.Arrays) Archaius2Ext(com.netflix.titus.common.util.archaius2.Archaius2Ext) SortedSet(java.util.SortedSet) CollectionsExt(com.netflix.titus.common.util.CollectionsExt) JOB_ATTRIBUTES_RUNTIME_PREDICTION_SELECTOR_INFO(com.netflix.titus.api.jobmanager.JobAttributes.JOB_ATTRIBUTES_RUNTIME_PREDICTION_SELECTOR_INFO) LoggerFactory(org.slf4j.LoggerFactory) JobRuntimePrediction(com.netflix.titus.runtime.connector.prediction.JobRuntimePrediction) HashMap(java.util.HashMap) Random(java.util.Random) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) TreeSet(java.util.TreeSet) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) CloseableReference(com.netflix.titus.common.util.closeable.CloseableReference) ConfigListener(com.netflix.archaius.api.ConfigListener) Map(java.util.Map) Logger(org.slf4j.Logger) Config(com.netflix.archaius.api.Config) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) Optional(java.util.Optional) PropertiesExt(com.netflix.titus.common.util.PropertiesExt) Collections(java.util.Collections) JobAttributes(com.netflix.titus.api.jobmanager.JobAttributes) Config(com.netflix.archaius.api.Config) ConfigListener(com.netflix.archaius.api.ConfigListener) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Aggregations

Iterators (com.google.common.collect.Iterators)1 Config (com.netflix.archaius.api.Config)1 ConfigListener (com.netflix.archaius.api.ConfigListener)1 JobAttributes (com.netflix.titus.api.jobmanager.JobAttributes)1 JOB_ATTRIBUTES_RUNTIME_PREDICTION_SELECTOR_INFO (com.netflix.titus.api.jobmanager.JobAttributes.JOB_ATTRIBUTES_RUNTIME_PREDICTION_SELECTOR_INFO)1 CollectionsExt (com.netflix.titus.common.util.CollectionsExt)1 DateTimeExt (com.netflix.titus.common.util.DateTimeExt)1 PropertiesExt (com.netflix.titus.common.util.PropertiesExt)1 Archaius2Ext (com.netflix.titus.common.util.archaius2.Archaius2Ext)1 CloseableReference (com.netflix.titus.common.util.closeable.CloseableReference)1 JobRuntimePrediction (com.netflix.titus.runtime.connector.prediction.JobRuntimePrediction)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Random (java.util.Random)1 Set (java.util.Set)1