// 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 com.yahoo.config.application.api.ApplicationMetaData;
import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.provision.AllocatedHosts;
import com.yahoo.config.provision.TenantName;
import com.yahoo.lang.SettableOptional;
import com.yahoo.transaction.Transaction;
import com.yahoo.vespa.config.server.GlobalComponentRegistry;
import com.yahoo.vespa.config.server.application.ApplicationSet;
import com.yahoo.vespa.config.server.modelfactory.ActivatedModelsBuilder;
import com.yahoo.vespa.curator.Curator;
import org.apache.zookeeper.KeeperException;

import java.time.Clock;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * A RemoteSession represents a session created on another config server. This session can
 * be regarded as read only, and this interface only allows reading information about a session.
 *
 * @author Ulf Lilleengen
 */
public class RemoteSession extends Session {

    private static final Logger log = Logger.getLogger(RemoteSession.class.getName());
    private ApplicationSet applicationSet = null;
    private final ActivatedModelsBuilder applicationLoader;
    private final Clock clock;

    /**
     * Creates a session. This involves loading the application, validating it and distributing it.
     *
     * @param tenant The name of the tenant creating session
     * @param sessionId The session id for this session.
     * @param componentRegistry a registry of global components
     * @param zooKeeperClient a SessionZooKeeperClient instance
     */
    public RemoteSession(TenantName tenant,
                         long sessionId,
                         GlobalComponentRegistry componentRegistry,
                         SessionZooKeeperClient zooKeeperClient) {
        super(tenant, sessionId, zooKeeperClient);
        this.applicationLoader = new ActivatedModelsBuilder(tenant, sessionId, zooKeeperClient, componentRegistry);
        this.clock = componentRegistry.getClock();
    }

    void prepare() {
        Curator.CompletionWaiter waiter = sessionZooKeeperClient.getPrepareWaiter();
        ensureApplicationLoaded();
        notifyCompletion(waiter);
    }

    private ApplicationSet loadApplication() {
        ApplicationPackage applicationPackage = sessionZooKeeperClient.loadApplicationPackage();

        // Read hosts allocated on the config server instance which created this
        Optional<AllocatedHosts> allocatedHosts = applicationPackage.getAllocatedHosts();

        return ApplicationSet.fromList(applicationLoader.buildModels(sessionZooKeeperClient.readApplicationId(),
                                                                     sessionZooKeeperClient.readDockerImageRepository(),
                                                                     sessionZooKeeperClient.readVespaVersion(),
                                                                     applicationPackage,
                                                                     new SettableOptional<>(allocatedHosts),
                                                                     clock.instant()));
    }

    public synchronized ApplicationSet ensureApplicationLoaded() {
        return applicationSet == null ? applicationSet = loadApplication() : applicationSet;
    }

    public synchronized void deactivate() {
        applicationSet = null;
    }

    public Transaction createDeleteTransaction() {
        return sessionZooKeeperClient.createWriteStatusTransaction(Status.DELETE);
    }
    
    void confirmUpload() {
        Curator.CompletionWaiter waiter = sessionZooKeeperClient.getUploadWaiter();
        log.log(Level.FINE, "Notifying upload waiter for session " + getSessionId());
        notifyCompletion(waiter);
        log.log(Level.FINE, "Done notifying upload for session " + getSessionId());
    }

    void notifyCompletion(Curator.CompletionWaiter completionWaiter) {
        try {
            completionWaiter.notifyCompletion();
        } catch (RuntimeException e) {
            // Throw only if we get something else than NoNodeException -- NoNodeException might happen when
            // the session is no longer in use (e.g. the app using this session has been deleted) and this method
            // has not been called yet for the previous session operation
            // on a minority of the config servers (see awaitInternal() method in this class)
            if (e.getCause().getClass() != KeeperException.NoNodeException.class) {
                throw e;
            } else {
                log.log(Level.INFO, "Not able to notify completion for session: " + getSessionId() + ", node has been deleted");
            }
        }
    }

    public void delete() {
        Transaction transaction = sessionZooKeeperClient.deleteTransaction();
        transaction.commit();
        transaction.close();
    }

    public ApplicationMetaData getMetaData() {
        return sessionZooKeeperClient.loadApplicationPackage().getMetaData();
    }

}

@@ -793,24 +793,46 @@
 l.Optional;%0A
+import java.util.Set;%0A
 import java.
@@ -4302,10 +4302,46 @@
 ion 
---
+or NodeExistsException.%0A            //
  NoN
@@ -4370,31 +4370,16 @@
 pen when
-%0A            //
  the ses
@@ -4435,16 +4435,31 @@
  session
+%0A            //
  has bee
@@ -4484,31 +4484,16 @@
 s method
-%0A            //
  has not
@@ -4543,16 +4543,21 @@
 peration
+ on a
 %0A       
@@ -4563,21 +4563,16 @@
       //
- on a
  minorit
@@ -4598,51 +4598,125 @@
 vers
- (see awaitInternal() method
+.%0A            // NodeExistsException might happen if an event for this node is delivered more than once,
  in th
-is
+at
  c
-l
 as
-s)
+e
 %0A   
@@ -4728,268 +4728,791 @@
     
-if (e.getCause().getClass() != KeeperException.NoNodeException.class) %7B%0A                throw e;%0A            %7D else %7B%0A                log.log(Level.INFO, %22Not able to notify completion for session: %22 + getSessionId() + %22, node has been deleted%22);%0A            %7D
+// this is a no-op%0A            Set%3CClass%3C? extends KeeperException%3E%3E acceptedExceptions = Set.of(KeeperException.NoNodeException.class,%0A                                                                              KeeperException.NodeExistsException.class);%0A            Class%3C? extends Throwable%3E exceptionClass = e.getCause().getClass();%0A            if (acceptedExceptions.contains(exceptionClass))%0A                log.log(Level.INFO, %22Not able to notify completion for session: %22 + getSessionId() + %22, node %22 +%0A                                    (exceptionClass.equals(KeeperException.NoNodeException.class)%0A                                            ? %22has been deleted%22%0A                                            : %22already exists%22));%0A            else%0A                throw e;
 %0A   

Handle notifyCompletion being called more than once for a session