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 = "IoTComputeNode";
59 public LoadBalancer(boolean _bVerbose) {
61 hmHostAddress = new HashMap<String, Integer>();
64 arrNumProcesses = null;
66 tbl = new Table(STR_TABLE_COMPUTE_NODE, _bVerbose);
68 RuntimeOutput.print("LoadBalancer: Creating a load-balancer!", bVerbose);
72 * setupLoadBalancer() method loads host information from DB
76 public void setupLoadBalancer() {
78 String[][] arrTbl = tbl.getGeneralDBTable();
79 arrProcessor = new int[arrTbl.length];
80 arrMemory = new int[arrTbl.length];
81 arrNumProcesses = new int[arrTbl.length];
82 arrLoadScore = new int[arrTbl.length];
84 for(int i=0; i<arrTbl.length; i++) {
86 // Iterate per row from the DB table
87 hmHostAddress.put((String) arrTbl[i][0], i);
88 arrProcessor[i] = Integer.parseInt((String) arrTbl[i][1]);
89 arrMemory[i] = Integer.parseInt((String) arrTbl[i][2]);
91 // Initialize #process to 0 for all entries in the beginning
92 // Initialize load score to maximum integer value
93 arrNumProcesses[i] = 0;
94 arrLoadScore[i] = Integer.MAX_VALUE;
96 RuntimeOutput.print("LoadBalancer: Initializing load balancer...", bVerbose);
100 * selectHost() method selects a host based on the metrics
104 public String selectHost() {
106 // Variable for highest score that we are going to select
107 int iHighestScore = 0;
109 //String strHostMACAddress = null;
110 String strHostIPAddress = null;
112 RuntimeOutput.print("LoadBalancer: Host address number: " + hmHostAddress.size(), bVerbose);
114 // Get the first host address from the hashmap
115 strHostIPAddress = (String) hmHostAddress.keySet().toArray()[0];
116 for(Map.Entry<String, Integer> mapHost : hmHostAddress.entrySet()) {
118 // Get the current entry load score
119 int iEntryScore = arrLoadScore[mapHost.getValue()];
121 // Compare highest score and entry score; select the highest
122 if (iHighestScore < iEntryScore) {
123 iHighestScore = iEntryScore;
124 strHostIPAddress = mapHost.getKey();
128 // Calculate the new score for this host and return the host address
129 calculateHostScore(strHostIPAddress);
130 RuntimeOutput.print("LoadBalancer: Selected host: " + strHostIPAddress, bVerbose);
132 return strHostIPAddress;
136 * calculateHostScore() calculates score for a host based on the metrics
138 * It also stores the results back to the corresponding hashmaps
140 * @param strHostAddress String host address
143 private void calculateHostScore(String strHostAddress) {
145 // Get the previous values
146 int iIndex = hmHostAddress.get(strHostAddress);
147 int iPrevNumProcesses = arrNumProcesses[iIndex];
149 // Calculate the current values
150 // Every time we call this method, we increment #process by 1
151 // (we add one new process)
152 int iCurrNumProcesses = iPrevNumProcesses + 1;
153 int iProcessor = arrProcessor[iIndex];
154 int iMemory = arrMemory[iIndex];
156 // We calculate the score simply with this formula
157 // Score = (Processor/current #process) x (Memory/current #process)
158 // The more processes a certain node has, the lower its score is.
159 // Therefore, we always choose a node that has the highest score.
160 // P.S. In this formula we also take the processor and memory specs
162 int iCurrScore = (iProcessor * iMemory) / iCurrNumProcesses;
163 arrLoadScore[iIndex] = iCurrScore;
164 arrNumProcesses[iIndex] = iCurrNumProcesses;
165 RuntimeOutput.print("LoadBalancer: Calculate host load score for " + strHostAddress, bVerbose);
169 * printHostInfo() method prints the host information at runtime
173 public void printHostInfo() {
175 for(Map.Entry<String, Integer> mapHost : hmHostAddress.entrySet()) {
177 RuntimeOutput.print("Host address : " + mapHost.getKey(), bVerbose);
178 RuntimeOutput.print("Processor : " + arrProcessor[mapHost.getValue()], bVerbose);
179 RuntimeOutput.print("Memory : " + arrMemory[mapHost.getValue()], bVerbose);
180 RuntimeOutput.print("Number of processes : " + arrNumProcesses[mapHost.getValue()], bVerbose);
181 RuntimeOutput.print("Host score : " + arrLoadScore[mapHost.getValue()], bVerbose);
185 public static void main(String[] args) {
187 LoadBalancer lb = new LoadBalancer(true);
188 lb.setupLoadBalancer();
189 System.out.println("Chosen host: " + lb.selectHost());
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());