1 package iotruntime.master;
3 import java.util.Collection;
4 import java.util.HashMap;
7 import java.util.Arrays;
9 import iotinstaller.MySQLInterface;
10 import iotinstaller.TableProperty;
11 import iotinstaller.Table;
13 /** Class LoadBalancer is a class that derives information
14 * about hosts (compute nodes) from the database and select
15 * a certain host to do a computation at a certain situation
16 * based on the metrics given to calculate the most load-balanced
19 * @author Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
23 public final class LoadBalancer {
26 * LoadBalancer class properties
28 * Class properties to contain host information from table
29 * HOSTADDRESS is in the form of MAC address that gets translated
31 * hmNumProcesses tracks the usage of a certain host/compute node
32 * hmLoadScore tracks the score of the load for each host;
33 * host selection for the next process is based on these scores
34 * +----------------------+-----------+--------+------------+
35 * | HOSTADDRESS | PROCESSOR | MEMORY | #PROCESSES |
36 * +----------------------+-----------+--------+------------+
37 * | XX:XX:XX:XX:XX:XX | 3500 | 32 | 1 |
38 * | XX:XX:XX:XX:XX:XX | 3500 | 32 | 4 |
39 * | ... | ... | ... | ... |
40 * | ... | ... | ... | ... |
41 * +----------------------+-----------+--------+------------+
43 private HashMap<String, Integer> hmHostAddress;
44 private int[] arrProcessor;
45 private int[] arrMemory;
46 private int[] arrNumProcesses;
47 private int[] arrLoadScore;
49 private boolean bVerbose;
52 * LoadBalancer class constants
54 // private static final String STR_TABLE_COMPUTE_NODE = "IoTComputeNodePC";
55 private static final String STR_TABLE_COMPUTE_NODE = "IoTComputeNode";
60 public LoadBalancer(boolean _bVerbose) {
62 hmHostAddress = new HashMap<String, Integer>();
65 arrNumProcesses = null;
67 tbl = new Table(STR_TABLE_COMPUTE_NODE, _bVerbose);
69 RuntimeOutput.print("LoadBalancer: Creating a load-balancer!", bVerbose);
73 * setupLoadBalancer() method loads host information from DB
77 public void setupLoadBalancer() {
79 String[][] arrTbl = tbl.getGeneralDBTable();
80 arrProcessor = new int[arrTbl.length];
81 arrMemory = new int[arrTbl.length];
82 arrNumProcesses = new int[arrTbl.length];
83 arrLoadScore = new int[arrTbl.length];
85 for(int i=0; i<arrTbl.length; i++) {
87 // Iterate per row from the DB table
88 hmHostAddress.put((String) arrTbl[i][0], i);
89 arrProcessor[i] = Integer.parseInt((String) arrTbl[i][1]);
90 arrMemory[i] = Integer.parseInt((String) arrTbl[i][2]);
92 // Initialize #process to 0 for all entries in the beginning
93 // Initialize load score to maximum integer value
94 arrNumProcesses[i] = 0;
95 arrLoadScore[i] = Integer.MAX_VALUE;
97 RuntimeOutput.print("LoadBalancer: Initializing load balancer...", bVerbose);
101 * selectHost() method selects a host based on the metrics
105 public String selectHost() {
107 // Variable for highest score that we are going to select
108 int iHighestScore = 0;
110 //String strHostMACAddress = null;
111 String strHostIPAddress = null;
113 RuntimeOutput.print("LoadBalancer: Host address number: " + hmHostAddress.size(), bVerbose);
115 // Get the first host address from the hashmap
116 strHostIPAddress = (String) hmHostAddress.keySet().toArray()[0];
117 for(Map.Entry<String, Integer> mapHost : hmHostAddress.entrySet()) {
119 // Get the current entry load score
120 int iEntryScore = arrLoadScore[mapHost.getValue()];
122 // Compare highest score and entry score; select the highest
123 if (iHighestScore < iEntryScore) {
124 iHighestScore = iEntryScore;
125 strHostIPAddress = mapHost.getKey();
129 // Calculate the new score for this host and return the host address
130 calculateHostScore(strHostIPAddress);
131 RuntimeOutput.print("LoadBalancer: Selected host: " + strHostIPAddress, bVerbose);
133 return strHostIPAddress;
137 * calculateHostScore() calculates score for a host based on the metrics
139 * It also stores the results back to the corresponding hashmaps
141 * @param strHostAddress String host address
144 private void calculateHostScore(String strHostAddress) {
146 // Get the previous values
147 int iIndex = hmHostAddress.get(strHostAddress);
148 int iPrevNumProcesses = arrNumProcesses[iIndex];
150 // Calculate the current values
151 // Every time we call this method, we increment #process by 1
152 // (we add one new process)
153 int iCurrNumProcesses = iPrevNumProcesses + 1;
154 int iProcessor = arrProcessor[iIndex];
155 int iMemory = arrMemory[iIndex];
157 // We calculate the score simply with this formula
158 // Score = (Processor/current #process) x (Memory/current #process)
159 // The more processes a certain node has, the lower its score is.
160 // Therefore, we always choose a node that has the highest score.
161 // P.S. In this formula we also take the processor and memory specs
163 int iCurrScore = (iProcessor * iMemory) / iCurrNumProcesses;
164 arrLoadScore[iIndex] = iCurrScore;
165 arrNumProcesses[iIndex] = iCurrNumProcesses;
166 RuntimeOutput.print("LoadBalancer: Calculate host load score for " + strHostAddress, bVerbose);
170 * printHostInfo() method prints the host information at runtime
174 public void printHostInfo() {
176 for(Map.Entry<String, Integer> mapHost : hmHostAddress.entrySet()) {
178 RuntimeOutput.print("Host address : " + mapHost.getKey(), bVerbose);
179 RuntimeOutput.print("Processor : " + arrProcessor[mapHost.getValue()], bVerbose);
180 RuntimeOutput.print("Memory : " + arrMemory[mapHost.getValue()], bVerbose);
181 RuntimeOutput.print("Number of processes : " + arrNumProcesses[mapHost.getValue()], bVerbose);
182 RuntimeOutput.print("Host score : " + arrLoadScore[mapHost.getValue()], bVerbose);
186 public static void main(String[] args) {
188 LoadBalancer lb = new LoadBalancer(true);
189 lb.setupLoadBalancer();
190 System.out.println("Chosen host: " + lb.selectHost());
191 System.out.println("Chosen host: " + lb.selectHost());
192 System.out.println("Chosen host: " + lb.selectHost());
193 System.out.println("Chosen host: " + lb.selectHost());
194 System.out.println("Chosen host: " + lb.selectHost());
195 System.out.println("Chosen host: " + lb.selectHost());
196 System.out.println("Chosen host: " + lb.selectHost());
197 System.out.println("Chosen host: " + lb.selectHost());