1 /**
2 * Copyright Terracotta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.ehcache.transaction.local;
17
18 import net.sf.ehcache.transaction.TransactionID;
19 import net.sf.ehcache.transaction.TransactionIDFactory;
20
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.concurrent.CopyOnWriteArrayList;
26
27 /**
28 * The local transactions mode recovery manager which is used to trigger full recovery of all
29 * caches configured with local transaction mode.
30 *
31 * @author Ludovic Orban
32 */
33 public class LocalRecoveryManager {
34
35 private final TransactionIDFactory transactionIdFactory;
36 private final List<LocalTransactionStore> localTransactionStores = new CopyOnWriteArrayList<LocalTransactionStore>();
37 private volatile Set<TransactionID> previouslyRecoveredTransactionIDs = Collections.emptySet();
38
39 /**
40 * Create a LocalRecoveryManager instance
41 * @param transactionIdFactory the TransactionIDFactory
42 */
43 public LocalRecoveryManager(TransactionIDFactory transactionIdFactory) {
44 this.transactionIdFactory = transactionIdFactory;
45 }
46
47 /**
48 * Register a LocalTransactionStore from the recovery manager
49 * @param localTransactionStore the LocalTransactionStore
50 */
51 void register(LocalTransactionStore localTransactionStore) {
52 localTransactionStores.add(localTransactionStore);
53 }
54
55 /**
56 * Unregister a LocalTransactionStore from the recovery manager
57 * @param localTransactionStore the LocalTransactionStore
58 */
59 void unregister(LocalTransactionStore localTransactionStore) {
60 localTransactionStores.remove(localTransactionStore);
61 }
62
63 /**
64 * Run recovery on all registered local transaction stores. The latter
65 * are used internally by caches when they're configured with local transaction mode.
66 * @return the set of recovered TransactionIDs
67 */
68 public Set<TransactionID> recover() {
69 Set<TransactionID> recovered = new HashSet<TransactionID>();
70 // first ask all stores to cleanup their soft locks
71 for (LocalTransactionStore localTransactionStore : localTransactionStores) {
72 recovered.addAll(localTransactionStore.recover());
73 }
74 // then clear the transaction ID
75 for (TransactionID transactionId : recovered) {
76 transactionIdFactory.clear(transactionId);
77 }
78
79 previouslyRecoveredTransactionIDs = recovered;
80 return recovered;
81 }
82
83 /**
84 * Get the set of transaction IDs collected by the previous recover() call
85 * @return the set of previously recovered TransactionIDs
86 */
87 public Set<TransactionID> getPreviouslyRecoveredTransactionIDs() {
88 return previouslyRecoveredTransactionIDs;
89 }
90 }
91