aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-reindexer/src/test/java/ai/vespa/reindexing/ReindexingMaintainerTest.java
blob: e92d1e2ce1a7b42bba104c68d727d310053d3480 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.reindexing;

import ai.vespa.reindexing.Reindexer.Cluster;
import ai.vespa.reindexing.Reindexing.Trigger;
import com.yahoo.cloud.config.ClusterListConfig;
import com.yahoo.document.DocumentTypeManager;
import com.yahoo.document.config.DocumentmanagerConfig;
import com.yahoo.schema.derived.Deriver;
import com.yahoo.vespa.config.content.AllClustersBucketSpacesConfig;
import com.yahoo.vespa.config.content.reindexing.ReindexingConfig;
import org.junit.jupiter.api.Test;

import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Map;

import static ai.vespa.reindexing.ReindexingMaintainer.parseCluster;
import static ai.vespa.reindexing.ReindexingMaintainer.parseReady;
import static ai.vespa.reindexing.ReindexingMaintainer.scheduleStaggered;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
 * @author jonmv
 */
class ReindexingMaintainerTest {

    @Test
    void testParsing() {
        DocumentmanagerConfig musicConfig = Deriver.getDocumentManagerConfig("src/test/resources/schemas/music.sd").build();
        DocumentTypeManager manager = new DocumentTypeManager(musicConfig);

        assertEquals(List.of(new Trigger(manager.getDocumentType("music"), Instant.ofEpochMilli(123), 0.5)),
                     parseReady(new ReindexingConfig.Clusters.Builder()
                                        .documentTypes("music", new ReindexingConfig.Clusters.DocumentTypes.Builder().readyAtMillis(123).speed(0.5))
                                        .build(),
                                manager));

        // Unknown document type fails
        assertThrows(NullPointerException.class,
                     () -> parseReady(new ReindexingConfig.Clusters.Builder()
                                              .documentTypes("poetry", new ReindexingConfig.Clusters.DocumentTypes.Builder().readyAtMillis(123))
                                              .build(),
                                      manager));

        assertEquals(new Cluster("cluster", Map.of(manager.getDocumentType("music"), "default")),
                     parseCluster("cluster",
                                  new ClusterListConfig.Builder()
                                          .storage(new ClusterListConfig.Storage.Builder()
                                                           .name("oyster")
                                                           .configid("configId"))
                                          .storage(new ClusterListConfig.Storage.Builder()
                                                           .name("cluster")
                                                           .configid("configId"))
                                          .build(),
                                  new AllClustersBucketSpacesConfig.Builder()
                                          .cluster("oyster", new AllClustersBucketSpacesConfig.Cluster.Builder()
                                                  .documentType("music", new AllClustersBucketSpacesConfig.Cluster.DocumentType.Builder()
                                                          .bucketSpace("global")))
                                          .cluster("cluster", new AllClustersBucketSpacesConfig.Cluster.Builder()
                                                  .documentType("music", new AllClustersBucketSpacesConfig.Cluster.DocumentType.Builder()
                                                          .bucketSpace("default")))
                                          .build(),
                                  manager));

        // Cluster missing in bucket space list fails.
        assertThrows(NullPointerException.class,
                     () -> parseCluster("cluster",
                                        new ClusterListConfig.Builder()
                                                .storage(new ClusterListConfig.Storage.Builder()
                                                                 .name("cluster")
                                                                 .configid("configId"))
                                                .build(),
                                        new AllClustersBucketSpacesConfig.Builder()
                                                .cluster("oyster", new AllClustersBucketSpacesConfig.Cluster.Builder()
                                                        .documentType("music", new AllClustersBucketSpacesConfig.Cluster.DocumentType.Builder()
                                                                .bucketSpace("global")))
                                                .build(),
                                        manager));

        // Cluster missing in cluster list fails.
        assertThrows(IllegalStateException.class,
                     () -> parseCluster("cluster",
                                        new ClusterListConfig.Builder()
                                                .storage(new ClusterListConfig.Storage.Builder()
                                                                 .name("oyster")
                                                                 .configid("configId"))
                                                .build(),
                                        new AllClustersBucketSpacesConfig.Builder()
                                                .cluster("cluster", new AllClustersBucketSpacesConfig.Cluster.Builder()
                                                        .documentType("music", new AllClustersBucketSpacesConfig.Cluster.DocumentType.Builder()
                                                                .bucketSpace("default")))
                                                .build(),
                                        manager));
    }

    @Test
    void testStaggering() {
        scheduleStaggered((delayMillis, intervalMillis) -> {
                              assertEquals(0, delayMillis);
                              assertEquals(10, intervalMillis);
                          },
                          Duration.ofMillis(10),
                          Instant.ofEpochMilli(27),
                          "host",
                          "nys:123,hark:123");

        scheduleStaggered((delayMillis, intervalMillis) -> {
                              assertEquals(3, delayMillis);
                              assertEquals(10, intervalMillis);
                          },
                          Duration.ofMillis(10),
                          Instant.ofEpochMilli(27),
                          "host",
                          "host:123");

        scheduleStaggered((delayMillis, intervalMillis) -> {
                              assertEquals(7, delayMillis);
                              assertEquals(20, intervalMillis);
                          },
                          Duration.ofMillis(10),
                          Instant.ofEpochMilli(13),
                          "host",
                          "host:123,:nys:321");

        scheduleStaggered((delayMillis, intervalMillis) -> {
                              assertEquals(17, delayMillis);
                              assertEquals(20, intervalMillis);
                          },
                          Duration.ofMillis(10),
                          Instant.ofEpochMilli(13),
                          "nys",
                          "host:123,nys:321");
    }

}