package org.apache.cassandra.tools;

import org.apache.cassandra.service.StorageService;
import org.apache.cassandra.thrift.CassandraDaemon;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.silveregg.wrapper.WrapperListener;
import com.silveregg.wrapper.WrapperManager;


/**
 * Wrapper listener for cassandra server
 * 
 * @author Oleg Anastasyev<oa@hq.one.lv>
 *
 */
public class WrapperMain implements WrapperListener
{
/*    /**
     * Logger for this class
     */
    private static final Logger logger = LoggerFactory.getLogger(WrapperMain.class);
    private CassandraDaemon daemon;

    
    /**
     * 
     */
    public WrapperMain(CassandraDaemon daemon)
    {
        this.daemon = daemon;
    }
    
    

    public static void main (String[] args)
    {
        try
        {
            WrapperManager.start(new WrapperMain(new CassandraDaemon()), args);
        } catch (Exception e)
        {
            e.printStackTrace();
            WrapperManager.stop(-1);
        }

    }

    public Integer start(final String[] args) 
    {
        WrapperManager.signalStarting(3600000);

        try {
            daemon.setup();
            WrapperManager.signalStarting(30000);
            
            new Thread(new Runnable()
            {
                
                @Override
                public void run()
                {
                    daemon.start();
                }
            },"ThriftMain").start();
            
        }
        catch (Throwable e)
        {
            String msg = "Exception encountered during startup.";
            logger.error(msg, e);

            // try to warn user on stdout too, if we haven't already detached
            System.out.println(msg);
            e.printStackTrace();

            System.exit(3);
        }
        
        return null;
    }
    
    public int stop(int exitCode)
    {
        WrapperManager.signalStopping(30000);
        daemon.stop();
        
        WrapperManager.signalStopping(300000);
        try {
            StorageService.instance.drain();
        } catch (Exception e) {
            logger.error("Drain failed",e);
        }
        
        return exitCode;
    }

    public void controlEvent(int event)
    {
        if (!WrapperManager.isControlledByNativeWrapper())
            WrapperManager.stop(0);
    }

}

@@ -30,16 +30,71 @@
 tools;%0A%0A
+import org.apache.cassandra.config.DatabaseDescriptor;%0A
 import o
@@ -2014,32 +2014,390 @@
 top();%0A        %0A
+        // making pause before proceeding, so all thrift clients %0A        // go to failover node%0A        WrapperManager.signalStopping((int) (DatabaseDescriptor.getRpcTimeout()*3));%0A        try %7B%0A            Thread.sleep(DatabaseDescriptor.getRpcTimeout()*2);%0A        %7D catch (InterruptedException e1) %7B%0A            logger.error(%22Hm%22,e1);%0A        %7D%0A        %0A
         WrapperM

make pause after shutting down thrift, do not disturb failover node while all clients switch to it