summaryrefslogtreecommitdiffstats
path: root/config-proxy/src/main/java/com/yahoo/vespa/config/proxy/UpstreamConfigSubscriber.java
blob: f8df16cb3d230450fcf33b1ff21afa4af9030d83 (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
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2017 Yahoo Holdings. 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.ConfigSource;
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.log.LogLevel;
import com.yahoo.vespa.config.ConfigKey;
import com.yahoo.yolean.Exceptions;
import com.yahoo.vespa.config.RawConfig;
import com.yahoo.vespa.config.TimingValues;

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

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

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

    private final RawConfig config;
    private final ConfigSourceClient configSourceClient;
    private final ConfigSource configSourceSet;
    private final TimingValues timingValues;
    private final Map<ConfigSourceSet, JRTConfigRequester> requesterPool;
    private final MemoryCache memoryCache;
    private GenericConfigSubscriber subscriber;
    private GenericConfigHandle handle;

    UpstreamConfigSubscriber(RawConfig config, ConfigSourceClient configSourceClient, ConfigSource configSourceSet,
                             TimingValues timingValues, Map<ConfigSourceSet, JRTConfigRequester> requesterPool,
                             MemoryCache memoryCache) {
        this.config = config;
        this.configSourceClient = configSourceClient;
        this.configSourceSet = configSourceSet;
        this.timingValues = timingValues;
        this.requesterPool = requesterPool;
        this.memoryCache = memoryCache;
    }

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

    @Override
    public void run() {
        do {
            if (! subscriber.nextGeneration()) continue;

            try {
                updateWithNewConfig(handle);
            } catch (Exception e) {  // To avoid thread throwing exception and loop never running this again
                log.log(LogLevel.WARNING, "Got exception: " + Exceptions.toMessageString(e));
            } catch (Throwable e) {
                com.yahoo.protect.Process.logAndDie("Got error, exiting: " + Exceptions.toMessageString(e));
            }
        } while (!subscriber.isClosed());
    }

    private void updateWithNewConfig(GenericConfigHandle handle) {
        RawConfig newConfig = handle.getRawConfig();
        log.log(LogLevel.DEBUG, () -> "config to be returned for '" + newConfig.getKey() +
                "', generation=" + newConfig.getGeneration() +
                ", payload=" + newConfig.getPayload());
        memoryCache.update(newConfig);
        configSourceClient.updateSubscribers(newConfig);
    }

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

}