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.Map; |
| 20 | import java.util.concurrent.CompletableFuture; |
| 21 | import java.util.concurrent.Executors; |
| 22 | |
| 23 | import net.kuujo.copycat.CopycatConfig; |
| 24 | import net.kuujo.copycat.cluster.ClusterConfig; |
| 25 | import net.kuujo.copycat.cluster.internal.coordinator.ClusterCoordinator; |
| 26 | import net.kuujo.copycat.cluster.internal.coordinator.DefaultClusterCoordinator; |
| 27 | import net.kuujo.copycat.util.concurrent.NamedThreadFactory; |
| 28 | |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 29 | /** |
| 30 | * Manages a PartitionedDatabase. |
| 31 | */ |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 32 | public interface PartitionedDatabaseManager { |
| 33 | /** |
| 34 | * Opens the database. |
| 35 | * |
| 36 | * @return A completable future to be completed with the result once complete. |
| 37 | */ |
| 38 | CompletableFuture<PartitionedDatabase> open(); |
| 39 | |
| 40 | /** |
| 41 | * Closes the database. |
| 42 | * |
| 43 | * @return A completable future to be completed with the result once complete. |
| 44 | */ |
| 45 | CompletableFuture<Void> close(); |
| 46 | |
| 47 | /** |
| 48 | * Sets the partitioner to use for mapping keys to partitions. |
| 49 | * |
| 50 | * @param partitioner partitioner |
| 51 | */ |
| 52 | void setPartitioner(Partitioner<String> partitioner); |
| 53 | |
| 54 | /** |
| 55 | * Registers a new partition. |
| 56 | * |
| 57 | * @param partitionName partition name. |
| 58 | * @param partition partition. |
| 59 | */ |
| 60 | void registerPartition(String partitionName, Database partition); |
| 61 | |
| 62 | /** |
| 63 | * Returns all the registered database partitions. |
| 64 | * |
| 65 | * @return mapping of all registered database partitions. |
| 66 | */ |
| 67 | Map<String, Database> getRegisteredPartitions(); |
| 68 | |
| 69 | |
| 70 | /** |
| 71 | * Creates a new partitioned database. |
| 72 | * |
| 73 | * @param name The database name. |
| 74 | * @param clusterConfig The cluster configuration. |
| 75 | * @param partitionedDatabaseConfig The database configuration. |
| 76 | |
| 77 | * @return The database. |
| 78 | */ |
| 79 | public static PartitionedDatabase create( |
| 80 | String name, |
| 81 | ClusterConfig clusterConfig, |
| 82 | PartitionedDatabaseConfig partitionedDatabaseConfig) { |
| 83 | CopycatConfig copycatConfig = new CopycatConfig() |
| 84 | .withName(name) |
| 85 | .withClusterConfig(clusterConfig) |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 86 | .withDefaultSerializer(new DatabaseSerializer()) |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 87 | .withDefaultExecutor(Executors.newSingleThreadExecutor(new NamedThreadFactory("copycat-coordinator-%d"))); |
| 88 | ClusterCoordinator coordinator = new DefaultClusterCoordinator(copycatConfig.resolve()); |
| 89 | PartitionedDatabase partitionedDatabase = new PartitionedDatabase(coordinator); |
| 90 | partitionedDatabaseConfig.partitions().forEach((partitionName, partitionConfig) -> |
| 91 | partitionedDatabase.registerPartition(partitionName , |
| 92 | coordinator.getResource(partitionName, partitionConfig.resolve(clusterConfig) |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 93 | .withSerializer(copycatConfig.getDefaultSerializer()) |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 94 | .withDefaultExecutor(copycatConfig.getDefaultExecutor())))); |
| 95 | partitionedDatabase.setPartitioner( |
Madan Jampani | 0934270 | 2015-02-05 23:32:40 -0800 | [diff] [blame] | 96 | new SimpleKeyHashPartitioner(partitionedDatabase.getRegisteredPartitions())); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 97 | return partitionedDatabase; |
| 98 | } |
| 99 | } |