Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 1 | package org.onlab.onos.store.service.impl; |
| 2 | |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 3 | import static com.google.common.base.Preconditions.checkNotNull; |
Yuta HIGUCHI | 39da979 | 2014-11-14 02:07:04 -0800 | [diff] [blame] | 4 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 5 | |
Yuta HIGUCHI | b9d6866 | 2014-11-14 16:06:03 -0800 | [diff] [blame] | 6 | import java.util.Collections; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 7 | import java.util.List; |
Yuta HIGUCHI | 841c0b6 | 2014-11-13 20:27:14 -0800 | [diff] [blame] | 8 | import java.util.Map; |
Madan Jampani | f5d263b | 2014-11-13 10:04:40 -0800 | [diff] [blame] | 9 | import java.util.Set; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 10 | import java.util.concurrent.CompletableFuture; |
Yuta HIGUCHI | 39da979 | 2014-11-14 02:07:04 -0800 | [diff] [blame] | 11 | import java.util.concurrent.CountDownLatch; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 12 | import java.util.concurrent.ExecutionException; |
Yuta HIGUCHI | 39da979 | 2014-11-14 02:07:04 -0800 | [diff] [blame] | 13 | import java.util.concurrent.TimeUnit; |
Yuta HIGUCHI | b9d6866 | 2014-11-14 16:06:03 -0800 | [diff] [blame] | 14 | import java.util.concurrent.TimeoutException; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 15 | |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 16 | import net.kuujo.copycat.Copycat; |
Yuta HIGUCHI | 39da979 | 2014-11-14 02:07:04 -0800 | [diff] [blame] | 17 | import net.kuujo.copycat.event.EventHandler; |
| 18 | import net.kuujo.copycat.event.LeaderElectEvent; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 19 | |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 20 | import org.onlab.onos.store.service.BatchReadRequest; |
| 21 | import org.onlab.onos.store.service.BatchWriteRequest; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 22 | import org.onlab.onos.store.service.DatabaseException; |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 23 | import org.onlab.onos.store.service.ReadResult; |
Yuta HIGUCHI | 841c0b6 | 2014-11-13 20:27:14 -0800 | [diff] [blame] | 24 | import org.onlab.onos.store.service.VersionedValue; |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 25 | import org.onlab.onos.store.service.WriteResult; |
Yuta HIGUCHI | 39da979 | 2014-11-14 02:07:04 -0800 | [diff] [blame] | 26 | import org.slf4j.Logger; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 27 | |
Madan Jampani | 686fa18 | 2014-11-04 23:16:27 -0800 | [diff] [blame] | 28 | /** |
| 29 | * Client for interacting with the Copycat Raft cluster. |
| 30 | */ |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 31 | public class DatabaseClient { |
| 32 | |
Yuta HIGUCHI | b9d6866 | 2014-11-14 16:06:03 -0800 | [diff] [blame] | 33 | private static final int RETRIES = 5; |
| 34 | |
| 35 | private static final int TIMEOUT_MS = 2000; |
| 36 | |
Yuta HIGUCHI | 39da979 | 2014-11-14 02:07:04 -0800 | [diff] [blame] | 37 | private final Logger log = getLogger(getClass()); |
| 38 | |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 39 | private final Copycat copycat; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 40 | |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 41 | public DatabaseClient(Copycat copycat) { |
| 42 | this.copycat = checkNotNull(copycat); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Yuta HIGUCHI | 39da979 | 2014-11-14 02:07:04 -0800 | [diff] [blame] | 45 | public void waitForLeader() { |
| 46 | if (copycat.leader() != null) { |
| 47 | return; |
| 48 | } |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 49 | |
Yuta HIGUCHI | 39da979 | 2014-11-14 02:07:04 -0800 | [diff] [blame] | 50 | log.info("No leader in cluster, waiting for election."); |
| 51 | final CountDownLatch latch = new CountDownLatch(1); |
| 52 | final EventHandler<LeaderElectEvent> leaderLsnr = new EventHandler<LeaderElectEvent>() { |
| 53 | |
| 54 | @Override |
| 55 | public void handle(LeaderElectEvent event) { |
| 56 | log.info("Leader chosen: {}", event); |
| 57 | latch.countDown(); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | copycat.event(LeaderElectEvent.class).registerHandler(leaderLsnr); |
| 62 | try { |
| 63 | while (copycat.leader() == null) { |
| 64 | latch.await(200, TimeUnit.MILLISECONDS); |
| 65 | } |
| 66 | log.info("Leader appeared: {}", copycat.leader()); |
| 67 | return; |
| 68 | } catch (InterruptedException e) { |
| 69 | log.error("Interrupted while waiting for Leader", e); |
| 70 | Thread.currentThread().interrupt(); |
| 71 | } finally { |
| 72 | copycat.event(LeaderElectEvent.class).unregisterHandler(leaderLsnr); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public boolean createTable(String tableName) { |
| 77 | waitForLeader(); |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 78 | CompletableFuture<Boolean> future = copycat.submit("createTable", tableName); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 79 | try { |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 80 | return future.get(); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 81 | } catch (InterruptedException | ExecutionException e) { |
| 82 | throw new DatabaseException(e); |
| 83 | } |
| 84 | } |
| 85 | |
Madan Jampani | def2c65 | 2014-11-12 13:50:10 -0800 | [diff] [blame] | 86 | public boolean createTable(String tableName, int ttlMillis) { |
Yuta HIGUCHI | 39da979 | 2014-11-14 02:07:04 -0800 | [diff] [blame] | 87 | waitForLeader(); |
Madan Jampani | f5d263b | 2014-11-13 10:04:40 -0800 | [diff] [blame] | 88 | CompletableFuture<Boolean> future = copycat.submit("createTableWithExpiration", tableName); |
Madan Jampani | def2c65 | 2014-11-12 13:50:10 -0800 | [diff] [blame] | 89 | try { |
| 90 | return future.get(); |
| 91 | } catch (InterruptedException | ExecutionException e) { |
| 92 | throw new DatabaseException(e); |
| 93 | } |
| 94 | } |
| 95 | |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 96 | public void dropTable(String tableName) { |
Yuta HIGUCHI | 39da979 | 2014-11-14 02:07:04 -0800 | [diff] [blame] | 97 | waitForLeader(); |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 98 | CompletableFuture<Void> future = copycat.submit("dropTable", tableName); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 99 | try { |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 100 | future.get(); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 101 | } catch (InterruptedException | ExecutionException e) { |
| 102 | throw new DatabaseException(e); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | public void dropAllTables() { |
Yuta HIGUCHI | 39da979 | 2014-11-14 02:07:04 -0800 | [diff] [blame] | 107 | waitForLeader(); |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 108 | CompletableFuture<Void> future = copycat.submit("dropAllTables"); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 109 | try { |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 110 | future.get(); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 111 | } catch (InterruptedException | ExecutionException e) { |
| 112 | throw new DatabaseException(e); |
| 113 | } |
| 114 | } |
| 115 | |
Madan Jampani | f5d263b | 2014-11-13 10:04:40 -0800 | [diff] [blame] | 116 | public Set<String> listTables() { |
Yuta HIGUCHI | 39da979 | 2014-11-14 02:07:04 -0800 | [diff] [blame] | 117 | waitForLeader(); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 118 | try { |
Yuta HIGUCHI | b9d6866 | 2014-11-14 16:06:03 -0800 | [diff] [blame] | 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(); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 132 | } catch (InterruptedException | ExecutionException e) { |
| 133 | throw new DatabaseException(e); |
| 134 | } |
| 135 | } |
| 136 | |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 137 | public List<ReadResult> batchRead(BatchReadRequest batchRequest) { |
Yuta HIGUCHI | 39da979 | 2014-11-14 02:07:04 -0800 | [diff] [blame] | 138 | waitForLeader(); |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 139 | CompletableFuture<List<ReadResult>> future = copycat.submit("read", batchRequest); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 140 | try { |
Yuta HIGUCHI | b9d6866 | 2014-11-14 16:06:03 -0800 | [diff] [blame] | 141 | return future.get(TIMEOUT_MS, TimeUnit.MILLISECONDS); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 142 | } catch (InterruptedException | ExecutionException e) { |
| 143 | throw new DatabaseException(e); |
Yuta HIGUCHI | b9d6866 | 2014-11-14 16:06:03 -0800 | [diff] [blame] | 144 | } catch (TimeoutException e) { |
| 145 | throw new DatabaseException(e); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 149 | public List<WriteResult> batchWrite(BatchWriteRequest batchRequest) { |
Yuta HIGUCHI | 39da979 | 2014-11-14 02:07:04 -0800 | [diff] [blame] | 150 | waitForLeader(); |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 151 | CompletableFuture<List<WriteResult>> future = copycat.submit("write", batchRequest); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 152 | try { |
Yuta HIGUCHI | b9d6866 | 2014-11-14 16:06:03 -0800 | [diff] [blame] | 153 | return future.get(TIMEOUT_MS, TimeUnit.MILLISECONDS); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 154 | } catch (InterruptedException | ExecutionException e) { |
| 155 | throw new DatabaseException(e); |
Yuta HIGUCHI | b9d6866 | 2014-11-14 16:06:03 -0800 | [diff] [blame] | 156 | } catch (TimeoutException e) { |
| 157 | throw new DatabaseException(e); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 158 | } |
| 159 | } |
Yuta HIGUCHI | 841c0b6 | 2014-11-13 20:27:14 -0800 | [diff] [blame] | 160 | |
| 161 | public Map<String, VersionedValue> getAll(String tableName) { |
Yuta HIGUCHI | 39da979 | 2014-11-14 02:07:04 -0800 | [diff] [blame] | 162 | waitForLeader(); |
Yuta HIGUCHI | 841c0b6 | 2014-11-13 20:27:14 -0800 | [diff] [blame] | 163 | CompletableFuture<Map<String, VersionedValue>> future = copycat.submit("getAll", tableName); |
| 164 | try { |
Yuta HIGUCHI | b9d6866 | 2014-11-14 16:06:03 -0800 | [diff] [blame] | 165 | return future.get(TIMEOUT_MS, TimeUnit.MILLISECONDS); |
Yuta HIGUCHI | 841c0b6 | 2014-11-13 20:27:14 -0800 | [diff] [blame] | 166 | } catch (InterruptedException | ExecutionException e) { |
| 167 | throw new DatabaseException(e); |
Yuta HIGUCHI | b9d6866 | 2014-11-14 16:06:03 -0800 | [diff] [blame] | 168 | } catch (TimeoutException e) { |
| 169 | throw new DatabaseException(e); |
Yuta HIGUCHI | 841c0b6 | 2014-11-13 20:27:14 -0800 | [diff] [blame] | 170 | } |
| 171 | } |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 172 | } |