aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-reindexer/src/main/java/ai/vespa/reindexing/ReindexingCurator.java
blob: 4c8e6dabba8f23668429e438a831e884d2ea2cd4 (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
180
181
182
183
184
185
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.reindexing;

import ai.vespa.reindexing.Reindexing.Status;
import ai.vespa.reindexing.Reindexing.Trigger;
import com.yahoo.concurrent.UncheckedTimeoutException;
import com.yahoo.document.DocumentTypeManager;
import com.yahoo.path.Path;
import com.yahoo.slime.Cursor;
import com.yahoo.slime.Inspector;
import com.yahoo.slime.Slime;
import com.yahoo.slime.SlimeUtils;
import com.yahoo.vespa.curator.Curator;
import com.yahoo.vespa.curator.Lock;
import com.yahoo.yolean.Exceptions;

import java.io.Closeable;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;

import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toUnmodifiableMap;

/**
 * Reads and writes status of initiated reindexing jobs.
 *
 * @author jonmv
 */
public class ReindexingCurator implements Closeable {

    private static final Logger log = Logger.getLogger(ReindexingCurator.class.getName());

    private final Curator curator;
    private final ReindexingSerializer serializer;
    private final Duration lockTimeout;

    public ReindexingCurator(Curator curator, DocumentTypeManager manager) {
        this(curator, manager, Duration.ofSeconds(1));
    }

    ReindexingCurator(Curator curator, DocumentTypeManager manager, Duration lockTimeout) {
        this.curator = curator;
        this.serializer = new ReindexingSerializer(manager);
        this.lockTimeout = lockTimeout;
    }

    /** If no reindexing data exists (has been wiped), assume current ready documents are already done. */
    public void initializeIfEmpty(String cluster, List<Trigger> ready, Instant now) {
        if ( ! curator.exists(statusPath(cluster))) {
            try (Lock lock = lockReindexing(cluster)) {
                if (curator.exists(statusPath(cluster)))
                    return; // Some other node already did this.

                Reindexing reindexing = Reindexing.empty();
                for (Trigger trigger : ready)
                    if (trigger.readyAt().isBefore(now))
                        reindexing = reindexing.with(trigger.type(), Status.ready(now).running().successful(now));

                log.log(Level.INFO, "Creating initial reindexing status at '" + statusPath(cluster) + "'");
                writeReindexing(reindexing, cluster);
            }
            catch (ReindexingLockException ignored) {
                // Some other node took ownership and is doing this.
            }
        }
    }

    public Reindexing readReindexing(String cluster) {
        Reindexing reindexing = curator.getData(statusPath(cluster)).map(serializer::deserialize)
                                       .orElse(Reindexing.empty());
        log.log(Level.FINE, () -> "Read reindexing status '" + reindexing + "' from '" + statusPath(cluster) + "'");
        return reindexing;
    }

    public void writeReindexing(Reindexing reindexing, String cluster) {
        log.log(Level.FINE, () -> "Writing reindexing status '" + reindexing + "' to '" + statusPath(cluster) + "'");
        curator.set(statusPath(cluster), serializer.serialize(reindexing));
    }

    /** This lock must be held to manipulate reindexing state, or by whoever has a running visitor. */
    public Lock lockReindexing(String cluster) throws ReindexingLockException {
        try {
            return curator.lock(lockPath(cluster), lockTimeout);
        }
        catch (UncheckedTimeoutException e) {
            throw new ReindexingLockException(e);
        }
    }

    private Path rootPath(String clusterName) { return Path.fromString("/reindexing/v1/" + clusterName); }
    private Path statusPath(String clusterName) { return rootPath(clusterName).append("status"); }
    private Path lockPath(String clusterName) { return rootPath(clusterName).append("lock"); }

    @Override
    public void close() {
        curator.close();
    }


    private static class ReindexingSerializer {

        private static final String STATUS = "status";
        private static final String TYPE =  "type";
        private static final String STARTED_MILLIS = "startedMillis";
        private static final String ENDED_MILLIS = "endedMillis";
        private static final String PROGRESS = "progress";
        private static final String STATE = "state";
        private static final String MESSAGE = "message";

        private final DocumentTypeManager types;

        public ReindexingSerializer(DocumentTypeManager types) {
            this.types = types;
        }

        private byte[] serialize(Reindexing reindexing) {
            Cursor root = new Slime().setObject();
            Cursor statusArray = root.setArray(STATUS);
            reindexing.status().forEach((type, status) -> {
                Cursor statusObject = statusArray.addObject();
                statusObject.setString(TYPE, type.getName());
                statusObject.setLong(STARTED_MILLIS, status.startedAt().toEpochMilli());
                status.endedAt().ifPresent(endedAt -> statusObject.setLong(ENDED_MILLIS, endedAt.toEpochMilli()));
                status.progress().ifPresent(progress -> statusObject.setString(PROGRESS, progress.serializeToString()));
                statusObject.setString(STATE, toString(status.state()));
                status.message().ifPresent(message -> statusObject.setString(MESSAGE, message));
            });
            return Exceptions.uncheck(() -> SlimeUtils.toJsonBytes(root));
        }

        private Reindexing deserialize(byte[] json) {
            return new Reindexing(SlimeUtils.entriesStream(SlimeUtils.jsonToSlimeOrThrow(json).get().field(STATUS))
                                            .filter(object -> require(TYPE, object, field -> types.hasDocumentType(field.asString()))) // Forget unknown documents.
                                            .collect(toUnmodifiableMap(object -> require(TYPE, object, field -> types.getDocumentType(field.asString())),
                                                                       object -> new Status(require(STARTED_MILLIS, object, field -> Instant.ofEpochMilli(field.asLong())),
                                                                                            get(ENDED_MILLIS, object, field -> Instant.ofEpochMilli(field.asLong())),
                                                                                            get(PROGRESS, object, field -> field.asString()),
                                                                                            require(STATE, object, field -> toState(field.asString())),
                                                                                            get(MESSAGE, object, field -> field.asString())))));
        }

        private static <T> T get(String name, Inspector object, Function<Inspector, T> mapper) {
            return object.field(name).valid() ? mapper.apply(object.field(name)) : null;
        }

        private static <T> T require(String name, Inspector object, Function<Inspector, T> mapper) {
            return requireNonNull(get(name, object, mapper));
        }

        private static String toString(Reindexing.State state) {
            switch (state) {
                case READY: return "ready";
                case RUNNING: return "running";
                case SUCCESSFUL: return "successful";
                case FAILED: return "failed";
                default: throw new IllegalArgumentException("Unexpected state '" + state + "'");
            }
        }

        private static Reindexing.State toState(String value) {
            switch (value) {
                case "ready": return Reindexing.State.READY;
                case "running": return Reindexing.State.RUNNING;
                case "successful": return Reindexing.State.SUCCESSFUL;
                case "failed": return Reindexing.State.FAILED;
                default: throw new IllegalArgumentException("Unknown state '" + value + "'");
            }
        }

    }

    /** Indicates that taking the reindexing lock failed within the allotted time. */
    static class ReindexingLockException extends Exception {

        ReindexingLockException(UncheckedTimeoutException cause) {
            super("Failed to obtain the reindexing lock", cause);
        }

    }

}