KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > l2 > ha > ClusterState


1 /*
2  * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.l2.ha;
6
7 import com.tc.logging.TCLogger;
8 import com.tc.logging.TCLogging;
9 import com.tc.net.protocol.transport.ConnectionID;
10 import com.tc.net.protocol.transport.ConnectionIDFactory;
11 import com.tc.objectserver.persistence.api.PersistentMapStore;
12 import com.tc.util.Assert;
13 import com.tc.util.State;
14 import com.tc.util.sequence.ObjectIDSequence;
15
16 import java.util.HashSet JavaDoc;
17 import java.util.Set JavaDoc;
18
19 public class ClusterState {
20
21   private static final TCLogger logger = TCLogging.getLogger(ClusterState.class);
22
23   private static final String JavaDoc L2_STATE_KEY = "CLUSTER_STATE::L2_STATE_KEY";
24   private static final String JavaDoc CLUSTER_ID_KEY = "CLUSTER_STATE::CLUSTER_ID_KEY";
25
26   private final PersistentMapStore clusterStateStore;
27   private final ObjectIDSequence oidSequence;
28   private final ConnectionIDFactory connectionIdFactory;
29
30   private final Set JavaDoc connections = new HashSet JavaDoc();
31   private long nextAvailObjectID = -1;
32   private long nextAvailChannelID = -1;
33   private State currentState;
34   private String JavaDoc clusterID;
35
36   public ClusterState(PersistentMapStore clusterStateStore, ObjectIDSequence oidSequence,
37                       ConnectionIDFactory connectionIdFactory) {
38     this.clusterStateStore = clusterStateStore;
39     this.oidSequence = oidSequence;
40     this.connectionIdFactory = connectionIdFactory;
41     this.clusterID = clusterStateStore.get(CLUSTER_ID_KEY);
42   }
43
44   public void setNextAvailableObjectID(long nextAvailOID) {
45     if (nextAvailOID < nextAvailObjectID) {
46       // Could happen when two actives fight it out. Dont want to assert, let the state manager fight it out.
47
logger.error("Trying to set Next Available ObjectID to a lesser value : known = " + nextAvailObjectID
48                    + " new value = " + nextAvailOID + " IGNORING");
49       return;
50     }
51     this.nextAvailObjectID = nextAvailOID;
52   }
53
54   public long getNextAvailableObjectID() {
55     return nextAvailObjectID;
56   }
57
58   public long getNextAvailableChannelID() {
59     return nextAvailChannelID;
60   }
61
62   public void setNextAvailableChannelID(long nextAvailableCID) {
63     if (nextAvailableCID < nextAvailChannelID) {
64       // Could happen when two actives fight it out. Dont want to assert, let the state manager fight it out.
65
logger.error("Trying to set Next Available ChannelID to a lesser value : known = " + nextAvailChannelID
66                    + " new value = " + nextAvailableCID + " IGNORING");
67       return;
68     }
69     this.nextAvailChannelID = nextAvailableCID;
70   }
71   
72   public void syncInternal() {
73     syncConnectionIDs();
74     syncOIDSequence();
75   }
76
77   private void syncConnectionIDs() {
78     Assert.assertNotNull(clusterID);
79     connectionIdFactory.init(clusterID, nextAvailChannelID, connections);
80   }
81
82   public String JavaDoc getClusterID() {
83     return clusterID;
84   }
85
86   public void setClusterID(String JavaDoc uid) {
87     if (clusterID != null && !clusterID.equals(uid)) {
88       logger.error("Cluster ID doesnt match !! Mine : " + clusterID + " Active sent clusterID as : " + uid);
89       throw new AssertionError JavaDoc("ClusterIDs dont match : " + clusterID + " <-> " + uid);
90     }
91     clusterID = uid;
92     synchClusterIDToDB();
93   }
94
95   private void synchClusterIDToDB() {
96     clusterStateStore.put(CLUSTER_ID_KEY, clusterID);
97   }
98
99   private void syncOIDSequence() {
100     long nextOID = getNextAvailableObjectID();
101     if (nextOID != -1) {
102       logger.info("Setting the Next Available OID to " + nextOID);
103       this.oidSequence.setNextAvailableObjectID(nextOID);
104     }
105   }
106
107   public void setCurrentState(State state) {
108     this.currentState = state;
109     syncCurrentStateToDB();
110   }
111
112   private void syncCurrentStateToDB() {
113     clusterStateStore.put(L2_STATE_KEY, currentState.getName());
114   }
115
116   public void addNewConnection(ConnectionID connID) {
117     if (connID.getChannelID() >= nextAvailChannelID) {
118       nextAvailChannelID = connID.getChannelID() + 1;
119     }
120     connections.add(connID);
121   }
122
123   public void removeConnection(ConnectionID connectionID) {
124     boolean removed = connections.remove(connectionID);
125     if(!removed) {
126       logger.warn("Connection ID not found : " + connectionID + " Current Connections count : " + connections.size());
127     }
128   }
129
130   public Set JavaDoc getAllConnections() {
131     return new HashSet JavaDoc(connections);
132   }
133
134 }
135
Popular Tags