From c0c580090e60fc7a6545d27d45490f876e239e35 Mon Sep 17 00:00:00 2001 From: wangxuxin Date: Wed, 5 Jul 2023 16:35:39 +0800 Subject: [PATCH] feat: generate tree --- build.gradle | 2 +- .../bdware/sc/crdt/planning/PlanningTest.java | 34 +++++++--- .../planning/PlanningWith0Expansivity.java | 6 +- .../planning/PlanningWith1Expansivity.java | 6 +- .../planning/PlanningWithkExpansivity.java | 6 +- .../planning/SharableNetworkPlanning.java | 68 +++++++++++++++++++ 6 files changed, 107 insertions(+), 15 deletions(-) diff --git a/build.gradle b/build.gradle index e87c40e..afcf4d1 100644 --- a/build.gradle +++ b/build.gradle @@ -71,7 +71,7 @@ jar { // while develop at local use "false" configurations.runtimeClasspath.filter { it.getAbsolutePath().contains("/lib/") - false + // false }.collect { it.isDirectory() ? it : zipTree(it) } diff --git a/src/main/java/org/bdware/sc/crdt/planning/PlanningTest.java b/src/main/java/org/bdware/sc/crdt/planning/PlanningTest.java index b5c8b19..9d5c90e 100644 --- a/src/main/java/org/bdware/sc/crdt/planning/PlanningTest.java +++ b/src/main/java/org/bdware/sc/crdt/planning/PlanningTest.java @@ -3,30 +3,48 @@ package org.bdware.sc.crdt.planning; public class PlanningTest { public static void main(String[] args) { - int[] writers = new int[100]; - int[] readers = new int[100]; + final int nodeIdsCount = 1800; + final int writerCount = 1000; + final int readerCount = 1000; + String[] nodeIds = new String[nodeIdsCount]; + int[] writers = new int[writerCount]; + int[] readers = new int[readerCount]; + for (int i = 0; i < nodeIdsCount; ++i) { + nodeIds[i] = "node_" + i; + if (i < writerCount) { + writers[i] = i; + } + if (i >= nodeIdsCount - readerCount) { + readers[i - nodeIdsCount + readerCount] = i; + } + } long maxDelay = 60; - int bandwidthUpload = 10 * 1024 * 1024; - int bandwidthDownload = 10 * 1024 * 1024; - int datasize = 100 * 1024; + int bandwidthUpload = 100 * 1024 * 1024; + int bandwidthDownload = 100 * 1024 * 1024; + int datasize = 10 * 1024; double domainSize = 100 * datasize; - PlanningWith0Expansivity planning0 = new PlanningWith0Expansivity(writers, readers, maxDelay, bandwidthDownload, bandwidthUpload, datasize); - PlanningWithkExpansivity planningK = new PlanningWithkExpansivity(writers, readers, maxDelay, bandwidthDownload, bandwidthUpload, datasize, domainSize); - PlanningWith1Expansivity planning1 = new PlanningWith1Expansivity(writers, readers, maxDelay, bandwidthDownload, bandwidthUpload, datasize); + PlanningWith0Expansivity planning0 = new PlanningWith0Expansivity(nodeIds, writers, readers, maxDelay, bandwidthDownload, bandwidthUpload, datasize); + PlanningWithkExpansivity planningK = new PlanningWithkExpansivity(nodeIds, writers, readers, maxDelay, bandwidthDownload, bandwidthUpload, datasize, domainSize); + PlanningWith1Expansivity planning1 = new PlanningWith1Expansivity(nodeIds, writers, readers, maxDelay, bandwidthDownload, bandwidthUpload, datasize); long start = System.currentTimeMillis(); planning0.adjustAndCalc(); + planning0.allocate(); long end = System.currentTimeMillis(); System.out.println("took " + (end - start)); + System.out.println(); start = System.currentTimeMillis(); planningK.adjustAndCalc(); + planningK.allocate(); end = System.currentTimeMillis(); System.out.println("took " + (end - start)); + System.out.println(); start = System.currentTimeMillis(); planning1.adjustAndCalc(); + planning1.allocate(); end = System.currentTimeMillis(); System.out.println("took " + (end - start)); } diff --git a/src/main/java/org/bdware/sc/crdt/planning/PlanningWith0Expansivity.java b/src/main/java/org/bdware/sc/crdt/planning/PlanningWith0Expansivity.java index 60e6399..714d276 100644 --- a/src/main/java/org/bdware/sc/crdt/planning/PlanningWith0Expansivity.java +++ b/src/main/java/org/bdware/sc/crdt/planning/PlanningWith0Expansivity.java @@ -1,7 +1,8 @@ package org.bdware.sc.crdt.planning; public class PlanningWith0Expansivity extends SharableNetworkPlanning { - public PlanningWith0Expansivity(int[] writers, int[] readers, long maxDelay, int bandwidthDownload, int bandwidthUpload, int dataSize) { + public PlanningWith0Expansivity(String[] nodeIds, int[] writers, int[] readers, long maxDelay, int bandwidthDownload, int bandwidthUpload, int dataSize) { + this.nodeIds = nodeIds; this.writers = writers; this.readers = readers; this.maxDelay = maxDelay; @@ -61,6 +62,7 @@ public class PlanningWith0Expansivity extends SharableNetworkPlanning { public static void main(String[] args) { long start = System.currentTimeMillis(); + String[] nodeIds = new String[1500]; int[] writers = new int[1000]; int[] readers = new int[1000]; long maxDelay = 60; @@ -68,7 +70,7 @@ public class PlanningWith0Expansivity extends SharableNetworkPlanning { int bandwidthDownload = 10 * 1024 * 1024; int datasize = 10 * 1024; - PlanningWith0Expansivity planning = new PlanningWith0Expansivity(writers, readers, maxDelay, bandwidthDownload, bandwidthUpload, datasize); + PlanningWith0Expansivity planning = new PlanningWith0Expansivity(nodeIds, writers, readers, maxDelay, bandwidthDownload, bandwidthUpload, datasize); long minTotalData = Long.MAX_VALUE; String result = ""; diff --git a/src/main/java/org/bdware/sc/crdt/planning/PlanningWith1Expansivity.java b/src/main/java/org/bdware/sc/crdt/planning/PlanningWith1Expansivity.java index 1cac931..12a516b 100644 --- a/src/main/java/org/bdware/sc/crdt/planning/PlanningWith1Expansivity.java +++ b/src/main/java/org/bdware/sc/crdt/planning/PlanningWith1Expansivity.java @@ -1,7 +1,8 @@ package org.bdware.sc.crdt.planning; public class PlanningWith1Expansivity extends SharableNetworkPlanning { - public PlanningWith1Expansivity(int[] writers, int[] readers, long maxDelay, int bandwidthDownload, int bandwidthUpload, int dataSize) { + public PlanningWith1Expansivity(String[] nodeIds, int[] writers, int[] readers, long maxDelay, int bandwidthDownload, int bandwidthUpload, int dataSize) { + this.nodeIds = nodeIds; this.writers = writers; this.readers = readers; this.maxDelay = maxDelay; @@ -61,6 +62,7 @@ public class PlanningWith1Expansivity extends SharableNetworkPlanning { public static void main(String[] args) { long start = System.currentTimeMillis(); + String[] nodeIds = new String[1500]; int[] writers = new int[1000]; int[] readers = new int[1000]; long maxDelay = 60; @@ -68,7 +70,7 @@ public class PlanningWith1Expansivity extends SharableNetworkPlanning { int bandwidthDownload = 10 * 1024 * 1024; int datasize = 10 * 1024; - PlanningWith1Expansivity planning = new PlanningWith1Expansivity(writers, readers, maxDelay, bandwidthDownload, bandwidthUpload, datasize); + PlanningWith1Expansivity planning = new PlanningWith1Expansivity(nodeIds, writers, readers, maxDelay, bandwidthDownload, bandwidthUpload, datasize); long minTotalData = Long.MAX_VALUE; String result = ""; diff --git a/src/main/java/org/bdware/sc/crdt/planning/PlanningWithkExpansivity.java b/src/main/java/org/bdware/sc/crdt/planning/PlanningWithkExpansivity.java index 9a51701..b096188 100644 --- a/src/main/java/org/bdware/sc/crdt/planning/PlanningWithkExpansivity.java +++ b/src/main/java/org/bdware/sc/crdt/planning/PlanningWithkExpansivity.java @@ -5,7 +5,8 @@ public class PlanningWithkExpansivity extends SharableNetworkPlanning { private double domainSize; - public PlanningWithkExpansivity(int[] writers, int[] readers, long maxDelay, int bandwidthDownload, int bandwidthUpload, int dataSize, double domainSize) { + public PlanningWithkExpansivity(String[] nodeIds, int[] writers, int[] readers, long maxDelay, int bandwidthDownload, int bandwidthUpload, int dataSize, double domainSize) { + this.nodeIds = nodeIds; this.writers = writers; this.readers = readers; this.maxDelay = maxDelay; @@ -84,6 +85,7 @@ public class PlanningWithkExpansivity extends SharableNetworkPlanning { public static void main(String[] args) { long start = System.currentTimeMillis(); + String[] nodeIds = new String[1500]; int[] writers = new int[1000]; int[] readers = new int[1000]; long maxDelay = 60; @@ -92,7 +94,7 @@ public class PlanningWithkExpansivity extends SharableNetworkPlanning { int datasize = 10 * 1024; double domainSize = 1000 * 1024; - PlanningWithkExpansivity planning = new PlanningWithkExpansivity(writers, readers, maxDelay, bandwidthDownload, bandwidthUpload, datasize, domainSize); + PlanningWithkExpansivity planning = new PlanningWithkExpansivity(nodeIds, writers, readers, maxDelay, bandwidthDownload, bandwidthUpload, datasize, domainSize); planning.adjustAndCalc(); diff --git a/src/main/java/org/bdware/sc/crdt/planning/SharableNetworkPlanning.java b/src/main/java/org/bdware/sc/crdt/planning/SharableNetworkPlanning.java index 79f9941..8407add 100644 --- a/src/main/java/org/bdware/sc/crdt/planning/SharableNetworkPlanning.java +++ b/src/main/java/org/bdware/sc/crdt/planning/SharableNetworkPlanning.java @@ -1,6 +1,9 @@ package org.bdware.sc.crdt.planning; +import java.util.*; + public class SharableNetworkPlanning { + protected String[] nodeIds; protected int[] writers; protected int[] readers; protected long maxDelay; @@ -102,6 +105,71 @@ public class SharableNetworkPlanning { return true; } + public void allocate() { + Set writerOnlySet = new LinkedHashSet<>(); + for (int i : writers) { + writerOnlySet.add(i); + } + Set readerOnlySet = new LinkedHashSet<>(); + Set rwSet = new LinkedHashSet<>(); + for (int i : readers) { + if (writerOnlySet.contains(i)) { + rwSet.add(i); + writerOnlySet.remove(i); + } else { + readerOnlySet.add(i); + } + } + int[] writerParents = allocateTreeNode(writerOnlySet, rwSet, (int) rootCountW, (int) treeDegreeW); + int[] readerParents = allocateTreeNode(readerOnlySet, rwSet, (int) rootCountR, (int) treeDegreeR); + System.out.println(readerParents); + } + + private int[] allocateTreeNode(Set onlySet, Set rwSet, int rootCount, int degree) { + int[] result = new int[nodeIds.length]; + Arrays.fill(result, -2); + + Map> children = new HashMap<>(); + Deque notFullNodesIdx = new LinkedList<>(); + for (Integer idx : onlySet) { + if (children.size() < rootCount) { + children.put(idx, new ArrayList<>()); + notFullNodesIdx.add(idx); + result[idx] = -1; + } else { + int parentIdx = notFullNodesIdx.peek(); + children.computeIfAbsent(parentIdx, k -> new ArrayList<>()).add(idx); + notFullNodesIdx.addLast(idx); + if (children.get(parentIdx).size() >= degree) { + notFullNodesIdx.pop(); + } + } + } + + // 读写节点放在尽量靠近叶子节点,以便带宽的平衡 + for (Integer idx : rwSet) { + if (children.size() < rootCount) { + children.put(idx, new ArrayList<>()); + notFullNodesIdx.add(idx); + result[idx] = -1; + } else { + int parentIdx = notFullNodesIdx.peek(); + children.computeIfAbsent(parentIdx, k -> new ArrayList<>()).add(idx); + notFullNodesIdx.addLast(idx); + if (children.get(parentIdx).size() >= degree) { + notFullNodesIdx.pop(); + } + } + } + + for (Integer parentId : children.keySet()) { + for (int childId : children.get(parentId)) { + result[childId] = parentId; + } + } + return result; + } + @Override public String toString() { StringBuilder result = new StringBuilder();