summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/application/validation/change/search/AttributeChangeValidator.java
blob: a10aac302987ec8e01cec5690a74135196a30140 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.application.validation.change.search;

import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.documentmodel.NewDocumentType;
import com.yahoo.searchdefinition.derived.AttributeFields;
import com.yahoo.searchdefinition.derived.IndexSchema;
import com.yahoo.searchdefinition.document.Attribute;
import com.yahoo.config.application.api.ValidationOverrides;
import com.yahoo.searchdefinition.document.HnswIndexParams;
import com.yahoo.vespa.model.application.validation.change.VespaConfigChangeAction;
import com.yahoo.vespa.model.application.validation.change.VespaRefeedAction;
import com.yahoo.vespa.model.application.validation.change.VespaRestartAction;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * Validates the changes between the current and next set of attribute fields in a document database.
 *
 * @author geirst
 */
public class AttributeChangeValidator {

    private final ClusterSpec.Id id;
    private final AttributeFields currentFields;
    private final IndexSchema currentIndexSchema;
    private final NewDocumentType currentDocType;
    private final AttributeFields nextFields;
    private final IndexSchema nextIndexSchema;
    private final NewDocumentType nextDocType;

    public AttributeChangeValidator(ClusterSpec.Id id,
                                    AttributeFields currentFields,
                                    IndexSchema currentIndexSchema,
                                    NewDocumentType currentDocType,
                                    AttributeFields nextFields,
                                    IndexSchema nextIndexSchema,
                                    NewDocumentType nextDocType) {
        this.id = id;
        this.currentFields = currentFields;
        this.currentIndexSchema = currentIndexSchema;
        this.currentDocType = currentDocType;
        this.nextFields = nextFields;
        this.nextIndexSchema = nextIndexSchema;
        this.nextDocType = nextDocType;
    }

    public List<VespaConfigChangeAction> validate(ValidationOverrides overrides, Instant now) {
        List<VespaConfigChangeAction> result = new ArrayList<>();
        result.addAll(validateAddAttributeAspect());
        result.addAll(validateRemoveAttributeAspect());
        result.addAll(validateAttributeSettings());
        result.addAll(validateTensorTypes(overrides, now));
        return result;
    }

    private List<VespaConfigChangeAction> validateAddAttributeAspect() {
        return nextFields.attributes().stream().
                map(attr -> attr.getName()).
                filter(attrName -> !currentFields.containsAttribute(attrName) &&
                                   currentDocType.containsField(attrName)).
                map(attrName -> new VespaRestartAction(id, new ChangeMessageBuilder(attrName).addChange("add attribute aspect").build())).
                collect(Collectors.toList());
    }

    private List<VespaConfigChangeAction> validateRemoveAttributeAspect() {
        return currentFields.attributes().stream().
                map(attr -> attr.getName()).
                filter(attrName -> !nextFields.containsAttribute(attrName) &&
                                   nextDocType.containsField(attrName) &&
                                   !isIndexField(attrName)).
                map(attrName -> new VespaRestartAction(id, new ChangeMessageBuilder(attrName).addChange("remove attribute aspect").build())).
                collect(Collectors.toList());
    }

    private boolean isIndexField(String fieldName) {
        return currentIndexSchema.containsField(fieldName) && nextIndexSchema.containsField(fieldName);
    }

    private static boolean hasHnswIndex(Attribute attribute) {
        return attribute.hnswIndexParams().isPresent();
    }

    private List<VespaConfigChangeAction> validateAttributeSettings() {
        List<VespaConfigChangeAction> result = new ArrayList<>();
        for (Attribute nextAttr : nextFields.attributes()) {
            Attribute currAttr = currentFields.getAttribute(nextAttr.getName());
            if (currAttr != null) {
                validateAttributeSetting(id, currAttr, nextAttr, Attribute::isFastSearch, "fast-search", result);
                validateAttributeSetting(id, currAttr, nextAttr, Attribute::isFastAccess, "fast-access", result);
                validateAttributeSetting(id, currAttr, nextAttr, Attribute::isHuge, "huge", result);
                validateAttributeSetting(id, currAttr, nextAttr, Attribute::densePostingListThreshold, "dense-posting-list-threshold", result);
                validateAttributeSetting(id, currAttr, nextAttr, Attribute::isEnabledOnlyBitVector, "rank: filter", result);
                validateAttributeSetting(id, currAttr, nextAttr, AttributeChangeValidator::hasHnswIndex, "indexing: index", result);
                validateAttributeSetting(id, currAttr, nextAttr, Attribute::distanceMetric, "distance-metric", result);
                if (hasHnswIndex(currAttr) && hasHnswIndex(nextAttr)) {
                    validateAttributeHnswIndexSetting(id, currAttr, nextAttr, HnswIndexParams::maxLinksPerNode, "max-links-per-node", result);
                    validateAttributeHnswIndexSetting(id, currAttr, nextAttr, HnswIndexParams::neighborsToExploreAtInsert, "neighbors-to-explore-at-insert", result);
                }
            }
        }
        return result;
    }

