aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-reindexer/src/main/java/ai/vespa/reindexing/http/ReindexingV1ApiHandler.java
blob: e488b8a17ab6de7b13237b7be1225affa2ef3fc1 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.reindexing.http;

import ai.vespa.reindexing.Reindexing;
import ai.vespa.reindexing.ReindexingCurator;
import com.yahoo.cloud.config.ZookeepersConfig;
import com.yahoo.component.annotation.Inject;
import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.container.jdisc.RequestView;
import com.yahoo.container.jdisc.ThreadedHttpRequestHandler;
import com.yahoo.container.jdisc.utils.CapabilityRequiringRequestHandler;
import com.yahoo.document.DocumentTypeManager;
import com.yahoo.jdisc.Metric;
import com.yahoo.restapi.ErrorResponse;
import com.yahoo.restapi.Path;
import com.yahoo.restapi.SlimeJsonResponse;
import com.yahoo.security.tls.Capability;
import com.yahoo.slime.Cursor;
import com.yahoo.slime.Slime;
import com.yahoo.vespa.config.content.reindexing.ReindexingConfig;
import com.yahoo.vespa.curator.Curator;
import com.yahoo.vespa.zookeeper.VespaZooKeeperServer;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.Executor;

import static com.yahoo.jdisc.http.HttpRequest.Method.GET;

/**
 * Allows inspecting reindexing status over HTTP.
 *
 * @author jonmv
 */
public class ReindexingV1ApiHandler extends ThreadedHttpRequestHandler implements CapabilityRequiringRequestHandler {

    private final ReindexingCurator database;
    private final List<String> clusterNames;

    @Inject
    public ReindexingV1ApiHandler(Executor executor, Metric metric,
                                  @SuppressWarnings("unused") VespaZooKeeperServer ensureZkHasStarted,
                                  ZookeepersConfig zookeepersConfig,
                                  ReindexingConfig reindexingConfig,
                                  DocumentTypeManager documentTypeManager) {
        this(executor,
             metric,
             reindexingConfig.clusters().keySet(),
             new ReindexingCurator(Curator.create(zookeepersConfig.zookeeperserverlist()), documentTypeManager));
    }

    ReindexingV1ApiHandler(Executor executor, Metric metric, Collection<String> clusterNames, ReindexingCurator database) {
        super(executor, metric);
        this.database = database;
        this.clusterNames = List.copyOf(clusterNames);
    }

    @Override public Capability requiredCapability(RequestView __) { return Capability.CLUSTER_CONTROLLER__REINDEXING; }

    @Override
    public HttpResponse handle(HttpRequest request) {
        Path path = new Path(request.getUri());
        if (request.getMethod() != GET)
            return ErrorResponse.methodNotAllowed("Only GET is supported under /reindexing/v1/");

        if (path.matches("/reindexing/v1"))        return getRoot();
        if (path.matches("/reindexing/v1/status")) return getStatus();

        return ErrorResponse.notFoundError("Nothing at " + request.getUri().getRawPath());
    }

    @Override
    public void destroy() {
        database.close();
    }

    HttpResponse getRoot() {
        Slime slime = new Slime();
        slime.setObject().setArray("resources").addObject().setString("url", "/reindexing/v1/status");
        return new SlimeJsonResponse(slime);
    }

    HttpResponse getStatus() {
        Slime slime = new Slime();
        Cursor clustersObject = slime.setObject().setObject("clusters");
        for (String clusterName : clusterNames) {
            Cursor documentTypesObject = clustersObject.setObject(clusterName).setObject("documentTypes");
            database.readReindexing(clusterName).status().forEach((type, status) -> {
                Cursor statusObject = documentTypesObject.setObject(type.getName());
                statusObject.setLong("startedMillis", status.startedAt().toEpochMilli());
                status.endedAt().ifPresent(endedAt -> statusObject.setLong("endedMillis", endedAt.toEpochMilli()));
                status.progress().ifPresent(progress -> statusObject.setDouble("progress", progress.percentFinished() * 1e-2));
                statusObject.setString("state", toString(status.state()));
                status.message().ifPresent(message -> statusObject.setString("message", message));
            });
        }
        return new SlimeJsonResponse(slime);
    }

    private static String toString(Reindexing.State state) {
        switch (state) {
            case READY: return "pending";
            case RUNNING: return "running";
            case SUCCESSFUL: return "successful";
            case FAILED: return "failed";
            default: throw new IllegalArgumentException("Unexpected state '" + state + "'");
        }
    }

}