<commit_before>// 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.session;

import java.util.logging.Level;
import com.yahoo.text.Utf8;
import com.yahoo.vespa.config.server.ReloadHandler;
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.Logger;

/**
 * Watches one particular session (/config/v2/tenants/&lt;tenantName&gt;/sessions/&lt;n&gt;/sessionState in ZooKeeper)
 * The session must be in the session repo.
 *
 * @author <NAME>
 */
public class RemoteSessionStateWatcher {

    private static final Logger log = Logger.getLogger(RemoteSessionStateWatcher.class.getName());
    // One thread pool for all instances of this class

    private final Curator.FileCache fileCache;
    private final ReloadHandler reloadHandler;
    private final RemoteSession session;
    private final MetricUpdater metrics;
    private final Executor zkWatcherExecutor;

    RemoteSessionStateWatcher(Curator.FileCache fileCache,
                              ReloadHandler reloadHandler,
                              RemoteSession session,
                              MetricUpdater metrics,
                              Executor zkWatcherExecutor) {
        this.fileCache = fileCache;
        this.reloadHandler = reloadHandler;
        this.session = session;
        this.metrics = metrics;
        this.fileCache.start();
        this.fileCache.addListener(this::nodeChanged);
        this.zkWatcherExecutor = zkWatcherExecutor;
    }

    private void sessionChanged(Session.Status status) {
        log.log(Level.FINE, session.logPre() + "Session change: Remote session " + session.getSessionId() + " changed status to " + status);

        // valid for NEW -> PREPARE transitions, not ACTIVATE -> PREPARE.
        if (status.equals(Session.Status.PREPARE)) {
            log.log(Level.FINE, session.logPre() + "Loading prepared session: " + session.getSessionId());
            session.loadPrepared();
        } else if (status.equals(Session.Status.ACTIVATE)) {
            session.makeActive(reloadHandler);
        } else if (status.equals(Session.Status.DEACTIVATE)) {
            session.deactivate();
        } else if (status.equals(Session.Status.DELETE)) {
            session.deactivate();
        }
    }

    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(() -> {
            Session.Status currentStatus = session.getStatus();
            Session.Status newStatus = Session.Status.NONE;
            try {
                ChildData node = fileCache.getCurrentData();
                if (node != null) {
                    newStatus = Session.Status.parse(Utf8.toString(node.getData()));
                    sessionChanged(newStatus);
                }
            } catch (Exception e) {
                log.log(Level.WARNING, session.logPre() + "Error handling session change from " + currentStatus.name() +
                                       " to " + newStatus.name() + " for session " + getSessionId(), e);
                metrics.incSessionChangeErrors();
            }
        });
    }

}<commit_msg>Do nothing if session status is unchanged<commit_after>// 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.session;

import java.util.logging.Level;
import com.yahoo.text.Utf8;
import com.yahoo.vespa.config.server.ReloadHandler;
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.Logger;

/**
 * Watches one particular session (/config/v2/tenants/&lt;tenantName&gt;/sessions/&lt;n&gt;/sessionState in ZooKeeper)
 * The session must be in the session repo.
 *
 * @author <NAME>
 */
public class RemoteSessionStateWatcher {

    private static final Logger log = Logger.getLogger(RemoteSessionStateWatcher.class.getName());
    // One thread pool for all instances of this class

    private final Curator.FileCache fileCache;
    private final ReloadHandler reloadHandler;
    private final RemoteSession session;
    private final MetricUpdater metrics;
    private final Executor zkWatcherExecutor;

    RemoteSessionStateWatcher(Curator.FileCache fileCache,
                              ReloadHandler reloadHandler,
                              RemoteSession session,
                              MetricUpdater metrics,
                              Executor zkWatcherExecutor) {
        this.fileCache = fileCache;
        this.reloadHandler = reloadHandler;
        this.session = session;
        this.metrics = metrics;
        this.fileCache.start();
        this.fileCache.addListener(this::nodeChanged);
        this.zkWatcherExecutor = zkWatcherExecutor;
    }

    private void sessionChanged(Session.Status status) {
        log.log(Level.FINE, session.logPre() + "Session change: Remote session " + session.getSessionId() + " changed status to " + status);

        // valid for NEW -> PREPARE transitions, not ACTIVATE -> PREPARE.
        if (status.equals(Session.Status.PREPARE)) {
            log.log(Level.FINE, session.logPre() + "Loading prepared session: " + session.getSessionId());
            session.loadPrepared();
        } else if (status.equals(Session.Status.ACTIVATE)) {
            session.makeActive(reloadHandler);
        } else if (status.equals(Session.Status.DEACTIVATE)) {
            session.deactivate();
        } else if (status.equals(Session.Status.DELETE)) {
            session.deactivate();
        }
    }

    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(() -> {
            Session.Status currentStatus = session.getStatus();
            Session.Status newStatus = Session.Status.NONE;
            try {
                ChildData node = fileCache.getCurrentData();
                if (node != null) {
                    newStatus = Session.Status.parse(Utf8.toString(node.getData()));
                    if (newStatus != currentStatus)
                        sessionChanged(newStatus);
                }
            } catch (Exception e) {
                log.log(Level.WARNING, session.logPre() + "Error handling session change from " + currentStatus.name() +
                                       " to " + newStatus.name() + " for session " + getSessionId(), e);
                metrics.incSessionChangeErrors();
            }
        });
    }

}
{'fraction_non_alphanumeric': 0.10118229309870773, 'fraction_numerical': 0.002474566950783613, 'mean_word_length': 2.3143507972665147, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2898179', 'n_tokens_mistral': 1955, 'n_tokens_neox': 1879, 'n_words': 441}