Committed by
Yuta Higuchi
DistributedLockManger creates a locks table if one does not exist
Change-Id: Ifa766ad441f677a4071b68d8f6caa564cf320869
Showing
1 changed file
with
21 additions
and
0 deletions
... | @@ -5,6 +5,7 @@ import static org.slf4j.LoggerFactory.getLogger; | ... | @@ -5,6 +5,7 @@ import static org.slf4j.LoggerFactory.getLogger; |
5 | 5 | ||
6 | import java.util.Iterator; | 6 | import java.util.Iterator; |
7 | import java.util.List; | 7 | import java.util.List; |
8 | +import java.util.Set; | ||
8 | import java.util.concurrent.CompletableFuture; | 9 | import java.util.concurrent.CompletableFuture; |
9 | import java.util.concurrent.ExecutorService; | 10 | import java.util.concurrent.ExecutorService; |
10 | import java.util.concurrent.Executors; | 11 | import java.util.concurrent.Executors; |
... | @@ -20,6 +21,8 @@ import org.onlab.onos.cluster.ClusterService; | ... | @@ -20,6 +21,8 @@ import org.onlab.onos.cluster.ClusterService; |
20 | import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService; | 21 | import org.onlab.onos.store.cluster.messaging.ClusterCommunicationService; |
21 | import org.onlab.onos.store.cluster.messaging.ClusterMessage; | 22 | import org.onlab.onos.store.cluster.messaging.ClusterMessage; |
22 | import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler; | 23 | import org.onlab.onos.store.cluster.messaging.ClusterMessageHandler; |
24 | +import org.onlab.onos.store.service.DatabaseAdminService; | ||
25 | +import org.onlab.onos.store.service.DatabaseException; | ||
23 | import org.onlab.onos.store.service.DatabaseService; | 26 | import org.onlab.onos.store.service.DatabaseService; |
24 | import org.onlab.onos.store.service.Lock; | 27 | import org.onlab.onos.store.service.Lock; |
25 | import org.onlab.onos.store.service.LockEventListener; | 28 | import org.onlab.onos.store.service.LockEventListener; |
... | @@ -41,6 +44,8 @@ public class DistributedLockManager implements LockService { | ... | @@ -41,6 +44,8 @@ public class DistributedLockManager implements LockService { |
41 | 44 | ||
42 | public static final String ONOS_LOCK_TABLE_NAME = "onos-locks"; | 45 | public static final String ONOS_LOCK_TABLE_NAME = "onos-locks"; |
43 | 46 | ||
47 | + public static final int DEAD_LOCK_TIMEOUT_MS = 5000; | ||
48 | + | ||
44 | private final ListMultimap<String, LockRequest> locksToAcquire = | 49 | private final ListMultimap<String, LockRequest> locksToAcquire = |
45 | Multimaps.synchronizedListMultimap(LinkedListMultimap.<String, LockRequest>create()); | 50 | Multimaps.synchronizedListMultimap(LinkedListMultimap.<String, LockRequest>create()); |
46 | 51 | ||
... | @@ -48,6 +53,9 @@ public class DistributedLockManager implements LockService { | ... | @@ -48,6 +53,9 @@ public class DistributedLockManager implements LockService { |
48 | private ClusterCommunicationService clusterCommunicator; | 53 | private ClusterCommunicationService clusterCommunicator; |
49 | 54 | ||
50 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
56 | + private DatabaseAdminService databaseAdminService; | ||
57 | + | ||
58 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
51 | private DatabaseService databaseService; | 59 | private DatabaseService databaseService; |
52 | 60 | ||
53 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
... | @@ -55,9 +63,22 @@ public class DistributedLockManager implements LockService { | ... | @@ -55,9 +63,22 @@ public class DistributedLockManager implements LockService { |
55 | 63 | ||
56 | @Activate | 64 | @Activate |
57 | public void activate() { | 65 | public void activate() { |
66 | + try { | ||
67 | + Set<String> tableNames = databaseAdminService.listTables(); | ||
68 | + if (!tableNames.contains(ONOS_LOCK_TABLE_NAME)) { | ||
69 | + if (databaseAdminService.createTable(ONOS_LOCK_TABLE_NAME, DEAD_LOCK_TIMEOUT_MS)) { | ||
70 | + log.info("Created {} table.", ONOS_LOCK_TABLE_NAME); | ||
71 | + } | ||
72 | + } | ||
73 | + } catch (DatabaseException e) { | ||
74 | + log.error("DistributedLockManager#activate failed.", e); | ||
75 | + throw e; | ||
76 | + } | ||
77 | + | ||
58 | clusterCommunicator.addSubscriber( | 78 | clusterCommunicator.addSubscriber( |
59 | DatabaseStateMachine.DATABASE_UPDATE_EVENTS, | 79 | DatabaseStateMachine.DATABASE_UPDATE_EVENTS, |
60 | new LockEventMessageListener()); | 80 | new LockEventMessageListener()); |
81 | + | ||
61 | log.info("Started."); | 82 | log.info("Started."); |
62 | 83 | ||
63 | } | 84 | } | ... | ... |
-
Please register or login to post a comment