File: OnlineBackupTest3.java

package info (click to toggle)
derby 10.14.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 79,056 kB
  • sloc: java: 691,961; sql: 42,686; xml: 20,512; sh: 3,373; sed: 96; makefile: 60
file content (532 lines) | stat: -rw-r--r-- 20,063 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*

   Derby - Class org.apache.derbyTesting.functionTests.store.OnlineBackupTest3

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */

package org.apache.derbyTesting.functionTests.tests.store;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.derby.tools.ij;
import org.apache.derbyTesting.functionTests.util.TestUtil;

/*
 * This class tests online backup when jar actions
 * are running in parallel to the backup thread. 
 *
 * @version 1.0
 */

public class OnlineBackupTest3 {

    private static final String TEST_DATABASE_NAME = "wombat" ;
    private static final String BACKUP_PATH = "extinout/onlinebackuptest3";

    public static void main(String[] argv) throws Throwable {

        OnlineBackupTest3 test = new OnlineBackupTest3();
        ij.getPropertyArg(argv); 

        try {
            test.runTest();
        }
        catch (SQLException sqle) {
            dumpSQLException(sqle);
        } 
    }


    /*
     * Test online backup with unlogged jar operations running in parallel. 
     */
    private void runTest() throws Exception{
        logMessage("Begin Online Backup Test3");
        Connection conn = ij.startJBMS();
        conn.setAutoCommit(false);
        Statement stmt = conn.createStatement();
        stmt.execute("create table t1(a int ) ");
        stmt.execute("insert into t1 values(1)");
        stmt.execute("insert into t1 values(2)");
        stmt.execute("create table customer(id int , name varchar(100))");
        stmt.execute("insert into customer values(1, 'ABC')");
        stmt.execute("insert into customer values(2, 'XYZ')");
        String crproc = "create procedure addCustomer(id INT, name VARCHAR(100)) " +
            "MODIFIES SQL DATA " + 
            "external name " + 
            "'org.apache.derbyTesting.backupRestore.Customer.addCustomer' " + 
            " language java parameter style java ";
            
        stmt.execute(crproc);

        String dvfunc = "create function dv(P1 INT) RETURNS INT NO SQL " +
            " external name 'dbytesting.CodeInAJar.doubleMe' " + 
            " language java parameter style java " ;

        stmt.execute(dvfunc) ;
        conn.commit();
        
        logMessage("Initial Setup Complete");

        // perform install jar operation with 
        // online backup running in parallel.
        installJarTest();

        // perform remove jar operation with 
        // online backup running in parallel.
        removeJarTest();

        logMessage("End Online Backup Test3");
    }


    /**
     * Shutdown the datbase
     * @param  dbName  Name of the database to shutdown.
     */
    void shutdown(String dbName) {

        try{
            //shutdown
            TestUtil.getConnection(dbName, "shutdown=true");
        }catch(SQLException se){
            if (se.getSQLState() != null && se.getSQLState().equals("08006"))
                System.out.println("database shutdown properly");
            else
                dumpSQLException(se);
        }
    }

    /**
     * Write message to the standard output.
     */
    void logMessage(String   str){
        System.out.println(str);
    }

    /**
     * dump the SQLException to the standard output.
     */
    static private void dumpSQLException(SQLException sqle) {

        org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(System.out, sqle);
        sqle.printStackTrace(System.out);
    }

    
    private int countRows(Connection conn, 
                          String tableName) 
        throws SQLException
    {
        Statement s = conn.createStatement();
        ResultSet rs = s.executeQuery("SELECT count(*) from " +  tableName );
        rs.next();
        int noRows = rs.getInt(1);
        rs.close();
        s.close();
        return noRows;
    }

