summaryrefslogtreecommitdiffstats
path: root/clustercontroller-standalone/src/main/java/com/yahoo/vespa/clustercontroller/standalone/ClusterControllerConfigFetcher.java
blob: 64faeeefb0bad017f03046f888e8af00c48be8f7 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * Used to manually fetch config for the stand alone application.
 */
package com.yahoo.vespa.clustercontroller.standalone;

import com.yahoo.vespa.clustercontroller.apps.clustercontroller.ClusterControllerClusterConfigurer;
import com.yahoo.vespa.clustercontroller.core.FleetControllerOptions;
import com.yahoo.vespa.config.content.FleetcontrollerConfig;
import com.yahoo.vespa.config.content.StorDistributionConfig;
import com.yahoo.cloud.config.SlobroksConfig;
import com.yahoo.config.subscription.ConfigSubscriber;
import com.yahoo.config.subscription.ConfigHandle;
import com.yahoo.cloud.config.ZookeepersConfig;

import java.util.logging.Logger;

public class ClusterControllerConfigFetcher {
    private static Logger log = Logger.getLogger(ClusterControllerConfigFetcher.class.getName());

    private final ConfigSubscriber configSubscriber = new ConfigSubscriber();
    private final ConfigHandle<FleetcontrollerConfig> fleetcontrollerConfigHandle;
    private final ConfigHandle<SlobroksConfig> slobrokConfigHandle;
    private final ConfigHandle<StorDistributionConfig> distributionConfigHandle;
    private final ConfigHandle<ZookeepersConfig> zookeeperConfigHandle;

    private FleetControllerOptions options;

    public ClusterControllerConfigFetcher() throws Exception {
        String configId = createConfigId();
        log.fine("Using fleetcontroller config id \"" + configId + '"');
        String slobrokConfigId = createSlobrokConfigId();
        log.fine("Using slobrok config id \"" + slobrokConfigId + '"');

        fleetcontrollerConfigHandle = configSubscriber.subscribe(FleetcontrollerConfig.class, configId);
        slobrokConfigHandle = configSubscriber.subscribe(SlobroksConfig.class, slobrokConfigId);
        distributionConfigHandle = configSubscriber.subscribe(StorDistributionConfig.class, configId);
        zookeeperConfigHandle = configSubscriber.subscribe(ZookeepersConfig.class, configId);

        if (!configReady()) {
            throw new IllegalStateException("Initial configuration failed.");
        }
        options = generateOptions();
    }

    public void close() {
        log.fine("Shutting down fleetcontroller config subscription");
        configSubscriber.close();
    }

    private String createConfigId() {
        return System.getProperty("config.id");
    }

    private String createSlobrokConfigId() {
        return System.getProperty("slobrok.config.id");
    }

    public FleetControllerOptions getOptions() {
        return options;
    }

    public FleetControllerOptions generateOptions() throws Exception {
        ClusterControllerClusterConfigurer configurer = new ClusterControllerClusterConfigurer(
                null,
                distributionConfigHandle.getConfig(),
                fleetcontrollerConfigHandle.getConfig(),
                slobrokConfigHandle.getConfig(),
                zookeeperConfigHandle.getConfig(),
                null);
        return configurer.getOptions();
    }

    /** Test to see if the config has been updated, and if so, update the config. */
    public boolean updated(long timeoutMillis) throws Exception {
        if (configUpdated(timeoutMillis)) {
            log.fine("Updated fleetcontroller config.");
            options = generateOptions();
            return true;
        } else {
            return false;
        }
    }

    public long getGeneration() {
        return configSubscriber.getGeneration();
    }

    boolean configReady() {
        return configSubscriber.nextConfig();
    }

    boolean configUpdated(long timeoutMillis) {
        return configSubscriber.nextConfig(timeoutMillis);
    }
}