1 8 9 package com.sleepycat.je.recovery; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.util.logging.Level ; 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 envHome; 32 33 public CheckpointActivationTest() { 34 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 35 } 36 37 public void setUp() 38 throws IOException { 39 40 TestUtils.removeLogFiles("Setup", envHome, false); 41 } 42 43 public void tearDown() 44 throws Exception { 45 46 TestUtils.removeLogFiles("TearDown", envHome, false); 47 } 48 49 53 public void testLogSizeBasedCheckpoints() 54 throws Exception { 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 64 assert CKPT_INTERVAL % N_TRACES == 0; 65 int msgBytesPerTrace = (CKPT_INTERVAL / N_TRACES) - TRACER_OVERHEAD; 66 StringBuffer traceBuf = new StringBuffer (); 67 for (int i = 0; i < msgBytesPerTrace; i += 1) { 68 traceBuf.append('-'); 69 } 70 String 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 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 96 StatsConfig statsConfig = new StatsConfig(); 97 statsConfig.setFast(true); 98 statsConfig.setClear(true); 99 EnvironmentStats stats = env.getStats(statsConfig); 101 stats = env.getStats(statsConfig); assertEquals(0, stats.getNCheckpoints()); 103 long lastCkptEnd = stats.getLastCheckpointEnd(); 104 105 106 EnvironmentImpl envImpl = 107 DbInternal.envGetEnvironmentImpl(env); 108 Thread ckptThread = envImpl.getCheckpointer().getThread(); 109 while (true) { 110 Thread.State state = ckptThread.getState(); 111 if (state == Thread.State.WAITING || 112 state == Thread.State.TIMED_WAITING) { 113 break; 114 } 115 } 116 117 118 for (int i = 0; i < N_CHECKPOINTS; i += 1) { 119 120 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 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 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 e) { 171 172 176 e.printStackTrace(); 177 throw e; 178 } finally { 179 if (env != null) { 180 env.close(); 181 } 182 } 183 } 184 185 186 public void testApiCalls() 187 throws Exception { 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 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 212 StatsConfig statsConfig = new StatsConfig(); 213 statsConfig.setFast(true); 214 statsConfig.setClear(true); 215 EnvironmentStats stats = env.getStats(statsConfig); 217 stats = env.getStats(statsConfig); assertEquals(0, stats.getNCheckpoints()); 219 220 225 CheckpointConfig checkpointConfig = new CheckpointConfig(); 226 227 228 checkpointConfig.setKBytes(1); 229 env.checkpoint(checkpointConfig); 230 stats = env.getStats(statsConfig); assertEquals(0, stats.getNCheckpoints()); 232 233 234 String 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); assertEquals(1, stats.getNCheckpoints()); 242 243 244 checkpointConfig.setKBytes(0); 245 checkpointConfig.setMinutes(1); 246 env.checkpoint(checkpointConfig); 247 stats = env.getStats(statsConfig); assertEquals(0, stats.getNCheckpoints()); 249 250 254 Thread.sleep(1000); 255 env.checkpoint(checkpointConfig); 256 stats = env.getStats(statsConfig); assertEquals(0, stats.getNCheckpoints()); 258 259 260 Tracer.trace(Level.SEVERE, envImpl, filler); 261 env.checkpoint(checkpointConfig); 262 stats = env.getStats(statsConfig); 267 } catch (Exception e) { 268 272 e.printStackTrace(); 273 throw e; 274 } finally { 275 if (env != null) { 276 env.close(); 277 } 278 } 279 } 280 } 281 | Popular Tags |