How to Simulate Host Failure and Recovery Mechanisms in Containercloudsim to Evaluate Fault Tolerance and Resource Migration Efficiency?
Share
Condition for Simulate Host Failure and Recovery Mechanisms in Containercloudsim
Description: This project focuses on the simulation of fault tolerance and recovery in a containerized cloud computing environment using ContainerCloudSim. The objective is to model how the system behaves when one or more hosts fail unexpectedly and to analyze how effectively the virtual machines (VMs) and containers are migrated or recovered to maintain system reliability and performance.The implementation introduces a failure injection mechanism that randomly or periodically simulates host failures during the simulation runtime. When a host fails, the simulation identifies the affected VMs and containers, attempts to migrate them to other healthy hosts based on resource availability and utilization thresholds, and records detailed recovery statistics, including migration success rates, recovery time, and impact on running cloudlets.By observing the system’s recovery behavior, this experiment helps researchers and cloud engineers understand how fault-tolerant resource management policies can minimize downtime and maintain Quality of Service (QoS) in dynamic cloud environments. The simulation also highlights the importance of VM migration efficiency, container restart behavior, and host selection policies in achieving resilient cloud operations.
/**
* ContainerCloudSim with Failure Injection Simulation Task: Simulate host
* failures and observe recovery behavior Learning Objective: Understand fault
* tolerance in cloud systems
*/
public class FaultTolerance {
/**
* The cloudlet list.
*/
private static List cloudletList;
/**
* The vmlist.
*/
private static List vmList;
/**
* The container list.
*/
private static List containerList;
/**
* The hostList.
*/
private static List hostList;
/**
* Statistics for failure simulation
*/
private static class FailureStatistics {
private int totalHostFailures = 0;
private int affectedVMs = 0;
private int successfullyMigratedVMs = 0;
private int failedCloudlets = 0;
private int recoveredCloudlets = 0;
private long totalRecoveryTime = 0;
// Get VMs running on failed host
List vmsOnFailedHost = new ArrayList<>();
List containersOnFailedHost = new ArrayList<>();
for (ContainerVm vm : vmList) {
if (vm.getHost() == failedHost) {
vmsOnFailedHost.add(vm);
Log.printLine(" VM " + vm.getId() + " affected (MIPS: " + vm.getMips() + ", RAM: " + vm.getRam() + ")");
// Get containers on this VM
for (Container container : containerList) {
if (container.getVm() == vm) {
containersOnFailedHost.add(container);
Log.printLine(" Container " + container.getId() + " affected");
}
}
}
}
failureStats.affectedVMs += vmsOnFailedHost.size();
Log.printLine(" Total Affected VMs: " + vmsOnFailedHost.size());
Log.printLine(" Total Affected Containers: " + containersOnFailedHost.size());
// Simulate migration to available hosts
simulateVMMigration(vmsOnFailedHost, containersOnFailedHost, failedHostId, failureTime);
/**
* Simulate VM migration to available hosts
*/
private static void simulateVMMigration(List vmsToMigrate, List containersToMigrate,
int failedHostId, double failureTime) {
Log.printLine("\n--- VM MIGRATION PROCESS ---");
int successfulMigrations = 0;
int failedMigrations = 0;
for (ContainerVm vm : vmsToMigrate) {
ContainerHost targetHost = findSuitableHostForMigration(vm, failedHostId);
} else {
Log.printLine(" No suitable host found for VM " + vm.getId() + " - VM will be terminated");
failedMigrations++;
failureStats.failedCloudlets += countCloudletsOnVM(vm);
}
}
/**
* Find suitable host for VM migration
*/
private static ContainerHost findSuitableHostForMigration(ContainerVm vm, int failedHostId) {
ContainerHost bestHost = null;
double bestScore = Double.MAX_VALUE;
for (ContainerHost host : hostList) {
// Skip failed host and hosts that are already overloaded
if (host.getId() == failedHostId || host.isFailed()) {
continue;
}
// Check if host has enough resources
boolean hasEnoughMips = host.getAvailableMips() >= vm.getMips();
boolean hasEnoughRam = host.getContainerVmRamProvisioner().getAvailableRam() >= vm.getRam();
if (hasEnoughMips && hasEnoughRam) {
// Calculate score based on current utilization (lower is better)
double cpuUtilization = 1 - (host.getAvailableMips() / host.getTotalMips());
double ramUtilization = 1 - ((double) host.getContainerVmRamProvisioner().getAvailableRam() / host.getRam());
double score = (cpuUtilization * 0.6) + (ramUtilization * 0.4);
if (bestHost != null) {
Log.printLine(" Selected Host " + bestHost.getId() + " for VM " + vm.getId()
+ " (Score: " + String.format("%.3f", bestScore) + ")");
}
return bestHost;
}
/**
* Calculate migration time based on VM size and network bandwidth
*/
private static double calculateMigrationTime(ContainerVm vm, ContainerHost targetHost) {
// Simplified migration time calculation
double vmSizeMB = (vm.getRam() / 1024.0); // Convert RAM to MB
double networkBandwidthMbps = 1000; // Assume 1 Gbps network
double migrationTime = (vmSizeMB * 8) / networkBandwidthMbps; // Convert to seconds
// Add overhead
migrationTime += 0.5;
return Math.max(migrationTime, 1.0); // Minimum 1 second
}
/**
* Migrate containers for a VM
*/
private static void migrateContainersForVM(ContainerVm vm, List containersToMigrate, double migrationTime) {
int migratedContainers = 0;
int recoveredCloudlets = 0;
for (Container container : containersToMigrate) {
if (container.getVm() == vm) {
// Simulate container restart/migration
double recoveryTime = migrationTime + (Math.random() * 2.0); // Add some variability
/**
* Count cloudlets on a VM
*/
private static int countCloudletsOnVM(ContainerVm vm) {
int count = 0;
for (ContainerCloudlet cloudlet : cloudletList) {
if (cloudlet.getVmId() == vm.getId()) {
count++;
}
}
return count;
}
/**
* Simulate random host failures during simulation
*/
private static void simulateRandomFailures() {
Log.printLine("\n" + " INITIATING RANDOM FAILURE SIMULATION");
Log.printLine("=".repeat(50));
// Schedule multiple failures at different times
scheduleFailure(0, 300.0); // Early failure
scheduleFailure(1, 600.0); // Mid-simulation failure
scheduleFailure(2, 900.0); // Late failure
/**
* Get host ID for a VM
*/
private static String getHostIdForVm(int vmId) {
for (ContainerVm vm : vmList) {
if (vm.getId() == vmId && vm.getHost() != null) {
return String.valueOf(vm.getHost().getId());
}
}
return "N/A";
}
/**
* Creates main() to run this example with failure simulation
*/
public static void main(String[] args) {
Log.printLine("Starting ContainerCloudSim with Failure Injection Simulation...");
Log.printLine("Objective: Understand fault tolerance and recovery in cloud systems");
try {
// Initialize simulation
int num_user = 1;
Calendar calendar = Calendar.getInstance();
boolean trace_flag = false;
CloudSim.init(num_user, calendar, trace_flag);
// Setup policies
ContainerAllocationPolicy containerAllocationPolicy = new PowerContainerAllocationPolicySimple();
PowerContainerVmSelectionPolicy vmSelectionPolicy = new PowerContainerVmSelectionPolicyMaximumUsage();
HostSelectionPolicy hostSelectionPolicy = new HostSelectionPolicyFirstFit();
// Simulate failures at different times
simulateHostFailure(hostList, 0, 300.0); // Early failure
simulateHostFailure(hostList, 1, 600.0); // Mid-simulation failure
CloudSim.stopSimulation();
// Record end time
long endTime = System.currentTimeMillis();
// Collect and print results
List newList = broker.getCloudletReceivedList();
printCloudletListWithFailureInfo(newList);
// ========== ORIGINAL METHODS (Unchanged) ==========
/**
* It creates a specific name for the experiment which is used for creating
* the Log address folder.
*/
private static String getExperimentName(String... args) {
StringBuilder experimentName = new StringBuilder();
for (int i = 0; i < args.length; ++i) {
if (!args[i].isEmpty()) {
if (i != 0) {
experimentName.append("_");
}
/**
* Create the Virtual machines and add them to the list
*/
private static ArrayList createVmList(int brokerId, int containerVmsNumber) {
ArrayList containerVms = new ArrayList();
for (int i = 0; i < containerVmsNumber; ++i) {
ArrayList peList = new ArrayList();
int vmType = i / (int) Math.ceil((double) containerVmsNumber / 4.0D);
for (int j = 0; j < ConstantsExamples.VM_PES[vmType]; ++j) {
peList.add(new ContainerPe(j,
new ContainerPeProvisionerSimple((double) ConstantsExamples.VM_MIPS[vmType])));
}
containerVms.add(new PowerContainerVm(IDs.pollId(ContainerVm.class), brokerId,
(double) ConstantsExamples.VM_MIPS[vmType], (float) ConstantsExamples.VM_RAM[vmType],
ConstantsExamples.VM_BW, ConstantsExamples.VM_SIZE, "Xen",
new ContainerSchedulerTimeSharedOverSubscription(peList),
new ContainerRamProvisionerSimple(ConstantsExamples.VM_RAM[vmType]),
new ContainerBwProvisionerSimple(ConstantsExamples.VM_BW),
peList, ConstantsExamples.SCHEDULING_INTERVAL));
}
return containerVms;
}
/**
* Create the host list
*/
public static List createHostList(int hostsNumber) {
ArrayList hostList = new ArrayList();
for (int i = 0; i < hostsNumber; ++i) {
int hostType = i / (int) Math.ceil((double) hostsNumber / 3.0D);
ArrayList peList = new ArrayList();
for (int j = 0; j < ConstantsExamples.HOST_PES[hostType]; ++j) {
peList.add(new ContainerVmPe(j,
new ContainerVmPeProvisionerSimple((double) ConstantsExamples.HOST_MIPS[hostType])));
}
hostList.add(new PowerContainerHostUtilizationHistory(IDs.pollId(ContainerHost.class),
new ContainerVmRamProvisionerSimple(ConstantsExamples.HOST_RAM[hostType]),
new ContainerVmBwProvisionerSimple(1000000L), 1000000L, peList,
new ContainerVmSchedulerTimeSharedOverSubscription(peList),
ConstantsExamples.HOST_POWER[hostType]));
}
/**
* Creating the cloudlet list
*/
public static List createContainerCloudletList(int brokerId, int numberOfCloudlets)
throws FileNotFoundException {
String inputFolderName = "/home/soft13/soft13/Project 2019/Cloudsim-4.0-examples/src/workload/planetlab";
ArrayList cloudletList = new ArrayList();
long fileSize = 300L;
long outputSize = 300L;
UtilizationModelNull utilizationModelNull = new UtilizationModelNull();
java.io.File inputFolder1 = new java.io.File(inputFolderName);
java.io.File[] files1 = inputFolder1.listFiles();
int createdCloudlets = 0;
if (files1 != null) {
for (java.io.File aFiles1 : files1) {
java.io.File inputFolder = new java.io.File(aFiles1.toString());
java.io.File[] files = inputFolder.listFiles();
if (files != null) {
for (int i = 0; i < files.length; ++i) {
if (createdCloudlets < numberOfCloudlets) {
ContainerCloudlet cloudlet = null;