Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -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 | package org.onosproject.store.consistent.impl; |
| 17 | |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableMap; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import org.onosproject.store.cluster.impl.NodeInfo; |
| 22 | |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.Collections; |
| 25 | import java.util.HashSet; |
| 26 | import java.util.List; |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 27 | import java.util.Map; |
| 28 | import java.util.Set; |
| 29 | |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 32 | /** |
| 33 | * Partitioned database configuration. |
| 34 | */ |
| 35 | public class DatabaseDefinition { |
| 36 | private Map<String, Set<NodeInfo>> partitions; |
| 37 | private Set<NodeInfo> nodes; |
| 38 | |
| 39 | /** |
| 40 | * Creates a new DatabaseDefinition. |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 41 | * |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 42 | * @param partitions partition map |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 43 | * @param nodes set of nodes |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 44 | * @return database definition |
| 45 | */ |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 46 | public static DatabaseDefinition from(Map<String, Set<NodeInfo>> partitions, |
| 47 | Set<NodeInfo> nodes) { |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 48 | checkNotNull(partitions); |
| 49 | checkNotNull(nodes); |
| 50 | DatabaseDefinition definition = new DatabaseDefinition(); |
| 51 | definition.partitions = ImmutableMap.copyOf(partitions); |
| 52 | definition.nodes = ImmutableSet.copyOf(nodes); |
| 53 | return definition; |
| 54 | } |
| 55 | |
| 56 | /** |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 57 | * Creates a new DatabaseDefinition using default partitions. |
| 58 | * |
| 59 | * @param nodes set of nodes |
| 60 | * @return database definition |
| 61 | */ |
| 62 | public static DatabaseDefinition from(Set<NodeInfo> nodes) { |
| 63 | return from(generateDefaultPartitions(nodes), nodes); |
| 64 | } |
| 65 | |
| 66 | /** |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 67 | * Returns the map of database partitions. |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 68 | * |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 69 | * @return db partition map |
| 70 | */ |
| 71 | public Map<String, Set<NodeInfo>> getPartitions() { |
| 72 | return partitions; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the set of nodes. |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 77 | * |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 78 | * @return nodes |
| 79 | */ |
| 80 | public Set<NodeInfo> getNodes() { |
| 81 | return nodes; |
| 82 | } |
Thomas Vachuska | de563cf | 2015-04-01 00:28:50 -0700 | [diff] [blame] | 83 | |
| 84 | |
| 85 | /** |
| 86 | * Generates set of default partitions using permutations of the nodes. |
| 87 | * |
| 88 | * @param nodes information about cluster nodes |
| 89 | * @return default partition map |
| 90 | */ |
| 91 | private static Map<String, Set<NodeInfo>> generateDefaultPartitions(Set<NodeInfo> nodes) { |
| 92 | List<NodeInfo> sorted = new ArrayList<>(nodes); |
| 93 | Collections.sort(sorted, (o1, o2) -> o1.getId().compareTo(o2.getId())); |
| 94 | Map<String, Set<NodeInfo>> partitions = Maps.newHashMap(); |
| 95 | |
| 96 | int length = nodes.size(); |
| 97 | int count = 3; |
| 98 | for (int i = 0; i < length; i++) { |
| 99 | Set<NodeInfo> set = new HashSet<>(count); |
| 100 | for (int j = 0; j < count; j++) { |
| 101 | set.add(sorted.get((i + j) % length)); |
| 102 | } |
| 103 | partitions.put("p" + (i + 1), set); |
| 104 | } |
| 105 | return partitions; |
| 106 | } |
| 107 | |
Madan Jampani | 0cb0067 | 2015-02-27 00:27:22 -0800 | [diff] [blame] | 108 | } |