KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > util > RunAction


1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002-2005
5 * Sleepycat Software. All rights reserved.
6 *
7 * $Id: RunAction.java,v 1.17 2005/06/07 14:23:48 mark Exp $
8 */

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 /**
31  * RunAction is a debugging aid that runs one of the background activities
32  * (cleaning, compressing, evicting, checkpointing).
33  */

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             /*
56              * Usage: -h <envHomeDir> (optional)
57              * -a <clean|compress|evict|checkpoint>
58              * -ro (read only)
59              */

60
61             String dbName = null;
62             int doAction = 0;
63             String envHome = "."; // default to current directory
64
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             /* Make an environment */
99             EnvironmentConfig envConfig = TestUtils.initEnvConfig();
100
101             /* Do debug log to the console.*/
102             envConfig.setConfigParam
103                 (EnvironmentParams.JE_LOGGING_CONSOLE.getName(), "true");
104
105             /* Don't debug log to the database log. */
106             if (readOnly) {
107
108                 envConfig.setConfigParam
109                     (EnvironmentParams.JE_LOGGING_DBLOG.getName(), "false");
110
111                 envConfig.setReadOnly(true);
112             }
113
114             /*
115              * If evicting, scan the given database first and don't run the
116              * background evictor.
117              */

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                 /* Since this is batch cleaning, repeat until no progress. */
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         /* push the cache size down by half to force eviction. */
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