aboutsummaryrefslogtreecommitdiffstats
path: root/orchestrator/src/test/java/com/yahoo/vespa/orchestrator/controller/ClusterControllerClientFactoryMock.java
blob: a5348ad3d0735a5854ec73ee1e802259fc8bced5 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.orchestrator.controller;

import com.yahoo.vespa.applicationmodel.ApplicationInstance;
import com.yahoo.vespa.applicationmodel.ApplicationInstanceId;
import com.yahoo.vespa.applicationmodel.ClusterId;
import com.yahoo.vespa.applicationmodel.HostName;
import com.yahoo.vespa.orchestrator.DummyServiceMonitor;
import com.yahoo.vespa.orchestrator.OrchestratorContext;
import com.yahoo.vespa.orchestrator.model.ContentService;
import com.yahoo.vespa.orchestrator.model.VespaModelUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Mock implementation of ClusterControllerClient
 * <p>
 *
 * @author smorgrav
 */
public class ClusterControllerClientFactoryMock implements ClusterControllerClientFactory {

    Map<String, ClusterControllerNodeState> nodes = new HashMap<>();

    public boolean isInMaintenance(ApplicationInstance appInstance, HostName hostName) {
        try {
            ClusterId clusterName = VespaModelUtil.getContentClusterName(appInstance, hostName);
            int storageNodeIndex = VespaModelUtil.getStorageNodeIndex(appInstance, hostName);
            String globalMapKey = clusterName + "/" + ContentService.STORAGE_NODE.nameInClusterController() + "/" + storageNodeIndex;
            return nodes.getOrDefault(globalMapKey, ClusterControllerNodeState.UP) == ClusterControllerNodeState.MAINTENANCE;
        } catch (Exception e) {
            //Catch all - meant to catch cases where the node is not part of a storage cluster
            return false;
        }
    }

    public void setAllDummyNodesAsUp() {
        for (ApplicationInstance app : DummyServiceMonitor.getApplications()) {
            Set<HostName> hosts = DummyServiceMonitor.getContentHosts(app.reference());
            for (HostName host : hosts) {
                ClusterId clusterName = VespaModelUtil.getContentClusterName(app, host);
                int storageNodeIndex = VespaModelUtil.getStorageNodeIndex(app, host);
                String globalMapKey = clusterName + "/" + ContentService.STORAGE_NODE.nameInClusterController() + "/" + storageNodeIndex;
                nodes.put(globalMapKey, ClusterControllerNodeState.UP);
            }
        }
    }

    @Override
    public ClusterControllerClient createClient(List<HostName> clusterControllers, String clusterName) {
        return new ClusterControllerClient() {
            @Override public boolean trySetNodeState(OrchestratorContext context, HostName host, int storageNodeIndex, ClusterControllerNodeState wantedState, ContentService contentService, boolean force) {
                nodes.put(clusterName + "/" + contentService.nameInClusterController() + "/" + storageNodeIndex, wantedState);
                return true;
            }
            @Override public void setNodeState(OrchestratorContext context, HostName host, int storageNodeIndex, ClusterControllerNodeState wantedState, ContentService contentService, boolean force) {
                trySetNodeState(context, host, storageNodeIndex, wantedState, contentService, false);
            }
            @Override public void setApplicationState(OrchestratorContext context, ApplicationInstanceId applicationId, ClusterControllerNodeState wantedState) {
                nodes.replaceAll((key, state) -> key.startsWith(clusterName + "/") ? wantedState : state);
            }
        };
    }

}