Search in sources :

Example 36 with PBD

use of com.xensource.xenapi.PBD in project cloudstack by apache.

the class Xenserver625StorageProcessor method createNewFileSr.

/**
 * Creates a new file SR for the given path. If any of XenServer's checked exception occurs, we use method {@link #removeSrAndPbdIfPossible(Connection, SR, PBD)} to clean the created PBD and SR entries.
 * To avoid race conditions between management servers, we are using a deterministic srUuid for the file SR to be created (we are leaving XenServer with the burden of managing race conditions). The UUID is based on the SR file path, and is generated using {@link UUID#nameUUIDFromBytes(byte[])}.
 * If there is an SR with the generated UUID, this means that some other management server has just created it. An exception will occur and this exception will be an {@link InternalError}. The exception will contain {@link InternalError#message} a message saying 'Db_exn.Uniqueness_constraint_violation'.
 * For cases where the previous described error happens, we catch the exception and use the method {@link #retrieveAlreadyConfiguredSrWithoutException(Connection, String)}.
 */
protected SR createNewFileSr(Connection conn, String srPath) {
    String hostUuid = hypervisorResource.getHost().getUuid();
    s_logger.debug(String.format("Creating file SR for path [%s] on host [%s]", srPath, this.hypervisorResource._host.getUuid()));
    SR sr = null;
    PBD pbd = null;
    try {
        Host host = Host.getByUuid(conn, hostUuid);
        String srUuid = UUID.nameUUIDFromBytes(srPath.getBytes()).toString();
        Map<String, String> smConfig = new HashMap<String, String>();
        sr = SR.introduce(conn, srUuid, srPath, srPath, "file", "file", false, smConfig);
        PBD.Record record = new PBD.Record();
        record.host = host;
        record.SR = sr;
        smConfig.put("location", srPath);
        record.deviceConfig = smConfig;
        pbd = PBD.create(conn, record);
        pbd.plug(conn);
        sr.scan(conn);
        return sr;
    } catch (XenAPIException | XmlRpcException e) {
        if (e instanceof Types.InternalError) {
            String expectedDuplicatedFileSrErrorMessage = "Db_exn.Uniqueness_constraint_violation";
            Types.InternalError internalErrorException = (Types.InternalError) e;
            if (StringUtils.contains(internalErrorException.message, expectedDuplicatedFileSrErrorMessage)) {
                s_logger.debug(String.format("It seems that we have hit a race condition case here while creating file SR for [%s]. Instead of creating one, we will reuse the one that already exist in the XenServer pool.", srPath));
                return retrieveAlreadyConfiguredSrWithoutException(conn, srPath);
            }
        }
        removeSrAndPbdIfPossible(conn, sr, pbd);
        s_logger.debug(String.format("Could not create file SR [%s] on host [%s].", srPath, hostUuid), e);
        return null;
    }
}
Also used : Types(com.xensource.xenapi.Types) HashMap(java.util.HashMap) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) PBD(com.xensource.xenapi.PBD) XmlRpcException(org.apache.xmlrpc.XmlRpcException) SR(com.xensource.xenapi.SR)

Example 37 with PBD

use of com.xensource.xenapi.PBD in project cloudstack by apache.

the class Xenserver625StorageProcessorTest method createNewFileSrTest.

@Test
@PrepareForTest({ Host.class, SR.class, PBD.class })
public void createNewFileSrTest() throws XenAPIException, XmlRpcException {
    String uuid = "hostUuid";
    Mockito.when(citrixResourceBase._host.getUuid()).thenReturn(uuid);
    SR srMock = Mockito.mock(SR.class);
    Mockito.doReturn(srMock).when(xenserver625StorageProcessor).retrieveAlreadyConfiguredSrWithoutException(connectionMock, pathMock);
    String srUuid = UUID.nameUUIDFromBytes(pathMock.getBytes()).toString();
    Host hostMock = Mockito.mock(Host.class);
    PowerMockito.mockStatic(Host.class);
    PowerMockito.when(Host.getByUuid(connectionMock, uuid)).thenReturn(hostMock);
    PowerMockito.mockStatic(SR.class);
    PowerMockito.when(SR.introduce(Mockito.eq(connectionMock), Mockito.eq(srUuid), Mockito.eq(pathMock), Mockito.eq(pathMock), Mockito.eq("file"), Mockito.eq("file"), Mockito.eq(false), Mockito.anyMapOf(String.class, String.class))).thenReturn(srMock);
    PowerMockito.mockStatic(PBD.class);
    PBD pbdMock = Mockito.mock(PBD.class);
    PowerMockito.when(PBD.create(Mockito.eq(connectionMock), Mockito.any(Record.class))).thenReturn(pbdMock);
    Mockito.doNothing().when(xenserver625StorageProcessor).removeSrAndPbdIfPossible(Mockito.eq(connectionMock), Mockito.any(SR.class), Mockito.any(PBD.class));
    SR sr = xenserver625StorageProcessor.createNewFileSr(connectionMock, pathMock);
    Assert.assertEquals(srMock, sr);
    Mockito.verify(xenserver625StorageProcessor, times(0)).removeSrAndPbdIfPossible(Mockito.eq(connectionMock), Mockito.any(SR.class), Mockito.any(PBD.class));
    Mockito.verify(xenserver625StorageProcessor, times(0)).retrieveAlreadyConfiguredSrWithoutException(connectionMock, pathMock);
    Mockito.verify(srMock).scan(connectionMock);
    Mockito.verify(pbdMock).plug(connectionMock);
    PowerMockito.verifyStatic(PBD.class);
    SR.introduce(Mockito.eq(connectionMock), Mockito.eq(srUuid), Mockito.eq(pathMock), Mockito.eq(pathMock), Mockito.eq("file"), Mockito.eq("file"), Mockito.eq(false), Mockito.anyMapOf(String.class, String.class));
    PBD.create(Mockito.eq(connectionMock), Mockito.any(Record.class));
}
Also used : PBD(com.xensource.xenapi.PBD) Host(com.xensource.xenapi.Host) Record(com.xensource.xenapi.PBD.Record) SR(com.xensource.xenapi.SR) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 38 with PBD

