1 9 10 package com.sleepycat.je.util; 11 12 import java.io.File; 13 import java.text.DecimalFormat; 14 15 import com.sleepycat.je.CheckpointConfig; 16 import com.sleepycat.je.Cursor; 17 import com.sleepycat.je.Database; 18 import com.sleepycat.je.DatabaseEntry; 19 import com.sleepycat.je.DatabaseException; 20 import com.sleepycat.je.DbInternal; 21 import com.sleepycat.je.Environment; 22 import com.sleepycat.je.EnvironmentConfig; 23 import com.sleepycat.je.EnvironmentMutableConfig; 24 import com.sleepycat.je.LockMode; 25 import com.sleepycat.je.OperationStatus; 26 import com.sleepycat.je.config.EnvironmentParams; 27 import com.sleepycat.je.dbi.EnvironmentImpl; 28 import com.sleepycat.je.utilint.CmdUtil; 29 30 34 public class RunAction { 35 36 private static final int CLEAN = 1; 37 private static final int COMPRESS = 2; 38 private static final int EVICT = 3; 39 private static final int CHECKPOINT = 4; 40 41 public static void main(String [] argv) { 42 43 long recoveryStart = 0; 44 long actionStart = 0; 45 long actionEnd = 0; 46 47 try { 48 int whichArg = 0; 49 50 if (argv.length == 0) { 51 usage(); 52 System.exit(1); 53 } 54 55 60 61 String dbName = null; 62 int doAction = 0; 63 String envHome = "."; boolean readOnly = false; 65 66 while (whichArg < argv.length) { 67 String nextArg = argv[whichArg]; 68 69 if (nextArg.equals("-h")) { 70 whichArg++; 71 envHome = CmdUtil.getArg(argv, whichArg); 72 } else if (nextArg.equals("-a")) { 73 whichArg++; 74 String action = CmdUtil.getArg(argv, whichArg); 75 if (action.equalsIgnoreCase("clean")) { 76 doAction = CLEAN; 77 } else if (action.equalsIgnoreCase("compress")) { 78 doAction = COMPRESS; 79 } else if (action.equalsIgnoreCase("checkpoint")) { 80 doAction = CHECKPOINT; 81 } else if (action.equalsIgnoreCase("evict")) { 82 doAction = EVICT; 83 } else { 84 usage(); 85 System.exit(1); 86 } 87 } else if (nextArg.equals("-ro")) { 88 readOnly = true; 89 } else if (nextArg.equals("-s")) { 90 dbName = argv[++whichArg]; 91 } else { 92 throw new IllegalArgumentException 93 (nextArg + " is not a supported option."); 94 } 95 whichArg++; 96 } 97 98 99 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 100 101 102 envConfig.setConfigParam 103 (EnvironmentParams.JE_LOGGING_CONSOLE.getName(), "true"); 104 105 106 if (readOnly) { 107 108 envConfig.setConfigParam 109 (EnvironmentParams.JE_LOGGING_DBLOG.getName(), "false"); 110 111 envConfig.setReadOnly(true); 112 } 113 114 118 if (doAction == EVICT) { 119 120 envConfig.setConfigParam( 121 EnvironmentParams.ENV_RUN_EVICTOR.getName(), "false"); 122 envConfig.setConfigParam( 123 EnvironmentParams.EVICTOR_CRITICAL_PERCENTAGE.getName(), 124 "1000"); 125 } 126 127 recoveryStart = System.currentTimeMillis(); 128 129 Environment envHandle = 130 new Environment(new File(envHome), envConfig); 131 132 CheckpointConfig forceConfig = new CheckpointConfig(); 133 forceConfig.setForce(true); 134 135 actionStart = System.currentTimeMillis(); 136 switch(doAction) { 137 case CLEAN: 138 139 while (true) { 140 int nFiles = envHandle.cleanLog(); 141 System.out.println("Files cleaned: " + nFiles); 142 if (nFiles == 0) { 143 break; 144 } 145 } 146 envHandle.checkpoint(forceConfig); 147 break; 148 case COMPRESS: 149 envHandle.compress(); 150 break; 151 case EVICT: 152 preload(envHandle, dbName); 153 break; 154 case CHECKPOINT: 155 envHandle.checkpoint(forceConfig); 156 break; 157 } 158 actionEnd = System.currentTimeMillis(); 159 160 envHandle.close(); 161 162 163 } catch (Exception e) { 164 e.printStackTrace(); 165 System.out.println(e.getMessage()); 166 usage(); 167 System.exit(1); 168 } finally { 169 DecimalFormat f = new DecimalFormat(); 170 f.setMaximumFractionDigits(2); 171 172 long recoveryDuration = actionStart - recoveryStart; 173 System.out.println("\nrecovery time = " + 174 f.format(recoveryDuration) + 175 " millis " + 176 f.format((double)recoveryDuration/60000) + 177 " minutes"); 178 179 long actionDuration = actionEnd - actionStart; 180 System.out.println("action time = " + 181 f.format(actionDuration) + 182 " millis " + 183 f.format(actionDuration/60000) + 184 " minutes"); 185 186 } 187 } 188 189 190 private static void preload(Environment env, String dbName) 191 throws DatabaseException { 192 System.out.println("Preload starting"); 193 Database db = env.openDatabase(null, dbName, null); 194 Cursor cursor = db.openCursor(null, null); 195 try { 196 DatabaseEntry key = new DatabaseEntry(); 197 DatabaseEntry data = new DatabaseEntry(); 198 int count = 0; 199 while (cursor.getNext(key, data, LockMode.DEFAULT) == 200 OperationStatus.SUCCESS) { 201 count++; 202 if ((count % 50000) == 0) { 203 System.out.println(count + "..."); 204 } 205 } 206 System.out.println("Preloaded " + count + " records"); 207 } finally { 208 cursor.close(); 209 db.close(); 210 } 211 } 212 213 private static void doEvict(Environment env) 214 throws DatabaseException { 215 216 217 EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env); 218 long cacheUsage = envImpl.getMemoryBudget().getCacheMemoryUsage(); 219 EnvironmentMutableConfig c = new EnvironmentMutableConfig(); 220 c.setCacheSize(cacheUsage/2); 221 env.setMutableConfig(c); 222 223 long start = System.currentTimeMillis(); 224 env.evictMemory(); 225 long end = System.currentTimeMillis(); 226 227 DecimalFormat f = new DecimalFormat(); 228 f.setMaximumFractionDigits(2); 229 System.out.println("evict time=" + f.format(end-start)); 230 } 231 232 private static void usage() { 233 System.out.println("Usage: RunAction"); 234 System.out.println(" -h <environment home> "); 235 System.out.println(" -a <clean|compress|evict|checkpoint>"); 236 System.out.println(" -ro (read-only - defaults to read-write)"); 237 System.out.println(" -s <dbName> (for preloading of evict)"); 238 } 239 } 240 | Popular Tags |