aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/test/java/com/yahoo/vespa/hosted/provision/RealDataScenarioTest.java
blob: b812a547edecc5aca5cde4373cadeb414cc0e2cd (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
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision;

import com.yahoo.component.Version;
import com.yahoo.config.model.builder.xml.XmlHelper;
import com.yahoo.config.provision.ActivationContext;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ApplicationTransaction;
import com.yahoo.config.provision.Capacity;
import com.yahoo.config.provision.Cloud;
import com.yahoo.config.provision.ClusterResources;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.HostSpec;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.config.provision.ProvisionLock;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.Zone;
import com.yahoo.config.provisioning.FlavorsConfig;
import com.yahoo.transaction.NestedTransaction;
import com.yahoo.vespa.config.ConfigPayload;
import com.yahoo.vespa.hosted.provision.node.Agent;
import com.yahoo.vespa.hosted.provision.persistence.NodeSerializer;
import com.yahoo.vespa.hosted.provision.provisioning.ProvisioningTester;
import com.yahoo.vespa.model.builder.xml.dom.DomConfigPayloadBuilder;
import org.junit.Ignore;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static com.yahoo.config.provision.NodeResources.DiskSpeed.any;
import static com.yahoo.config.provision.NodeResources.DiskSpeed.fast;
import static com.yahoo.config.provision.NodeResources.StorageType.local;
import static com.yahoo.config.provision.NodeResources.StorageType.remote;
import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * Scenario tester with real node-repository data loaded from ZK snapshot file
 *
 * @author valerijf
 */
public class RealDataScenarioTest {
    private static final Logger log = Logger.getLogger(RealDataScenarioTest.class.getSimpleName());

    @Ignore
    @Test
    public void test() {
        ProvisioningTester tester = new ProvisioningTester.Builder()
                .zone(new Zone(Cloud.builder().dynamicProvisioning(true).build(), SystemName.defaultSystem(), Environment.prod, RegionName.defaultName()))
                .flavorsConfig(parseFlavors(Paths.get("flavors.xml")))
                .build();
        initFromZk(tester.nodeRepository(), Paths.get("snapshot"));

        ApplicationId app = ApplicationId.from("tenant", "app", "default");
        Version version = Version.fromString("7.123.4");

        Capacity[] capacities = new Capacity[]{
                Capacity.from(new ClusterResources(1, 1, new NodeResources(0.5, 4, 50, 0.3, any, remote))),
                Capacity.from(new ClusterResources(4, 1, new NodeResources(8, 16, 100, 0.3, fast, remote))),
                Capacity.from(new ClusterResources(2, 1, new NodeResources(4, 8, 100, 0.3, fast, local)))
        };
        ClusterSpec[] specs = new ClusterSpec[]{
                ClusterSpec.request(ClusterSpec.Type.admin, ClusterSpec.Id.from("logserver")).vespaVersion(version).build(),
                ClusterSpec.request(ClusterSpec.Type.container, ClusterSpec.Id.from("container")).vespaVersion(version).build(),
                ClusterSpec.request(ClusterSpec.Type.content, ClusterSpec.Id.from("content")).vespaVersion(version).build()
        };

        deploy(tester, app, specs, capacities);
        tester.nodeRepository().nodes().list().owner(app).cluster(specs[1].id()).forEach(System.out::println);
    }

    private void deploy(ProvisioningTester tester, ApplicationId app, ClusterSpec[] specs, Capacity[] capacities) {
        List<HostSpec> hostSpecs = IntStream.range(0, capacities.length)
                .mapToObj(i -> tester.provisioner().prepare(app, specs[i], capacities[i], log::log).stream())
                .flatMap(s -> s)
                .collect(Collectors.toList());
        NestedTransaction transaction = new NestedTransaction();
        tester.provisioner().activate(hostSpecs, new ActivationContext(0), new ApplicationTransaction(new ProvisionLock(app, () -> {}), transaction));
        transaction.commit();
    }

    private static FlavorsConfig parseFlavors(Path path) {
        try {
            var element = XmlHelper.getDocumentBuilder().parse(path.toFile()).getDocumentElement();
            return ConfigPayload.fromBuilder(new DomConfigPayloadBuilder(null).build(element)).toInstance(FlavorsConfig.class, "");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static void initFromZk(NodeRepository nodeRepository, Path pathToZkSnapshot) {
        NodeSerializer nodeSerializer = new NodeSerializer(nodeRepository.flavors(), 1000);
        AtomicReference<Node.State> state = new AtomicReference<>();
        Pattern zkNodePathPattern = Pattern.compile(".?/provision/v1/([a-z]+)/[a-z0-9.-]+\\.(com|cloud).?");
        Consumer<String> consumer = input -> {
            if (state.get() != null) {
                String json = input.substring(input.indexOf("{\""), input.lastIndexOf('}') + 1);
                Node node = nodeSerializer.fromJson(state.get(), json.getBytes(UTF_8));
                nodeRepository.database().addNodesInState(List.of(node), state.get(), Agent.system);
                state.set(null);
            } else {
                Matcher matcher = zkNodePathPattern.matcher(input);
                if (!matcher.matches()) return;
                String stateStr = matcher.group(1);
                Node.State s = "deallocated".equals(stateStr) ? Node.State.inactive :
                        "allocated".equals(stateStr) ? Node.State.active : Node.State.valueOf(stateStr);
                state.set(s);
            }
        };

        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(Files.newInputStream(pathToZkSnapshot), UTF_8))) {
            StringBuilder sb = new StringBuilder(1000);
            for (int r; (r = reader.read()) != -1; ) {
                if (r < 0x20 || r >= 0x7F) {
                    if (sb.length() > 0) {
                        consumer.accept(sb.toString());
                        sb.setLength(0);
                    }
                } else sb.append((char) r);
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
}