KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > recovery > CheckpointActivationTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: CheckpointActivationTest.java,v 1.18 2006/11/27 23:15:06 mark Exp $
7  */

8
9 package com.sleepycat.je.recovery;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.util.logging.Level JavaDoc;
14
15 import junit.framework.TestCase;
16
17 import com.sleepycat.je.CheckpointConfig;
18 import com.sleepycat.je.DbInternal;
19 import com.sleepycat.je.Environment;
20 import com.sleepycat.je.EnvironmentConfig;
21 import com.sleepycat.je.EnvironmentStats;
22 import com.sleepycat.je.StatsConfig;
23 import com.sleepycat.je.config.EnvironmentParams;
24 import com.sleepycat.je.dbi.EnvironmentImpl;
25 import com.sleepycat.je.util.TestUtils;
26 import com.sleepycat.je.utilint.DbLsn;
27 import com.sleepycat.je.utilint.Tracer;
28
29 public class CheckpointActivationTest extends TestCase {
30
31     private File JavaDoc envHome;
32
33     public CheckpointActivationTest() {
34         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
35     }
36
37     public void setUp()
38         throws IOException JavaDoc {
39
40         TestUtils.removeLogFiles("Setup", envHome, false);
41     }
42     
43     public void tearDown()
44         throws Exception JavaDoc {
45
46         TestUtils.removeLogFiles("TearDown", envHome, false);
47     }
48
49     /**
50      * Write elements to the log, check that the right number of
51      * checkpoints ran.
52      */

53     public void testLogSizeBasedCheckpoints()
54         throws Exception JavaDoc {
55
56         final int CKPT_INTERVAL = 5000;
57         final int TRACER_OVERHEAD = 26;
58         final int N_TRACES = 100;
59         final int N_CHECKPOINTS = 10;
60         final int WAIT_FOR_CHECKPOINT_SECS = 10;
61         final int FILE_SIZE = 20000000;
62         
63         /* Init trace message with hyphens. */
64         assert CKPT_INTERVAL % N_TRACES == 0;
65         int msgBytesPerTrace = (CKPT_INTERVAL / N_TRACES) - TRACER_OVERHEAD;
66         StringBuffer JavaDoc traceBuf = new StringBuffer JavaDoc();
67         for (int i = 0; i < msgBytesPerTrace; i += 1) {
68             traceBuf.append('-');
69         }
70         String JavaDoc traceMsg = traceBuf.toString();
71
72         Environment env = null;
73         try {
74             EnvironmentConfig envConfig = TestUtils.initEnvConfig();
75             envConfig.setAllowCreate(true);
76             envConfig.setConfigParam(EnvironmentParams.
77                                      CHECKPOINTER_BYTES_INTERVAL.getName(),
78                                      String.valueOf(CKPT_INTERVAL));
79
80             /*
81              * This test needs to control exactly how much goes into the log,
82              * so disable daemons.
83              */

84             envConfig.setConfigParam(EnvironmentParams.
85                                      ENV_RUN_EVICTOR.getName(), "false");
86             envConfig.setConfigParam(EnvironmentParams.
87                                      ENV_RUN_INCOMPRESSOR.getName(), "false");
88             envConfig.setConfigParam(EnvironmentParams.
89                                      ENV_RUN_CLEANER.getName(), "false");
90             env = new Environment(envHome, envConfig);
91
92             /*
93              * Get a first reading on number of checkpoints run. Read once
94              * to clear, then read again.
95              */

96             StatsConfig statsConfig = new StatsConfig();
97             statsConfig.setFast(true);
98             statsConfig.setClear(true);
99             EnvironmentStats stats = env.getStats(statsConfig); // clear stats
100

101             stats = env.getStats(statsConfig); // read again
102
assertEquals(0, stats.getNCheckpoints());
103             long lastCkptEnd = stats.getLastCheckpointEnd();
104
105             /* Wait for checkpointer thread to start and go to wait state. */
106             EnvironmentImpl envImpl =
107                 DbInternal.envGetEnvironmentImpl(env);
108             Thread JavaDoc ckptThread = envImpl.getCheckpointer().getThread();
109             while (true) {
110                 Thread.State JavaDoc state = ckptThread.getState();
111                 if (state == Thread.State.WAITING ||
112                     state == Thread.State.TIMED_WAITING) {
113                     break;
114                 }
115             }
116
117             /* Run several checkpoints to ensure they occur as expected. */
118             for (int i = 0; i < N_CHECKPOINTS; i += 1) {
119
120                 /*
121                  * Write enough to prompt a checkpoint. 20% extra bytes are
122                  * written to be sure that we exceed the chekcpoint interval.
123                  */

124                 long lastLsn = envImpl.getFileManager().getNextLsn();
125                 while (DbLsn.getNoCleaningDistance
126                         (lastLsn, envImpl.getFileManager().getNextLsn(),
127                          FILE_SIZE) < CKPT_INTERVAL + CKPT_INTERVAL/5) {
128                     Tracer.trace(Level.SEVERE, envImpl, traceMsg);
129                 }
130
131                 /*
132                  * Wait for a checkpoint to start (if the test succeeds it will
133                  * start right away). We take advantage of the fact that the
134                  * NCheckpoints stat is set at the start of a checkpoint.
135                  */

136                  long startTime = System.currentTimeMillis();
137                  boolean started = false;
138                  while (!started &&
139                         (System.currentTimeMillis() - startTime <
140                          WAIT_FOR_CHECKPOINT_SECS * 1000)) {
141                     Thread.yield();
142                     Thread.sleep(1);
143                     stats = env.getStats(statsConfig);
144                     if (stats.getNCheckpoints() > 0) {
145                         started = true;
146                     }
147                 }
148                 assertTrue("Checkpoint " + i + " did not start after " +
149                            WAIT_FOR_CHECKPOINT_SECS + " seconds",
150                            started);
151
152                 /*
153                  * Wait for the checkpointer daemon to do its work. We do not
154                  * want to continue writing until the checkpoint is complete,
155                  * because the amount of data we write is calculated to be the
156                  * correct amount in between checkpoints. We know the
157                  * checkpoint is finished when the LastCheckpointEnd LSN
158                  * changes.
159                  */

160                 while (true) {
161                     Thread.yield();
162                     Thread.sleep(1);
163                     stats = env.getStats(statsConfig);
164                     if (lastCkptEnd != stats.getLastCheckpointEnd()) {
165                         lastCkptEnd = stats.getLastCheckpointEnd();
166                         break;
167                     }
168                 }
169             }
170         } catch (Exception JavaDoc e) {
171
172             /*
173              * print stack trace now, else it gets subsumed in exceptions
174              * caused by difficulty in removing log files.
175              */

176             e.printStackTrace();
177             throw e;
178         } finally {
179             if (env != null) {
180                 env.close();
181             }
182         }
183     }
184
185     /* Test programmatic call to checkpoint. */
186     public void testApiCalls()
187         throws Exception JavaDoc {
188
189         Environment env = null;
190         try {
191             EnvironmentConfig envConfig = TestUtils.initEnvConfig();
192             envConfig.setAllowCreate(true);
193             envConfig.setConfigParam(EnvironmentParams.
194                                      CHECKPOINTER_BYTES_INTERVAL.getName(),
195                                      "1000");
196
197             /* Disable all daemons */
198             envConfig.setConfigParam(EnvironmentParams.
199                                      ENV_RUN_EVICTOR.getName(), "false");
200             envConfig.setConfigParam(EnvironmentParams.
201                                      ENV_RUN_INCOMPRESSOR.getName(), "false");
202             envConfig.setConfigParam(EnvironmentParams.
203                                      ENV_RUN_CLEANER.getName(), "false");
204             envConfig.setConfigParam(EnvironmentParams.
205                                      ENV_RUN_CHECKPOINTER.getName(), "false");
206             env = new Environment(envHome, envConfig);
207
208             /*
209              * Get a first reading on number of checkpoints run. Read once
210              * to clear, then read again.
211              */

212             StatsConfig statsConfig = new StatsConfig();
213             statsConfig.setFast(true);
214             statsConfig.setClear(true);
215             EnvironmentStats stats = env.getStats(statsConfig); // clear stats
216

217             stats = env.getStats(statsConfig); // read again
218
assertEquals(0, stats.getNCheckpoints());
219
220             /*
221          * From the last checkpoint start LSN, there should be the
222          * checkpoint end log entry and a trace message. These take 196
223          * bytes.
224              */

225             CheckpointConfig checkpointConfig = new CheckpointConfig();
226
227             /* Should not cause a checkpoint, too little growth. */
228             checkpointConfig.setKBytes(1);
229             env.checkpoint(checkpointConfig);
230             stats = env.getStats(statsConfig); // read again
231
assertEquals(0, stats.getNCheckpoints());
232
233             /* Fill up the log, there should be a checkpoint. */
234             String JavaDoc filler = "123456789012345678901245678901234567890123456789";
235             EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env);
236             for (int i = 0; i < 20; i++) {
237                 Tracer.trace(Level.SEVERE, envImpl, filler);
238             }
239             env.checkpoint(checkpointConfig);
240             stats = env.getStats(statsConfig); // read again
241
assertEquals(1, stats.getNCheckpoints());
242
243             /* Try time based, should not checkpoint. */
244             checkpointConfig.setKBytes(0);
245             checkpointConfig.setMinutes(1);
246             env.checkpoint(checkpointConfig);
247             stats = env.getStats(statsConfig); // read again
248
assertEquals(0, stats.getNCheckpoints());
249
250             /*
251          * Sleep, enough time has passed for a checkpoint, but nothing was
252          * written to the log.
253              */

254             Thread.sleep(1000);
255             env.checkpoint(checkpointConfig);
256             stats = env.getStats(statsConfig); // read again
257
assertEquals(0, stats.getNCheckpoints());
258
259             /* Log something, now try a checkpoint. */
260             Tracer.trace(Level.SEVERE, envImpl, filler);
261             env.checkpoint(checkpointConfig);
262             stats = env.getStats(statsConfig); // read again
263
// TODO: make this test more timing independent. Sometimes
264
// the assertion will fail.
265
// assertEquals(1, stats.getNCheckpoints());
266

267         } catch (Exception JavaDoc e) {
268             /*
269              * print stack trace now, else it gets subsumed in exceptions
270              * caused by difficulty in removing log files.
271              */

272             e.printStackTrace();
273             throw e;
274         } finally {
275             if (env != null) {
276                 env.close();
277             }
278         }
279     }
280 }
281
Popular Tags