    /*
     * Test install jar running in parallel to backup and vice versa. 
     */
    void installJarTest() throws Exception{
        logMessage("Begin Install Jar Test");
        Connection conn1 = TestUtil.getConnection(TEST_DATABASE_NAME, null);
        conn1.setAutoCommit(false);
        Statement conn1_stmt = conn1.createStatement();
        Connection conn2 = TestUtil.getConnection(TEST_DATABASE_NAME, null);
        conn2.setAutoCommit(false);
        Statement conn2_stmt = conn2.createStatement();

        
        conn1_stmt.execute(
           "call sqlj.install_jar('extin/brtestjar.jar', 'math_routines', 0)");
        
        try {
            // followng backup call should fail because jar operation is pending 
           conn2_stmt.execute(
            "call SYSCS_UTIL.SYSCS_BACKUP_DATABASE_NOWAIT('extinout/mybackup')");
        } catch (SQLException sqle) {
            //above statement should have failed. 
            org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(System.out, sqle);
        }

        // invoke backup in another thread, it should block for the above install jar 
        // operation to install  'brtestjar.jar to commit.
        
        // start a  thread to perform online backup
        OnlineBackup backup = new OnlineBackup(TEST_DATABASE_NAME, BACKUP_PATH);
        Thread backupThread = new Thread(backup, "BACKUP1");
        backupThread.start();
        // wait for the backup to start
        backup.waitForBackupToBegin();
        logMessage("Backup-1 Started");

        // sleep for few seconds just to make sure backup thread has actually
        // gone into a wait state for unlogged actions to commit.
        java.lang.Thread.sleep(1000);
        
        // backup should not even start doing real work before the
        // unlogged transaction is commited
        if(!backup.isRunning())
            logMessage("Backup is not waiting for unlogged " +  
                       "install jar action to commit");

        //insert some rows that should appear in the backup.
        conn1_stmt.execute("insert into t1 values(3)");
        conn1_stmt.execute("insert into t1 values(4)");
        conn1_stmt.execute("insert into t1 values(5)");
        
        // set the database class with both the jars  installed above.
        conn1_stmt.execute("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY( " + 
                           "'derby.database.classpath', " + 
                           "'APP.math_routines') " ) ;

        //commit the transaction with jar opearation that is blocking the backup.
        conn1.commit();
        logMessage("The transaction that was blocking the backup has ended");

        // wait for backup to finish. 
        backup.waitForBackupToEnd();
        backupThread.join();
        logMessage("Backup-1 Completed");
        
        // Case : jar op should block if backup is in progress
        // add a index that will block the backup until it is committted.
        conn1_stmt.execute("create index idx1 on customer(id)");
        conn1_stmt.execute("insert into t1 values(6)");
        
        // start a  thread to perform online backup
        backup = new OnlineBackup(TEST_DATABASE_NAME, BACKUP_PATH);
        backupThread = new Thread(backup, "BACKUP2");
        backupThread.start();
        // wait for the backup to start
        backup.waitForBackupToBegin();
        logMessage("Backup-2 Started");

        // sleep for few seconds just to make sure backup thread is actually
        // gone to a wait state for unlogged actions to commit.
        java.lang.Thread.sleep(1000);

        // backup should not even start doing real work before the
        // unlogged transaction is commited
        if(!backup.isRunning())
            logMessage("Backup is not waiting for unlogged " +  
                       "index action to commit");


        // add another jar file  , this one should block and 
        // should not get into the backup. Backup does not allow new 
        // jar operation if it is already waiting for backup blocking
        // to complete(commit/rollback). 

        AsyncStatementThread asyncJarActionThread = 
            new AsyncStatementThread(conn2, 
          "call sqlj.install_jar('extin/obtest_customer.jar', 'customer_app', 0)");
        asyncJarActionThread.start();
        logMessage("Started obtest_customer.jar addition in seperate thread");

        //sleep for few seconds to give a chance for the 
        //jar addition thread to get into action.
        java.lang.Thread.sleep(1000);

        //roll back the index op. Backup should proceed now.
        conn1.rollback();
        logMessage("The transaction that was blocking the backup has ended");

        // wait for backup to finish. 
        backup.waitForBackupToEnd();
        backupThread.join();
        logMessage("Backup-2 Completed");
        
        // wait for customer app jar installation to finish now. 
        asyncJarActionThread.join();
        logMessage("obtest_customer.jar addition is complete");

        // set the database class with both the jars  installed above.
        conn1_stmt.execute("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY( " + 
                           "'derby.database.classpath', " + 
                           "'APP.customer_app:APP.math_routines') " ) ;
        
        conn1.commit();

        // second jar must have got installed after the backup. 
        // call a function in the custome_app jar
        conn1_stmt.execute("call addCustomer(3 , 'John')");
        conn1.commit();
        
        logMessage("No of rows in table t1: " + countRows(conn1, "T1"));
        logMessage("No of rows in table customer: " + 
                   countRows(conn1, "customer"));
        conn1.commit();
        conn2.commit();
        conn1_stmt.close();
        conn2_stmt.close();
        conn1.close();
        conn2.close();
        
        //shutdown the test db 
        shutdown(TEST_DATABASE_NAME);
        // restore the database from the backup and run some checks 
        backup.restoreFromBackup();
        logMessage("Restored From the Backup");
        Connection conn = TestUtil.getConnection(TEST_DATABASE_NAME, null);
        Statement stmt = conn.createStatement();
        logMessage("No of rows in table t1: " + countRows(conn, "T1"));
        logMessage("No of rows in table customer: " + 
                   countRows(conn, "customer"));
        // execute select statement using the "dv" funciont.  
        stmt.execute("select dv(a) from t1");

        
        try {
            // set the database class with both the jars  installed above.
            stmt.execute("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY( " + 
                           "'derby.database.classpath', " + 
                           "'APP.customer_app:APP.math_routines') " ) ;
            stmt.execute("call addCustomer(3 , 'John')"); 
        }catch(SQLException se) {
            //ignore for now. No sure way to 
            //check that jar did not get into backup 
            //without debug flags. 
        }

        stmt.close();
        conn.close();

        //shutdown the test db 
        shutdown(TEST_DATABASE_NAME);
        logMessage("End Of Install Jar Test.");

    }


