Design a simple load distributor for a data center that can do the following:
Implement the DCLoadBalancer
class:
DCLoadBalancer()
Initializes the object.void addMachine(int machineId, int capacity)
Registers a machine with the given machineId
and maximum capacity
.void removeMachine(int machineId)
Removes the machine with the given machineId
. All applications running on this machine are automatically reallocated to other machines in the same order as they were added to this machine. The applications should be reallocated in the same manner as addApplication
.int addApplication(int appId, int loadUse)
Allocates an application with the given appId
and loadUse
to the machine with the largest remaining capacity that can handle the additional request. If there is a tie, the machine with the lowest ID is used. Returns the machine ID that the application is allocated to. If no machine can handle the request, return -1
.void stopApplication(int appId)
Stops and removes the application with the given appId
from the machine it is running on, freeing up the machine's capacity by its corresponding loadUse
. If the application does not exist, nothing happens.List<Integer> getApplications(int machineId)
Returns a list of application IDs running on a machine with the given machineId
in the order in which they were added. If there are more than 10
applications, return only the first 10
IDs.
Example 1:
Input ["DCLoadBalancer", "addMachine", "addMachine", "addMachine", "addMachine", "addApplication", "addApplication", "addApplication", "addApplication", "getApplications", "addMachine", "addApplication", "stopApplication", "addApplication", "getApplications", "removeMachine", "getApplications"] [[], [1, 1], [2, 10], [3, 10], [4, 15], [1, 3], [2, 11], [3, 6], [4, 5], [2], [5, 10], [5, 5], [3], [6, 5], [4], [4], [2]] Output [null, null, null, null, null, 4, 4, 2, 3, [3], null, 5, null, 2, [1, 2], null, [6, 1]] Explanation DCLoadBalancer dCLoadBalancer = new DCLoadBalancer(); dCLoadBalancer.addMachine(1, 1); // Capacity Left: [1] dCLoadBalancer.addMachine(2, 10); // Capacity Left: [1,10] dCLoadBalancer.addMachine(3, 10); // Capacity Left: [1,10,10] dCLoadBalancer.addMachine(4, 15); // Capacity Left: [1,10,10,15] dCLoadBalancer.addApplication(1, 3); // return 4, Capacity Left: [1,10,10,12] // Machine 4 had the largest capacity left at 15. dCLoadBalancer.addApplication(2, 11); // return 4, Capacity Left: [1,10,10,1] // Machine 4 had the largest capacity left at 12. dCLoadBalancer.addApplication(3, 6); // return 2, Capacity Left: [1,4,10,1] // Machine 2 and 3 had the same largest capacity but machine 2 has the lower ID. dCLoadBalancer.addApplication(4, 5); // return 3, Capacity Left: [1,4,5,1] // Machine 3 had the largest capacity at 10. dCLoadBalancer.getApplications(2); // return [3], Machine 2 only has application 3. dCLoadBalancer.addMachine(5, 10); // Capacity Left: [1,4,5,1,10] dCLoadBalancer.addApplication(5, 5); // return 5, Capacity Left: [1,4,5,1,5] // Machine 5 had the largest capacity at 10. dCLoadBalancer.stopApplication(3); // Capacity Left: [1,10,5,1,5], // Application 3 was running on machine 2. dCLoadBalancer.addApplication(6, 5); // return 2, Capacity Left: [1,5,5,1,5] // Machine 2 had the largest capacity at 10. dCLoadBalancer.getApplications(4); // return [1, 2], Machine 4 has applications 1 and 2. dCLoadBalancer.removeMachine(4); // Capacity Left: [1,2,5,-,5] // Machine 4 had applications 1 and 2. // Application 1 has a load of 3 and is added to machine 2. // Application 2 has a load of 11 and cannot be added to any machine so it is removed. dCLoadBalancer.getApplications(2); // return [6, 1], Machine 2 has applications 6 and 1.
Constraints:
1 <= machineId, appId <= 105
1 <= loadUse, capacity <= 105
machineId
will not have the same ID as any active machine across all calls to addMachine
.machineId
will be the ID of an active machine across all calls to removeMachine
and getApplications
.appId
will be unique across all calls to addApplication
.10
calls will be made to removeMachine
.2 * 104
calls in total will be made to addMachine
, removeMachine
, addApplication
, stopApplication
and getApplications
.