Madan Jampani
Committed by Gerrit Code Review

Fix how default partitions are created

Change-Id: Icb4881fb87b982d88502a0201175dbc2bc209c9c
......@@ -13,17 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.cluster.impl;
package org.onosproject.cluster;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Collection;
import java.util.Objects;
import org.onosproject.cluster.NodeId;
import org.onosproject.cluster.Partition;
import org.onosproject.cluster.PartitionId;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
......
......@@ -34,6 +34,7 @@ import org.onosproject.cluster.ClusterService;
import org.onosproject.cluster.ClusterStore;
import org.onosproject.cluster.ClusterStoreDelegate;
import org.onosproject.cluster.ControllerNode;
import org.onosproject.cluster.DefaultPartition;
import org.onosproject.cluster.NodeId;
import org.onosproject.cluster.Partition;
import org.onosproject.cluster.PartitionId;
......@@ -179,35 +180,18 @@ public class ClusterManager
Collections.sort(sorted, (o1, o2) -> o1.id().toString().compareTo(o2.id().toString()));
Collection<Partition> partitions = Lists.newArrayList();
// add p0 partition
partitions.add(new Partition() {
@Override
public PartitionId getId() {
return PartitionId.from((0));
}
@Override
public Collection<NodeId> getMembers() {
return Sets.newHashSet(Collections2.transform(nodes, ControllerNode::id));
}
});
partitions.add(new DefaultPartition(PartitionId.from(0),
Sets.newHashSet(Collections2.transform(nodes, ControllerNode::id))));
// add extended partitions
int length = nodes.size();
int count = 3;
int count = Math.min(3, length);
for (int i = 0; i < length; i++) {
int index = i;
Set<NodeId> set = new HashSet<>(count);
for (int j = 0; j < count; j++) {
set.add(sorted.get((i + j) % length).id());
}
partitions.add(new Partition() {
@Override
public PartitionId getId() {
return PartitionId.from((index + 1));
}
@Override
public Collection<NodeId> getMembers() {
return set;
}
});
partitions.add(new DefaultPartition(PartitionId.from((index + 1)), set));
}
return partitions;
}
......
......@@ -41,6 +41,7 @@ import org.onosproject.cluster.ClusterMetadataStore;
import org.onosproject.cluster.ClusterMetadataStoreDelegate;
import org.onosproject.cluster.ControllerNode;
import org.onosproject.cluster.DefaultControllerNode;
import org.onosproject.cluster.DefaultPartition;
import org.onosproject.cluster.NodeId;
import org.onosproject.cluster.Partition;
import org.onosproject.cluster.PartitionId;
......@@ -94,6 +95,8 @@ public class StaticClusterMetadataStore
module.addSerializer(ControllerNode.class, new ControllerNodeSerializer());
module.addDeserializer(ControllerNode.class, new ControllerNodeDeserializer());
module.addDeserializer(Partition.class, new PartitionDeserializer());
module.addSerializer(PartitionId.class, new PartitionIdSerializer());
module.addDeserializer(PartitionId.class, new PartitionIdDeserializer());
mapper.registerModule(module);
File metadataFile = new File(CLUSTER_METADATA_FILE);
if (metadataFile.exists()) {
......@@ -107,21 +110,14 @@ public class StaticClusterMetadataStore
String localIp = getSiteLocalAddress();
ControllerNode localNode =
new DefaultControllerNode(new NodeId(localIp), IpAddress.valueOf(localIp), DEFAULT_ONOS_PORT);
Partition defaultPartition = new Partition() {
@Override
public PartitionId getId() {
return PartitionId.from(1);
}
@Override
public Collection<NodeId> getMembers() {
return Sets.newHashSet(localNode.id());
}
};
// p0 partition
Partition basePartition = new DefaultPartition(PartitionId.from(0), Sets.newHashSet(localNode.id()));
// p1 partition
Partition extendedPartition = new DefaultPartition(PartitionId.from(1), Sets.newHashSet(localNode.id()));
metadata.set(ClusterMetadata.builder()
.withName("default")
.withControllerNodes(Arrays.asList(localNode))
.withPartitions(Lists.newArrayList(defaultPartition))
.withPartitions(Lists.newArrayList(basePartition, extendedPartition))
.build());
version = System.currentTimeMillis();
}
......@@ -194,6 +190,23 @@ public class StaticClusterMetadataStore
}
}
private static class PartitionIdSerializer extends JsonSerializer<PartitionId> {
@Override
public void serialize(PartitionId partitionId, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeNumber(partitionId.asInt());
}
}
private class PartitionIdDeserializer extends JsonDeserializer<PartitionId> {
@Override
public PartitionId deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
return new PartitionId(node.asInt());
}
}
private static class ControllerNodeSerializer extends JsonSerializer<ControllerNode> {
@Override
public void serialize(ControllerNode node, JsonGenerator jgen, SerializerProvider provider)
......
......@@ -36,6 +36,7 @@ import org.onosproject.cluster.ClusterMetadataEventListener;
import org.onosproject.cluster.ClusterMetadataService;
import org.onosproject.cluster.ControllerNode;
import org.onosproject.cluster.DefaultControllerNode;
import org.onosproject.cluster.DefaultPartition;
import org.onosproject.cluster.NodeId;
import org.onosproject.cluster.Partition;
import org.onosproject.cluster.PartitionId;
......@@ -79,7 +80,6 @@ import org.onosproject.net.provider.AbstractProviderService;
import org.onosproject.net.provider.ProviderId;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
......@@ -953,16 +953,7 @@ public class LldpLinkProviderTest {
public ClusterMetadata getClusterMetadata() {
final NodeId nid = new NodeId("test-node");
final IpAddress addr = IpAddress.valueOf(0);
final Partition p = new Partition() {
public PartitionId getId() {
return PartitionId.from(1);
}
@Override
public Collection<NodeId> getMembers() {
return Sets.newHashSet(nid);
}
};
final Partition p = new DefaultPartition(PartitionId.from(1), Sets.newHashSet(nid));
return ClusterMetadata.builder()
.withName("test-cluster")
.withControllerNodes(Sets.newHashSet(new DefaultControllerNode(nid, addr)))
......