/* This file is part of VoltDB.
 * Copyright (C) 2008-2015 VoltDB Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package txnIdSelfCheck;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

import org.voltdb.client.Client;
import org.voltdb.client.ClientResponse;
import org.voltdb.client.ProcCallException;

public class DdlThread extends BenchmarkThread {

    public static AtomicLong progressInd = new AtomicLong(0);
    final Client client;
    final AtomicBoolean m_shouldContinue = new AtomicBoolean(true);
    final AtomicBoolean m_needsBlock = new AtomicBoolean(false);
    final String[] createOrDrop = { "create table anothertable (a int);",
                                    "drop table anothertable if exists;" };

    public DdlThread(Client client) {
        setName("DdlThread");
        this.client = client;
    }

    void shutdown() {
        m_shouldContinue.set(false);
        this.interrupt();
    }

    @Override
    public void run() {
        int count = 0;
        int errcnt = 0;
        while (m_shouldContinue.get()) {
            // call a ddl transaction
            log.info (createOrDrop[count]);
            try {
                ClientResponse cr = TxnId2Utils.doAdHoc(client, createOrDrop[count]);
                if (cr.getStatus() != ClientResponse.SUCCESS) {
                    hardStop("DDL failed: " + cr.getStatusString());
                } else {
                    log.info("DDL success #" + Long.toString(progressInd.get()) + " : " + createOrDrop[count]);
                    progressInd.getAndIncrement();
                    Benchmark.txnCount.incrementAndGet();
                    errcnt = 0;
                }
            }
            catch (ProcCallException e) {
                ClientResponse cr = e.getClientResponse();
                if (cr.getStatusString().matches("Unexpected exception applying DDL statements to original catalog: DDL Error: \"object name already exists:.*")) {
                    if (errcnt > 1) {
                        hardStop("too many ddl errors", e);
                    } else
                        errcnt++;
                }
            }
            catch (Exception e) {
                hardStop("DdlThread threw an error:", e);
            }
            count = ++count & 1;
            try { Thread.sleep(10000); }
            catch (Exception e) {}
        }
        log.info(getName() + " thread has stopped");
    }
}

@@ -2850,36 +2850,44 @@
                 
-if (
+String ss = 
 cr.getStatusStri
@@ -2894,136 +2894,283 @@
 ng()
-.matches(%22Unexpected exception applying DDL statements to original catalog: DDL Error: %5C%22object name already exists:.*%22)) %7B%0A    
+;%0A                // nb if rejoining and table exists create table will get table exists but drop table will get rejoin in progress%0A                if (ss.matches(%22(?s).*Can't do a catalog update while an elastic join or rejoin is active.*%22))%0A                    errcnt = 0;%0A
     
@@ -3195,28 +3195,24 @@
 rcnt %3E 1) %7B%0A
-    
             
@@ -3275,23 +3275,17 @@
     
-    
 %7D else
-%0A    
+ %7B%0A
     

fix throwing 'too many ddl errors' in rejoin tests