aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/maintenance/ArchiveAccessMaintainer.java
blob: 33a4802360e53d56165a75a7d00085b8b33d5d94 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.maintenance;

import ai.vespa.metrics.ControllerMetrics;
import com.yahoo.config.provision.TenantName;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.jdisc.Metric;
import com.yahoo.vespa.hosted.controller.Controller;
import com.yahoo.vespa.hosted.controller.api.integration.archive.ArchiveService;
import com.yahoo.vespa.hosted.controller.api.integration.zone.ZoneRegistry;
import com.yahoo.vespa.hosted.controller.archive.CuratorArchiveBucketDb;
import com.yahoo.vespa.hosted.controller.tenant.ArchiveAccess;
import com.yahoo.vespa.hosted.controller.tenant.CloudTenant;
import com.yahoo.vespa.hosted.controller.tenant.Tenant;

import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Update archive access permissions with roles from tenants
 *
 * @author andreer
 */
public class ArchiveAccessMaintainer extends ControllerMaintainer {

    private static final String bucketCountMetricName = ControllerMetrics.ARCHIVE_BUCKET_COUNT.baseName();

    private final CuratorArchiveBucketDb archiveBucketDb;
    private final ArchiveService archiveService;
    private final ZoneRegistry zoneRegistry;
    private final Metric metric;

    public ArchiveAccessMaintainer(Controller controller, Metric metric, Duration interval) {
        super(controller, interval);
        this.archiveBucketDb = controller.archiveBucketDb();
        this.archiveService = controller.serviceRegistry().archiveService();
        this.zoneRegistry = controller().zoneRegistry();
        this.metric = metric;
    }

    @Override
    protected double maintain() {
        // Count buckets - so we can alert if we get close to the AWS account limit of 1000
        zoneRegistry.zonesIncludingSystem().all().zones().forEach(z ->
                metric.set(bucketCountMetricName, archiveBucketDb.buckets(z.getVirtualId()).vespaManaged().size(),
                        metric.createContext(Map.of(
                                "zone", z.getVirtualId().value(),
                                "cloud", z.getCloudName().value()))));

        zoneRegistry.zonesIncludingSystem().controllerUpgraded().zones().forEach(z -> {
            ZoneId zoneId = z.getVirtualId();
            try {
                var tenantArchiveAccessRoles = cloudTenantArchiveExternalAccessRoles();
                var buckets = archiveBucketDb.buckets(zoneId).vespaManaged();
                archiveService.updatePolicies(zoneId, buckets, tenantArchiveAccessRoles);
            } catch (Exception e) {
                throw new RuntimeException("Failed to maintain archive access in " + zoneId.value(), e);
            }
        });

        return 1.0;
    }

    private Map<TenantName, ArchiveAccess> cloudTenantArchiveExternalAccessRoles() {
        List<Tenant> tenants = controller().tenants().asList();
        return tenants.stream()
                .filter(t -> t instanceof CloudTenant)
                .map(t -> (CloudTenant) t)
                .collect(Collectors.toUnmodifiableMap(
                        Tenant::name, CloudTenant::archiveAccess));
    }

}