feat: generate tree

This commit is contained in:
Xuxin Wang 2023-07-05 16:35:39 +08:00
parent 0b253a4625
commit c0c580090e
6 changed files with 107 additions and 15 deletions

View File

@ -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)
}

View File

@ -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));
}

View File

@ -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 = "";

View File

@ -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 = "";

View File

@ -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();

View File

@ -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<Integer> writerOnlySet = new LinkedHashSet<>();
for (int i : writers) {
writerOnlySet.add(i);
}
Set<Integer> readerOnlySet = new LinkedHashSet<>();
Set<Integer> 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<Integer> onlySet, Set<Integer> rwSet, int rootCount, int degree) {
int[] result = new int[nodeIds.length];
Arrays.fill(result, -2);
Map<Integer, List<Integer>> children = new HashMap<>();
Deque<Integer> 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();