Skip to content
Snippets Groups Projects
Commit 0773a503 authored by fareed.hussain's avatar fareed.hussain
Browse files

264-c02.1.214-create-function-Adaptation-Strategy-Load-Balancing-Thermodynamic...

264-c02.1.214-create-function-Adaptation-Strategy-Load-Balancing-Thermodynamic-Load-Balancer-090702025
parent 35000336
No related branches found
No related tags found
No related merge requests found
package com.informationcatalyst.enact.application_controller.loadbalancinghandler.controllers;
import com.example.loadbalancer.application.ThermodynamicBalancer;
import com.example.loadbalancer.model.HeatDissipationReport;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/load")
public class ThermodynamicBalancerController {
private final ThermodynamicBalancer balancer;
public TrafficController(ThermodynamicBalancer balancer) {
this.balancer = balancer;
}
@PostMapping("/calculate-entropy")
public String calculateEntropy() {
balancer.calculateTrafficEntropy();
return "Entropy calculated";
}
@PostMapping("/redistribute")
public String redistribute() {
balancer.applyMaxEntropyDistribution();
return "Load redistributed using max entropy principle";
}
@GetMapping("/energy-report")
public HeatDissipationReport getEnergyReport() {
return balancer.measureEnergyFlows();
}
}
package com.informationcatalyst.enact.application_controller.loadbalancinghandler.interfaces;
import com.example.loadbalancer.model.HeatDissipationReport;
public interface ThermodynamicBalancer {
void calculateTrafficEntropy();
void applyMaxEntropyDistribution();
HeatDissipationReport measureEnergyFlows();
}
package com.informationcatalyst.enact.application_controller.loadbalancinghandler.models;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class HeatDissipationReport {
private double systemEntropy;
private double thermalGradient;
private double energyEfficiency;
private String distributionQuality;
}
\ No newline at end of file
package com.informationcatalyst.enact.application_controller.loadbalancinghandler.services;
import com.example.loadbalancer.model.HeatDissipationReport;
import com.example.loadbalancer.model.ServerStats;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class ThermodynamicBalancerService implements ThermodynamicBalancer {
private final LoadStateService loadStateService;
public ThermodynamicBalancerImpl(LoadStateService loadStateService) {
this.loadStateService = loadStateService;
}
@Override
public void calculateTrafficEntropy() {
Map<String, ServerStats> servers = loadStateService.getServerStats();
double totalLoad = servers.values().stream()
.mapToDouble(ServerStats::getCurrentLoad)
.sum();
double entropy = servers.values().stream()
.mapToDouble(stats -> {
double probability = stats.getCurrentLoad() / totalLoad;
return probability * log2(probability);
})
.sum() * -1;
loadStateService.setSystemEntropy(Double.isNaN(entropy) ? 0 : entropy);
}
@Override
public void applyMaxEntropyDistribution() {
Map<String, ServerStats> servers = new java.util.HashMap<>(loadStateService.getServerStats());
double totalLoad = servers.values().stream()
.mapToDouble(ServerStats::getCurrentLoad)
.sum();
int serverCount = servers.size();
double idealLoad = totalLoad / serverCount;
servers.replaceAll((id, stats) -> new ServerStats(
id,
(int) Math.round(idealLoad),
stats.getTemperature(),
stats.getCapacity()));
servers.forEach((id, stats) -> loadStateService.updateServerLoad(id, stats.getCurrentLoad()));
}
@Override
public HeatDissipationReport measureEnergyFlows() {
Map<String, ServerStats> servers = loadStateService.getServerStats();
// Calculate thermal gradient (max temp difference)
double maxTemp = servers.values().stream()
.mapToDouble(ServerStats::getTemperature)
.max().orElse(0);
double minTemp = servers.values().stream()
.mapToDouble(ServerStats::getTemperature)
.min().orElse(0);
double thermalGradient = maxTemp - minTemp;
// Calculate energy efficiency
double totalLoad = servers.values().stream()
.mapToDouble(ServerStats::getCurrentLoad)
.sum();
double totalCapacity = servers.values().stream()
.mapToDouble(ServerStats::getCapacity)
.sum();
double energyEfficiency = (totalLoad / totalCapacity) * 100;
return new HeatDissipationReport(
loadStateService.getSystemEntropy(),
thermalGradient,
energyEfficiency,
determineDistributionQuality(thermalGradient));
}
private double log2(double n) {
return Math.log(n) / Math.log(2);
}
private String determineDistributionQuality(double gradient) {
if (gradient < 5)
return "OPTIMAL";
if (gradient < 10)
return "GOOD";
if (gradient < 15)
return "FAIR";
return "POOR";
}
}
package com.informationcatalyst.enact.application_controller.loadbalancinghandler.utils;
import com.example.loadbalancer.model.ServerStats;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@Service
public class LoadStateService {
private final Map<String, ServerStats> serverStats = new ConcurrentHashMap<>();
private double systemEntropy = 0.0;
public LoadStateService() {
// Initialize with sample servers
serverStats.put("server1", new ServerStats("server1", 25, 35.4, 100));
serverStats.put("server2", new ServerStats("server2", 40, 42.1, 100));
serverStats.put("server3", new ServerStats("server3", 15, 30.2, 100));
}
public Map<String, ServerStats> getServerStats() {
return Collections.unmodifiableMap(serverStats);
}
public void updateServerLoad(String serverId, int newLoad) {
serverStats.computeIfPresent(serverId,
(id, stats) -> new ServerStats(id, newLoad, stats.getTemperature(), stats.getCapacity()));
}
public void setSystemEntropy(double entropy) {
this.systemEntropy = entropy;
}
public double getSystemEntropy() {
return systemEntropy;
}
}
......@@ -3,11 +3,18 @@ package com.informationcatalyst.enact.application_controller.loadbalancinghandle
import lombok.Data;
import org.springframework.stereotype.Component;
import com.informationcatalyst.enact.application_controller.config.LoadBalancerConfig;
import java.util.concurrent.atomic.AtomicInteger;
@Data
@Component
public class ServerStats {
private String serverId;
private int currentLoad;
private double temperature;
private double capacity;
private final AtomicInteger totalRequests = new AtomicInteger(0);
private final AtomicInteger[] serverRequestCounts;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment