summaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/configserver/ConfigServerApiImpl.java
blob: c29028c493ec887d13e04037e86718fc76d829ba (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.configserver;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yahoo.config.provision.HostName;
import com.yahoo.vespa.hosted.node.admin.component.ConfigServerInfo;
import com.yahoo.vespa.hosted.node.admin.util.PrefixLogger;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * Retries request on config server a few times before giving up. Assumes that all requests should be sent with
 * content-type application/json
 *
 * @author dybdahl
 */
public class ConfigServerApiImpl implements ConfigServerApi {
    private static final PrefixLogger NODE_ADMIN_LOGGER = PrefixLogger.getNodeAdminLogger(ConfigServerApiImpl.class);

    private final ObjectMapper mapper = new ObjectMapper();

    private final List<URI> configServerHosts;

    // If this instance is associated with asynchronous updating, this field is set
    // to unregister from the updater at close()
    private Optional<SslConnectionSocketFactoryUpdater> socketFactoryUpdater = Optional.empty();

    /**
     * The 'client' will be periodically re-created by clientRefresherScheduler if we provide keyStoreOptions
     * or trustStoreOptions. This is needed because the key/trust stores are updated outside of node-admin,
     * but we want to use the most recent store.
     *
     * The 'client' reference must be volatile because it is set and read in different threads, and visibility
     * of changes is only guaranteed for volatile variables.
     */
    private volatile SelfCloseableHttpClient client;

    /**
     * Creates an api for talking to the config servers.
     *
     * <p>Registers with a SslConnectionSocketFactoryUpdater to keep the socket factory
     * up to date as e.g. any client certificate expires (and unregisters at {@link #close()})
     */
    public static ConfigServerApiImpl create(ConfigServerInfo configServerInfo,
                                             SslConnectionSocketFactoryUpdater updater) {
        return createFor(updater, configServerInfo.getConfigServerUris());
    }

    /**
     * Creates an api for talking to a single config server.
     *
     * <p>Registers with a SslConnectionSocketFactoryUpdater to keep the socket factory
     * up to date as e.g. any client certificate expires (and unregisters at {@link #close()})
     */
    public static ConfigServerApiImpl createFor(ConfigServerInfo configServerInfo,
                                                SslConnectionSocketFactoryUpdater updater,
                                                HostName configServer) {
        URI uri = configServerInfo.getConfigServerUri(configServer.value());
        return createFor(updater, Collections.singletonList(uri));
    }

    /**
     * Creates an api for talking to the config servers with a fixed socket factory.
     *
     * <p>This may be used to avoid requiring background certificate signing requests (CSR)
     * against the config server when client validation is enabled in the config server.
     */
    public static ConfigServerApiImpl createWithSocketFactory(
            List<URI> configServerHosts,
            SSLConnectionSocketFactory socketFactory) {
        return new ConfigServerApiImpl(configServerHosts, socketFactory);
    }

    static ConfigServerApiImpl createForTestingWithClient(List<URI> configServerHosts,
                                                          SelfCloseableHttpClient client) {
        return new ConfigServerApiImpl(configServerHosts, client);
    }

    private static ConfigServerApiImpl createFor(SslConnectionSocketFactoryUpdater updater,
                                                 List<URI> configServers) {
        ConfigServerApiImpl api = new ConfigServerApiImpl(
                configServers,
                // Passing null here (only) works because startSocketFactoryUpdating will
                // set the client. This avoids an unnecessary allocation of a client.
                (SelfCloseableHttpClient) null);
        api.startSocketFactoryUpdating(updater);
        assert api.client != null;
        return api;
    }

    private ConfigServerApiImpl(Collection<URI> configServerUris,
                                SSLConnectionSocketFactory sslConnectionSocketFactory) {
        this(configServerUris, new SelfCloseableHttpClient(sslConnectionSocketFactory));
    }

    private ConfigServerApiImpl(Collection<URI> configServerHosts, SelfCloseableHttpClient client) {
        this.configServerHosts = randomizeConfigServerUris(configServerHosts);
        this.client = client;
    }

    interface CreateRequest {
        HttpUriRequest createRequest(URI configServerUri) throws JsonProcessingException, UnsupportedEncodingException;
    }

    private <T> T tryAllConfigServers(CreateRequest requestFactory, Class<T> wantedReturnType) {
        Exception lastException = null;
        for (URI configServer : configServerHosts) {
            final CloseableHttpResponse response;
            try {
                response = client.execute(requestFactory.createRequest(configServer));
            } catch (Exception e) {
                // Failure to communicate with a config server is not abnormal, as they are
                // upgraded at the same time as Docker hosts.
                if (e.getMessage().indexOf("(Connection refused)") > 0) {
                    NODE_ADMIN_LOGGER.info("Connection refused to " + configServer + " (upgrading?), will try next");
                } else {
                    NODE_ADMIN_LOGGER.warning("Failed to communicate with " + configServer + ", will try next: " + e.getMessage());
                }
                lastException = e;
                continue;
            }

            try {
                Optional<HttpException> retryableException = HttpException.handleStatusCode(
                        response.getStatusLine().getStatusCode(),
                        "Config server " + configServer);
                if (retryableException.isPresent()) {
                    lastException = retryableException.get();
                    continue;
                }

                try {
                    return mapper.readValue(response.getEntity().getContent(), wantedReturnType);
                } catch (IOException e) {
                    throw new RuntimeException("Response didn't contain nodes element, failed parsing?", e);
                }
            } finally {
                try {
                    response.close();
                } catch (IOException e) {
                    NODE_ADMIN_LOGGER.warning("Ignoring exception from closing response", e);
                }
            }
        }

        throw new RuntimeException("All requests against the config servers ("
                + configServerHosts + ") failed, last as follows:", lastException);
    }

    @Override
    public <T> T put(String path, Optional<Object> bodyJsonPojo, Class<T> wantedReturnType) {
        return tryAllConfigServers(configServer -> {
            HttpPut put = new HttpPut(configServer.resolve(path));
            setContentTypeToApplicationJson(put);
            if (bodyJsonPojo.isPresent()) {
                put.setEntity(new StringEntity(mapper.writeValueAsString(bodyJsonPojo.get())));
            }
            return put;
        }, wantedReturnType);
    }

    @Override
    public <T> T patch(String path, Object bodyJsonPojo, Class<T> wantedReturnType) {
        return tryAllConfigServers(configServer -> {
            HttpPatch patch = new HttpPatch(configServer.resolve(path));
            setContentTypeToApplicationJson(patch);
            patch.setEntity(new StringEntity(mapper.writeValueAsString(bodyJsonPojo)));
            return patch;
        }, wantedReturnType);
    }

    @Override
    public <T> T delete(String path, Class<T> wantedReturnType) {
        return tryAllConfigServers(configServer ->
                new HttpDelete(configServer.resolve(path)), wantedReturnType);
    }

    @Override
    public <T> T get(String path, Class<T> wantedReturnType) {
        return tryAllConfigServers(configServer ->
                new HttpGet(configServer.resolve(path)), wantedReturnType);
    }

    @Override
    public <T> T post(String path, Object bodyJsonPojo, Class<T> wantedReturnType) {
        return tryAllConfigServers(configServer -> {
            HttpPost post = new HttpPost(configServer.resolve(path));
            setContentTypeToApplicationJson(post);
            post.setEntity(new StringEntity(mapper.writeValueAsString(bodyJsonPojo)));
            return post;
        }, wantedReturnType);
    }

    private void setContentTypeToApplicationJson(HttpRequestBase request) {
        request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
    }

    @Override
    public void setSSLConnectionSocketFactory(SSLConnectionSocketFactory sslSocketFactory) {
        this.client = new SelfCloseableHttpClient(sslSocketFactory);
    }

    // Shuffle config server URIs to balance load
    private static List<URI> randomizeConfigServerUris(Collection<URI> configServerUris) {
        List<URI> shuffledConfigServerHosts = new ArrayList<>(configServerUris);
        Collections.shuffle(shuffledConfigServerHosts);
        return shuffledConfigServerHosts;
    }

    private void startSocketFactoryUpdating(SslConnectionSocketFactoryUpdater updater) {
        updater.registerConfigServerApi(this);
        this.socketFactoryUpdater = Optional.of(updater);
    }

    private void stopSocketFactoryUpdating() {
        this.socketFactoryUpdater.ifPresent(updater -> updater.unregisterConfigServerApi(this));
        this.socketFactoryUpdater = Optional.empty();
    }

    @Override
    public void close() {
        stopSocketFactoryUpdating();
        client.close();
    }
}