Madan Jampani | 2546111 | 2015-02-17 14:17:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 17 | package org.onosproject.store.consistent.impl; |
| 18 | |
| 19 | import java.util.Collection; |
| 20 | import java.util.List; |
| 21 | import java.util.Map; |
| 22 | import java.util.Map.Entry; |
| 23 | import java.util.Set; |
| 24 | import java.util.concurrent.CompletableFuture; |
| 25 | import java.util.concurrent.CopyOnWriteArrayList; |
| 26 | import java.util.concurrent.atomic.AtomicBoolean; |
| 27 | import java.util.concurrent.atomic.AtomicInteger; |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 28 | import java.util.stream.Collectors; |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 29 | |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 30 | import org.onosproject.cluster.NodeId; |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 31 | import org.onosproject.store.service.DatabaseUpdate; |
| 32 | import org.onosproject.store.service.Transaction; |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 33 | import org.onosproject.store.service.Versioned; |
| 34 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 35 | import com.google.common.collect.Lists; |
| 36 | import com.google.common.collect.Maps; |
| 37 | import com.google.common.collect.Sets; |
| 38 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 39 | import net.kuujo.copycat.Task; |
| 40 | import net.kuujo.copycat.cluster.Cluster; |
Madan Jampani | ed1b7fc | 2015-04-27 23:30:07 -0700 | [diff] [blame] | 41 | import net.kuujo.copycat.resource.ResourceState; |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 42 | import static com.google.common.base.Preconditions.checkState; |
| 43 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 44 | /** |
| 45 | * A database that partitions the keys across one or more database partitions. |
| 46 | */ |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 47 | public class PartitionedDatabase implements Database { |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 48 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 49 | private final String name; |
| 50 | private final Partitioner<String> partitioner; |
| 51 | private final List<Database> partitions; |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 52 | private final AtomicBoolean isOpen = new AtomicBoolean(false); |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 53 | private static final String DB_NOT_OPEN = "Partitioned Database is not open"; |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 54 | private TransactionManager transactionManager; |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 55 | |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 56 | public PartitionedDatabase( |
| 57 | String name, |
| 58 | Collection<Database> partitions) { |
| 59 | this.name = name; |
| 60 | this.partitions = partitions |
| 61 | .stream() |
| 62 | .sorted((db1, db2) -> db1.name().compareTo(db2.name())) |
| 63 | .collect(Collectors.toList()); |
| 64 | this.partitioner = new SimpleKeyHashPartitioner(this.partitions); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns the databases for individual partitions. |
| 69 | * @return list of database partitions |
| 70 | */ |
| 71 | public List<Database> getPartitions() { |
| 72 | return partitions; |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 75 | /** |
| 76 | * Returns true if the database is open. |
| 77 | * @return true if open, false otherwise |
| 78 | */ |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 79 | @Override |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 80 | public boolean isOpen() { |
| 81 | return isOpen.get(); |
| 82 | } |
| 83 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 84 | @Override |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 85 | public CompletableFuture<Set<String>> tableNames() { |
| 86 | checkState(isOpen.get(), DB_NOT_OPEN); |
| 87 | Set<String> tableNames = Sets.newConcurrentHashSet(); |
| 88 | return CompletableFuture.allOf(partitions |
| 89 | .stream() |
| 90 | .map(db -> db.tableNames().thenApply(tableNames::addAll)) |
| 91 | .toArray(CompletableFuture[]::new)) |
| 92 | .thenApply(v -> tableNames); |
| 93 | } |
| 94 | |
| 95 | @Override |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 96 | public CompletableFuture<Map<String, Long>> counters() { |
| 97 | checkState(isOpen.get(), DB_NOT_OPEN); |
| 98 | Map<String, Long> counters = Maps.newConcurrentMap(); |
| 99 | return CompletableFuture.allOf(partitions |
| 100 | .stream() |
| 101 | .map(db -> db.counters() |
| 102 | .thenApply(m -> { |
| 103 | counters.putAll(m); |
| 104 | return null; |
| 105 | })) |
| 106 | .toArray(CompletableFuture[]::new)) |
| 107 | .thenApply(v -> counters); |
| 108 | } |
| 109 | |
| 110 | @Override |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 111 | public CompletableFuture<Integer> size(String tableName) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 112 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 113 | AtomicInteger totalSize = new AtomicInteger(0); |
| 114 | return CompletableFuture.allOf(partitions |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 115 | .stream() |
| 116 | .map(p -> p.size(tableName).thenApply(totalSize::addAndGet)) |
| 117 | .toArray(CompletableFuture[]::new)) |
| 118 | .thenApply(v -> totalSize.get()); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public CompletableFuture<Boolean> isEmpty(String tableName) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 123 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 124 | return size(tableName).thenApply(size -> size == 0); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public CompletableFuture<Boolean> containsKey(String tableName, String key) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 129 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 130 | return partitioner.getPartition(tableName, key).containsKey(tableName, key); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public CompletableFuture<Boolean> containsValue(String tableName, byte[] value) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 135 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 136 | AtomicBoolean containsValue = new AtomicBoolean(false); |
| 137 | return CompletableFuture.allOf(partitions |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 138 | .stream() |
| 139 | .map(p -> p.containsValue(tableName, value).thenApply(v -> containsValue.compareAndSet(false, v))) |
| 140 | .toArray(CompletableFuture[]::new)) |
| 141 | .thenApply(v -> containsValue.get()); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public CompletableFuture<Versioned<byte[]>> get(String tableName, String key) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 146 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 147 | return partitioner.getPartition(tableName, key).get(tableName, key); |
| 148 | } |
| 149 | |
| 150 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 151 | public CompletableFuture<Result<Versioned<byte[]>>> put(String tableName, String key, byte[] value) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 152 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 153 | return partitioner.getPartition(tableName, key).put(tableName, key, value); |
| 154 | } |
| 155 | |
| 156 | @Override |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 157 | public CompletableFuture<Result<UpdateResult<Versioned<byte[]>>>> putAndGet(String tableName, |
| 158 | String key, |
| 159 | byte[] value) { |
| 160 | checkState(isOpen.get(), DB_NOT_OPEN); |
| 161 | return partitioner.getPartition(tableName, key).putAndGet(tableName, key, value); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public CompletableFuture<Result<UpdateResult<Versioned<byte[]>>>> putIfAbsentAndGet(String tableName, |
| 166 | String key, |
| 167 | byte[] value) { |
| 168 | checkState(isOpen.get(), DB_NOT_OPEN); |
| 169 | return partitioner.getPartition(tableName, key).putIfAbsentAndGet(tableName, key, value); |
| 170 | } |
| 171 | |
| 172 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 173 | public CompletableFuture<Result<Versioned<byte[]>>> remove(String tableName, String key) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 174 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 175 | return partitioner.getPartition(tableName, key).remove(tableName, key); |
| 176 | } |
| 177 | |
| 178 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 179 | public CompletableFuture<Result<Void>> clear(String tableName) { |
| 180 | AtomicBoolean isLocked = new AtomicBoolean(false); |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 181 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 182 | return CompletableFuture.allOf(partitions |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 183 | .stream() |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 184 | .map(p -> p.clear(tableName) |
| 185 | .thenApply(v -> isLocked.compareAndSet(false, Result.Status.LOCKED == v.status()))) |
| 186 | .toArray(CompletableFuture[]::new)) |
| 187 | .thenApply(v -> isLocked.get() ? Result.locked() : Result.ok(null)); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public CompletableFuture<Set<String>> keySet(String tableName) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 192 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 193 | Set<String> keySet = Sets.newConcurrentHashSet(); |
| 194 | return CompletableFuture.allOf(partitions |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 195 | .stream() |
| 196 | .map(p -> p.keySet(tableName).thenApply(keySet::addAll)) |
| 197 | .toArray(CompletableFuture[]::new)) |
| 198 | .thenApply(v -> keySet); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public CompletableFuture<Collection<Versioned<byte[]>>> values(String tableName) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 203 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 204 | List<Versioned<byte[]>> values = new CopyOnWriteArrayList<>(); |
| 205 | return CompletableFuture.allOf(partitions |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 206 | .stream() |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 207 | .map(p -> p.values(tableName).thenApply(values::addAll)) |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 208 | .toArray(CompletableFuture[]::new)) |
| 209 | .thenApply(v -> values); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public CompletableFuture<Set<Entry<String, Versioned<byte[]>>>> entrySet(String tableName) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 214 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 215 | Set<Entry<String, Versioned<byte[]>>> entrySet = Sets.newConcurrentHashSet(); |
| 216 | return CompletableFuture.allOf(partitions |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 217 | .stream() |
| 218 | .map(p -> p.entrySet(tableName).thenApply(entrySet::addAll)) |
| 219 | .toArray(CompletableFuture[]::new)) |
| 220 | .thenApply(v -> entrySet); |
| 221 | } |
| 222 | |
| 223 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 224 | public CompletableFuture<Result<Versioned<byte[]>>> putIfAbsent(String tableName, String key, byte[] value) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 225 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 226 | return partitioner.getPartition(tableName, key).putIfAbsent(tableName, key, value); |
| 227 | } |
| 228 | |
| 229 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 230 | public CompletableFuture<Result<Boolean>> remove(String tableName, String key, byte[] value) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 231 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 232 | return partitioner.getPartition(tableName, key).remove(tableName, key, value); |
| 233 | } |
| 234 | |
| 235 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 236 | public CompletableFuture<Result<Boolean>> remove(String tableName, String key, long version) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 237 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 238 | return partitioner.getPartition(tableName, key).remove(tableName, key, version); |
| 239 | } |
| 240 | |
| 241 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 242 | public CompletableFuture<Result<Boolean>> replace( |
| 243 | String tableName, String key, byte[] oldValue, byte[] newValue) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 244 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 245 | return partitioner.getPartition(tableName, key).replace(tableName, key, oldValue, newValue); |
| 246 | } |
| 247 | |
| 248 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 249 | public CompletableFuture<Result<Boolean>> replace( |
| 250 | String tableName, String key, long oldVersion, byte[] newValue) { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 251 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 252 | return partitioner.getPartition(tableName, key).replace(tableName, key, oldVersion, newValue); |
| 253 | } |
| 254 | |
| 255 | @Override |
Madan Jampani | 346d4f5 | 2015-05-04 11:09:39 -0700 | [diff] [blame] | 256 | public CompletableFuture<Result<UpdateResult<Versioned<byte[]>>>> replaceAndGet( |
| 257 | String tableName, String key, long oldVersion, byte[] newValue) { |
| 258 | checkState(isOpen.get(), DB_NOT_OPEN); |
| 259 | return partitioner.getPartition(tableName, key).replaceAndGet(tableName, key, oldVersion, newValue); |
| 260 | } |
| 261 | |
| 262 | @Override |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 263 | public CompletableFuture<Long> counterGet(String counterName) { |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 264 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 265 | return partitioner.getPartition(counterName, counterName).counterGet(counterName); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | @Override |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 269 | public CompletableFuture<Long> counterAddAndGet(String counterName, long delta) { |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 270 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 271 | return partitioner.getPartition(counterName, counterName).counterAddAndGet(counterName, delta); |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public CompletableFuture<Long> counterGetAndAdd(String counterName, long delta) { |
| 276 | checkState(isOpen.get(), DB_NOT_OPEN); |
| 277 | return partitioner.getPartition(counterName, counterName).counterGetAndAdd(counterName, delta); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Madan Jampani | 63c659f | 2015-06-11 00:52:58 -0700 | [diff] [blame] | 280 | |
| 281 | @Override |
| 282 | public CompletableFuture<Long> queueSize(String queueName) { |
| 283 | checkState(isOpen.get(), DB_NOT_OPEN); |
| 284 | return partitioner.getPartition(queueName, queueName).queueSize(queueName); |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public CompletableFuture<Set<NodeId>> queuePush(String queueName, byte[] entry) { |
| 289 | checkState(isOpen.get(), DB_NOT_OPEN); |
| 290 | return partitioner.getPartition(queueName, queueName).queuePush(queueName, entry); |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public CompletableFuture<byte[]> queuePop(String queueName, NodeId nodeId) { |
| 295 | checkState(isOpen.get(), DB_NOT_OPEN); |
| 296 | return partitioner.getPartition(queueName, queueName).queuePop(queueName, nodeId); |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public CompletableFuture<byte[]> queuePeek(String queueName) { |
| 301 | checkState(isOpen.get(), DB_NOT_OPEN); |
| 302 | return partitioner.getPartition(queueName, queueName).queuePeek(queueName); |
| 303 | } |
| 304 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 305 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 306 | public CompletableFuture<Boolean> prepareAndCommit(Transaction transaction) { |
| 307 | Map<Database, Transaction> subTransactions = createSubTransactions(transaction); |
| 308 | if (subTransactions.isEmpty()) { |
| 309 | return CompletableFuture.completedFuture(true); |
| 310 | } else if (subTransactions.size() == 1) { |
| 311 | Entry<Database, Transaction> entry = |
| 312 | subTransactions.entrySet().iterator().next(); |
| 313 | return entry.getKey().prepareAndCommit(entry.getValue()); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 314 | } else { |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 315 | if (transactionManager != null) { |
| 316 | throw new IllegalStateException("TransactionManager is not initialized"); |
| 317 | } |
| 318 | return transactionManager.execute(transaction); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | @Override |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 323 | public CompletableFuture<Boolean> prepare(Transaction transaction) { |
| 324 | Map<Database, Transaction> subTransactions = createSubTransactions(transaction); |
| 325 | AtomicBoolean status = new AtomicBoolean(true); |
| 326 | return CompletableFuture.allOf(subTransactions.entrySet() |
| 327 | .stream() |
| 328 | .map(entry -> entry |
| 329 | .getKey() |
| 330 | .prepare(entry.getValue()) |
| 331 | .thenApply(v -> status.compareAndSet(true, v))) |
| 332 | .toArray(CompletableFuture[]::new)) |
| 333 | .thenApply(v -> status.get()); |
| 334 | } |
| 335 | |
| 336 | @Override |
| 337 | public CompletableFuture<Boolean> commit(Transaction transaction) { |
| 338 | Map<Database, Transaction> subTransactions = createSubTransactions(transaction); |
| 339 | return CompletableFuture.allOf(subTransactions.entrySet() |
| 340 | .stream() |
| 341 | .map(entry -> entry.getKey().commit(entry.getValue())) |
| 342 | .toArray(CompletableFuture[]::new)) |
| 343 | .thenApply(v -> true); |
| 344 | } |
| 345 | |
| 346 | @Override |
| 347 | public CompletableFuture<Boolean> rollback(Transaction transaction) { |
| 348 | Map<Database, Transaction> subTransactions = createSubTransactions(transaction); |
| 349 | return CompletableFuture.allOf(subTransactions.entrySet() |
| 350 | .stream() |
| 351 | .map(entry -> entry.getKey().rollback(entry.getValue())) |
| 352 | .toArray(CompletableFuture[]::new)) |
| 353 | .thenApply(v -> true); |
| 354 | } |
| 355 | |
| 356 | @Override |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 357 | public CompletableFuture<Database> open() { |
| 358 | return CompletableFuture.allOf(partitions |
| 359 | .stream() |
| 360 | .map(Database::open) |
| 361 | .toArray(CompletableFuture[]::new)) |
| 362 | .thenApply(v -> { |
| 363 | isOpen.set(true); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 364 | return this; |
| 365 | }); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public CompletableFuture<Void> close() { |
Madan Jampani | 7f72c3f | 2015-03-01 17:34:59 -0800 | [diff] [blame] | 370 | checkState(isOpen.get(), DB_NOT_OPEN); |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 371 | return CompletableFuture.allOf(partitions |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 372 | .stream() |
| 373 | .map(database -> database.close()) |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 374 | .toArray(CompletableFuture[]::new)); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 375 | } |
Madan Jampani | f1b8e17 | 2015-03-23 11:42:02 -0700 | [diff] [blame] | 376 | |
| 377 | @Override |
| 378 | public boolean isClosed() { |
| 379 | return !isOpen.get(); |
| 380 | } |
| 381 | |
| 382 | @Override |
| 383 | public String name() { |
| 384 | return name; |
| 385 | } |
| 386 | |
| 387 | @Override |
| 388 | public Cluster cluster() { |
| 389 | throw new UnsupportedOperationException(); |
| 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public Database addStartupTask(Task<CompletableFuture<Void>> task) { |
| 394 | throw new UnsupportedOperationException(); |
| 395 | } |
| 396 | |
| 397 | @Override |
| 398 | public Database addShutdownTask(Task<CompletableFuture<Void>> task) { |
| 399 | throw new UnsupportedOperationException(); |
| 400 | } |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 401 | |
Madan Jampani | ed1b7fc | 2015-04-27 23:30:07 -0700 | [diff] [blame] | 402 | @Override |
| 403 | public ResourceState state() { |
| 404 | throw new UnsupportedOperationException(); |
| 405 | } |
| 406 | |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 407 | private Map<Database, Transaction> createSubTransactions( |
| 408 | Transaction transaction) { |
| 409 | Map<Database, List<DatabaseUpdate>> perPartitionUpdates = Maps.newHashMap(); |
| 410 | for (DatabaseUpdate update : transaction.updates()) { |
| 411 | Database partition = partitioner.getPartition(update.tableName(), update.key()); |
| 412 | List<DatabaseUpdate> partitionUpdates = |
| 413 | perPartitionUpdates.computeIfAbsent(partition, k -> Lists.newLinkedList()); |
| 414 | partitionUpdates.add(update); |
| 415 | } |
| 416 | Map<Database, Transaction> subTransactions = Maps.newHashMap(); |
| 417 | perPartitionUpdates.forEach((k, v) -> subTransactions.put(k, new DefaultTransaction(transaction.id(), v))); |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 418 | return subTransactions; |
| 419 | } |
Madan Jampani | 50589ac | 2015-06-08 11:38:46 -0700 | [diff] [blame] | 420 | |
| 421 | protected void setTransactionManager(TransactionManager tranasactionManager) { |
| 422 | this.transactionManager = transactionManager; |
| 423 | } |
Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 424 | } |