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
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.store.AutomaticIndexStatisticsMultiTest
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.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.IndexStatsUtil;
import org.apache.derbyTesting.junit.IndexStatsUtil.IdxStats;
/**
* Tests that the update triggering code can handle a high amount of requests,
* both when the data dictionary is operating in cached mode and in DDL mode.
*/
public class AutomaticIndexStatisticsMultiTest
extends BaseJDBCTestCase {
private static final String TAB = "MTSEL";
private static final int _100K = 100000;
public AutomaticIndexStatisticsMultiTest(String name) {
super(name);
}
public void testMTSelect()
throws Exception {
prepareTable(true);
// Create threads compiling a select query for the table
int threadCount = 5;
long runTime = 10000;
MTCompileThread[] compileThreads = new MTCompileThread[threadCount];
for (int i=0; i < threadCount; i++) {
compileThreads[i] =
new MTCompileThread(openDefaultConnection(), runTime);
}
// Start the threads and let them run for a while.
Thread[] threads = new Thread[threadCount];
for (int i=0; i < threadCount; i++) {
threads[i] = new Thread(compileThreads[i]);
threads[i].start();
}
// Wait for the threads to finish.
for (int i=0; i < threadCount; i++) {
threads[i].join(600*1000); // Time out after 600 seconds.
}
int total = 0;
int totalError = 0;
List<SQLException> errors = new ArrayList<SQLException>();
for (int i=0; i < threadCount; i++) {
MTCompileThread ct = compileThreads[i];
int count = ct.getCount();
int errorCount = ct.getErrorCount();
total += count;
totalError += errorCount;
errors.addAll(ct.getErrors());
}
println("TOTAL = " + total + " (of which " + totalError + " errors)");
if (totalError > 0) {
// Build an informative failure string.
StringWriter msg = new StringWriter();
PrintWriter out = new PrintWriter(msg);
out.println(totalError + " select/compile errors reported:");
for (SQLException sqle : errors) {
out.println("------");
sqle.printStackTrace(out);
}
out.close();
fail(msg.toString());
}
verifyStatistics();
// Shutdown database to log daemon stats (if logging is enabled).
getTestConfiguration().shutdownDatabase();
}
public void testMTSelectWithDDL()
throws Exception {
prepareTable(true);
// Create threads compiling a select query for the table
int threadCount = 5;
long runTime = 10000;
MTCompileThread[] compileThreads = new MTCompileThread[threadCount];
for (int i=0; i < threadCount; i++) {
compileThreads[i] =
new MTCompileThread(openDefaultConnection(), runTime);
}
// Start the drop/create thread.
MTCreateDropThread createThread = new MTCreateDropThread(
openDefaultConnection(), runTime);
Thread[] threads = new Thread[threadCount +1];
threads[0] = new Thread(createThread);
threads[0].start();
// Start the threads and let them run for a while.
for (int i=0; i < threadCount; i++) {
threads[i+1] = new Thread(compileThreads[i]);
threads[i+1].start();
}
// Wait for the threads to finish.
for (int i=0; i < threadCount; i++) {
threads[i].join(600*1000); // Time out after 600 seconds.
}
int total = 0;
int totalError = 0;
List<SQLException> errors = new ArrayList<SQLException>();
for (int i=0; i < threadCount; i++) {
MTCompileThread ct = compileThreads[i];
int count = ct.getCount();
int errorCount = ct.getErrorCount();
total += count;
totalError += errorCount;
errors.addAll(ct.getErrors());
}
println("TOTAL = " + total + " (of which " + totalError +
" errors) CREATES = " + createThread.getCreates());
if (totalError > 0 || createThread.failed()) {
// Build an informative failure string.
StringWriter msg = new StringWriter();
PrintWriter out = new PrintWriter(msg);
out.println("create/drop thread " +
(createThread.failed() ? "died" : "survived") + ", " +
totalError + " select/compile errors reported:");
if (createThread.failed()) {
out.println("create/drop thread error: ");
createThread.getError().printStackTrace(out);
}
out.println("select/compile errors:");
for (SQLException sqle : errors) {
out.println("------");
sqle.printStackTrace(out);
}
out.close();
fail(msg.toString());
}
verifyStatistics();
// Shutdown database to log daemon stats (if logging is enabled).
getTestConfiguration().shutdownDatabase();
}
private void verifyStatistics()
throws SQLException {
// DERBY-5097: On machines with a single core/CPU the load generated
// by the test threads may cause the index statistics daemon worker
// thread to be "starved". Add a timeout to give it a chance to do
// what it has been told to do.
IndexStatsUtil stats = new IndexStatsUtil(getConnection(), 5000);
IdxStats[] myStats = stats.getStatsTable(TAB, 2);
for (int i=0; i < myStats.length; i++) {
IdxStats s = myStats[i];
assertEquals(_100K, s.rows);
switch (s.lcols) {
case 1:
assertEquals(10, s.card);
break;
case 2:
assertEquals(_100K, s.card);
break;
default:
fail("unexpected number of leading columns: " + s.lcols);
}
}
}
private void prepareTable(boolean dropIfExists)
throws SQLException {
Statement stmt = createStatement();
ResultSet rs = getConnection().getMetaData().getTables(
null, null, TAB, null);
if (rs.next()) {
assertFalse(rs.next());
rs.close();
if (dropIfExists) {
println("table " + TAB + " already exists, dropping");
stmt.executeUpdate("drop table " + TAB);
} else {
println("table " + TAB + " already exists, reusing");
return;
}
} else {
rs.close();
}
stmt.executeUpdate("create table " + TAB + " (val1 int, val2 int)");
stmt.executeUpdate("create index " +
"mtsel_idx on " + TAB + " (val1, val2)");
setAutoCommit(false);
PreparedStatement ps = prepareStatement(
"insert into " + TAB + " values (?,?)");
// Insert blocks of 10000 rows.
int blockCount = 10;
int blockSize = 10000;
for (int i=0; i < blockCount; i++) {
ps.setInt(1, i);
for (int j=0; j < blockSize; j++) {
ps.setInt(2, j);
ps.addBatch();
}
ps.executeBatch();
commit();
println("inserted block " + (i+1) + "/" + blockCount);
}
setAutoCommit(true);
}
public static Test suite() {
return new BaseTestSuite(AutomaticIndexStatisticsMultiTest.class);
}
private static class MTCompileThread
implements Runnable {
private final Random rand = new Random();
private final Connection con;
private final long runTime;
private final ArrayList<SQLException> errors =
new ArrayList<SQLException>();
private volatile int count;
public MTCompileThread(Connection con, long runTime)
throws SQLException {
this.con = con;
this.runTime = runTime;
}
public void run() {
final long started = System.currentTimeMillis();
int counter = 0;
while (System.currentTimeMillis() - started < runTime) {
try {
con.prepareStatement("select * from mtsel where " +
(++counter) + " = " + counter + " AND val2 = " +
(1 + rand.nextInt(10)));
} catch (SQLException sqle) {
synchronized (this) {
errors.add(sqle);
}
}
count++;
}
}
public int getCount() {
return count;
}
public synchronized int getErrorCount() {
return errors.size();
}
public synchronized List<SQLException> getErrors() {
return new ArrayList<SQLException>(errors);
}
}
private static class MTCreateDropThread
implements Runnable {
private final Connection con;
private final long runTime;
private volatile long creates;
private volatile SQLException error;
public MTCreateDropThread(Connection con, long runTime)
throws SQLException {
this.con = con;
this.runTime = runTime;
}
public void run() {
final long started = System.currentTimeMillis();
try {
ResultSet rs = con.getMetaData().getTables(
null, null, "TMPTABLE", null);
boolean lastWasCreate = rs.next();
rs.close();
Statement stmt = con.createStatement();
while (System.currentTimeMillis() - started < runTime) {
if (lastWasCreate) {
// Drop the table
stmt.executeUpdate("drop table TMPTABLE");
} else {
// Create the table
stmt.executeUpdate("create table TMPTABLE("
+ "i int primary key, v varchar(30), b blob)");
creates++;
}
lastWasCreate = !lastWasCreate;
}
} catch (SQLException sqle) {
error = sqle;
println("create/drop thread failed: " + sqle.getMessage());
}
}
public long getCreates() {
return creates;
}
public boolean failed() {
return error != null;
}
public SQLException getError() {
return error;
}
}
}
|