Repository navigation
Expand file tree
/
Copy pathCrashTest.java
More file actions
349 lines (284 loc) · 11.4 KB
/
Copy pathCrashTest.java
File metadata and controls
349 lines (284 loc) · 11.4 KB
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
package org.mapdb;
import junit.framework.AssertionFailedError;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.io.*;
import java.util.*;
import static org.junit.Assert.*;
/*
* Runs WAL and crashes JVM to test it
*
* This test start new JVM and kills it (kill PID -9) after random interval (up to 1 minute).
* Than it checks content of the file, starts new JVM and repeats.
*
* Forked JVM inserts random value based on random seed. Seed file is created before and after each commit,
* so we know what seed value was.
*
*/
@RunWith(Parameterized.class)
public class CrashTest {
static final int MIN_RUNTIME = 1000*1;
static final int MAX_RUNTIME = 1000*6;
public static File DIR;
public static final class Params implements Serializable{
final int index;
final File dir;
final DBMaker.Maker dbMaker;
final boolean clearMap;
final boolean hashMap;
final boolean largeVals;
final int mapSize;
public Params(int index, File dir, DBMaker.Maker dbMaker, boolean clearMap, boolean hashMap, boolean largeVals, int mapSize) throws IOException {
this.index = index;
this.dir = dir;
this.dbMaker = dbMaker;
this.clearMap = clearMap;
this.hashMap = hashMap;
this.largeVals = largeVals;
this.mapSize = mapSize;
}
}
final Params p;
public CrashTest(Params p) {
this.p = p;
}
@Parameterized.Parameters
public static List<Object[]> params() throws IOException {
List ret = new ArrayList();
if(TT.shortTest())
return ret;
int index=0;
for( boolean notAppend:TT.BOOLS)
for( boolean mmap:TT.BOOLS)
for( boolean cache : TT.BOOLS)
for( boolean largeVals : TT.BOOLS)
for( boolean clearMap : TT.BOOLS)
for( boolean hashMap : TT.BOOLS)
for( int mapSize :new int[]{10,0,1000})
{
File f = DIR !=null? DIR :
new File(System.getProperty("java.io.tmpdir")
+"/mapdbTest"+System.currentTimeMillis()+Math.random());
DBMaker.Maker maker = notAppend ?
DBMaker.fileDB(new File(f, "store")) :
DBMaker.appendFileDB(new File(f,"store"));
maker.fileLockDisable();
if (mmap)
maker.fileMmapEnableIfSupported().fileMmapCleanerHackEnable();
if (cache)
maker.cacheHashTableEnable();
ret.add(new Object[]{
new Params(index++, f, maker, clearMap,
hashMap, largeVals, mapSize)});
}
return ret;
}
@Test
public void test() throws IOException, InterruptedException {
if(TT.scale()==0)
return;
//create folders
p.dir.mkdirs();
long end = TT.nowPlusMinutes(10);
if(p.dir.getFreeSpace()<10e9)
fail("not enough free disk space, at least 10GB needed: "+p.dir.getFreeSpace());
assertTrue(p.dir.exists() && p.dir.isDirectory() && p.dir.canWrite());
long oldSeed=0;
long crashCount = 0;
while(end>System.currentTimeMillis()) {
//fork JVM, pass current dir and config index as param
{
ProcessBuilder b = new ProcessBuilder(
jvmExecutable(),
"-classpath",
System.getProperty("java.class.path"),
"-Dmdbtest=" + TT.scale(),
this.getClass().getName(),
p.dir.getAbsolutePath(),
"" + this.p.index);
Process pr = b.start();
pr.waitFor(); //it should kill itself after some time
Thread.sleep(100);// just in case
//handle output streams
String out = outStreamToString(pr.getInputStream());
System.err.print(outStreamToString(pr.getErrorStream()));
assertTrue(out, out.startsWith("started_"));
assertTrue(out, out.endsWith("_killed"));
assertEquals(137, pr.exitValue());
}
//now reopen file and check its content
DB db = p.dbMaker.make();
Atomic.Long dbSeed = db.atomicLong("seed");
assertTrue(dbSeed.get()>=oldSeed);
File seedStartDir = new File(p.dir,"seedStart");
File seedEndDir = new File(p.dir,"seedEnd");
File[] seedStartFiles = seedStartDir.listFiles();
File[] seedEndFiles = seedEndDir.listFiles();
if(seedStartFiles.length==0) {
// JVM interrupted before creating any seed files
// in that case seed should not change
if(oldSeed!=0)
assertEquals(oldSeed, dbSeed.get());
}else if(seedEndFiles.length== seedStartFiles.length ){
//commit finished fine,
assertEquals(getSeed(seedStartDir,0), getSeed(seedEndDir,0));
//content of database should be applied
assertEquals(dbSeed.get(),getSeed(seedStartDir,0));
}else if(seedStartFiles.length==1){
//only single commit started, in that case it did not succeeded, or it did succeeded
assertTrue(dbSeed.get()==oldSeed || dbSeed.get()==getSeed(seedStartDir, 0));
}else{
long minimalSeed =
seedEndFiles.length>0?
getSeed(seedEndDir,0):
oldSeed;
assertTrue(minimalSeed<=dbSeed.get());
//either last started commit succeeded or commit before that succeeded
assertTrue(dbSeed.get()==getSeed(seedStartDir, 0) || dbSeed.get()==getSeed(seedStartDir, 1));
}
if(dbSeed.get()!=oldSeed)
crashCount++;
Map<Long,byte[]> m = map(p,db);
//check content of map
Random r = new Random(dbSeed.get());
for (long i = 0; i < p.mapSize; i++) {
byte[] b = getBytes(p, r);
if (!Arrays.equals(b, m.get(i))) {
throw new AssertionFailedError("Wrong arrays");
}
}
oldSeed = dbSeed.get();
db.close();
//cleanup seeds
TT.dirDelete(seedEndDir);
TT.dirDelete(seedStartDir);
if(p.dir.getFreeSpace()<1e9){
System.out.println("Not enough free space, delete store and start over");
TT.dirDelete(p.dir);
p.dir.mkdirs();
assertTrue(p.dir.exists() && p.dir.isDirectory() && p.dir.canWrite());
}
}
assertTrue("no commits were made",crashCount>0);
System.out.println("Finished after " + crashCount + " crashes");
}
@After
public void clean(){
TT.dirDelete(p.dir);
}
public static void main(String[] args) throws IOException {
try {
//start kill timer
killThisJVM(MIN_RUNTIME + new Random().nextInt(MAX_RUNTIME - MIN_RUNTIME));
System.out.print("started_");
//collect all parameters
DIR = new File(args[0]);
int index = Integer.valueOf(args[1]);
Params p = (Params) params().get(index)[0];
File seedStartDir = new File(p.dir,"seedStart");
File seedEndDir = new File(p.dir,"seedEnd");
seedStartDir.mkdirs();
seedEndDir.mkdirs();
DB db = p.dbMaker.make();
Atomic.Long dbSeed = db.atomicLong("seed");
Map<Long, byte[]> m = map(p, db);
long seed;
while (true) {
seed = System.currentTimeMillis();
dbSeed.set(seed);
Random r = new Random(seed);
for (long i = 0; i < p.mapSize; i++) {
byte[] b = getBytes(p, r);
m.put(i, b);
}
//create seed file before commit
assertTrue(new File(seedStartDir, "" + seed).createNewFile());
db.commit();
//create seed file after commit
assertTrue(new File(seedEndDir, "" + seed).createNewFile());
//wait until clock increases
while(seed==System.currentTimeMillis()) {
Thread.sleep(1);
}
//randomly delete content of map
if (p.clearMap && r.nextInt(10) <= 1)
m.clear();
}
}catch(Throwable e){
if(DIR !=null)
System.err.println("Free space: "+ DIR.getFreeSpace());
e.printStackTrace();
System.exit(-1111);
}
}
private static byte[] getBytes(Params p, Random r) {
int size = r.nextInt(p.largeVals ? 10000 : 10);
return TT.randomByteArray(size, r.nextInt());
}
private static Map map(Params p, DB db) {
return (Map) (
p.hashMap ?
db.hashMapCreate("hash")
.keySerializer(Serializer.LONG)
.valueSerializer(Serializer.BYTE_ARRAY)
.makeOrGet() :
db.treeMapCreate("hash")
.keySerializer(Serializer.LONG)
.valueSerializer(Serializer.BYTE_ARRAY)
.valuesOutsideNodesEnable()
.makeOrGet());
}
static void killThisJVM(final long delay){
Thread t = new Thread(){
@Override
public void run() {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
killThisJVM();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t.setDaemon(true);
t.start();
}
static void killThisJVM() throws IOException, InterruptedException {
String pid = new File("/proc/self").getCanonicalFile().getName();
Long.valueOf(pid);
System.out.print("killed");
ProcessBuilder b = new ProcessBuilder("kill", "-9", pid);
b.start();
while(true){
Thread.sleep(10000);
System.out.println("KILL - Still alive");
}
}
static String outStreamToString(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
for(int b=in.read();b!=-1;b=in.read()){
out.write(b);
}
return new String(out.toByteArray());
}
static long getSeed(File seedDir, int indexFromEnd){
File[] f = seedDir.listFiles();
Arrays.sort(f);
return Long.valueOf(f[f.length-1-indexFromEnd].getName());
}
static String jvmExecutable(){
String exec = System.getProperty("os.name").startsWith("Win") ? "java.exe":"java";
String javaHome = System.getProperty("java.home");
if(javaHome==null ||"".equals(javaHome))
return exec;
return javaHome+ File.separator + "bin" + File.separator + exec;
}
}