1 package org.apache.helix.tools;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Random;
27
28 public class YAISCalculator
29 {
30 static class Assignment
31 {
32 private final int numNodes;
33 private final int replication;
34 Partition[] partitions;
35 Node[] nodes;
36
37 public Assignment(int numNodes, int numPartitions, int replication)
38 {
39 this.numNodes = numNodes;
40 this.replication = replication;
41 partitions = new Partition[numPartitions];
42 for (int i = 0; i < numPartitions; i++)
43 {
44 partitions[i] = new Partition(i, replication);
45 }
46 nodes = new Node[numNodes];
47 for (int i = 0; i < numNodes; i++)
48 {
49 nodes[i] = new Node(replication);
50 }
51 }
52
53 public void assign(int partitionId, int replicaId, int nodeId)
54 {
55 System.out.println("Assigning (" + partitionId + "," + replicaId
56 + ") to " + nodeId);
57 partitions[partitionId].nodeIds[replicaId] = nodeId;
58 nodes[nodeId].partitionLists.get(replicaId).push(partitionId);
59 }
60
61 public void unassign(int partitionId, int replicaId)
62 {
63
64 }
65
66 Integer[] getPartitionsPerNode(int nodeId, int replicaId)
67 {
68 List<Integer> partitionsList = new ArrayList<Integer>();
69 for (Partition p : partitions)
70 {
71 if (p.nodeIds[replicaId] == nodeId)
72 {
73 partitionsList.add(p.partionId);
74 }
75 }
76 Integer[] array = new Integer[partitionsList.size()];
77 partitionsList.toArray(array);
78 return array;
79 }
80
81 public void printPerNode()
82 {
83 for (int nodeId = 0; nodeId < numNodes; nodeId++)
84 {
85 for (int r = 0; r < replication; r++)
86 {
87 StringBuilder sb = new StringBuilder();
88 sb.append("(").append(nodeId).append(",").append(r).append("):\t");
89 Node node = nodes[nodeId];
90 LinkedList<Integer> linkedList = node.partitionLists.get(r);
91 for (int partitionId : linkedList)
92 {
93 sb.append(partitionId).append(",");
94 }
95 System.out.println(sb.toString());
96 }
97
98 }
99 }
100 }
101
102 static class Partition
103 {
104
105 final int partionId;
106
107 public Partition(int partionId, int replication)
108 {
109 this.partionId = partionId;
110 nodeIds = new int[replication];
111 Arrays.fill(nodeIds, -1);
112 }
113
114 int nodeIds[];
115 }
116
117 static class Node
118 {
119 private final int replication;
120 ArrayList<LinkedList<Integer>> partitionLists;
121
122 public Node(int replication)
123 {
124 this.replication = replication;
125 partitionLists = new ArrayList<LinkedList<Integer>>(replication);
126 for (int i = 0; i < replication; i++)
127 {
128 partitionLists.add(new LinkedList<Integer>());
129 }
130 }
131
132 }
133
134 public static void main(String[] args)
135 {
136 doAssignment(new int[]
137 { 5 }, 120, 3);
138 }
139
140 private static void doAssignment(int[] nodes, int partitions, int replication)
141 {
142 int N = nodes[0];
143 int totalNodes = 0;
144 for (int temp : nodes)
145 {
146 totalNodes += temp;
147 }
148 Assignment assignment = new Assignment(totalNodes, partitions, replication);
149 int nodeId = 0;
150 for (int i = 0; i < partitions; i++)
151 {
152 assignment.assign(i, 0, nodeId);
153 nodeId = (nodeId + 1) % N;
154 }
155 Random random = new Random();
156 for (int r = 1; r < replication; r++)
157 {
158 for (int id = 0; id < N; id++)
159 {
160 Integer[] partitionsPerNode = assignment.getPartitionsPerNode(id, 0);
161 boolean[] used = new boolean[partitionsPerNode.length];
162 Arrays.fill(used, false);
163 System.out.println(id + "-" + partitionsPerNode.length);
164 nodeId = (id + r) % N;
165 int count = partitionsPerNode.length;
166 boolean done = false;
167 do
168 {
169 if (nodeId != id)
170 {
171 int nextInt = random.nextInt(count);
172 int temp = 0;
173 for (int b = 0; b < used.length; b++)
174 {
175 if (!used[b] && temp == nextInt)
176 {
177 assignment.assign(partitionsPerNode[b], r, nodeId);
178 used[b] = true;
179 break;
180 }
181 }
182 }
183 nodeId = (nodeId + 1) % N;
184 } while (count > 0);
185
186 }
187 }
188 if (nodes.length > 1)
189 {
190 int prevNodeCount = nodes[0];
191 for (int i = 1; i < nodes.length; i++)
192 {
193 int newNodeCount = prevNodeCount + nodes[i];
194 int masterPartitionsToMove = (int) ((partitions * 1.0 / prevNodeCount - partitions
195 * 1.0 / newNodeCount) * 1 * prevNodeCount);
196 while (masterPartitionsToMove > 0)
197 {
198
199 }
200
201 }
202 }
203 assignment.printPerNode();
204 }
205
206 }