01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.objectserver.handler;
06:
07: import com.tc.async.api.AbstractEventHandler;
08: import com.tc.async.api.ConfigurationContext;
09: import com.tc.async.api.EventContext;
10: import com.tc.async.api.Sink;
11: import com.tc.l2.api.ReplicatedClusterStateManager;
12: import com.tc.objectserver.core.api.ServerConfigurationContext;
13: import com.tc.objectserver.gtx.GlobalTransactionIDSequenceProvider;
14: import com.tc.util.sequence.BatchSequenceReceiver;
15: import com.tc.util.sequence.MutableSequence;
16:
17: public class GlobalTransactionIDBatchRequestHandler extends
18: AbstractEventHandler implements
19: GlobalTransactionIDSequenceProvider {
20:
21: private Sink requestBatchSink;
22: private final MutableSequence sequence;
23: private ReplicatedClusterStateManager clusterStateMgr;
24:
25: public GlobalTransactionIDBatchRequestHandler(
26: MutableSequence sequence) {
27: this .sequence = sequence;
28: }
29:
30: public void setRequestBatchSink(Sink sink) {
31: this .requestBatchSink = sink;
32: }
33:
34: // EventHandler interface
35: public void handleEvent(EventContext context) {
36: GlobalTransactionIDBatchRequestContext ctxt = (GlobalTransactionIDBatchRequestContext) context;
37: BatchSequenceReceiver receiver = ctxt.getReceiver();
38: int batchSize = ctxt.getBatchSize();
39: long start = sequence.nextBatch(batchSize);
40: this .clusterStateMgr
41: .publishNextAvailableGlobalTransactionID(start
42: + batchSize);
43: receiver.setNextBatch(start, start + batchSize);
44: }
45:
46: public void initialize(ConfigurationContext context) {
47: super .initialize(context);
48: ServerConfigurationContext scc = (ServerConfigurationContext) context;
49: this .clusterStateMgr = scc.getL2Coordinator()
50: .getReplicatedClusterStateManager();
51: }
52:
53: // BatchSequenceProvider interface
54: public void requestBatch(BatchSequenceReceiver receiver, int size) {
55: this .requestBatchSink
56: .add(new GlobalTransactionIDBatchRequestContext(
57: receiver, size));
58: }
59:
60: public void setNextAvailableGID(long nextGID) {
61: sequence.setNext(nextGID);
62: }
63:
64: public static final class GlobalTransactionIDBatchRequestContext
65: implements EventContext {
66: private final BatchSequenceReceiver receiver;
67: private final int size;
68:
69: public GlobalTransactionIDBatchRequestContext(
70: BatchSequenceReceiver receiver, int size) {
71: this .receiver = receiver;
72: this .size = size;
73: }
74:
75: public BatchSequenceReceiver getReceiver() {
76: return this .receiver;
77: }
78:
79: public int getBatchSize() {
80: return this.size;
81: }
82: }
83: }
|