summaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/Tenant.java
blob: cabf4bed323e906b39202dfa13ee7665ef9cc69d (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Deployer;
import com.yahoo.config.provision.TenantName;
import com.yahoo.log.LogLevel;
import com.yahoo.path.Path;
import com.yahoo.vespa.config.server.application.ApplicationRepo;
import com.yahoo.vespa.config.server.deploy.TenantFileSystemDirs;
import com.yahoo.vespa.config.server.session.LocalSessionRepo;
import com.yahoo.vespa.config.server.session.RemoteSessionRepo;
import com.yahoo.vespa.config.server.session.SessionFactory;
import com.yahoo.vespa.curator.Curator;

import java.time.Duration;
import java.util.logging.Logger;

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

    private static final Logger log = Logger.getLogger(Tenant.class.getName());
    static final String SESSIONS = "sessions";
    static final String APPLICATIONS = "applications";

    private final TenantName name;
    private final RemoteSessionRepo remoteSessionRepo;
    private final Path path;
    private final SessionFactory sessionFactory;
    private final LocalSessionRepo localSessionRepo;
    private final ApplicationRepo applicationRepo;
    private final ActivateLock activateLock;
    private final RequestHandler requestHandler;
    private final ReloadHandler reloadHandler;
    private final TenantFileSystemDirs tenantFileSystemDirs;
    private final Curator curator;

    Tenant(TenantName name,
           Path path,
           SessionFactory sessionFactory,
           LocalSessionRepo localSessionRepo,
           RemoteSessionRepo remoteSessionRepo,
           RequestHandler requestHandler,
           ReloadHandler reloadHandler,
           ApplicationRepo applicationRepo,
           Curator curator,
           TenantFileSystemDirs tenantFileSystemDirs) {
        this.name = name;
        this.path = path;
        this.requestHandler = requestHandler;
        this.reloadHandler = reloadHandler;
        this.remoteSessionRepo = remoteSessionRepo;
        this.sessionFactory = sessionFactory;
        this.localSessionRepo = localSessionRepo;
        this.activateLock = new ActivateLock(curator, path);
        this.applicationRepo = applicationRepo;
        this.tenantFileSystemDirs = tenantFileSystemDirs;
        this.curator = curator;
    }

    /**
     * The reload handler for this
     *
     * @return handler
     */
    public ReloadHandler getReloadHandler() {
        return reloadHandler;
    }

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

    /**
     * The RemoteSessionRepo for this
     *
     * @return repo
     */
    public RemoteSessionRepo getRemoteSessionRepo() {
        return remoteSessionRepo;
    }

    public TenantName getName() {
        return name;
    }

    public Path getPath() {
        return path;
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public LocalSessionRepo getLocalSessionRepo() {
        return localSessionRepo;
    }

    /**
     * The activation lock for this
     * @return lock
     */
    public ActivateLock getActivateLock() {
        return activateLock;
    }
    
    @Override
    public String toString() {
        return getName().value();
    }

    public ApplicationRepo getApplicationRepo() {
        return applicationRepo;
    }

    public Curator getCurator() {
        return curator;
    }

    @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. Also removes any local state
     * in filesystem.
     */
    public void close() {
        tenantFileSystemDirs.delete();
        remoteSessionRepo.close();
        applicationRepo.close();
    }

    /**
     * Deletes a tenant from ZooKeeper and filesystem.
     */
    public void delete() {
        localSessionRepo.deleteAllSessions();
        curator.delete(path);
    }

    public void redeployApplications(Deployer deployer) {
        // TODO: Configurable timeout
        applicationRepo.listApplications().stream()
                .forEach(applicationId -> redeployApplication(applicationId, deployer));
    }

    private void redeployApplication(ApplicationId applicationId, Deployer deployer) {
        try {
            log.log(LogLevel.DEBUG, "Redeploying " + applicationId);
            deployer.deployFromLocalActive(applicationId, Duration.ofMinutes(30))
                    .ifPresent(deployment -> {
                        deployment.prepare();
                        deployment.activate();
                    });
        } catch (Exception e) {
            log.log(LogLevel.ERROR, "Redeploying " + applicationId + " failed", e);
        }
    }
}