|
| 1 | +/* |
| 2 | + * CloudSim Plus: A modern, highly-extensible and easier-to-use Framework for |
| 3 | + * Modeling and Simulation of Cloud Computing Infrastructures and Services. |
| 4 | + * http://cloudsimplus.org |
| 5 | + * |
| 6 | + * Copyright (C) 2015-2021 Universidade da Beira Interior (UBI, Portugal) and |
| 7 | + * the Instituto Federal de Educação Ciência e Tecnologia do Tocantins (IFTO, Brazil). |
| 8 | + * |
| 9 | + * This file is part of CloudSim Plus. |
| 10 | + * |
| 11 | + * CloudSim Plus is free software: you can redistribute it and/or modify |
| 12 | + * it under the terms of the GNU General Public License as published by |
| 13 | + * the Free Software Foundation, either version 3 of the License, or |
| 14 | + * (at your option) any later version. |
| 15 | + * |
| 16 | + * CloudSim Plus is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU General Public License |
| 22 | + * along with CloudSim Plus. If not, see <http://www.gnu.org/licenses/>. |
| 23 | + */ |
| 24 | +package org.cloudsimplus.integrationtests; |
| 25 | + |
| 26 | +import org.cloudsimplus.brokers.DatacenterBroker; |
| 27 | +import org.cloudsimplus.brokers.DatacenterBrokerSimple; |
| 28 | +import org.cloudsimplus.cloudlets.Cloudlet; |
| 29 | +import org.cloudsimplus.cloudlets.CloudletSimple; |
| 30 | +import org.cloudsimplus.core.CloudSimPlus; |
| 31 | +import org.cloudsimplus.datacenters.DatacenterSimple; |
| 32 | +import org.cloudsimplus.hosts.Host; |
| 33 | +import org.cloudsimplus.hosts.HostSimple; |
| 34 | +import org.cloudsimplus.resources.Pe; |
| 35 | +import org.cloudsimplus.resources.PeSimple; |
| 36 | +import org.cloudsimplus.vms.Vm; |
| 37 | +import org.cloudsimplus.vms.VmSimple; |
| 38 | +import org.junit.jupiter.api.BeforeEach; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | +import org.junit.jupiter.api.Timeout; |
| 41 | + |
| 42 | +import java.util.ArrayList; |
| 43 | +import java.util.List; |
| 44 | +import java.util.concurrent.atomic.AtomicInteger; |
| 45 | + |
| 46 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 47 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 48 | + |
| 49 | +/** |
| 50 | + * An Integration Test (IT) to check that a VM that can't be placed in |
| 51 | + * the only existing Datacenter is retried just the maximum number of times |
| 52 | + * defined in the broker's {@link org.cloudsimplus.brokers.VmCreation}, |
| 53 | + * instead of being retried forever. |
| 54 | + * |
| 55 | + * @see <a href="https://github.com/cloudsimplus/cloudsimplus-examples/issues/31">Issue #31 in the examples repository</a> |
| 56 | + */ |
| 57 | +final class VmCreationRetryIntegrationTest { |
| 58 | + private static final int TIMEOUT_SECS = 10; |
| 59 | + private static final int HOST_PES = 4; |
| 60 | + private static final int VM_PES = HOST_PES; |
| 61 | + private static final int MIPS = 1000; |
| 62 | + private static final int VMS = 2; |
| 63 | + private static final int CLOUDLET_LENGTH = 10_000; |
| 64 | + private static final int MAX_RETRIES = 3; |
| 65 | + private static final double RETRY_DELAY = 5; |
| 66 | + |
| 67 | + private CloudSimPlus simulation; |
| 68 | + private DatacenterBroker broker; |
| 69 | + private Vm failedVm; |
| 70 | + private AtomicInteger creationFailures; |
| 71 | + |
| 72 | + @BeforeEach |
| 73 | + void setUp() { |
| 74 | + simulation = new CloudSimPlus(); |
| 75 | + new DatacenterSimple(simulation, List.of(createHost())); |
| 76 | + broker = new DatacenterBrokerSimple(simulation); |
| 77 | + creationFailures = new AtomicInteger(); |
| 78 | + |
| 79 | + final var vmList = createVms(); |
| 80 | + failedVm = vmList.getLast(); |
| 81 | + failedVm.addOnCreationFailureListener(evt -> creationFailures.incrementAndGet()); |
| 82 | + broker.submitVmList(vmList); |
| 83 | + broker.submitCloudletList(List.of(createCloudlet(vmList.getFirst()))); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + @Timeout(value = TIMEOUT_SECS, threadMode = Timeout.ThreadMode.SEPARATE_THREAD) |
| 88 | + void retriesFailedVmUpToMaxRetries() { |
| 89 | + broker.getVmCreation().setRetryDelay(RETRY_DELAY).setMaxRetries(MAX_RETRIES); |
| 90 | + simulation.start(); |
| 91 | + |
| 92 | + //The first creation attempt plus all retries |
| 93 | + assertEquals(MAX_RETRIES + 1, creationFailures.get()); |
| 94 | + assertTrue(broker.getVmFailedList().contains(failedVm)); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + @Timeout(value = TIMEOUT_SECS, threadMode = Timeout.ThreadMode.SEPARATE_THREAD) |
| 99 | + void doesNotRetryFailedVmWhenRetryIsDisabled() { |
| 100 | + broker.getVmCreation().setRetryDelay(-1); |
| 101 | + simulation.start(); |
| 102 | + |
| 103 | + assertEquals(1, creationFailures.get()); |
| 104 | + assertTrue(broker.getVmFailedList().contains(failedVm)); |
| 105 | + } |
| 106 | + |
| 107 | + private static Host createHost() { |
| 108 | + final var peList = new ArrayList<Pe>(HOST_PES); |
| 109 | + for (int i = 0; i < HOST_PES; i++) { |
| 110 | + peList.add(new PeSimple(MIPS)); |
| 111 | + } |
| 112 | + |
| 113 | + return new HostSimple(peList); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Creates VMs that require all Host PEs, so that only the first one can be placed. |
| 118 | + */ |
| 119 | + private static List<Vm> createVms() { |
| 120 | + final var vmList = new ArrayList<Vm>(VMS); |
| 121 | + for (int i = 0; i < VMS; i++) { |
| 122 | + vmList.add(new VmSimple(MIPS, VM_PES)); |
| 123 | + } |
| 124 | + |
| 125 | + return vmList; |
| 126 | + } |
| 127 | + |
| 128 | + private static Cloudlet createCloudlet(final Vm vm) { |
| 129 | + return new CloudletSimple(CLOUDLET_LENGTH, 1).setVm(vm); |
| 130 | + } |
| 131 | +} |
0 commit comments