KickJava   Java API By Example, From Geeks To Geeks.

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


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

8
9 package com.sleepycat.je.util;
10
11 import java.io.BufferedReader JavaDoc;
12 import java.io.File JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.InputStreamReader JavaDoc;
15 import java.text.DecimalFormat JavaDoc;
16
17 import com.sleepycat.je.CheckpointConfig;
18 import com.sleepycat.je.Cursor;
19 import com.sleepycat.je.Database;
20 import com.sleepycat.je.DatabaseEntry;
21 import com.sleepycat.je.DatabaseException;
22 import com.sleepycat.je.DbInternal;
23 import com.sleepycat.je.Environment;
24 import com.sleepycat.je.EnvironmentConfig;
25 import com.sleepycat.je.EnvironmentMutableConfig;
26 import com.sleepycat.je.LockMode;
27 import com.sleepycat.je.OperationStatus;
28 import com.sleepycat.je.Transaction;
29 import com.sleepycat.je.config.EnvironmentParams;
30 import com.sleepycat.je.dbi.EnvironmentImpl;
31 import com.sleepycat.je.utilint.CmdUtil;
32
33 /**
34  * DbRunAction is a debugging aid that can invoke a JE operation or
35  * background activity from the command line.
36  *
37  * batchClean calls Environment.cleanLog() in a loop
38  * compress calls Environment.compress
39  * evict calls Environment.preload, then evictMemory
40  * removeDb calls Environment.removeDatabase, but doesn't do any cleaning
41  * removeDbAndClean calls removeDatabase, then cleanLog in a loop
42  * activateCleaner wakes up the cleaner, and then the main thread waits
43  * until you type "y" to the console before calling Environment.close().
44  * The control provide by the prompt is necessary for daemon activities
45  * because often threads check and bail out if the environment is closed.
46  */

