db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From oyste...@apache.org
Subject svn commit: r641329 - in /db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/replicationTests: ReplicationRun.java ReplicationRun_Local.java
Date Wed, 26 Mar 2008 14:41:04 GMT
Author: oysteing
Date: Wed Mar 26 07:41:02 2008
New Revision: 641329

URL: http://svn.apache.org/viewvc?rev=641329&view=rev
Log:
DERBY-3382: Adds regression test for out of sync checking between master and slave.
Contributed by Jorgen Loland

Merged to 10.4 branch with command:
svn merge -r641220:641221 https://svn.apache.org/repos/asf/db/derby/code/trunk/


Modified:
    db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/replicationTests/ReplicationRun.java
    db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/replicationTests/ReplicationRun_Local.java

Modified: db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/replicationTests/ReplicationRun.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/replicationTests/ReplicationRun.java?rev=641329&r1=641328&r2=641329&view=diff
==============================================================================
--- db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/replicationTests/ReplicationRun.java
(original)
+++ db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/replicationTests/ReplicationRun.java
Wed Mar 26 07:41:02 2008
@@ -32,6 +32,7 @@
 import org.apache.derby.jdbc.ClientDataSource;
 import org.apache.derby.shared.common.reference.SQLState;
 
+import org.apache.derbyTesting.junit.BaseJDBCTestCase;
 import org.apache.derbyTesting.junit.BaseTestCase;
 import org.apache.derbyTesting.junit.SecurityManagerSetup;
 
@@ -133,6 +134,11 @@
 
     String classPath = null; // Used in "localhost" testing.
     
+    /** A Connection to the master database*/
+    private Connection masterConn = null;
+    /** The exception thrown as a result of a startSlave connection attempt  */
+    private volatile Exception startSlaveException = null;
+
     /**
      * Creates a new instance of ReplicationRun
      * @param testcaseName Identifying the test.
@@ -902,8 +908,7 @@
                         {
                             util.DEBUG("Master already started?");
                         }
-                        se.printStackTrace(System.out);
-                        return; // Trying to continue. Is that reasonable?
+                        throw se;
                     }
                 }
 
@@ -912,6 +917,38 @@
     }
     
     /**
+     * Get a connection to the master database.
+     * @return A connection to the master database
+     */
+    protected Connection getMasterConnection() {
+        if (masterConn == null) {
+            String url = DB_PROTOCOL + "://" + masterServerHost + ":" +
+                         masterServerPort + "/" +
+                         masterDatabasePath + FS + masterDbSubPath + FS +
+                         replicatedDb;
+            try {
+                masterConn = DriverManager.getConnection(url);
+            } catch (SQLException sqle) {
+                fail("Could not connect to master database");
+            }
+        }
+        return masterConn;
+    }
+
+
+    /**
+     * Execute SQL on the master database through a Statement
+     * @param sql The sql that should be executed on the master database
+     * @throws java.sql.SQLException thrown if an error occured while
+     * executing the sql
+     */
+    protected void executeOnMaster(String sql) throws SQLException {
+         Statement s = getMasterConnection().createStatement();
+         s.execute(sql);
+         s.close();
+    }
+
+    /**
      * Set slave db in replication slave mode
      */
     void startSlave(String clientVM,
@@ -1046,7 +1083,6 @@
     private void startSlave_direct(String dbName,
             String slaveHost,  // Where the slave db is run.
             int slaveServerPort, // slave server interface accepting client requests
-            
             String slaveReplInterface,
             int slaveReplPort)
             throws Exception
@@ -1071,6 +1107,7 @@
             {
                 public void run()
                 {
+                    startSlaveException = null;
                     Connection conn = null;
                     try {
                         // NB! WIll hang here until startMaster is executed!
@@ -1090,6 +1127,8 @@
                     }
                     catch (SQLException se)
                     {
+                        startSlaveException = se;
+                        /*
                         int errCode = se.getErrorCode();
                         String msg = se.getMessage();
                         String state = se.getSQLState();
@@ -1105,12 +1144,12 @@
                             util.DEBUG("Got Exception " + msg);
                             se.printStackTrace();
                         }
-                        ;
+                        ;*/
                     }
                     catch (Exception ex)
                     {
+                        startSlaveException = ex;
                         util.DEBUG("Got Exception " + ex.getMessage());
-                        ex.printStackTrace();
                     }
                 }
             }
@@ -3100,5 +3139,37 @@
     static Load masterPostSlave;
     static Load slavePostSlave;
     ///////////////////////////////////////////////////////////////////////////
+
+    /**
+     * Assert that the latest startSlave connection attempt got the expected
+     * SQLState. The method will wait for upto 5 seconds for the startSlave
+     * connection attemt to complete. If the connection attempt has not
+     * completed after 5 seconds it is assumed to have failed.
+     * @param expected the expected SQLState
+     * @throws java.lang.Exception the Exception to check the SQLState of
+     */
+    protected void assertSqlStateSlaveConn(String expected) throws Exception {
+        boolean verified = false;
+        for (int i = 0; i < 10; i++) {
+            if (startSlaveException != null) {
+                if (startSlaveException instanceof SQLException) {
+                    BaseJDBCTestCase.
+                        assertSQLState("Unexpexted SQL State",
+                                       expected,
+                                       (SQLException)startSlaveException);
+                    verified = true;
+                    break;
+                } else {
+                    throw startSlaveException;
+                }
+            } else {
+                Thread.sleep(500);
+            }
+        }
+        if (!verified) {
+            fail("Attempt to start slave hangs. Expected SQL state " +
+                 expected);
+        }
+    }
     
 }

