blob: 3ebce09bf7e4434fbe8b5c8625e839fcd24e68fe [file] [log] [blame]
Madan Jampani25461112015-02-17 14:17:29 -08001/*
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 Jampani94c23532015-02-05 17:40:01 -080017package org.onosproject.store.consistent.impl;
18
19import java.util.Map;
20import java.util.concurrent.CompletableFuture;
21import java.util.concurrent.Executors;
22
23import net.kuujo.copycat.CopycatConfig;
24import net.kuujo.copycat.cluster.ClusterConfig;
25import net.kuujo.copycat.cluster.internal.coordinator.ClusterCoordinator;
26import net.kuujo.copycat.cluster.internal.coordinator.DefaultClusterCoordinator;
27import net.kuujo.copycat.util.concurrent.NamedThreadFactory;
28
Madan Jampani09342702015-02-05 23:32:40 -080029/**
30 * Manages a PartitionedDatabase.
31 */
Madan Jampani94c23532015-02-05 17:40:01 -080032public 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 Jampani393e0f02015-02-12 07:35:39 +053086 .withDefaultSerializer(new DatabaseSerializer())
Madan Jampani94c23532015-02-05 17:40:01 -080087 .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 Jampani393e0f02015-02-12 07:35:39 +053093 .withSerializer(copycatConfig.getDefaultSerializer())
Madan Jampani94c23532015-02-05 17:40:01 -080094 .withDefaultExecutor(copycatConfig.getDefaultExecutor()))));
95 partitionedDatabase.setPartitioner(
Madan Jampani09342702015-02-05 23:32:40 -080096 new SimpleKeyHashPartitioner(partitionedDatabase.getRegisteredPartitions()));
Madan Jampani94c23532015-02-05 17:40:01 -080097 return partitionedDatabase;
98 }
99}