aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/ContainerEndpointsCache.java
blob: e588e030ce2399804bf05d27b57ddfe348b2d8fb (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.tenant;

import com.yahoo.config.model.api.ContainerEndpoint;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.path.Path;
import com.yahoo.slime.SlimeUtils;
import com.yahoo.vespa.curator.Curator;
import com.yahoo.vespa.curator.transaction.CuratorOperations;
import com.yahoo.vespa.curator.transaction.CuratorTransaction;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.List;


/**
 * Persists assignment of rotations to an application in ZooKeeper.
 *
 * The entries are {@link ContainerEndpoint} instances, which keep track of the container
 * cluster that is the target, the endpoint name, and the rotation used to
 * give availability to that cluster.
 *
 * @author ogronnesby
 */
public class ContainerEndpointsCache {

    private final Path cachePath;
    private final Curator curator;

    public ContainerEndpointsCache(Path tenantPath, Curator curator) {
        this.cachePath = tenantPath.append("containerEndpointsCache/");
        this.curator = curator;
    }

    public List<ContainerEndpoint> read(ApplicationId applicationId) {
        var optionalData = curator.getData(containerEndpointsPath(applicationId));
        return optionalData.map(SlimeUtils::jsonToSlime)
                           .map(ContainerEndpointSerializer::endpointListFromSlime)
                           .orElseGet(List::of);
    }

    public void write(ApplicationId applicationId, List<ContainerEndpoint> endpoints) {
        var slime = ContainerEndpointSerializer.endpointListToSlime(endpoints);
        try {
            var bytes = SlimeUtils.toJsonBytes(slime);
            curator.set(containerEndpointsPath(applicationId), bytes);
        } catch (IOException e) {
            throw new UncheckedIOException("Error writing endpoints of: " + applicationId, e);
        }
    }

    /** Returns a transaction which deletes these rotations if they exist */
    public CuratorTransaction delete(ApplicationId application) {
        if ( ! curator.exists(containerEndpointsPath(application))) return CuratorTransaction.empty(curator);
        return CuratorTransaction.from(CuratorOperations.delete(containerEndpointsPath(application).getAbsolute()), curator);
    }

    private Path containerEndpointsPath(ApplicationId applicationId) {
        return cachePath.append(applicationId.serializedForm());
    }

}