Modified: db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/replicationTests/ReplicationRun_Local.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/replicationTests/ReplicationRun_Local.java?rev=641329&r1=641328&r2=641329&view=diff
==============================================================================
--- db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/replicationTests/ReplicationRun_Local.java
(original)
+++ db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/replicationTests/ReplicationRun_Local.java
Wed Mar 26 07:41:02 2008
@@ -20,8 +20,10 @@
  */
 package org.apache.derbyTesting.functionTests.tests.replicationTests;
 
+import java.sql.SQLException;
 import junit.framework.Test;
 import junit.framework.TestSuite;
+import org.apache.derbyTesting.junit.BaseJDBCTestCase;
 import org.apache.derbyTesting.junit.SecurityManagerSetup;
 
 
@@ -167,5 +169,90 @@
         // do a 'kill pid' after ending the test run
         
     }
+
+    /**
+     * DERBY-3382: Test that start replication fails if master db is updated
+     * after copying the db to the slave location
+     * @throws java.lang.Exception on test errors.
+     */
+    public void testLogFilesSynched() throws Exception {
+
+        cleanAllTestHosts();
+        initEnvironment();
+        initMaster(masterServerHost, replicatedDb);
+
+        masterServer = startServer(masterJvmVersion,
+                                   derbyMasterVersion,
+                                   masterServerHost,
+                                   ALL_INTERFACES,
+                                   masterServerPort,
+                                   masterDbSubPath);
+        slaveServer = startServer(slaveJvmVersion,
+                                  derbySlaveVersion,
+                                  slaveServerHost,
+                                  ALL_INTERFACES,
+                                  slaveServerPort,
+                                  slaveDbSubPath);
+
+        startServerMonitor(slaveServerHost);
+
+        bootMasterDatabase(jvmVersion,
+                           masterDatabasePath + FS + masterDbSubPath,
+                           replicatedDb,
+                           masterServerHost,
+                           masterServerPort,
+                           null);
+
+        // copy db to slave
+        initSlave(slaveServerHost,
+                  jvmVersion,
+                  replicatedDb);
+
+        // database has now been copied to slave. Updating the master
+        // database at this point will cause unsynced log files
+        executeOnMaster("call syscs_util.syscs_unfreeze_database()");
+        executeOnMaster("create table breakLogSynch (v varchar(20))");
+        executeOnMaster("drop table breakLogSynch");
+
+        // startSlave is supposed do fail. We check the sql state in
+        // assertSqlStateSlaveConn below
+        startSlave(jvmVersion, replicatedDb,
+                   slaveServerHost,
+                   slaveServerPort,
+                   slaveServerHost,
+                   slaveReplPort,
+                   testClientHost);
+
+        SQLException sqlexception = null;
+        try {
+            startMaster(jvmVersion, replicatedDb,
+                        masterServerHost,
+                        masterServerPort,
+                        masterServerHost,
+                        slaveServerPort,
+                        slaveServerHost,
+                        slaveReplPort);
+        } catch (SQLException sqle) {
+            sqlexception = sqle;
+        }
+        // the startMaster connection attempt should fail with exception XRE05
+        if (sqlexception == null) {
+            fail("Start master did not get the expected SQL Exception XRE05");
+        } else {
+            BaseJDBCTestCase.assertSQLState("Unexpected SQL state.",
+                                            "XRE05",
+                                            sqlexception);
+        }
+
+        // The startSlave connection attempt should fail with exception XJ040
+        assertSqlStateSlaveConn("XJ040");
+
+        stopServer(jvmVersion, derbyVersion,
+                   masterServerHost, masterServerPort);
+        stopServer(jvmVersion, derbyVersion,
+                   slaveServerHost, slaveServerPort);
+
+    }
+
     
 }



Mime
View raw message