Yuta HIGUCHI

DatabaseService related fixes.

- Note: This patch does not fix the issue running as single node.

Change-Id: Iabfa548ca4e40e4ec5c9e76ae936300437e53d22
...@@ -39,6 +39,7 @@ import org.onlab.onos.store.service.BatchWriteRequest; ...@@ -39,6 +39,7 @@ import org.onlab.onos.store.service.BatchWriteRequest;
39 import org.onlab.onos.store.service.BatchWriteRequest.Builder; 39 import org.onlab.onos.store.service.BatchWriteRequest.Builder;
40 import org.onlab.onos.store.service.BatchWriteResult; 40 import org.onlab.onos.store.service.BatchWriteResult;
41 import org.onlab.onos.store.service.DatabaseAdminService; 41 import org.onlab.onos.store.service.DatabaseAdminService;
42 +import org.onlab.onos.store.service.DatabaseException;
42 import org.onlab.onos.store.service.DatabaseService; 43 import org.onlab.onos.store.service.DatabaseService;
43 import org.onlab.onos.store.service.VersionedValue; 44 import org.onlab.onos.store.service.VersionedValue;
44 import org.onlab.onos.store.service.WriteRequest; 45 import org.onlab.onos.store.service.WriteRequest;
...@@ -108,7 +109,21 @@ public class DistributedLinkResourceStore implements LinkResourceStore { ...@@ -108,7 +109,21 @@ public class DistributedLinkResourceStore implements LinkResourceStore {
108 109
109 serializer = new KryoSerializer(); 110 serializer = new KryoSerializer();
110 111
111 - Set<String> tables = databaseAdminService.listTables(); 112 + Set<String> tables = null;
113 + int retries = 0;
114 + do {
115 + try {
116 + tables = databaseAdminService.listTables();
117 + } catch (DatabaseException e) {
118 + log.debug("DatabaseException", e);
119 + retries++;
120 + if (retries > 10) {
121 + log.error("Failed to list tables, moving on", e);
122 + tables = new HashSet<>();
123 + }
124 + }
125 + } while (tables == null);
126 +
112 if (!tables.contains(LINK_RESOURCE_ALLOCATIONS)) { 127 if (!tables.contains(LINK_RESOURCE_ALLOCATIONS)) {
113 databaseAdminService.createTable(LINK_RESOURCE_ALLOCATIONS); 128 databaseAdminService.createTable(LINK_RESOURCE_ALLOCATIONS);
114 } 129 }
...@@ -116,6 +131,7 @@ public class DistributedLinkResourceStore implements LinkResourceStore { ...@@ -116,6 +131,7 @@ public class DistributedLinkResourceStore implements LinkResourceStore {
116 databaseAdminService.createTable(INTENT_ALLOCATIONS); 131 databaseAdminService.createTable(INTENT_ALLOCATIONS);
117 } 132 }
118 133
134 +
119 log.info("Started"); 135 log.info("Started");
120 } 136 }
121 137
......
...@@ -112,7 +112,7 @@ public class ClusterMessagingProtocolClient implements ProtocolClient { ...@@ -112,7 +112,7 @@ public class ClusterMessagingProtocolClient implements ProtocolClient {
112 clusterService.addListener(listener); 112 clusterService.addListener(listener);
113 113
114 // wait for specified controller node to come up 114 // wait for specified controller node to come up
115 - return null; 115 + return appeared;
116 } 116 }
117 117
118 @Override 118 @Override
......
...@@ -81,7 +81,7 @@ public class ClusterMessagingProtocolServer implements ProtocolServer { ...@@ -81,7 +81,7 @@ public class ClusterMessagingProtocolServer implements ProtocolServer {
81 } 81 }
82 } 82 }
83 if (handler == null) { 83 if (handler == null) {
84 - log.error("There was no handler for registered!"); 84 + log.error("There was no handler registered!");
85 return; 85 return;
86 } 86 }
87 } 87 }
......
1 package org.onlab.onos.store.service.impl; 1 package org.onlab.onos.store.service.impl;
2 2
3 +import static com.google.common.base.Preconditions.checkNotNull;
3 import static org.slf4j.LoggerFactory.getLogger; 4 import static org.slf4j.LoggerFactory.getLogger;
4 5
5 import java.util.Arrays; 6 import java.util.Arrays;
...@@ -12,7 +13,6 @@ import java.util.concurrent.ExecutionException; ...@@ -12,7 +13,6 @@ import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.TimeUnit; 13 import java.util.concurrent.TimeUnit;
13 import java.util.concurrent.TimeoutException; 14 import java.util.concurrent.TimeoutException;
14 15
15 -import net.kuujo.copycat.cluster.Member;
16 import net.kuujo.copycat.cluster.TcpMember; 16 import net.kuujo.copycat.cluster.TcpMember;
17 import net.kuujo.copycat.event.EventHandler; 17 import net.kuujo.copycat.event.EventHandler;
18 import net.kuujo.copycat.event.LeaderElectEvent; 18 import net.kuujo.copycat.event.LeaderElectEvent;
...@@ -40,23 +40,28 @@ public class DatabaseClient implements EventHandler<LeaderElectEvent> { ...@@ -40,23 +40,28 @@ public class DatabaseClient implements EventHandler<LeaderElectEvent> {
40 private final Logger log = getLogger(getClass()); 40 private final Logger log = getLogger(getClass());
41 41
42 private final DatabaseProtocolService protocol; 42 private final DatabaseProtocolService protocol;
43 - private volatile ProtocolClient copycat = null; 43 + private volatile ProtocolClient client = null;
44 - private volatile Member currentLeader = null; 44 + private volatile TcpMember currentLeader = null;
45 +
45 46
46 public DatabaseClient(DatabaseProtocolService protocol) { 47 public DatabaseClient(DatabaseProtocolService protocol) {
47 - this.protocol = protocol; 48 + this.protocol = checkNotNull(protocol);
48 } 49 }
49 50
51 + // FIXME This handler relies on a fact that local node is part of Raft cluster
50 @Override 52 @Override
51 public void handle(LeaderElectEvent event) { 53 public void handle(LeaderElectEvent event) {
52 - Member newLeader = event.leader(); 54 + final TcpMember newLeader = event.leader();
53 if (newLeader != null && !newLeader.equals(currentLeader)) { 55 if (newLeader != null && !newLeader.equals(currentLeader)) {
56 + log.info("{} became the new leader", newLeader);
57 + ProtocolClient prevClient = client;
58 + ProtocolClient newclient = protocol.createClient(newLeader);
59 + newclient.connect();
60 + client = newclient;
54 currentLeader = newLeader; 61 currentLeader = newLeader;
55 - if (copycat != null) { 62 + if (prevClient != null) {
56 - copycat.close(); 63 + prevClient.close();
57 } 64 }
58 - copycat = protocol.createClient((TcpMember) currentLeader);
59 - copycat.connect();
60 } 65 }
61 } 66 }
62 67
...@@ -92,7 +97,7 @@ public class DatabaseClient implements EventHandler<LeaderElectEvent> { ...@@ -92,7 +97,7 @@ public class DatabaseClient implements EventHandler<LeaderElectEvent> {
92 SubmitRequest request = 97 SubmitRequest request =
93 new SubmitRequest(nextRequestId(), operationName, Arrays.asList(args)); 98 new SubmitRequest(nextRequestId(), operationName, Arrays.asList(args));
94 99
95 - CompletableFuture<SubmitResponse> submitResponse = copycat.submit(request); 100 + CompletableFuture<SubmitResponse> submitResponse = client.submit(request);
96 101
97 log.debug("Sent {} to {}", request, currentLeader); 102 log.debug("Sent {} to {}", request, currentLeader);
98 103
......
...@@ -173,9 +173,10 @@ public class DatabaseManager implements DatabaseService, DatabaseAdminService { ...@@ -173,9 +173,10 @@ public class DatabaseManager implements DatabaseService, DatabaseAdminService {
173 Log consensusLog = new MapDBLog(LOG_FILE_PREFIX + localNode.id(), 173 Log consensusLog = new MapDBLog(LOG_FILE_PREFIX + localNode.id(),
174 ClusterMessagingProtocol.SERIALIZER); 174 ClusterMessagingProtocol.SERIALIZER);
175 175
176 + copycat = new Copycat(stateMachine, consensusLog, cluster, copycatMessagingProtocol);
177 +
176 client = new DatabaseClient(copycatMessagingProtocol); 178 client = new DatabaseClient(copycatMessagingProtocol);
177 179
178 - copycat = new Copycat(stateMachine, consensusLog, cluster, copycatMessagingProtocol);
179 180
180 copycat.event(LeaderElectEvent.class).registerHandler(client); 181 copycat.event(LeaderElectEvent.class).registerHandler(client);
181 copycat.event(LeaderElectEvent.class).registerHandler(expirationTracker); 182 copycat.event(LeaderElectEvent.class).registerHandler(expirationTracker);
......