aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-core/src/main/java/com/yahoo/vespa/clustercontroller/core/restapiv2/requests/ClusterStateRequest.java
blob: 1df37637dcfce44f934cbab74a109fe7d2602af7 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.core.restapiv2.requests;

import com.yahoo.vdslib.state.NodeType;
import com.yahoo.vespa.clustercontroller.core.ClusterStateBundle;
import com.yahoo.vespa.clustercontroller.core.RemoteClusterControllerTask;
import com.yahoo.vespa.clustercontroller.core.restapiv2.Id;
import com.yahoo.vespa.clustercontroller.core.restapiv2.Request;
import com.yahoo.vespa.clustercontroller.core.restapiv2.Response;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.StateRestApiException;
import com.yahoo.vespa.clustercontroller.utils.staterestapi.response.DistributionState;

import java.util.stream.Collectors;

public class ClusterStateRequest extends Request<Response.ClusterResponse> {
    private final Id.Cluster id;
    private final int recursive;

    public ClusterStateRequest(Id.Cluster id, int recursive) {
        super(MasterState.MUST_BE_MASTER);
        this.id = id;
        this.recursive = recursive;
    }

    @Override
    public Response.ClusterResponse calculateResult(RemoteClusterControllerTask.Context context) throws StateRestApiException {
        Response.ClusterResponse result = new Response.ClusterResponse();
        result.addState("generated", new Response.UnitStateImpl(context.currentConsolidatedState.getClusterState()));
        for (NodeType type : NodeType.getTypes()) {
            Id.Service serviceId = new Id.Service(id, type);
            if (recursive > 0) {
                ServiceStateRequest ssr = new ServiceStateRequest(serviceId, recursive - 1);
                result.addEntry("service", type.toString(), ssr.calculateResult(context));
            } else {
                result.addLink("service", type.toString(), serviceId.toString());
            }
        }
        result.setPublishedState(bundleToDistributionState(context.publishedClusterStateBundle));
        return result;
    }

    private static DistributionState bundleToDistributionState(ClusterStateBundle bundle) {
        return new DistributionState(bundle.getBaselineClusterState().toString(),
                bundle.getDerivedBucketSpaceStates().entrySet().stream()
                        .collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue().getClusterState().toString())));
    }
}