File: DropWhileConnectingTest.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 (406 lines) | stat: -rw-r--r-- 15,845 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
/*

   Derby - Class org.apache.derbyTesting.functionsTests.tests.memorydb.DropWhileConnectingTest

   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.memorydb;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import junit.framework.Test;
import org.apache.derbyTesting.functionTests.util.PrivilegedFileOpsForTests;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;

/**
 * <em>WARNING: This test isn't finalized!</em>
 *
 * Tests the behavior when several threads are accessing the database
 * and one thread comes in and drops the database.
 * The success of the test is defined by allowing only certain exceptions
 * from the access threads / connections. During connection, the following
 * exceptions are allowed:
 * <ul> <li>XJ004: Database not found.</li>
 *      <li>?????: Database access blocked.<li>
 * </ul>
 * During normal operation, the set is:
 * <ul>
 *      <li>XJ001*: Shutdown exception.</li>
 *      <li>08003: No current connection.</li>
 *  </ul>
 * <p>
 * This test has a known weakness in that it doesn't execute long-running
 * queries. It is not clear how these react when the database is dropped
 * under their feet.
 */
public class DropWhileConnectingTest
        extends BaseJDBCTestCase {

    public DropWhileConnectingTest(String name) {
        super(name);
    }

    public void testConcurrentAccessAndDrop()
            throws SQLException {
        final String url = "jdbc:derby:memory:testDB";
        // Database owner is the default user APP.
        Connection con =
                MemoryDbManager.getSharedInstance().createDatabase("testDB");
        con.close();
        String threadsStr = getSystemProperty("derby.tests.threadCount");
        if (threadsStr == null) {
            threadsStr = "20";
        }
        int accessThreads = Integer.parseInt(threadsStr);
        println("threadCount=" + accessThreads);
        Report report = new Report(getFailureFolder(), accessThreads);
        // Start the access threads.
        for (int i=0; i < accessThreads; i++) {
            Thread t = new Thread(new AccessThread(report, url));
            t.start();
        }
        // Signal start, then wait a little before deleting the database.
        report.start();
        sleep(2500);
        try {
            MemoryDbManager.getSharedInstance().dropDatabase("testDB");
            fail("Dropping database should have raised exception.");
        } catch (SQLException sqle) {
            assertSQLState("08006", sqle);
        }
        println("Drop database request executed.");
        // Wait for all the threads to finish (they may be sleeping).
        while (!report.allThreadsDone()) {
            println("Waiting for " + report.remainingThreads() +
                    " remaining thread(s) to finish...");
            sleep(500);
        }
        assertFalse(report.toString(), report.hasUnexpectedExceptions());
        println(report.toString());
    }

    public static Test suite() {
       return new BaseTestSuite(DropWhileConnectingTest.class);
    }

    /**
     * Simple report class holding results from the test run. Also used to
     * control the start of the worker threads.
     */
    private static class Report {
        /** Sync object used to start the threads. */
        private final Object sync = new Object();
        //@GuardedBy("sync")
        private boolean ready;

        /** Failure folder where any exceptions will be logged to file. */
        private final File failureFolder;
        /** Writer used to log stack traces, lazily initialized. */
        private PrintWriter writer;
        /** The number of successful connections made by the worker threads. */
        private final int[] accessCounts;
        /** Any unexpected exceptions encountered by the worker threads. */
        private final Throwable[] exceptions;
        private int threadsDone;
        private boolean hasExceptions;
       
        /**
         * Creates a report object.
         *
         * @param failureFolder where to write exceptions to
         * @param accessThreads number of worker threads
         */
        public Report(File failureFolder, int accessThreads) {
            this.failureFolder = failureFolder;
            accessCounts = new int[accessThreads];
            exceptions = new Throwable[accessThreads];
        }

        public synchronized boolean hasUnexpectedExceptions() {
            return this.hasExceptions;
        }

        /**
         * Reports the access count for the specified worker thread.
         *
         * @param id worker thread id, must be in the range [0, threadCount&gt;
         * @param accessCount number of successful accesses made to the db
         */
        public synchronized void reportAccessCount(int id, int accessCount) {
            accessCounts[id] = accessCount;
            threadsDone++;
        }

        /**
         * Reports an unexpected error and the access count for the specified
         * worker thread.
         *
         * @param id worker thread id, must be in the range [0, threadCount&gt;
         * @param accessCount number of successful accesses made to the db
         * @param error error to report
         */
        public synchronized void reportError(int id, int accessCount,
                                             Throwable error) {
            reportAccessCount(id, accessCount);
            exceptions[id] = error;
            hasExceptions = true;
            // Print the stack trace to file.
            dumpToFile(id, error);
        }

        /**
         * Tells if all the access threads have finished.
         *
         * @return {@code true} if all threads have finished,
         *      {@code false} otherwise.
         */
        public synchronized boolean allThreadsDone() {
            return (threadsDone == accessCounts.length);
        }

        public synchronized int remainingThreads() {
            return accessCounts.length - threadsDone;
        }

        public Object getSync() {
            return this.sync;
        }

        /**
         * Tells if the test is ready to start.
         *
         * @return {@code true} if the access threads can start.
         */
        public boolean ready() {
            synchronized (sync) {
                return ready;
            }
        }

        public void start() {
            synchronized (sync) {
                ready = true;
                sync.notifyAll();
            }
        }

        public synchronized String toString() {
            int totalAccessCount = 0;
            for (int i=0; i < accessCounts.length; i++) {
                int c = accessCounts[i];
                if (c > 0) {
                    totalAccessCount += c;
                }
            }
            String sep = "\n";
            StringBuffer sb = new StringBuffer(sep +
                    "Number of access threads: ").append(accessCounts.length).
                    append(sep);
            sb.append("Access count: " + totalAccessCount).append(sep);
            if (hasExceptions) {
                sb.append("Exceptions (see " + failureFolder +
                        "/exceptions.log):" + sep);
                for (int i=0; i < exceptions.length; i++) {
                    Throwable t = exceptions[i];
                    if (t instanceof SQLException) {
                        SQLException s = (SQLException)t;
                        sb.append("id=").append(i).append(" : (").
                        append(s.getSQLState()).append(") ").
                        append(s.getMessage()).append(sep);
                    } else if (t != null) {
                        sb.append("id=").append(i).append(" : (     ) ").
                        append(t.getMessage()).append(sep);

                       
                    }
                }
            }
            return sb.toString();
        }

        /**
         * Dumps the exception to file.
         *
         * @param id internal id for the thread that got the exception
         * @param exception the exception to dump
         */
        private void dumpToFile(int id, Throwable exception) {
            if (writer == null) {
                try {
                    writer = new PrintWriter(
                            PrivilegedFileOpsForTests.getFileOutputStream(
                            new File(failureFolder, ("exceptions.log"))));
                    writer.println(new java.util.Date());
                } catch (IOException ioe) {
                    alarm("Failed to create exception log file: " +
                            ioe.getMessage());
                }
            }
            if (writer != null) {
                writer.println("-----");
                writer.println("id=" + id);
                writer.println("--");
                exception.printStackTrace(writer);
                writer.flush();
            }
        }
    }

    /**
     * Access thread connection to the database and performing a simple SQL
     * select query. Will accept a few specific exceptions as the database
     * is deleted "under its feet", all other exceptions are considered a
     * failure and will be reported.
     */
    private static class AccessThread
            implements Runnable {

        private static final Object LOCK = new Object();
        //@GuardedBy("LOCK")
        private static int idCounter = 0;
        /** Whether to wait at certain points in the execution. */
        private static final boolean noWait;
        static {
            String tmp = getSystemProperty("derby.tests.noWait");
            noWait = Boolean.valueOf(tmp).booleanValue();
            println("noWait=" + noWait);
        }

        private final int id;
        private final Report master;
        private final String url;
        private final Random rnd = new Random();
        /** Flag used to avoid waiting at multiple points in the execution. */
        private boolean waited;

        public AccessThread(Report master, String url) {
            synchronized (LOCK) {
                this.id = idCounter++;                
            }
            this.master = master;
            // Connect with a different user than the DBO. There is no real
            // reaon for doing this, other than making sure this user will
            // be blocked out by Derby during the shutdown.
            this.url = url + ";user=test;password=test";
        }

        public void run() {
            int access = 0;
            Connection con = null;
            // Wait for signal to start testing.
            while (!master.ready()) {
                synchronized (master.getSync()) {
                    try {
                        master.getSync().wait();
                    } catch (InterruptedException ie) {
                        // Ignore, just check the loop condition again.
                    }
                }
            }
            // Start accessing the database.
            try {
                while (true) {
                    waited = false;
                    try {
                        con = DriverManager.getConnection(url);
                        access++;
                    } catch (SQLException sqle) {
                        // See if the exception says database not found.
                        // An additional check would be to make sure this
                        // happens after the database has been dropped, and
                        // that it is not caused by a bug during boot.
                        if (sqle.getSQLState().equals("XJ004")) {
                            master.reportAccessCount(id, access);
                            break;
                        }
                        // TODO: Adjust SQLState or remove.
                        if (sqle.getSQLState().equals("XJ005")) {
                            // Attempt blocked, keep pounding on the database.
                            allowWait(false);
                            continue;
                        }
                        // The connection process failed unexpectedly.
                        throw sqle;
                    }
                    // The set of allowed exceptions here is different from
                    // the one during connection.
                    try {
                        Statement stmt = con.createStatement();
                        allowWait(true);
                        ResultSet rs = stmt.executeQuery(
                            "select * from sys.systables order by random()");
                        allowWait(true);
                        while (rs.next()) {
                            allowWait(true);
                            rs.getString(1);
                        }
                        rs.close();
                        stmt.close();
                        con.close();
                        allowWait(false);
                    } catch (SQLException sqle) {
                        // Accept no current connection here
                        if (sqle.getSQLState().equals("08003")) {
                            master.reportAccessCount(id, access);
                        } else if (sqle.getSQLState().equals("XJ001") &&
                                sqle.getMessage().indexOf("ShutdownException")
                                                                        != -1) {
                            master.reportAccessCount(id, access);
                        } else {
                            master.reportError(id, access, sqle);
                        }
                    }
                }
            } catch (Throwable t) {
                if (t instanceof org.apache.derby.shared.common.error.ShutdownException){
                    // Not sure if this is a good thing yet.
                    System.out.println(
                            "Got ShutdownException (extends RuntimeException)");
                    master.reportAccessCount(id, access);
                } else {
                    master.reportError(id, access, t);
                }
            }
        }

        /**
         * Method mostly doing nothing, but sometimes it decides to put the
         * thread to sleep for a little while.
         */
        private void allowWait(boolean onlyWaitOnce) {
            if (!noWait && ((!waited && onlyWaitOnce) || !onlyWaitOnce)) {
                int split = rnd.nextInt(100);
                if (split >= 97) {
                    // Potentially a long sleep
                    sleep(100 + (long)(rnd.nextDouble() * 1200));
                    waited = true;
                } else if (split > 80){
                    sleep((long)(rnd.nextDouble() * 100));
                    waited = true;
                }
            }
        }
    }
}