summaryrefslogtreecommitdiffstats
path: root/orchestrator/src/main/java/com/yahoo/vespa/orchestrator/controller/RetryingClusterControllerClientFactory.java
blob: 7a0b96c2231c43be1bc0b46531d91ec7d4c0769c (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.orchestrator.controller;

import com.google.inject.Inject;
import com.yahoo.vespa.applicationmodel.HostName;
import com.yahoo.vespa.jaxrs.client.JaxRsClientFactory;
import com.yahoo.vespa.jaxrs.client.JaxRsStrategy;
import com.yahoo.vespa.jaxrs.client.JaxRsStrategyFactory;
import com.yahoo.vespa.jaxrs.client.JerseyJaxRsClientFactory;

import java.util.HashSet;
import java.util.List;

/**
 * @author bakksjo
 */
public class RetryingClusterControllerClientFactory implements ClusterControllerClientFactory {

    // TODO: Figure this port out dynamically.
    public static final int HARDCODED_CLUSTERCONTROLLER_PORT = 19050;
    public static final String CLUSTERCONTROLLER_API_PATH = "/";
    public static final String CLUSTERCONTROLLER_SCHEME = "http";

    private JaxRsClientFactory jaxRsClientFactory;

    @Inject
    public RetryingClusterControllerClientFactory() {
        this(new JerseyJaxRsClientFactory());
    }

    public RetryingClusterControllerClientFactory(JaxRsClientFactory jaxRsClientFactory) {
        this.jaxRsClientFactory = jaxRsClientFactory;
    }

    @Override
    public ClusterControllerClient createClient(List<HostName> clusterControllers, String clusterName) {
        JaxRsStrategy<ClusterControllerJaxRsApi> jaxRsApi =
                new JaxRsStrategyFactory(
                        new HashSet<>(clusterControllers),
                        HARDCODED_CLUSTERCONTROLLER_PORT,
                        jaxRsClientFactory,
                        CLUSTERCONTROLLER_SCHEME)
                        .apiWithRetries(ClusterControllerJaxRsApi.class, CLUSTERCONTROLLER_API_PATH)
                        // Use max iteration 1. The JaxRsStrategyFactory will try host 1, 2, then 3:
                        //  - If host 1 responds, it will redirect to master if necessary. Otherwise
                        //  - If host 2 responds, it will redirect to master if necessary. Otherwise
                        //  - If host 3 responds, it may redirect to master if necessary (if they're up
                        //    after all), but more likely there's no quorum and this will fail too.
                        .setMaxIterations(1);
        return new ClusterControllerClientImpl(jaxRsApi, clusterName);
    }
}