comparison src/org/dancres/blitz/txn/OpCountingCheckpointTrigger.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package org.dancres.blitz.txn;
2
3 import java.io.IOException;
4
5 import java.util.logging.Level;
6
7 import org.dancres.blitz.ActiveObjectRegistry;
8
9 /**
10 A checkpoint trigger that fires off a checkpoint every time a certain
11 number of operations have been logged.
12 */
13 class OpCountingCheckpointTrigger implements CheckpointTrigger, Runnable {
14 private Checkpointer theCheckpointer;
15
16 private int theCommandThreshold;
17
18 private Thread theCheckpointThread;
19 private boolean stopCheckpointing = false;
20 private int theLogCount = 0;
21
22 OpCountingCheckpointTrigger(Checkpointer aCheckpointer,
23 int aCommandThreshold) {
24 theCheckpointer = aCheckpointer;
25 theCommandThreshold = aCommandThreshold;
26
27 ActiveObjectRegistry.add(this);
28 }
29
30 public void loggedCommand() {
31 synchronized(this) {
32 ++theLogCount;
33
34 if (theLogCount == theCommandThreshold) {
35 theLogCount = 0;
36 notify();
37 }
38 }
39 }
40
41 public void begin() {
42 theCheckpointThread = new Thread(this, "Checkpointer");
43 theCheckpointThread.start();
44 }
45
46 public void halt() {
47 synchronized(this) {
48 stopCheckpointing = true;
49 notify();
50 }
51
52 try {
53 theCheckpointThread.join();
54 theCheckpointThread = null;
55 } catch (InterruptedException anIE) {
56 TxnManager.theLogger.log(Level.SEVERE,
57 "Failed to wait for checkpointer",
58 anIE);
59 }
60 }
61
62 public void run() {
63 boolean timeToExit = false;
64
65 while(!timeToExit) {
66 synchronized(this) {
67 if (!stopCheckpointing) {
68 try {
69 wait();
70 } catch (InterruptedException anIE) {
71 TxnManager.theLogger.log(Level.INFO,
72 "Checkpointer interrupted",
73 anIE);
74 }
75 }
76
77 timeToExit = stopCheckpointing;
78 }
79
80 if (!timeToExit) {
81 try {
82 theCheckpointer.sync();
83 } catch (IOException anIOE) {
84 TxnManager.theLogger.log(Level.SEVERE,
85 "Checkpoint failed",
86 anIOE);
87 }
88 }
89 }
90 }
91
92 public boolean checkpointsDisabled() {
93 return false;
94 }
95 }