| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.store; |
| 7 |
|
| 8 |
import java.io.IOException; |
| 9 |
import java.io.OutputStreamWriter; |
| 10 |
import java.io.PrintWriter; |
| 11 |
import java.sql.SQLException; |
| 12 |
import java.util.HashSet; |
| 13 |
|
| 14 |
import org.h2.api.ErrorCode; |
| 15 |
import org.h2.engine.ConnectionInfo; |
| 16 |
import org.h2.engine.Constants; |
| 17 |
import org.h2.engine.Database; |
| 18 |
import org.h2.engine.SessionLocal; |
| 19 |
import org.h2.message.DbException; |
| 20 |
import org.h2.store.fs.FileUtils; |
| 21 |
import org.h2.store.fs.Recorder; |
| 22 |
import org.h2.store.fs.rec.FilePathRec; |
| 23 |
import org.h2.tools.Recover; |
| 24 |
import org.h2.util.IOUtils; |
| 25 |
import org.h2.util.StringUtils; |
| 26 |
import org.h2.util.Utils; |
| 27 |
|
| 28 |
/** |
| 29 |
* A tool that simulates a crash while writing to the database, and then |
| 30 |
* verifies the database doesn't get corrupt. |
| 31 |
*/ |
| 32 |
public class RecoverTester implements Recorder { |
| 33 |
|
| 34 |
private static final RecoverTester instance = new RecoverTester(); |
| 35 |
|
| 36 |
private String testDatabase = "memFS:reopen"; |
| 37 |
private int writeCount = Utils.getProperty("h2.recoverTestOffset", 0); |
| 38 |
private int testEvery = Utils.getProperty("h2.recoverTest", 64); |
| 39 |
private final long maxFileSize = Utils.getProperty( |
| 40 |
"h2.recoverTestMaxFileSize", Integer.MAX_VALUE) * 1024L * 1024; |
| 41 |
private int verifyCount; |
| 42 |
private final HashSet<String> knownErrors = new HashSet<>(); |
| 43 |
private volatile boolean testing; |
| 44 |
|
| 45 |
/** |
| 46 |
* Initialize the recover test. |
| 47 |
* |
| 48 |
* @param recoverTest the value of the recover test parameter |
| 49 |
*/ |
| 50 |
public static synchronized void init(String recoverTest) { |
| 51 |
if (StringUtils.isNumber(recoverTest)) { |
| 52 |
instance.setTestEvery(Integer.parseInt(recoverTest)); |
| 53 |
} |
| 54 |
FilePathRec.setRecorder(instance); |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public void log(int op, String fileName, byte[] data, long x) { |
| 59 |
if (op != Recorder.WRITE && op != Recorder.TRUNCATE) { |
| 60 |
return; |
| 61 |
} |
| 62 |
if (!fileName.endsWith(Constants.SUFFIX_MV_FILE)) { |
| 63 |
return; |
| 64 |
} |
| 65 |
writeCount++; |
| 66 |
if ((writeCount % testEvery) != 0) { |
| 67 |
return; |
| 68 |
} |
| 69 |
if (FileUtils.size(fileName) > maxFileSize) { |
| 70 |
// System.out.println(fileName + " " + IOUtils.length(fileName)); |
| 71 |
return; |
| 72 |
} |
| 73 |
if (testing) { |
| 74 |
// avoid deadlocks |
| 75 |
return; |
| 76 |
} |
| 77 |
testing = true; |
| 78 |
PrintWriter out = null; |
| 79 |
try { |
| 80 |
out = new PrintWriter( |
| 81 |
new OutputStreamWriter( |
| 82 |
FileUtils.newOutputStream(fileName + ".log", true))); |
| 83 |
testDatabase(fileName, out); |
| 84 |
} catch (IOException e) { |
| 85 |
throw DbException.convertIOException(e, null); |
| 86 |
} finally { |
| 87 |
IOUtils.closeSilently(out); |
| 88 |
testing = false; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
private synchronized void testDatabase(String fileName, PrintWriter out) { |
| 93 |
out.println("+ write #" + writeCount + " verify #" + verifyCount); |
| 94 |
try { |
| 95 |
IOUtils.copyFiles(fileName, testDatabase + Constants.SUFFIX_MV_FILE); |
| 96 |
verifyCount++; |
| 97 |
// avoid using the Engine class to avoid deadlocks |
| 98 |
ConnectionInfo ci = new ConnectionInfo("jdbc:h2:" + testDatabase + |
| 99 |
";FILE_LOCK=NO;TRACE_LEVEL_FILE=0", null, "", ""); |
| 100 |
Database database = new Database(ci, null); |
| 101 |
// close the database |
| 102 |
SessionLocal sysSession = database.getSystemSession(); |
| 103 |
sysSession.prepare("script to '" + testDatabase + ".sql'").query(0); |
| 104 |
sysSession.prepare("shutdown immediately").update(); |
| 105 |
database.removeSession(null); |
| 106 |
// everything OK - return |
| 107 |
return; |
| 108 |
} catch (DbException e) { |
| 109 |
SQLException e2 = DbException.toSQLException(e); |
| 110 |
int errorCode = e2.getErrorCode(); |
| 111 |
if (errorCode == ErrorCode.WRONG_USER_OR_PASSWORD) { |
| 112 |
return; |
| 113 |
} else if (errorCode == ErrorCode.FILE_ENCRYPTION_ERROR_1) { |
| 114 |
return; |
| 115 |
} |
| 116 |
e.printStackTrace(System.out); |
| 117 |
} catch (Exception e) { |
| 118 |
// failed |
| 119 |
int errorCode = 0; |
| 120 |
if (e instanceof SQLException) { |
| 121 |
errorCode = ((SQLException) e).getErrorCode(); |
| 122 |
} |
| 123 |
if (errorCode == ErrorCode.WRONG_USER_OR_PASSWORD) { |
| 124 |
return; |
| 125 |
} else if (errorCode == ErrorCode.FILE_ENCRYPTION_ERROR_1) { |
| 126 |
return; |
| 127 |
} |
| 128 |
e.printStackTrace(System.out); |
| 129 |
} |
| 130 |
out.println("begin ------------------------------ " + writeCount); |
| 131 |
try { |
| 132 |
Recover.execute(fileName.substring(0, fileName.lastIndexOf('/')), null); |
| 133 |
} catch (SQLException e) { |
| 134 |
// ignore |
| 135 |
} |
| 136 |
testDatabase += "X"; |
| 137 |
try { |
| 138 |
IOUtils.copyFiles(fileName, testDatabase + Constants.SUFFIX_MV_FILE); |
| 139 |
// avoid using the Engine class to avoid deadlocks |
| 140 |
ConnectionInfo ci = new ConnectionInfo("jdbc:h2:" + |
| 141 |
testDatabase + ";FILE_LOCK=NO", null, null, null); |
| 142 |
Database database = new Database(ci, null); |
| 143 |
// close the database |
| 144 |
database.removeSession(null); |
| 145 |
} catch (Exception e) { |
| 146 |
int errorCode = 0; |
| 147 |
if (e instanceof DbException) { |
| 148 |
e = ((DbException) e).getSQLException(); |
| 149 |
errorCode = ((SQLException) e).getErrorCode(); |
| 150 |
} |
| 151 |
if (errorCode == ErrorCode.WRONG_USER_OR_PASSWORD) { |
| 152 |
return; |
| 153 |
} else if (errorCode == ErrorCode.FILE_ENCRYPTION_ERROR_1) { |
| 154 |
return; |
| 155 |
} |
| 156 |
StringBuilder buff = new StringBuilder(); |
| 157 |
StackTraceElement[] list = e.getStackTrace(); |
| 158 |
for (int i = 0; i < 10 && i < list.length; i++) { |
| 159 |
buff.append(list[i].toString()).append('\n'); |
| 160 |
} |
| 161 |
String s = buff.toString(); |
| 162 |
if (!knownErrors.contains(s)) { |
| 163 |
out.println(writeCount + " code: " + errorCode + " " + e.toString()); |
| 164 |
e.printStackTrace(System.out); |
| 165 |
knownErrors.add(s); |
| 166 |
} else { |
| 167 |
out.println(writeCount + " code: " + errorCode); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
public void setTestEvery(int testEvery) { |
| 173 |
this.testEvery = testEvery; |
| 174 |
} |
| 175 |
|
| 176 |
} |