aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/content/utils/ContentClusterBuilder.java
blob: 61ce5172ceb828ee2dfc2d28355808eba0737659 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.content.utils;

import com.yahoo.config.model.test.MockRoot;
import com.yahoo.vespa.model.content.cluster.ContentCluster;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.yahoo.config.model.test.TestUtil.joinLines;

/**
 * Class for building a content cluster with indexed search (used for testing only).
 *
 * @author geirst
 */
public class ContentClusterBuilder {

    private String name = "mycluster";
    private int redundancy = 1;
    private int searchableCopies = 1;
    private List<DocType> docTypes = Arrays.asList(DocType.index("test"));
    private String groupXml = getSimpleGroupXml();
    private Optional<String> dispatchXml = Optional.empty();
    private Optional<Double> protonDiskLimit = Optional.empty();
    private Optional<Double> protonMemoryLimit = Optional.empty();
    private Optional<Double> clusterControllerDiskLimit = Optional.empty();
    private Optional<Double> clusterControllerMemoryLimit = Optional.empty();
    private Optional<Boolean> syncTransactionLog = Optional.empty();

    public ContentClusterBuilder() {
    }

    public ContentClusterBuilder name(String name) {
        this.name = name;
        return this;
    }

    public ContentClusterBuilder redundancy(int redundancy) {
        this.redundancy = redundancy;
        return this;
    }

    public ContentClusterBuilder syncTransactionLog(boolean syncTransactionLog) {
        this.syncTransactionLog = Optional.of(syncTransactionLog);
        return this;
    }

    public ContentClusterBuilder searchableCopies(int searchableCopies) {
        this.searchableCopies = searchableCopies;
        return this;
    }

    public ContentClusterBuilder docTypes(String ... docTypes) {
        this.docTypes = Arrays.asList(docTypes).stream().
                map(type -> DocType.index(type)).
                toList();
        return this;
    }

    public ContentClusterBuilder docTypes(List<DocType> docTypes) {
        this.docTypes = docTypes;
        return this;
    }

    public ContentClusterBuilder groupXml(String groupXml) {
        this.groupXml = groupXml;
        return this;
    }

    public ContentClusterBuilder dispatchXml(Optional<String> dispatchXml) {
        this.dispatchXml = dispatchXml;
        return this;
    }

    public ContentClusterBuilder protonDiskLimit(double limit) {
        protonDiskLimit = Optional.of(limit);
        return this;
    }

    public ContentClusterBuilder protonMemoryLimit(double limit) {
        protonMemoryLimit = Optional.of(limit);
        return this;
    }

    public ContentClusterBuilder clusterControllerDiskLimit(double limit) {
        clusterControllerDiskLimit = Optional.of(limit);
        return this;
    }

    public ContentClusterBuilder clusterControllerMemoryLimit(double limit) {
        clusterControllerMemoryLimit = Optional.of(limit);
        return this;
    }

    public ContentCluster build(MockRoot root) {
        return ContentClusterUtils.createCluster(getXml(), root);
    }

    public String getXml() {
        String xml = joinLines("<content version='1.0' id='" + name + "'>",
               "  <redundancy>" + redundancy + "</redundancy>",
               DocType.listToXml(docTypes),
               "  <engine>",
               "    <proton>",
               "      <searchable-copies>" + searchableCopies + "</searchable-copies>",
               getProtonResourceLimitsXml("      "),
               getTransactionLogSyncXml("      "),
               "    </proton>",
               "  </engine>");
        if (dispatchXml.isPresent()) {
            xml += dispatchXml.get();
        }
        xml += groupXml;
        xml += joinLines("  <tuning>",
               getTuningResourceLimitsXml("    "),
               "  </tuning>");
        return xml + "</content>";
    }

    private static String getSimpleGroupXml() {
        return joinLines("  <group>",
                "    <node distribution-key='0' hostalias='mockhost'/>",
                "  </group>");
    }

    private String getProtonResourceLimitsXml(String indent) {
        return getResourceLimitsXml(indent, protonDiskLimit, protonMemoryLimit);
    }

    private String getTuningResourceLimitsXml(String indent) {
        return getResourceLimitsXml(indent, clusterControllerDiskLimit, clusterControllerMemoryLimit);
    }

    private String getResourceLimitsXml(String indent, Optional<Double> diskLimit, Optional<Double> memoryLimit) {
        if (diskLimit.isPresent() || memoryLimit.isPresent()) {
            String xml = joinLines(indent + "<resource-limits>",
                    getXmlLine("disk", diskLimit, indent + "  "),
                    getXmlLine("memory", memoryLimit, indent + "  "),
                    indent + "</resource-limits>");
            return xml;
        }
        return "";
    }

    private String getTransactionLogSyncXml(String indent) {
        return getXmlLine("sync-transactionlog", syncTransactionLog, indent);
    }

    private static <T> String getXmlLine(String tag, Optional<T> value, String indent) {
        if (value.isPresent()) {
            return indent + "<" + tag + ">" + value.get() + "</" + tag + ">\n";
        }
        return "";
    }

}