47 public class DbRunAction {
48
49     private static final int NONE = 0;
50     private static final int BATCH_CLEAN = 1; // app-driven batch cleaning
51
private static final int COMPRESS = 2;
52     private static final int EVICT = 3;
53     private static final int CHECKPOINT = 4;
54     private static final int REMOVEDB = 5;
55     private static final int REMOVEDB_AND_CLEAN = 6;
56     private static final int ACTIVATE_CLEANER_THREADS = 7;
57                                            // wake up cleaner threads
58

59     public static void main(String JavaDoc [] argv) {
60
61         long recoveryStart = 0;
62         long actionStart = 0;
63         long actionEnd = 0;
64
65         try {
66             int whichArg = 0;
67             if (argv.length == 0) {
68                 usage();
69                 System.exit(1);
70             }
71
72             String JavaDoc dbName = null;
73             int doAction = 0;
74             String JavaDoc envHome = ".";
75             boolean readOnly = false;
76
77             while (whichArg < argv.length) {
78                 String JavaDoc nextArg = argv[whichArg];
79
80                 if (nextArg.equals("-h")) {
81                     whichArg++;
82                     envHome = CmdUtil.getArg(argv, whichArg);
83                 } else if (nextArg.equals("-a")) {
84                     whichArg++;
85                     String JavaDoc action = CmdUtil.getArg(argv, whichArg);
86                     if (action.equalsIgnoreCase("batchClean")) {
87                         doAction = BATCH_CLEAN;
88                     } else if (action.equalsIgnoreCase("compress")) {
89                         doAction = COMPRESS;
90                     } else if (action.equalsIgnoreCase("checkpoint")) {
91                         doAction = CHECKPOINT;
92                     } else if (action.equalsIgnoreCase("evict")) {
93                         doAction = EVICT;
94                     } else if (action.equalsIgnoreCase("removedb")) {
95                         doAction = REMOVEDB;
96                     } else if (action.equalsIgnoreCase("removedbAndClean")) {
97                         doAction = REMOVEDB_AND_CLEAN;
98                     } else if (action.equalsIgnoreCase("activateCleaner")) {
99                         doAction = ACTIVATE_CLEANER_THREADS;
100                     } else {
101                         usage();
102                         System.exit(1);
103                     }
104                 } else if (nextArg.equals("-ro")) {
105                     readOnly = true;
106                 } else if (nextArg.equals("-s")) {
107                     dbName = argv[++whichArg];
108                 } else {
109                     throw new IllegalArgumentException JavaDoc
110                         (nextArg + " is not a supported option.");
111                 }
112                 whichArg++;
113             }
114
115             /* Make an environment */
116             EnvironmentConfig envConfig = new EnvironmentConfig();
117
118             /* Do debug log to the console.*/
119             envConfig.setConfigParam
120                 (EnvironmentParams.JE_LOGGING_CONSOLE.getName(), "true");
121
122             /* Don't debug log to the database log. */
123             if (readOnly) {
124                 envConfig.setConfigParam
125                     (EnvironmentParams.JE_LOGGING_DBLOG.getName(), "false");
126
127                 envConfig.setReadOnly(true);
128             }
129
130             /*
131              * If evicting, scan the given database first and don't run the
132              * background evictor.
133              */

134             if (doAction == EVICT) {
135
136                 envConfig.setConfigParam(
137                   EnvironmentParams.ENV_RUN_EVICTOR.getName(), "false");
138                 envConfig.setConfigParam(
139                   EnvironmentParams.EVICTOR_CRITICAL_PERCENTAGE.getName(),
140                                           "1000");
141             }
142                 
143             recoveryStart = System.currentTimeMillis();
144
145             Environment env =
146         new Environment(new File JavaDoc(envHome), envConfig);
147
148             CheckpointConfig forceConfig = new CheckpointConfig();
149             forceConfig.setForce(true);
150             
151             boolean promptForShutdown = false;
152             actionStart = System.currentTimeMillis();
153             switch(doAction) {
154             case BATCH_CLEAN:
155                 /* Since this is batch cleaning, repeat until no progress. */
156                 while (true) {
157                     int nFiles = env.cleanLog();
158                     System.out.println("Files cleaned: " + nFiles);
159                     if (nFiles == 0) {
160                         break;
161                     }
162                 }
163                 env.checkpoint(forceConfig);
164                 break;
165             case COMPRESS:
166                 env.compress();
167                 break;
168             case CHECKPOINT:
169                 env.checkpoint(forceConfig);
170                 break;
171             case EVICT:
172                 preload(env, dbName);
173                 break;
174             case REMOVEDB:
175                 removeAndClean(env, dbName, false);
176                 break;
177             case REMOVEDB_AND_CLEAN:
178                 removeAndClean(env, dbName, true);
179                 break;
180             case ACTIVATE_CLEANER_THREADS:
181                 EnvironmentImpl envImpl =
182                     DbInternal.envGetEnvironmentImpl(env);
183                 envImpl.getCleaner().wakeup();
184                 promptForShutdown = true;
185                 break;
186             }
187             actionEnd = System.currentTimeMillis();
188
189             if (promptForShutdown) {
190                 /*
191                  * If the requested action is a daemon driven one, we
192                  * don't want the main thread to shutdown the environment
193                  * until we say we're ready
194                  */

195                 waitForShutdown();
196             }
197             env.close();
198         } catch (Exception JavaDoc e) {
199             e.printStackTrace();
200             System.out.println(e.getMessage());
201             usage();
202             System.exit(1);
203         } finally {
204             DecimalFormat JavaDoc f = new DecimalFormat JavaDoc();
205             f.setMaximumFractionDigits(2);
206
207             long recoveryDuration = actionStart - recoveryStart;
208             System.out.println("\nrecovery time = " +
209                                f.format(recoveryDuration) +
210                                " millis " +
211                                f.format((double)recoveryDuration/60000) +
212                                " minutes");
213
214             long actionDuration = actionEnd - actionStart;
215             System.out.println("action time = " +
216                                f.format(actionDuration) +
217                                " millis " +
218                                f.format(actionDuration/60000) +
219                                " minutes");
220
221         }
222     }
223     
224     private static void removeAndClean(Environment env,
225                                        String JavaDoc name,
226                                        boolean doCleaning)
227         throws DatabaseException {
228         
229         long a, b, c, d, e, f;
230
231         // Transaction txn = env.beginTransaction(null, null);
232
Transaction txn = null;
233         CheckpointConfig force = new CheckpointConfig();
234         force.setForce(true);
235
236         a = System.currentTimeMillis();
237         env.removeDatabase(txn, name);
238         b = System.currentTimeMillis();
239         // txn.commit();
240
c = System.currentTimeMillis();
241
242         int cleanedCount = 0;
243         if (doCleaning) {
244             while (env.cleanLog() > 0) {
245                 cleanedCount++;
246             }
247         }
248         d = System.currentTimeMillis();
249
250         System.out.println("cleanedCount=" + cleanedCount);
251         e = 0;
252         f = 0;
253         if (cleanedCount > 0) {
254             e = System.currentTimeMillis();
255             env.checkpoint(force);
256             f = System.currentTimeMillis();
257         }
258
259         System.out.println("Remove of " + name +
260                            " remove: " + getSecs(a, b) +
261                            " commit: " + getSecs(b, c) +
262                            " clean: " + getSecs(c, d) +
263                            " checkpoint: " + getSecs(e, f));
264     }
265
266     private static String JavaDoc getSecs(long start, long end) {
267         return (end-start)/1000 + " secs";
268     }
269
270     private static void preload(Environment env, String JavaDoc dbName)
271         throws DatabaseException {
272
273         System.out.println("Preload starting");
274         Database db = env.openDatabase(null, dbName, null);
275         Cursor cursor = db.openCursor(null, null);
276         try {
277             DatabaseEntry key = new DatabaseEntry();
278             DatabaseEntry data = new DatabaseEntry();
279             int count = 0;
280             while (cursor.getNext(key, data, LockMode.DEFAULT) ==
281                    OperationStatus.SUCCESS) {
282                 count++;
283                 if ((count % 50000) == 0) {
284                     System.out.println(count + "...");
285                 }
286             }
287             System.out.println("Preloaded " + count + " records");
288         } finally {
289             cursor.close();
290             db.close();
291         }
292     }
293
294     private static void doEvict(Environment env)
295         throws DatabaseException {
296         
297         /* push the cache size down by half to force eviction. */
298         EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env);
299         long cacheUsage = envImpl.getMemoryBudget().getCacheMemoryUsage();
300         EnvironmentMutableConfig c = new EnvironmentMutableConfig();
301         c.setCacheSize(cacheUsage/2);
302         env.setMutableConfig(c);
303
304         long start = System.currentTimeMillis();
305         env.evictMemory();
306         long end = System.currentTimeMillis();
307
308         DecimalFormat JavaDoc f = new DecimalFormat JavaDoc();
309         f.setMaximumFractionDigits(2);
310         System.out.println("evict time=" + f.format(end-start));
311     }
312
313     private static void waitForShutdown()
314         throws IOException JavaDoc {
315
316         System.out.println
317         ("Wait for daemon activity to run. When ready to stop, type (y)");
318         BufferedReader JavaDoc reader =
319             new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
320         do {
321             String JavaDoc val = reader.readLine();
322             if (val != null &&
323         (val.equalsIgnoreCase("y") ||
324                  val.equalsIgnoreCase("yes"))) {
325                 break;
326             } else {
327                 System.out.println("Shutdown? (y)");
328             }
329         } while (true);
330     }
331
332     private static void usage() {
333         System.out.println("Usage: \n " +
334                CmdUtil.getJavaCommand(DbRunAction.class));
335     System.out.println(" -h <environment home> ");
336         System.out.println(" -a <batchClean|compress|evict|checkpoint|" +
337                "removeDb|removeDbAndClean|activateCleaner>");
338         System.out.println(" -ro (read-only - defaults to read-write)");
339         System.out.println(" -s <dbName> (for removeDb)");
340     }
341 }
342
Popular Tags