    private static void validateAttributeSetting(ClusterSpec.Id id,
                                                 Attribute currentAttr, Attribute nextAttr,
                                                 Predicate<Attribute> predicate, String setting,
                                                 List<VespaConfigChangeAction> result) {
        boolean nextValue = predicate.test(nextAttr);
        if (predicate.test(currentAttr) != nextValue) {
            String change = nextValue ? "add" : "remove";
            result.add(new VespaRestartAction(id, new ChangeMessageBuilder(nextAttr.getName()).addChange(change + " attribute '" + setting + "'").build()));
        }
    }

    private static <T> void validateAttributeSetting(ClusterSpec.Id id,
                                                     Attribute currentAttr, Attribute nextAttr,
                                                     Function<Attribute, T> settingValueProvider, String setting,
                                                     List<VespaConfigChangeAction> result) {
        T currentValue = settingValueProvider.apply(currentAttr);
        T nextValue = settingValueProvider.apply(nextAttr);
        if ( ! Objects.equals(currentValue, nextValue)) {
            String message = String.format("change property '%s' from '%s' to '%s'", setting, currentValue, nextValue);
            result.add(new VespaRestartAction(id, new ChangeMessageBuilder(nextAttr.getName()).addChange(message).build()));
        }
    }

    private static <T> void validateAttributeHnswIndexSetting(ClusterSpec.Id id,
                                                              Attribute currentAttr, Attribute nextAttr,
                                                              Function<HnswIndexParams, T> settingValueProvider,
                                                              String setting,
                                                              List<VespaConfigChangeAction> result) {
        T currentValue = settingValueProvider.apply(currentAttr.hnswIndexParams().get());
        T nextValue = settingValueProvider.apply(nextAttr.hnswIndexParams().get());
        if (!Objects.equals(currentValue, nextValue)) {
            String message = String.format("change hnsw index property '%s' from '%s' to '%s'", setting, currentValue, nextValue);
            result.add(new VespaRestartAction(id, new ChangeMessageBuilder(nextAttr.getName()).addChange(message).build()));
        }
    }

    private List<VespaConfigChangeAction> validateTensorTypes(ValidationOverrides overrides, Instant now) {
        List<VespaConfigChangeAction> result = new ArrayList<>();

        for (Attribute nextAttr : nextFields.attributes()) {
            Attribute currentAttr = currentFields.getAttribute(nextAttr.getName());

            if (currentAttr != null && currentAttr.tensorType().isPresent()) {
                // If the tensor attribute is not present on the new attribute, it means that the data type of the attribute
                // has been changed. This is already handled by DocumentTypeChangeValidator, so we can ignore it here
                if (!nextAttr.tensorType().isPresent()) {
                    continue;
                }

                // Tensor attribute has changed type
                if (!nextAttr.tensorType().get().equals(currentAttr.tensorType().get())) {
                    result.add(createTensorTypeChangedRefeedAction(id, currentAttr, nextAttr, overrides, now));
                }
            }
        }

        return result;
    }

    private static VespaRefeedAction createTensorTypeChangedRefeedAction(ClusterSpec.Id id, Attribute currentAttr, Attribute nextAttr, ValidationOverrides overrides, Instant now) {
        return VespaRefeedAction.of(id,
                                    "tensor-type-change",
                                    overrides,
                                    new ChangeMessageBuilder(nextAttr.getName()).addChange("tensor type",
                                                                                           currentAttr.tensorType().get().toString(),
                                                                                           nextAttr.tensorType().get().toString()).build(), now);
    }

}