Yuta HIGUCHI

DatabaseClient: Add timeout

- timeout + retry to listTable
- timeout to service API

Change-Id: I8b54dd24d380dcc9e8d44baf3bbf5e379ccca53b
...@@ -3,6 +3,7 @@ package org.onlab.onos.store.service.impl; ...@@ -3,6 +3,7 @@ package org.onlab.onos.store.service.impl;
3 import static com.google.common.base.Preconditions.checkNotNull; 3 import static com.google.common.base.Preconditions.checkNotNull;
4 import static org.slf4j.LoggerFactory.getLogger; 4 import static org.slf4j.LoggerFactory.getLogger;
5 5
6 +import java.util.Collections;
6 import java.util.List; 7 import java.util.List;
7 import java.util.Map; 8 import java.util.Map;
8 import java.util.Set; 9 import java.util.Set;
...@@ -10,6 +11,7 @@ import java.util.concurrent.CompletableFuture; ...@@ -10,6 +11,7 @@ import java.util.concurrent.CompletableFuture;
10 import java.util.concurrent.CountDownLatch; 11 import java.util.concurrent.CountDownLatch;
11 import java.util.concurrent.ExecutionException; 12 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.TimeUnit; 13 import java.util.concurrent.TimeUnit;
14 +import java.util.concurrent.TimeoutException;
13 15
14 import net.kuujo.copycat.Copycat; 16 import net.kuujo.copycat.Copycat;
15 import net.kuujo.copycat.event.EventHandler; 17 import net.kuujo.copycat.event.EventHandler;
...@@ -28,6 +30,10 @@ import org.slf4j.Logger; ...@@ -28,6 +30,10 @@ import org.slf4j.Logger;
28 */ 30 */
29 public class DatabaseClient { 31 public class DatabaseClient {
30 32
33 + private static final int RETRIES = 5;
34 +
35 + private static final int TIMEOUT_MS = 2000;
36 +
31 private final Logger log = getLogger(getClass()); 37 private final Logger log = getLogger(getClass());
32 38
33 private final Copycat copycat; 39 private final Copycat copycat;
...@@ -109,9 +115,20 @@ public class DatabaseClient { ...@@ -109,9 +115,20 @@ public class DatabaseClient {
109 115
110 public Set<String> listTables() { 116 public Set<String> listTables() {
111 waitForLeader(); 117 waitForLeader();
112 - CompletableFuture<Set<String>> future = copycat.submit("listTables");
113 try { 118 try {
114 - return future.get(); 119 + for (int i = 0; i < RETRIES; ++i) {
120 + CompletableFuture<Set<String>> future = copycat.submit("listTables");
121 + try {
122 + return future.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
123 + } catch (TimeoutException e) {
124 + log.debug("Timed out retrying {}", i);
125 + future.cancel(true);
126 + waitForLeader();
127 + }
128 + }
129 + // TODO: proper timeout handling
130 + log.error("Timed out");
131 + return Collections.emptySet();
115 } catch (InterruptedException | ExecutionException e) { 132 } catch (InterruptedException | ExecutionException e) {
116 throw new DatabaseException(e); 133 throw new DatabaseException(e);
117 } 134 }
...@@ -121,9 +138,11 @@ public class DatabaseClient { ...@@ -121,9 +138,11 @@ public class DatabaseClient {
121 waitForLeader(); 138 waitForLeader();
122 CompletableFuture<List<ReadResult>> future = copycat.submit("read", batchRequest); 139 CompletableFuture<List<ReadResult>> future = copycat.submit("read", batchRequest);
123 try { 140 try {
124 - return future.get(); 141 + return future.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
125 } catch (InterruptedException | ExecutionException e) { 142 } catch (InterruptedException | ExecutionException e) {
126 throw new DatabaseException(e); 143 throw new DatabaseException(e);
144 + } catch (TimeoutException e) {
145 + throw new DatabaseException(e);
127 } 146 }
128 } 147 }
129 148
...@@ -131,9 +150,11 @@ public class DatabaseClient { ...@@ -131,9 +150,11 @@ public class DatabaseClient {
131 waitForLeader(); 150 waitForLeader();
132 CompletableFuture<List<WriteResult>> future = copycat.submit("write", batchRequest); 151 CompletableFuture<List<WriteResult>> future = copycat.submit("write", batchRequest);
133 try { 152 try {
134 - return future.get(); 153 + return future.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
135 } catch (InterruptedException | ExecutionException e) { 154 } catch (InterruptedException | ExecutionException e) {
136 throw new DatabaseException(e); 155 throw new DatabaseException(e);
156 + } catch (TimeoutException e) {
157 + throw new DatabaseException(e);
137 } 158 }
138 } 159 }
139 160
...@@ -141,9 +162,11 @@ public class DatabaseClient { ...@@ -141,9 +162,11 @@ public class DatabaseClient {
141 waitForLeader(); 162 waitForLeader();
142 CompletableFuture<Map<String, VersionedValue>> future = copycat.submit("getAll", tableName); 163 CompletableFuture<Map<String, VersionedValue>> future = copycat.submit("getAll", tableName);
143 try { 164 try {
144 - return future.get(); 165 + return future.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
145 } catch (InterruptedException | ExecutionException e) { 166 } catch (InterruptedException | ExecutionException e) {
146 throw new DatabaseException(e); 167 throw new DatabaseException(e);
168 + } catch (TimeoutException e) {
169 + throw new DatabaseException(e);
147 } 170 }
148 } 171 }
149 } 172 }
......