aboutsummaryrefslogtreecommitdiffstats
path: root/service-monitor/src/main/java/com/yahoo/vespa/service/health/StateV1HealthClient.java
blob: 5d32264bd18a79f5840cb72fabd1ffb8111e403c (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.service.health;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.logging.Level;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URL;
import java.time.Duration;
import java.util.function.Function;
import java.util.logging.Logger;

import static com.yahoo.yolean.Exceptions.uncheck;

/**
 * A thread-unsafe /state/v1/health endpoint client.
 *
 * @author hakonhall
 */
public class StateV1HealthClient implements AutoCloseable {

    private static final long MAX_CONTENT_LENGTH = 1L << 20; // 1 MB
    private static final ObjectMapper MAPPER = new ObjectMapper();
    private static final Logger logger = Logger.getLogger(StateV1HealthClient.class.getName());
    private final ApacheHttpClient httpClient;
    private final Function<HttpEntity, String> getContentFunction;

    StateV1HealthClient(URL url, Duration requestTimeout, Duration connectionKeepAlive) {
        this(new ApacheHttpClient(url, requestTimeout, connectionKeepAlive),
                entity -> uncheck(() -> EntityUtils.toString(entity)));
    }

    StateV1HealthClient(ApacheHttpClient apacheHttpClient, Function<HttpEntity, String> getContentFunction) {
        httpClient = apacheHttpClient;
        this.getContentFunction = getContentFunction;
    }

    HealthInfo get() throws Exception {
        return httpClient.get(this::handle);
    }

    private HealthInfo handle(CloseableHttpResponse httpResponse) throws IOException {
        int httpStatusCode = httpResponse.getStatusLine().getStatusCode();
        if (httpStatusCode < 200 || httpStatusCode >= 300) {
            return HealthInfo.fromBadHttpStatusCode(httpStatusCode);
        }

        HttpEntity bodyEntity = httpResponse.getEntity();
        long contentLength = bodyEntity.getContentLength();
        if (contentLength > MAX_CONTENT_LENGTH) {
            throw new IllegalArgumentException("Content too long: " + contentLength + " bytes");
        }
        String body = getContentFunction.apply(bodyEntity);
        HealthResponse healthResponse = MAPPER.readValue(body, HealthResponse.class);

        if (healthResponse.status == null || healthResponse.status.code == null) {
            return HealthInfo.fromHealthStatusCode(HealthResponse.Status.DEFAULT_STATUS);
        } else {
            return HealthInfo.fromHealthStatusCode(healthResponse.status.code);
        }
    }

    @Override
    public void close() {
        try {
            httpClient.close();
        } catch (Exception e) {
            logger.log(Level.WARNING, "Failed to close CloseableHttpClient", e);
        }
    }

}