1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.statetransfer; |
8 |
| |
9 |
| import org.apache.commons.logging.Log; |
10 |
| import org.apache.commons.logging.LogFactory; |
11 |
| import org.jboss.cache.CacheException; |
12 |
| import org.jboss.cache.CacheImpl; |
13 |
| import org.jboss.cache.Fqn; |
14 |
| import org.jboss.cache.NodeSPI; |
15 |
| import org.jboss.cache.loader.CacheLoaderManager; |
16 |
| import org.jboss.cache.lock.NodeLock; |
17 |
| import org.jboss.cache.lock.TimeoutException; |
18 |
| import org.jboss.cache.marshall.NodeData; |
19 |
| import org.jboss.cache.marshall.NodeDataMarker; |
20 |
| |
21 |
| import java.io.ObjectInputStream; |
22 |
| import java.io.ObjectOutputStream; |
23 |
| |
24 |
| |
25 |
| public class StateTransferManager |
26 |
| { |
27 |
| protected final static Log log = LogFactory.getLog(StateTransferManager.class); |
28 |
| |
29 |
| public static final NodeData STREAMING_DELIMITER_NODE = new NodeDataMarker(); |
30 |
| |
31 |
| public static final String PARTIAL_STATE_DELIMITER = "_PARTIAL_STATE_DELIMITER"; |
32 |
| |
33 |
| private final CacheImpl cache; |
34 |
| |
35 |
972
| public StateTransferManager(CacheImpl cache)
|
36 |
| { |
37 |
972
| this.cache = cache;
|
38 |
| } |
39 |
| |
40 |
3708
| public CacheImpl getTreeCache()
|
41 |
| { |
42 |
3708
| return cache;
|
43 |
| } |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
764
| public void getState(ObjectOutputStream out, Fqn fqn, long timeout, boolean force, boolean suppressErrors) throws Throwable
|
65 |
| { |
66 |
| |
67 |
764
| boolean canProvideState = (!cache.getRegionManager().isInactive(fqn) && cache.findNode(fqn) != null);
|
68 |
| |
69 |
764
| boolean fetchTransientState = cache.getConfiguration().isFetchInMemoryState();
|
70 |
764
| CacheLoaderManager cacheLoaderManager = cache.getCacheLoaderManager();
|
71 |
764
| boolean fetchPersistentState = cacheLoaderManager != null && cacheLoaderManager.isFetchPersistentState();
|
72 |
| |
73 |
764
| if (canProvideState && (fetchPersistentState || fetchTransientState))
|
74 |
| { |
75 |
728
| cache.getMarshaller().objectToObjectStream(true, out);
|
76 |
728
| StateTransferGenerator generator = getStateTransferGenerator();
|
77 |
728
| Object owner = getOwnerForLock();
|
78 |
728
| long startTime = System.currentTimeMillis();
|
79 |
728
| NodeSPI rootNode = cache.findNode(fqn);
|
80 |
| |
81 |
728
| try
|
82 |
| { |
83 |
728
| if (log.isDebugEnabled())
|
84 |
| { |
85 |
0
| log.debug("locking the " + fqn + " subtree to return the in-memory (transient) state");
|
86 |
| } |
87 |
728
| acquireLocksForStateTransfer(rootNode, owner, timeout, true, force);
|
88 |
722
| generator.generateState(out, rootNode, fetchTransientState, fetchPersistentState, suppressErrors);
|
89 |
720
| if (log.isDebugEnabled())
|
90 |
| { |
91 |
0
| log.debug("Successfully generated state in " + (System.currentTimeMillis() - startTime) + " msec");
|
92 |
| } |
93 |
| } |
94 |
| finally |
95 |
| { |
96 |
728
| releaseStateTransferLocks(rootNode, owner, true);
|
97 |
| } |
98 |
| } |
99 |
| else |
100 |
| { |
101 |
36
| cache.getMarshaller().objectToObjectStream(false, out);
|
102 |
36
| Exception e = null;
|
103 |
36
| if (!canProvideState)
|
104 |
| { |
105 |
36
| String exceptionMessage = "Cache instance at " + cache.getLocalAddress() + " cannot provide state for fqn " + fqn + ".";
|
106 |
| |
107 |
36
| if (cache.getRegionManager().isInactive(fqn))
|
108 |
| { |
109 |
36
| exceptionMessage += " Region for fqn " + fqn + " is inactive.";
|
110 |
| } |
111 |
36
| if (cache.findNode(fqn) == null)
|
112 |
| { |
113 |
34
| exceptionMessage += " There is no cache node at fqn " + fqn;
|
114 |
| } |
115 |
36
| e = new CacheException(exceptionMessage);
|
116 |
| } |
117 |
36
| if (!fetchPersistentState && !fetchTransientState)
|
118 |
| { |
119 |
0
| e = new CacheException("Cache instance at " + cache.getLocalAddress() + " is not configured to provide state");
|
120 |
| } |
121 |
36
| cache.getMarshaller().objectToObjectStream(e, out);
|
122 |
36
| throw e;
|
123 |
| } |
124 |
| } |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
777
| public void setState(ObjectInputStream in, Fqn targetRoot) throws Exception
|
143 |
| { |
144 |
777
| CacheImpl cache = getTreeCache();
|
145 |
777
| NodeSPI target = cache.findNode(targetRoot);
|
146 |
777
| if (target == null)
|
147 |
| { |
148 |
| |
149 |
221
| cache.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
|
150 |
221
| cache.put(targetRoot, null);
|
151 |
221
| target = cache.findNode(targetRoot);
|
152 |
| } |
153 |
777
| Object o = cache.getMarshaller().objectFromObjectStream(in);
|
154 |
777
| Boolean hasState = (Boolean) o;
|
155 |
777
| if (hasState)
|
156 |
| { |
157 |
741
| setState(in, target);
|
158 |
| } |
159 |
| else |
160 |
| { |
161 |
36
| throw new CacheException("Cache instance at " + cache.getLocalAddress()
|
162 |
| + " cannot integrate state since state provider could not provide state due to " + cache.getMarshaller().objectFromObjectStream(in)); |
163 |
| } |
164 |
| } |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
| |
180 |
741
| private void setState(ObjectInputStream state, NodeSPI targetRoot) throws Exception
|
181 |
| { |
182 |
741
| Object owner = getOwnerForLock();
|
183 |
741
| long timeout = cache.getConfiguration().getStateRetrievalTimeout();
|
184 |
741
| long startTime = System.currentTimeMillis();
|
185 |
| |
186 |
741
| try
|
187 |
| { |
188 |
| |
189 |
741
| acquireLocksForStateTransfer(targetRoot, owner, timeout, true, true);
|
190 |
| |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
| |
196 |
| |
197 |
| |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
734
| StateTransferIntegrator integrator = getStateTransferIntegrator(state, targetRoot.getFqn());
|
205 |
730
| if (log.isDebugEnabled())
|
206 |
| { |
207 |
0
| log.debug("starting state integration at node " + targetRoot);
|
208 |
| } |
209 |
730
| integrator.integrateState(state, targetRoot);
|
210 |
728
| if (log.isDebugEnabled())
|
211 |
| { |
212 |
0
| log.debug("successfully integrated state in " + (System.currentTimeMillis() - startTime) + " msec");
|
213 |
| } |
214 |
| } |
215 |
| finally |
216 |
| { |
217 |
741
| releaseStateTransferLocks(targetRoot, owner, true);
|
218 |
| } |
219 |
| } |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
1469
| protected void acquireLocksForStateTransfer(NodeSPI root,
|
226 |
| Object lockOwner, |
227 |
| long timeout, |
228 |
| boolean lockChildren, |
229 |
| boolean force) |
230 |
| throws Exception |
231 |
| { |
232 |
1469
| try
|
233 |
| { |
234 |
1469
| if (lockChildren)
|
235 |
| { |
236 |
1469
| root.getLock().acquireAll(lockOwner, timeout, NodeLock.LockType.READ);
|
237 |
| } |
238 |
| else |
239 |
| { |
240 |
0
| root.getLock().acquire(lockOwner, timeout, NodeLock.LockType.READ);
|
241 |
| } |
242 |
| } |
243 |
| catch (TimeoutException te) |
244 |
| { |
245 |
13
| log.error("Caught TimeoutException acquiring locks on region " +
|
246 |
| root.getFqn(), te); |
247 |
13
| if (force)
|
248 |
| { |
249 |
| |
250 |
| |
251 |
13
| throw te;
|
252 |
| |
253 |
| } |
254 |
| else |
255 |
| { |
256 |
0
| throw te;
|
257 |
| } |
258 |
| } |
259 |
| } |
260 |
| |
261 |
| |
262 |
| |
263 |
| |
264 |
| |
265 |
| |
266 |
1469
| protected void releaseStateTransferLocks(NodeSPI root,
|
267 |
| Object lockOwner, |
268 |
| boolean childrenLocked) |
269 |
| { |
270 |
1469
| try
|
271 |
| { |
272 |
1469
| if (childrenLocked)
|
273 |
| { |
274 |
1469
| root.getLock().releaseAll(lockOwner);
|
275 |
| } |
276 |
| else |
277 |
| { |
278 |
0
| root.getLock().release(lockOwner);
|
279 |
| } |
280 |
| } |
281 |
| catch (Throwable t) |
282 |
| { |
283 |
0
| log.error("failed releasing locks", t);
|
284 |
| } |
285 |
| } |
286 |
| |
287 |
728
| protected StateTransferGenerator getStateTransferGenerator()
|
288 |
| { |
289 |
728
| return StateTransferFactory.getStateTransferGenerator(getTreeCache());
|
290 |
| } |
291 |
| |
292 |
734
| protected StateTransferIntegrator getStateTransferIntegrator(ObjectInputStream istream, Fqn fqn) throws Exception
|
293 |
| { |
294 |
734
| return StateTransferFactory.getStateTransferIntegrator(istream, fqn, getTreeCache());
|
295 |
| } |
296 |
| |
297 |
| |
298 |
| |
299 |
| |
300 |
| |
301 |
1469
| private Object getOwnerForLock()
|
302 |
| { |
303 |
1469
| Object owner = getTreeCache().getCurrentTransaction();
|
304 |
1469
| if (owner == null)
|
305 |
| { |
306 |
1469
| owner = Thread.currentThread();
|
307 |
| } |
308 |
1469
| return owner;
|
309 |
| } |
310 |
| } |