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

import com.yahoo.text.Utf8;
import com.yahoo.vespa.config.server.monitoring.MetricUpdater;
import com.yahoo.vespa.curator.Curator;
import org.apache.curator.framework.recipes.cache.ChildData;

import java.util.concurrent.Executor;
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.yahoo.vespa.config.server.session.Session.Status;

/**
 * Watches one particular session (/config/v2/tenants/<tenantName>/sessions/<n>/sessionState in ZooKeeper)
 * The session must be in the session repo.
 *
 * @author Vegard Havdal
 * @author hmusum
 */
public class SessionStateWatcher {

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

    private final Curator.FileCache fileCache;
    private volatile RemoteSession session;
    private final MetricUpdater metrics;
    private final Executor zkWatcherExecutor;
    private final SessionRepository sessionRepository;

    SessionStateWatcher(Curator.FileCache fileCache,
                        RemoteSession session,
                        MetricUpdater metrics,
                        Executor zkWatcherExecutor,
                        SessionRepository sessionRepository) {
        this.fileCache = fileCache;
        this.session = session;
        this.metrics = metrics;
        this.fileCache.addListener(this::nodeChanged);
        this.fileCache.start();
        this.zkWatcherExecutor = zkWatcherExecutor;
        this.sessionRepository = sessionRepository;
    }

    private synchronized void sessionStatusChanged(Status newStatus) {
        long sessionId = session.getSessionId();
        switch (newStatus) {
            case NEW:
            case NONE:
                break;
            case PREPARE:
                createLocalSession(sessionId);
                sessionRepository.prepareRemoteSession(session);
                break;
            case ACTIVATE:
                createLocalSession(sessionId);
                sessionRepository.activate(session);
                break;
            case DEACTIVATE:
                sessionRepository.deactivate(session);
                break;
            case DELETE:
                sessionRepository.delete(session);
                break;
            default:
                throw new IllegalStateException("Unknown status " + newStatus);
        }
    }

    private void createLocalSession(long sessionId) {
        sessionRepository.createLocalSessionUsingDistributedApplicationPackage(sessionId);
    }

    public long getSessionId() {
        return session.getSessionId();
    }

    public void close() {
        try {
            fileCache.close();
        } catch (Exception e) {
            log.log(Level.WARNING, "Exception when closing watcher", e);
        }
    }

    private void nodeChanged() {
        zkWatcherExecutor.execute(() -> {
            Status newStatus = Status.NONE;
            try {
                ChildData node = fileCache.getCurrentData();
                if (node != null) {
                    newStatus = Status.parse(Utf8.toString(node.getData()));
                    log.log(Level.FINE, session.logPre() + "Session change: Session "
                                        + session.getSessionId() + " changed status to " + newStatus.name());
                    sessionStatusChanged(newStatus);
                }
            } catch (Exception e) {
                log.log(Level.WARNING, session.logPre() + "Error handling session change to " +
                                       newStatus.name() + " for session " + getSessionId(), e);
                metrics.incSessionChangeErrors();
            }
        });
    }

    public synchronized void updateRemoteSession(RemoteSession session) {
        this.session = session;
    }

}