aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/content/Redundancy.java
blob: 35b24f741b4e53373a201fb74bc62bace528e2e6 (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
// 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;

import com.yahoo.vespa.config.content.StorDistributionConfig;
import com.yahoo.vespa.config.search.core.ProtonConfig;

/**
 * Configuration of the redundancy of a content cluster.
 *
 * @author bratseth
 */
public class Redundancy implements StorDistributionConfig.Producer, ProtonConfig.Producer {

    // These numbers are all per group as wanted numbers.
    private final int initialRedundancy;
    private final int finalRedundancy;
    private final int readyCopies;

    private final int groups;

    /** The total number of nodes available in this cluster */
    private final int totalNodes;

    public Redundancy(Integer initialRedundancy, int finalRedundancy, int readyCopies, int groups, int totalNodes) {
        if (readyCopies > finalRedundancy)
            throw new IllegalArgumentException("Number of searchable copies (" + readyCopies +
                                               ") can not be higher than final redundancy (" + finalRedundancy + ")");
        this.initialRedundancy = initialRedundancy != null ? initialRedundancy : finalRedundancy;
        this.finalRedundancy = finalRedundancy;
        this.readyCopies = readyCopies;
        this.groups = groups;
        this.totalNodes = totalNodes;
    }

    /** Returns the final redundancy per group */
    public int finalRedundancy() { return effectiveFinalRedundancy()/groups; }

    public int readyCopies() { return effectiveReadyCopies()/groups; }
    public int groups() { return groups; }
    public int totalNodes() { return totalNodes; }

    public int effectiveInitialRedundancy() { return Math.min(totalNodes, initialRedundancy * groups); }

    /** Returns the final redundancy over all groups */
    public int effectiveFinalRedundancy() { return Math.min(totalNodes, finalRedundancy * groups); }
    public int effectiveReadyCopies() { return Math.min(totalNodes, readyCopies * groups); }

    public boolean isEffectivelyGloballyDistributed() {
        return totalNodes == effectiveFinalRedundancy();
    }

    @Override
    public void getConfig(StorDistributionConfig.Builder builder) {
        builder.initial_redundancy(effectiveInitialRedundancy());
        builder.redundancy(effectiveFinalRedundancy());
        builder.ready_copies(effectiveReadyCopies());
    }

    @Override
    public void getConfig(ProtonConfig.Builder builder) {
        ProtonConfig.Distribution.Builder distBuilder = new ProtonConfig.Distribution.Builder();
        distBuilder.redundancy(finalRedundancy());
        distBuilder.searchablecopies(readyCopies());
        builder.distribution(distBuilder);
    }

    public interface Provider {
        Redundancy redundancy();
    }

}