KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > cleaner > BackgroundIOTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: BackgroundIOTest.java,v 1.4 2006/10/30 21:14:41 bostic Exp $
7  */

8
9 package com.sleepycat.je.cleaner;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13
14 import junit.framework.TestCase;
15
16 import com.sleepycat.bind.tuple.TupleBase;
17 import com.sleepycat.bind.tuple.TupleOutput;
18 import com.sleepycat.je.CheckpointConfig;
19 import com.sleepycat.je.Database;
20 import com.sleepycat.je.DatabaseConfig;
21 import com.sleepycat.je.DatabaseEntry;
22 import com.sleepycat.je.DatabaseException;
23 import com.sleepycat.je.DbInternal;
24 import com.sleepycat.je.Environment;
25 import com.sleepycat.je.EnvironmentConfig;
26 import com.sleepycat.je.config.EnvironmentParams;
27 import com.sleepycat.je.dbi.EnvironmentImpl;
28 import com.sleepycat.je.latch.LatchSupport;
29 import com.sleepycat.je.log.FileManager;
30 import com.sleepycat.je.util.TestUtils;
31 import com.sleepycat.je.utilint.TestHook;
32
33 public class BackgroundIOTest extends TestCase {
34
35     private static CheckpointConfig forceConfig;
36     static {
37         forceConfig = new CheckpointConfig();
38         forceConfig.setForce(true);
39     }
40
41     private File JavaDoc envHome;
42     private Environment env;
43     private int nSleeps;
44
45     public BackgroundIOTest() {
46         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
47     }
48
49     public void setUp()
50         throws IOException JavaDoc, DatabaseException {
51
52         TestUtils.removeLogFiles("Setup", envHome, false);
53         TestUtils.removeFiles("Setup", envHome, FileManager.DEL_SUFFIX);
54     }
55
56     public void tearDown()
57         throws IOException JavaDoc, DatabaseException {
58
59         if (env != null) {
60             try {
61                 env.close();
62             } catch (Throwable JavaDoc e) {
63                 System.out.println("tearDown: " + e);
64             }
65             env = null;
66         }
67                 
68         //*
69
TestUtils.removeLogFiles("TearDown", envHome, true);
70         TestUtils.removeFiles("TearDown", envHome, FileManager.DEL_SUFFIX);
71         //*/
72
}
73
74     public void testBackgroundIO1()
75     throws DatabaseException, InterruptedException JavaDoc {
76
77         doTest(10, 10, 226, 246);
78     }
79
80     public void testBackgroundIO2()
81     throws DatabaseException, InterruptedException JavaDoc {
82
83         doTest(10, 5, 365, 385);
84     }
85
86     public void testBackgroundIO3()
87     throws DatabaseException, InterruptedException JavaDoc {
88
89         doTest(5, 10, 324, 344);
90     }
91
92     public void testBackgroundIO4()
93     throws DatabaseException, InterruptedException JavaDoc {
94
95         doTest(5, 5, 463, 483);
96     }
97
98     private void doTest(int readLimit,
99                         int writeLimit,
100                         int minSleeps,
101                         int maxSleeps)
102     throws DatabaseException, InterruptedException JavaDoc {
103
104         final int fileSize = 1000000;
105         EnvironmentConfig envConfig = TestUtils.initEnvConfig();
106         envConfig.setAllowCreate(true);
107         envConfig.setConfigParam
108         (EnvironmentParams.ENV_RUN_CLEANER.getName(), "false");
109         envConfig.setConfigParam
110         (EnvironmentParams.ENV_RUN_CHECKPOINTER.getName(), "false");
111         envConfig.setConfigParam
112         (EnvironmentParams.ENV_RUN_INCOMPRESSOR.getName(), "false");
113         envConfig.setConfigParam
114             (EnvironmentParams.LOG_BUFFER_MAX_SIZE.getName(),
115              Integer.toString(1024));
116         envConfig.setConfigParam
117             (EnvironmentParams.LOG_FILE_MAX.getName(),
118              Integer.toString(fileSize));
119         envConfig.setConfigParam
120         (EnvironmentParams.CLEANER_MIN_UTILIZATION.getName(), "60");
121         //*
122
envConfig.setConfigParam
123         (EnvironmentParams.ENV_BACKGROUND_READ_LIMIT.getName(),
124              String.valueOf(readLimit));
125         envConfig.setConfigParam
126         (EnvironmentParams.ENV_BACKGROUND_WRITE_LIMIT.getName(),
127              String.valueOf(writeLimit));
128         //*/
129
env = new Environment(envHome, envConfig);
130
131         EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env);
132         envImpl.setBackgroundSleepHook(new TestHook() {
133             public void doHook() {
134                 nSleeps += 1;
135                 assertEquals(0, LatchSupport.countLatchesHeld());
136             }
137             public Object JavaDoc getHookValue() {
138             throw new UnsupportedOperationException JavaDoc();
139             }
140             public void doIOHook() throws IOException JavaDoc {
141                 throw new UnsupportedOperationException JavaDoc();
142             }
143         });
144
145         DatabaseConfig dbConfig = new DatabaseConfig();
146         dbConfig.setAllowCreate(true);
147         dbConfig.setExclusiveCreate(true);
148         Database db = env.openDatabase(null, "BackgroundIO", dbConfig);
149
150         final int nFiles = 3;
151         final int keySize = 20;
152         final int dataSize = 10;
153         final int recSize = keySize + dataSize + 35 /* LN overhead */;
154         final int nRecords = nFiles * (fileSize / recSize);
155
156         /*
157          * Insert records first so we will have a sizeable checkpoint. Insert
158          * interleaved because sequential inserts flush the BINs, and we want
159          * to defer BIN flushing until the checkpoint.
160          */

161         DatabaseEntry key = new DatabaseEntry();
162         DatabaseEntry data = new DatabaseEntry(new byte[dataSize]);
163         for (int i = 0; i <= nRecords; i += 2) {
164             setKey(key, i, keySize);
165             db.put(null, key, data);
166         }
167         for (int i = 1; i <= nRecords; i += 2) {
168             setKey(key, i, keySize);
169             db.put(null, key, data);
170         }
171
172         /* Perform a checkpoint to perform background writes. */
173         env.checkpoint(forceConfig);
174
175         /* Delete records so we will have a sizable cleaning. */
176         for (int i = 0; i <= nRecords; i += 1) {
177             setKey(key, i, keySize);
178             db.delete(null, key);
179         }
180
181         /* Perform cleaning to perform background reading. */
182         env.checkpoint(forceConfig);
183         env.cleanLog();
184         env.checkpoint(forceConfig);
185
186     db.close();
187     env.close();
188         env = null;
189
190         String JavaDoc msg;
191         msg = "readLimit=" + readLimit +
192               " writeLimit=" + writeLimit +
193               " minSleeps=" + minSleeps +
194               " maxSleeps=" + maxSleeps +
195               " actualSleeps=" + nSleeps;
196         //System.out.println(msg);
197

198         //*
199
assertTrue(msg, nSleeps >= minSleeps && nSleeps <= maxSleeps);
200         //*/
201
}
202
203     /**
204      * Outputs an integer followed by pad bytes.
205      */

206     private void setKey(DatabaseEntry entry, int val, int len) {
207         TupleOutput out = new TupleOutput();
208         out.writeInt(val);
209         for (int i = 0; i < len - 4; i += 1) {
210             out.writeByte(0);
211         }
212         TupleBase.outputToEntry(out, entry);
213     }
214 }
215
Popular Tags