use of com.xensource.xenapi.PBD in project cloudstack by apache.

the class Xenserver625StorageProcessorTest method removeSrAndPbdIfPossibleBothPbdAndSrNotNull.

@Test
public void removeSrAndPbdIfPossibleBothPbdAndSrNotNull() {
    SR srMock = Mockito.mock(SR.class);
    Mockito.doNothing().when(xenserver625StorageProcessor).forgetSr(connectionMock, srMock);
    PBD pbdMock = Mockito.mock(PBD.class);
    Mockito.doNothing().when(xenserver625StorageProcessor).unplugPbd(connectionMock, pbdMock);
    xenserver625StorageProcessor.removeSrAndPbdIfPossible(connectionMock, srMock, pbdMock);
    Mockito.verify(xenserver625StorageProcessor).forgetSr(connectionMock, srMock);
    Mockito.verify(xenserver625StorageProcessor).unplugPbd(connectionMock, pbdMock);
}
Also used : PBD(com.xensource.xenapi.PBD) SR(com.xensource.xenapi.SR) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 39 with PBD

use of com.xensource.xenapi.PBD in project cloudstack by apache.

the class Xenserver625StorageProcessorTest method unplugPbdTest.

@Test
public void unplugPbdTest() throws XenAPIException, XmlRpcException {
    PBD pbdMock = Mockito.mock(PBD.class);
    xenserver625StorageProcessor.unplugPbd(connectionMock, pbdMock);
    Mockito.verify(pbdMock).getUuid(connectionMock);
    Mockito.verify(pbdMock).unplug(connectionMock);
}
Also used : PBD(com.xensource.xenapi.PBD) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 40 with PBD

use of com.xensource.xenapi.PBD in project cloudstack by apache.

the class CitrixResizeVolumeCommandWrapper method resizeSr.

private void resizeSr(Connection conn, ResizeVolumeCommand command) {
    // If this is managed storage, re-size the SR, too.
    // The logical unit/volume has already been re-sized, so the SR needs to fill up the new space.
    String iScsiName = command.get_iScsiName();
    try {
        Set<SR> srs = SR.getByNameLabel(conn, iScsiName);
        Set<PBD> allPbds = new HashSet<>();
        for (SR sr : srs) {
            if (!(CitrixResourceBase.SRType.LVMOISCSI.equals(sr.getType(conn)))) {
                continue;
            }
            Set<PBD> pbds = sr.getPBDs(conn);
            if (pbds.size() <= 0) {
                s_logger.debug("No PBDs found for the following SR: " + sr.getNameLabel(conn));
            }
            allPbds.addAll(pbds);
        }
        for (PBD pbd : allPbds) {
            PBD.Record pbdr = pbd.getRecord(conn);
            if (pbdr.currentlyAttached) {
                pbd.unplug(conn);
                pbd.plug(conn);
            }
        }
    } catch (Throwable ex) {
        throw new CloudRuntimeException("Unable to resize volume: " + ex.getMessage());
    }
}
Also used : PBD(com.xensource.xenapi.PBD) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SR(com.xensource.xenapi.SR) HashSet(java.util.HashSet)

Aggregations

PBD (com.xensource.xenapi.PBD)40 SR (com.xensource.xenapi.SR)35 Host (com.xensource.xenapi.Host)24 XenAPIException (com.xensource.xenapi.Types.XenAPIException)22 XmlRpcException (org.apache.xmlrpc.XmlRpcException)21 HashMap (java.util.HashMap)14 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)9 VDI (com.xensource.xenapi.VDI)6 Test (org.junit.Test)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 IOException (java.io.IOException)5 MalformedURLException (java.net.MalformedURLException)5 URISyntaxException (java.net.URISyntaxException)5 Map (java.util.Map)5 TimeoutException (java.util.concurrent.TimeoutException)5 ConfigurationException (javax.naming.ConfigurationException)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)5 SAXException (org.xml.sax.SAXException)5 InternalErrorException (com.cloud.exception.InternalErrorException)4