summaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/Tenant.java
blob: 7e0db0a00a24e75320ac09c59838f997208546aa (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
// Copyright 2017 Yahoo Holdings. 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.provision.TenantName;
import com.yahoo.path.Path;
import com.yahoo.vespa.config.server.RequestHandler;
import com.yahoo.vespa.config.server.application.TenantApplications;
import com.yahoo.vespa.config.server.session.SessionRepository;
import com.yahoo.vespa.curator.Curator;
import org.apache.zookeeper.data.Stat;

import java.time.Instant;
import java.util.Optional;

/**
 * Contains all tenant-level components for a single tenant, dealing with editing sessions and
 * applications for a single tenant.
 *
 * @author vegardh
 * @author Ulf Lilleengen
 */
public class Tenant implements TenantHandlerProvider {

    static final String SESSIONS = "sessions";
    static final String APPLICATIONS = "applications";

    private final TenantName name;
    private final Path path;
    private final SessionRepository sessionRepository;
    private final TenantApplications applicationRepo;
    private final RequestHandler requestHandler;
    private final Curator curator;

    Tenant(TenantName name,
           SessionRepository sessionRepository,
           RequestHandler requestHandler,
           TenantApplications applicationRepo,
           Curator curator) {
        this.name = name;
        this.path = TenantRepository.getTenantPath(name);
        this.requestHandler = requestHandler;
        this.sessionRepository = sessionRepository;
        this.applicationRepo = applicationRepo;
        this.curator = curator;
    }

    /**
     * The request handler for this
     *
     * @return handler
     */
    public RequestHandler getRequestHandler() {
        return requestHandler;
    }

    public TenantName getName() {
        return name;
    }

    public Path getPath() {
        return path;
    }

    public SessionRepository getSessionRepository() { return sessionRepository; }

    @Override
    public String toString() {
        return getName().value();
    }

    public TenantApplications getApplicationRepo() {
        return applicationRepo;
    }

    public Instant getCreatedTime() {
        Optional<Stat> stat = curator.getStat(path);
        if (stat.isPresent())
            return Instant.ofEpochMilli(stat.get().getCtime());
        else
            return Instant.now();
    }

    @Override
    public boolean equals(Object other) {
        if (!(other instanceof Tenant)) {
            return false;
        }
        Tenant that = (Tenant) other;
        return name.equals(that.name);
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }

    /**
     * Closes any watchers, thread pools that may react to changes in tenant state,
     * and removes any session data in filesystem and zookeeper.
     * Called by watchers as a reaction to deleting a tenant.
     */
    void close() {
        applicationRepo.close();                // Closes watchers.
        sessionRepository.close();              // Closes watchers, clears memory, and deletes local files and ZK session state.
    }

}