1 21 22 package org.apache.derbyTesting.functionTests.tests.store; 23 24 import java.sql.Connection ; 25 import java.sql.Statement ; 26 import java.sql.PreparedStatement ; 27 import java.sql.DriverManager ; 28 import java.sql.SQLException ; 29 import java.io.*; 30 import org.apache.derbyTesting.functionTests.util.TestUtil; 31 import java.util.Properties ; 32 33 68 public class TestDurabilityProperty { 69 public static void main(String [] args) { 70 try { 71 testNoSyncs(args); 74 75 String derbyHome = System.getProperty("derby.system.home"); 76 report("Is warning message about derby.system.durability=test present in derby.log =" 79 + isMessageInDerbyLog(derbyHome)); 80 markerInControlFile(derbyHome); 83 84 report( 90 "2. shutdown database and reboot database without " + 91 "derby.system.durability=test and test for marker in log.ctrl file"); 92 markerNotOverwritten(derbyHome); 93 94 } catch (Throwable e) { 95 report("FAIL -- unexpected exception: " + e); 96 e.printStackTrace(); 97 } 98 99 } 100 101 113 public static long timeTakenToInsert(String mode, boolean create, 114 boolean autoCommit) throws Exception { 115 System.setProperty("derby.system.durability", mode); 116 Connection conn = org.apache.derby.tools.ij.startJBMS(); 117 118 if (create) { 119 Statement s = conn.createStatement(); 120 s.execute("create table t1 (c1 int, c2 int)"); 121 s.close(); 122 } 123 124 long timeTaken = doInserts(conn, autoCommit); 125 126 try { 127 conn.close(); 128 TestUtil.getConnection("","shutdown=true"); 129 } catch (SQLException sqle) { 130 if ("XJ015".equals(sqle.getSQLState())) { 131 } else { 133 report(sqle.getSQLState()); 134 report("ERROR! during shutdown"); 135 sqle.printStackTrace(); 136 } 137 } 138 139 return timeTaken; 140 } 141 142 143 149 public static long doInserts(Connection conn,boolean autoCommit) throws Exception { 150 PreparedStatement ps = conn 151 .prepareStatement("insert into t1 values(?,?)"); 152 conn.setAutoCommit(autoCommit); 153 long count = 0; 154 155 long start = System.currentTimeMillis(); 156 157 for (int i = 0; i < 500; i++) { 158 ps.setInt(1, i); 159 ps.setInt(2, i); 160 count += ps.executeUpdate(); 161 } 162 163 if (!autoCommit) 164 conn.commit(); 165 long end = System.currentTimeMillis(); 166 if (count < 500) 167 report(" FAIL!! all rows didnt get inserted ?"); 168 169 return (end - start); 170 } 171 172 178 public static boolean isMessageInDerbyLog(String derbyHome) throws Exception { 179 BufferedReader reader = null; 180 File derbylog = null; 181 try { 182 derbylog = new File(derbyHome, "derby.log"); 183 reader = new BufferedReader(new FileReader(derbylog)); 184 185 String line = null; 186 while ((line = reader.readLine()) != null) { 187 if (line.indexOf("derby.system.durability=test") != -1) 188 return true; 189 190 } 191 return false; 192 } finally { 193 if (reader != null) { 194 reader.close(); 195 } 196 derbylog = null; 197 } 198 } 199 200 207 public static void markerInControlFile(String derbyHome) throws Exception { 208 RandomAccessFile controlFile = null; 209 try { 210 int testModeNoSyncMarkerPosition = 28; 211 byte testModeNoSyncMarker = 0x2; 212 controlFile = new RandomAccessFile(derbyHome 213 + "/wombat/log/log.ctrl", "r"); 214 controlFile.seek(testModeNoSyncMarkerPosition); 215 report("log.ctrl file has durability testMode no sync marker value = " 216 + ((controlFile.readByte() & testModeNoSyncMarker) != 0) ); 217 } finally { 218 if (controlFile != null) 219 controlFile.close(); 220 221 } 222 } 223 224 233 public static void markerNotOverwritten(String derbyHome) throws Exception 234 { 235 Connection conn = null; 237 System.setProperty("derby.system.durability",""); 239 conn = org.apache.derby.tools.ij.startJBMS(); 240 conn.close(); 241 markerInControlFile(derbyHome); 242 report("Is warning message about derby.system.durability=test present in derby.log =" 243 + isMessageInDerbyLog(derbyHome)); 244 } 245 246 250 public static void report(String msg) { 251 System.out.println(msg); 252 } 253 254 278 public static void testNoSyncs(String [] args) throws Exception { 279 boolean debug = false; 281 report("1. With derby.system.durability=test," 282 + "Test to see if syncs are not happening "); 283 org.apache.derby.tools.ij.getPropertyArg(args); 286 287 boolean create = true; 288 289 295 long timeCommitOn = timeTakenToInsert("", create, true); 298 String derbyHome = System.getProperty("derby.system.home"); 299 if (isMessageInDerbyLog(derbyHome)) 300 report("ERROR! System should not have been booted with" 301 + "derby.system.durability=test mode here"); 302 create = false; 303 long timeWithTestModeCommitOn = timeTakenToInsert("test", create, true); 305 long timeWithTestModeCommitOff = timeTakenToInsert("test", create, false); 307 308 if (debug) { 309 report("timeCommitOn = " + timeCommitOn); 310 report("timeWithTestModeCommitOn = " + timeWithTestModeCommitOn); 311 report("timeWithTestModeCommitOff = " + timeWithTestModeCommitOff); 312 } 313 314 long upperBound = 3000; 317 318 if (timeWithTestModeCommitOn > upperBound) { 323 324 long proximityRange = 1000; 325 326 if (Math.abs(timeWithTestModeCommitOn - timeWithTestModeCommitOff) > proximityRange) { 329 if (timeWithTestModeCommitOn > timeCommitOn 331 || (timeCommitOn < (1.5 * timeWithTestModeCommitOn))) { 332 report("FAIL -- derby.system.durability=test mode seems to be broken."); 333 report("-- In this mode one would expect that inserts with autocommit off and on " 334 + "would be in the same range as syncs are not happening but the difference " 335 + "here seems to be more than the approximate estimated range."); 336 report("-- Also comparing the time taken to do the inserts without this" + 337 " property set seems to be in the same" 338 + " range as with this property set."); 339 report("-- Please note this test times inserts and approximate estimates were " + 340 "considered to report this observation."); 341 } 342 } 343 } 344 345 } 346 } 347 | Popular Tags |