Madan Jampani
Committed by Gerrit Code Review

Fix how default partitions are created

Change-Id: Icb4881fb87b982d88502a0201175dbc2bc209c9c
...@@ -13,17 +13,13 @@ ...@@ -13,17 +13,13 @@
13 * See the License for the specific language governing permissions and 13 * See the License for the specific language governing permissions and
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 -package org.onosproject.store.cluster.impl; 16 +package org.onosproject.cluster;
17 17
18 import static com.google.common.base.Preconditions.checkNotNull; 18 import static com.google.common.base.Preconditions.checkNotNull;
19 19
20 import java.util.Collection; 20 import java.util.Collection;
21 import java.util.Objects; 21 import java.util.Objects;
22 22
23 -import org.onosproject.cluster.NodeId;
24 -import org.onosproject.cluster.Partition;
25 -import org.onosproject.cluster.PartitionId;
26 -
27 import com.google.common.base.MoreObjects; 23 import com.google.common.base.MoreObjects;
28 import com.google.common.collect.ImmutableSet; 24 import com.google.common.collect.ImmutableSet;
29 import com.google.common.collect.Sets; 25 import com.google.common.collect.Sets;
......
...@@ -34,6 +34,7 @@ import org.onosproject.cluster.ClusterService; ...@@ -34,6 +34,7 @@ import org.onosproject.cluster.ClusterService;
34 import org.onosproject.cluster.ClusterStore; 34 import org.onosproject.cluster.ClusterStore;
35 import org.onosproject.cluster.ClusterStoreDelegate; 35 import org.onosproject.cluster.ClusterStoreDelegate;
36 import org.onosproject.cluster.ControllerNode; 36 import org.onosproject.cluster.ControllerNode;
37 +import org.onosproject.cluster.DefaultPartition;
37 import org.onosproject.cluster.NodeId; 38 import org.onosproject.cluster.NodeId;
38 import org.onosproject.cluster.Partition; 39 import org.onosproject.cluster.Partition;
39 import org.onosproject.cluster.PartitionId; 40 import org.onosproject.cluster.PartitionId;
...@@ -179,35 +180,18 @@ public class ClusterManager ...@@ -179,35 +180,18 @@ public class ClusterManager
179 Collections.sort(sorted, (o1, o2) -> o1.id().toString().compareTo(o2.id().toString())); 180 Collections.sort(sorted, (o1, o2) -> o1.id().toString().compareTo(o2.id().toString()));
180 Collection<Partition> partitions = Lists.newArrayList(); 181 Collection<Partition> partitions = Lists.newArrayList();
181 // add p0 partition 182 // add p0 partition
182 - partitions.add(new Partition() { 183 + partitions.add(new DefaultPartition(PartitionId.from(0),
183 - @Override 184 + Sets.newHashSet(Collections2.transform(nodes, ControllerNode::id))));
184 - public PartitionId getId() {
185 - return PartitionId.from((0));
186 - }
187 - @Override
188 - public Collection<NodeId> getMembers() {
189 - return Sets.newHashSet(Collections2.transform(nodes, ControllerNode::id));
190 - }
191 - });
192 // add extended partitions 185 // add extended partitions
193 int length = nodes.size(); 186 int length = nodes.size();
194 - int count = 3; 187 + int count = Math.min(3, length);
195 for (int i = 0; i < length; i++) { 188 for (int i = 0; i < length; i++) {
196 int index = i; 189 int index = i;
197 Set<NodeId> set = new HashSet<>(count); 190 Set<NodeId> set = new HashSet<>(count);
198 for (int j = 0; j < count; j++) { 191 for (int j = 0; j < count; j++) {
199 set.add(sorted.get((i + j) % length).id()); 192 set.add(sorted.get((i + j) % length).id());
200 } 193 }
201 - partitions.add(new Partition() { 194 + partitions.add(new DefaultPartition(PartitionId.from((index + 1)), set));
202 - @Override
203 - public PartitionId getId() {
204 - return PartitionId.from((index + 1));
205 - }
206 - @Override
207 - public Collection<NodeId> getMembers() {
208 - return set;
209 - }
210 - });
211 } 195 }
212 return partitions; 196 return partitions;
213 } 197 }
......
...@@ -41,6 +41,7 @@ import org.onosproject.cluster.ClusterMetadataStore; ...@@ -41,6 +41,7 @@ import org.onosproject.cluster.ClusterMetadataStore;
41 import org.onosproject.cluster.ClusterMetadataStoreDelegate; 41 import org.onosproject.cluster.ClusterMetadataStoreDelegate;
42 import org.onosproject.cluster.ControllerNode; 42 import org.onosproject.cluster.ControllerNode;
43 import org.onosproject.cluster.DefaultControllerNode; 43 import org.onosproject.cluster.DefaultControllerNode;
44 +import org.onosproject.cluster.DefaultPartition;
44 import org.onosproject.cluster.NodeId; 45 import org.onosproject.cluster.NodeId;
45 import org.onosproject.cluster.Partition; 46 import org.onosproject.cluster.Partition;
46 import org.onosproject.cluster.PartitionId; 47 import org.onosproject.cluster.PartitionId;
...@@ -94,6 +95,8 @@ public class StaticClusterMetadataStore ...@@ -94,6 +95,8 @@ public class StaticClusterMetadataStore
94 module.addSerializer(ControllerNode.class, new ControllerNodeSerializer()); 95 module.addSerializer(ControllerNode.class, new ControllerNodeSerializer());
95 module.addDeserializer(ControllerNode.class, new ControllerNodeDeserializer()); 96 module.addDeserializer(ControllerNode.class, new ControllerNodeDeserializer());
96 module.addDeserializer(Partition.class, new PartitionDeserializer()); 97 module.addDeserializer(Partition.class, new PartitionDeserializer());
98 + module.addSerializer(PartitionId.class, new PartitionIdSerializer());
99 + module.addDeserializer(PartitionId.class, new PartitionIdDeserializer());
97 mapper.registerModule(module); 100 mapper.registerModule(module);
98 File metadataFile = new File(CLUSTER_METADATA_FILE); 101 File metadataFile = new File(CLUSTER_METADATA_FILE);
99 if (metadataFile.exists()) { 102 if (metadataFile.exists()) {
...@@ -107,21 +110,14 @@ public class StaticClusterMetadataStore ...@@ -107,21 +110,14 @@ public class StaticClusterMetadataStore
107 String localIp = getSiteLocalAddress(); 110 String localIp = getSiteLocalAddress();
108 ControllerNode localNode = 111 ControllerNode localNode =
109 new DefaultControllerNode(new NodeId(localIp), IpAddress.valueOf(localIp), DEFAULT_ONOS_PORT); 112 new DefaultControllerNode(new NodeId(localIp), IpAddress.valueOf(localIp), DEFAULT_ONOS_PORT);
110 - Partition defaultPartition = new Partition() { 113 + // p0 partition
111 - @Override 114 + Partition basePartition = new DefaultPartition(PartitionId.from(0), Sets.newHashSet(localNode.id()));
112 - public PartitionId getId() { 115 + // p1 partition
113 - return PartitionId.from(1); 116 + Partition extendedPartition = new DefaultPartition(PartitionId.from(1), Sets.newHashSet(localNode.id()));
114 - }
115 -
116 - @Override
117 - public Collection<NodeId> getMembers() {
118 - return Sets.newHashSet(localNode.id());
119 - }
120 - };
121 metadata.set(ClusterMetadata.builder() 117 metadata.set(ClusterMetadata.builder()
122 .withName("default") 118 .withName("default")
123 .withControllerNodes(Arrays.asList(localNode)) 119 .withControllerNodes(Arrays.asList(localNode))
124 - .withPartitions(Lists.newArrayList(defaultPartition)) 120 + .withPartitions(Lists.newArrayList(basePartition, extendedPartition))
125 .build()); 121 .build());
126 version = System.currentTimeMillis(); 122 version = System.currentTimeMillis();
127 } 123 }
...@@ -194,6 +190,23 @@ public class StaticClusterMetadataStore ...@@ -194,6 +190,23 @@ public class StaticClusterMetadataStore
194 } 190 }
195 } 191 }
196 192
193 + private static class PartitionIdSerializer extends JsonSerializer<PartitionId> {
194 + @Override
195 + public void serialize(PartitionId partitionId, JsonGenerator jgen, SerializerProvider provider)
196 + throws IOException, JsonProcessingException {
197 + jgen.writeNumber(partitionId.asInt());
198 + }
199 + }
200 +
201 + private class PartitionIdDeserializer extends JsonDeserializer<PartitionId> {
202 + @Override
203 + public PartitionId deserialize(JsonParser jp, DeserializationContext ctxt)
204 + throws IOException, JsonProcessingException {
205 + JsonNode node = jp.getCodec().readTree(jp);
206 + return new PartitionId(node.asInt());
207 + }
208 + }
209 +
197 private static class ControllerNodeSerializer extends JsonSerializer<ControllerNode> { 210 private static class ControllerNodeSerializer extends JsonSerializer<ControllerNode> {
198 @Override 211 @Override
199 public void serialize(ControllerNode node, JsonGenerator jgen, SerializerProvider provider) 212 public void serialize(ControllerNode node, JsonGenerator jgen, SerializerProvider provider)
......
...@@ -36,6 +36,7 @@ import org.onosproject.cluster.ClusterMetadataEventListener; ...@@ -36,6 +36,7 @@ import org.onosproject.cluster.ClusterMetadataEventListener;
36 import org.onosproject.cluster.ClusterMetadataService; 36 import org.onosproject.cluster.ClusterMetadataService;
37 import org.onosproject.cluster.ControllerNode; 37 import org.onosproject.cluster.ControllerNode;
38 import org.onosproject.cluster.DefaultControllerNode; 38 import org.onosproject.cluster.DefaultControllerNode;
39 +import org.onosproject.cluster.DefaultPartition;
39 import org.onosproject.cluster.NodeId; 40 import org.onosproject.cluster.NodeId;
40 import org.onosproject.cluster.Partition; 41 import org.onosproject.cluster.Partition;
41 import org.onosproject.cluster.PartitionId; 42 import org.onosproject.cluster.PartitionId;
...@@ -79,7 +80,6 @@ import org.onosproject.net.provider.AbstractProviderService; ...@@ -79,7 +80,6 @@ import org.onosproject.net.provider.AbstractProviderService;
79 import org.onosproject.net.provider.ProviderId; 80 import org.onosproject.net.provider.ProviderId;
80 81
81 import java.nio.ByteBuffer; 82 import java.nio.ByteBuffer;
82 -import java.util.Collection;
83 import java.util.HashMap; 83 import java.util.HashMap;
84 import java.util.HashSet; 84 import java.util.HashSet;
85 import java.util.List; 85 import java.util.List;
...@@ -953,16 +953,7 @@ public class LldpLinkProviderTest { ...@@ -953,16 +953,7 @@ public class LldpLinkProviderTest {
953 public ClusterMetadata getClusterMetadata() { 953 public ClusterMetadata getClusterMetadata() {
954 final NodeId nid = new NodeId("test-node"); 954 final NodeId nid = new NodeId("test-node");
955 final IpAddress addr = IpAddress.valueOf(0); 955 final IpAddress addr = IpAddress.valueOf(0);
956 - final Partition p = new Partition() { 956 + final Partition p = new DefaultPartition(PartitionId.from(1), Sets.newHashSet(nid));
957 - public PartitionId getId() {
958 - return PartitionId.from(1);
959 - }
960 -
961 - @Override
962 - public Collection<NodeId> getMembers() {
963 - return Sets.newHashSet(nid);
964 - }
965 - };
966 return ClusterMetadata.builder() 957 return ClusterMetadata.builder()
967 .withName("test-cluster") 958 .withName("test-cluster")
968 .withControllerNodes(Sets.newHashSet(new DefaultControllerNode(nid, addr))) 959 .withControllerNodes(Sets.newHashSet(new DefaultControllerNode(nid, addr)))
......