summaryrefslogtreecommitdiffstats
path: root/config-proxy/src/main/java/com/yahoo/vespa/config/proxy/Subscriber.java
blob: 70ff4456f6cb1600cbe9ccc000bda2a236699d64 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.proxy;

import com.yahoo.config.subscription.ConfigSourceSet;
import com.yahoo.config.subscription.impl.GenericConfigHandle;
import com.yahoo.config.subscription.impl.GenericConfigSubscriber;
import com.yahoo.config.subscription.impl.JRTConfigRequester;
import com.yahoo.vespa.config.ConfigKey;
import com.yahoo.vespa.config.RawConfig;
import com.yahoo.vespa.config.TimingValues;
import com.yahoo.yolean.Exceptions;

import java.util.Map;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author hmusum
 */
public class Subscriber {

    private final static Logger log = Logger.getLogger(Subscriber.class.getName());

    private final RawConfig config;
    private final ConfigSourceSet configSourceSet;
    private final TimingValues timingValues;
    private final GenericConfigSubscriber subscriber;
    private GenericConfigHandle handle;

    Subscriber(RawConfig config, ConfigSourceSet configSourceSet, TimingValues timingValues, JRTConfigRequester requester) {
        this.config = config;
        this.configSourceSet = configSourceSet;
        this.timingValues = timingValues;
        this.subscriber = new GenericConfigSubscriber(Map.of(configSourceSet, requester));
    }

    void subscribe() {
        ConfigKey<?> key = config.getKey();
        handle = subscriber.subscribe(new ConfigKey<>(key.getName(), key.getConfigId(), key.getNamespace()),
                                      config.getDefContent(), configSourceSet, timingValues);
    }

    public Optional<RawConfig> nextGeneration() {
        try {
            // 'isInitializing' argument to nextGeneration() is true, config proxy should never skip config due to not initializing
            if (subscriber.nextGeneration(0, true)) {
                RawConfig rawConfig = handle.getRawConfig();
                if (rawConfig == null)
                    log.log(Level.SEVERE, "Config for " + config.getKey() + " is null");
                return Optional.ofNullable(rawConfig);
            }
        } catch (Exception e) {  // To avoid thread throwing exception and loop never running this again
            log.log(Level.WARNING, "Got exception: " + Exceptions.toMessageString(e));
        } catch (Throwable e) {
            com.yahoo.protect.Process.logAndDie("Got error, exiting: " + Exceptions.toMessageString(e));
        }
        return Optional.empty();
    }

    public void cancel() {
        if (subscriber != null) {
            subscriber.close();
        }
    }

    boolean isClosed() {
        return subscriber.isClosed();
    }

}