1 21 22 package org.apache.derbyTesting.functionTests.tests.store; 23 import java.sql.*; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.DataOutputStream ; 26 import java.io.IOException ; 27 import java.util.zip.CRC32 ; 28 import org.apache.derbyTesting.functionTests.util.corruptio.CorruptibleIo; 29 import org.apache.derby.tools.ij; 30 31 48 49 public class LogChecksumSetup{ 50 51 private CorruptibleIo cbio; 52 53 LogChecksumSetup() 54 { 55 cbio = CorruptibleIo.getInstance(); 56 } 57 58 62 void insertAndCorrupt(Connection conn, int rowCount) throws SQLException { 63 64 PreparedStatement ps = conn.prepareStatement("INSERT INTO " + 65 "T1" + 66 " VALUES(?,?,?)"); 67 68 java.util.Random r = new java.util.Random (); 69 CRC32 checksum = new CRC32 (); boolean corrupt = false; 71 for (int i = 0; i < rowCount; i++) { 72 73 if (i == (rowCount -1 )) 75 { 76 setupLogCorruption(50, 10); 80 corrupt = true; 81 } 82 ps.setInt(1, i); byte[] dataBytes = generateBinaryData(r, 90000 , 1000 * i); 84 ps.setBytes(2, dataBytes); 85 checksum.update(dataBytes, 0, dataBytes.length); 87 checksum.reset(); 88 checksum.update(dataBytes, 0, dataBytes.length); 89 ps.setLong(3, checksum.getValue()); 90 ps.executeUpdate(); 91 conn.commit(); 92 } 93 } 94 95 96 101 102 void updateAndCorrupt(Connection conn, int rowCount) throws SQLException{ 103 104 PreparedStatement ps = conn.prepareStatement("update " + "T1" + 105 " SET " + 106 "DATA=?, DATACHECKSUM=? where ID=?"); 107 108 java.util.Random r = new java.util.Random (); 109 CRC32 checksum = new CRC32 (); int updateCount = 0; 111 boolean corrupt = false; 112 for (int i = 0; i < rowCount; i++) { 113 114 if (i == (rowCount -1 )) 116 { 117 setupLogCorruption(50, 10); 121 corrupt = true; 122 } 123 byte[] dataBytes = generateBinaryData(r, 1234 , 5000 * i); 124 ps.setBytes(1, dataBytes); 125 126 checksum.update(dataBytes, 0, dataBytes.length); 128 checksum.reset(); 129 checksum.update(dataBytes, 0, dataBytes.length); 130 131 ps.setLong(2, checksum.getValue()); 132 ps.setInt(3, i); updateCount += ps.executeUpdate(); 134 conn.commit(); 135 } 136 } 137 138 139 144 void verifyData(Connection conn, int expectedRowCount) throws SQLException { 145 146 Statement s = conn.createStatement(); 147 CRC32 checksum = new CRC32 (); 149 ResultSet rs = s.executeQuery("SELECT DATA , DATACHECKSUM, ID FROM " 150 + "T1" ); 151 int count = 0; 152 while(rs.next()) 153 { 154 byte[] dataBytes = rs.getBytes(1); 155 long ckmRead = rs.getLong(2); 156 int id = rs.getInt(3); 157 158 checksum.reset(); 159 checksum.update(dataBytes, 0, dataBytes.length); 160 161 if(checksum.getValue() != ckmRead ) 162 { 163 logMessage("CHECKSUMs ARE NOT MATCHING"); 164 logMessage("ID=" + id + " Checksum From DB:" + ckmRead); 165 logMessage("Recalcaulted sum :" + checksum.getValue()); 166 logMessage("Length of Data:" + dataBytes.length); 167 } 168 169 count++; 170 } 171 conn.commit(); 172 173 if(count != expectedRowCount) 174 { 175 logMessage("Expected Number Of Rows (" + 176 expectedRowCount + ")" + "!=" + 177 "No Of rows in the Table(" + 178 count + ")"); 179 } 180 } 181 182 185 private void createTable(Connection conn) throws SQLException { 186 187 Statement s = conn.createStatement(); 188 s.executeUpdate("CREATE TABLE " + "T1" + "(ID INT," + 189 "DATA BLOB(300000),"+ 190 "DATACHECKSUM BIGINT)"); 191 conn.commit(); 192 s.close(); 193 } 194 195 202 private void setupLogCorruption(int off , int len) 203 { 204 cbio.setLogCorruption(true); 205 cbio.setOffset(off); 206 cbio.setLength(len); 207 } 208 209 210 213 private byte[] generateBinaryData(java.util.Random r, 214 int factor, 215 int size) { 216 217 ByteArrayOutputStream baos = new ByteArrayOutputStream (64); 218 try{ 219 DataOutputStream daos = new DataOutputStream (baos); 220 for(int i = 0 ; i < size ; i++) 221 { 222 int p = r.nextInt() % factor; 223 if (p < 0) 224 p = p * -1; 225 daos.writeInt(p); 226 } 227 228 }catch(IOException ie) 229 { 230 logMessage(ie.getMessage()) ; 231 } 232 return baos.toByteArray(); 233 } 234 235 236 private void runTest(Connection conn) throws SQLException 237 { 238 logMessage("Begin LogCheckum Setup Test"); 239 createTable(conn); 240 insertAndCorrupt(conn, 11); 241 logMessage("End LogChecksum Setup Test"); 242 } 243 244 void logMessage(String str) 245 { 246 System.out.println(str); 247 } 248 249 public static void main(String [] argv) throws Throwable { 250 LogChecksumSetup lctest = new LogChecksumSetup(); 251 ij.getPropertyArg(argv); 252 Connection conn = ij.startJBMS(); 253 conn.setAutoCommit(false); 254 255 try { 256 lctest.runTest(conn); 257 } 258 catch (SQLException sqle) { 259 org.apache.derby.tools.JDBCDisplayUtil.ShowSQLException( 260 System.out, sqle); 261 sqle.printStackTrace(System.out); 262 } 263 } 264 } 265 | Popular Tags |