    /*
     * Test remove jar running in parallel to backup and vice versa. 
     */
    void removeJarTest() throws Exception{
        logMessage("Begin Remove Jar Test");
        Connection conn1 = TestUtil.getConnection(TEST_DATABASE_NAME, null);
        conn1.setAutoCommit(false);
        Statement conn1_stmt = conn1.createStatement();
        Connection conn2 = TestUtil.getConnection(TEST_DATABASE_NAME, null);
        conn2.setAutoCommit(false);
        Statement conn2_stmt = conn2.createStatement();
        try {
            conn1_stmt.execute(
           "call sqlj.install_jar('extin/obtest_customer.jar', 'customer_app', 0)");
        }catch(SQLException se) {
            //it is ok if was jar already there.
        }

        // remove both the jars from the class path , 
        // so that we can remove them from the database. 
        conn1_stmt.execute("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY( " + 
                        "'derby.database.classpath', '')") ;
        conn1.commit();

        conn1_stmt.execute(
           "call sqlj.remove_jar('APP.math_routines', 0)");
        
        // Case 0: backup call that is not waiting for unlogged 
        // opereation to complete should fail when a remove jar 
        // is not ended when backup started. 

        try {
            // followng backup call should fail because remove 
            // jar operation is pending 
           conn2_stmt.execute(
            "call SYSCS_UTIL.SYSCS_BACKUP_DATABASE_NOWAIT('extinout/mybackup')");
        } catch (SQLException sqle) {
            //above statement should have failed. 
            org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(System.out, sqle);
        }


                
        // Case 1: backup should block because when a remove jar
        // is not ended when backup started. 

        // invoke backup in another thread, should block for 
        // the above remove jar  to commit.
        
        // start a  thread to perform online backup
        OnlineBackup backup = new OnlineBackup(TEST_DATABASE_NAME, BACKUP_PATH);
        Thread backupThread = new Thread(backup, "BACKUP3");
        backupThread.start();
        // wait for the backup to start
        backup.waitForBackupToBegin();
        logMessage("Backup-3 Started");

        // sleep for few seconds just to make sure backup thread is actually
        // gone to a wait state for unlogged actions to commit.
        java.lang.Thread.sleep(1000);

        // backup should not even start doing real work before the
        // unlogged transaction is commited
        if(!backup.isRunning())
            logMessage("Backup is not waiting for unlogged " +  
                       "remove jar action to commit");

        //insert some rows that should appear in the backup.
        conn1_stmt.execute("insert into t1 values(10)");
        conn1_stmt.execute("insert into t1 values(11)");

        //commit the transaction with jar opearation that is blocking the backup.
        conn1.commit();
        logMessage("The transaction that was blocking the backup has ended");
        
        // wait for backup to finish. 
        backup.waitForBackupToEnd();
        backupThread.join();

        logMessage("Backup-3 Completed");
        
        // Case 2: remove jar op should block if backup is in progress
        // add a index that will block the backup until it is committted.
        conn1_stmt.execute("create index idx1 on customer(id)");
        conn1_stmt.execute("insert into t1 values(12)");
        
        // start a  thread to perform online backup
        backup = new OnlineBackup(TEST_DATABASE_NAME, BACKUP_PATH);
        backupThread = new Thread(backup, "BACKUP4");
        backupThread.start();
        // wait for the backup to start
        backup.waitForBackupToBegin();
        logMessage("Backup-4 Started");

        // sleep for few seconds just to make sure backup thread is actually
        // gone to a wait state for unlogged actions to commit.
        java.lang.Thread.sleep(1000);

        // backup should not even start doing real work before the
        // unlogged transaction is commited
        if(!backup.isRunning())
            logMessage("Backup is not waiting for unlogged " +  
                       "index action to commit");


        // remove another jar file  , this one should block and 
        // should not get into the backup. Backup does not allow new 
        // jar operation if it is already waiting for backup blocking
        // to complete(commit/rollback). 

        AsyncStatementThread asyncJarActionThread = 
            new AsyncStatementThread(conn2, 
          "call sqlj.remove_jar('APP.customer_app', 0)");
        asyncJarActionThread.start();
        logMessage("Started obtest_customer.jar remove in seperate thread");

        //sleep for few seconds to give a chance for the 
        //jar addition thread to get into action.
        java.lang.Thread.sleep(1000);

        //roll back the index op. Backup should proceed now.
        conn1.rollback();
        logMessage("The transaction that was blocking the backup has ended");
        // wait for backup to finish. 
        backup.waitForBackupToEnd();
        backupThread.join();
        logMessage("Backup-4 Completed");

        // wait for customer app jar removal to finish now. 
        asyncJarActionThread.join();
        logMessage("obtest_customer.jar remove is complete");
        
        //this insert should not apprear on restore.
        conn1_stmt.execute("insert into t1 values(13)");

        logMessage("No of rows in table t1: " + countRows(conn1, "T1"));
        logMessage("No of rows in table customer: " + 
                   countRows(conn1, "customer"));
        conn1.commit();
        conn2.commit();
        conn1_stmt.close();
        conn2_stmt.close();
        conn1.close();
        conn2.close();
        
        //shutdown the test db 
        shutdown(TEST_DATABASE_NAME);
        // restore the database from the backup and run some checks 
        backup.restoreFromBackup();
        logMessage("Restored From the Backup");
        Connection conn = TestUtil.getConnection(TEST_DATABASE_NAME, null);
        Statement stmt = conn.createStatement();
        logMessage("No of rows in table t1: " + countRows(conn, "T1"));
        logMessage("No of rows in table customer: " + 
                   countRows(conn, "customer"));

        // check if the jar removal was successful.
        // APP.math_routines should not be in backup.
        try {
            // set the database class path with the jar removed above, 
            // it should fail.
            stmt.execute("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY( " + 
                           "'derby.database.classpath', " + 
                           "'APP.math_routines') " ) ;
        }catch (SQLException sqle) {
            //above statement should have failed. 
            org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(System.out, sqle);
        }
        

        stmt.close();
        conn.close();

        //shutdown the test db 
        shutdown(TEST_DATABASE_NAME);
        logMessage("End Of Remove Jar Test.");

    }


    /*
     * Run a sql statement in a seperate thread. 
     */
    class AsyncStatementThread extends Thread {
        Connection conn;
        String stmt;
    
        AsyncStatementThread(Connection conn, String stmt) {
            this.conn = conn;
            this.stmt = stmt;
        }

        public void run() {
            Statement aStatement = null;
            try {
                aStatement = conn.createStatement();
                aStatement.execute(stmt);
                aStatement.close();
                // commit here, it is possible that 
                // this thread may have got into action 
                // before the backup went into wait state.
                conn.commit();
            } catch (SQLException sqle) {
                org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException(System.out, sqle);
                sqle.printStackTrace(System.out);
            }
            aStatement = null;
        }
    }

}