aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/session/SessionStateWatcher.java
blob: 88d584d3e5ede4eaef31838add77b2469a1f71b0 (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
// 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.session;

import com.yahoo.text.Utf8;
import com.yahoo.vespa.config.server.NotFoundException;
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 session state for a 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 final long sessionId;
    private final MetricUpdater metrics;
    private final Executor zkWatcherExecutor;
    private final SessionRepository sessionRepository;

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

    private synchronized void sessionStatusChanged(Status newStatus) {
        switch (newStatus) {
            case NEW:
            case UNKNOWN:
                break;
            case DELETE:
            case DEACTIVATE:
                sessionRepository.deactivateSession(sessionId);
                break;
            case PREPARE:
                sessionRepository.prepareRemoteSession(sessionId);
                break;
            case ACTIVATE:
                sessionRepository.activate(sessionId);
                break;
            default:
                throw new IllegalStateException("Unknown status " + newStatus);
        }
    }

    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.UNKNOWN;
            try {
                ChildData node = fileCache.getCurrentData();
                if (node != null) {
                    newStatus = Status.parse(Utf8.toString(node.getData()));
                    sessionStatusChanged(newStatus);
                }
            } catch (NotFoundException e) {
                log.log(Level.INFO, "Session or application not found when handling session change to " + newStatus.name() + " for session " + sessionId);
                metrics.incSessionChangeErrors();
            } catch (Exception e) {
                log.log(Level.WARNING, "Error handling session change to " + newStatus.name() + " for session " + sessionId, e);
                metrics.incSessionChangeErrors();
            }
        });
    }

}