/* This file is part of VoltDB.
 * Copyright (C) 2008-2012 VoltDB Inc.
 *
 * VoltDB is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VoltDB is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VoltDB.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.voltdb;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.apache.zookeeper_voltpatches.KeeperException;
import org.apache.zookeeper_voltpatches.ZooKeeper;

import org.voltcore.zk.LeaderElector;
import org.voltcore.zk.LeaderNoticeHandler;

/**
 * GlobalServiceElector performs leader election to determine which VoltDB cluster node
 * will be responsible for leading various cluster-wide services, particularly those which must
 * run on the same node.
 */
class GlobalServiceElector implements LeaderNoticeHandler
{
    private final LeaderElector m_leaderElector;
    private final List<Promotable> m_services = new ArrayList<Promotable>();

    GlobalServiceElector(ZooKeeper zk)
    {
        m_leaderElector = new LeaderElector(zk, VoltZK.leaders_globalservice,
                "globalservice", null, this);
    }

    /** Add a service to be notified if this node becomes the global leader */
    synchronized void registerService(Promotable service)
    {
        m_services.add(service);
    }

    /** Kick off the leader election */
    void start() throws KeeperException, InterruptedException, ExecutionException
    {
        m_leaderElector.start(true);
    }

    @Override
    synchronized public void becomeLeader()
    {
        for (Promotable service : m_services) {
            service.acceptPromotion();
        }
    }

    void shutdown() throws InterruptedException, KeeperException
    {
        m_leaderElector.shutdown();
    }
}

@@ -929,16 +929,57 @@
 eeper;%0A%0A
+import org.voltcore.logging.VoltLogger;%0A%0A
 import o
@@ -1330,16 +1330,86 @@
 ndler%0A%7B%0A
+    private static final VoltLogger hostLog = new VoltLogger(%22HOST%22);%0A
     priv
@@ -1525,16 +1525,48 @@
 able%3E();
+%0A    private final int m_hostId;
 %0A%0A    Gl
@@ -1596,16 +1596,28 @@
 eeper zk
+, int hostId
 )%0A    %7B%0A
@@ -1714,36 +1714,72 @@
     
-%22globalservice%22, null, this)
+Integer.toString(hostId), null, this);%0A        m_hostId = hostId
 ;%0A  
@@ -2194,32 +2194,122 @@
 eLeader()%0A    %7B%0A
+        hostLog.info(%22Host %22 + m_hostId + %22 promoted to be the global service provider%22);%0A
         for (Pro
@@ -2419,44 +2419,86 @@
 wn()
- throws InterruptedException, Keeper
+%0A    %7B%0A        try %7B%0A            m_leaderElector.shutdown();%0A        %7D catch (
 Exce
@@ -2494,36 +2494,35 @@
 catch (Exception
-%0A   
+ e)
  %7B%0A        m_lea
@@ -2520,35 +2520,115 @@
     
-m_leaderElector.shutdown();
+    VoltDB.crashLocalVoltDB(%22Error shutting down GlobalServiceElector's LeaderElector%22, true, e);%0A        %7D
 %0A   

Add some logging, other cleanup to GlobalServiceElector