Search in sources :

Example 11 with Task

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

the class XenServerResourceNewBase method waitForTask2.

protected void waitForTask2(final Connection c, final Task task, final long pollInterval, final long timeout) throws XenAPIException, XmlRpcException, TimeoutException {
    final long beginTime = System.currentTimeMillis();
    if (s_logger.isTraceEnabled()) {
        s_logger.trace("Task " + task.getNameLabel(c) + " (" + task.getType(c) + ") sent to " + c.getSessionReference() + " is pending completion with a " + timeout + "ms timeout");
    }
    final Set<String> classes = new HashSet<String>();
    classes.add("Task/" + task.toWireString());
    String token = "";
    final Double t = new Double(timeout / 1000);
    while (true) {
        final EventBatch map = Event.from(c, classes, token, t);
        token = map.token;
        @SuppressWarnings("unchecked") final Set<Event.Record> events = map.events;
        if (events.size() == 0) {
            final String msg = "No event for task " + task.toWireString();
            s_logger.warn(msg);
            task.cancel(c);
            throw new TimeoutException(msg);
        }
        for (final Event.Record rec : events) {
            if (!(rec.snapshot instanceof Task.Record)) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Skipping over " + rec);
                }
                continue;
            }
            final Task.Record taskRecord = (Task.Record) rec.snapshot;
            if (taskRecord.status != Types.TaskStatusType.PENDING) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Task, ref:" + task.toWireString() + ", UUID:" + taskRecord.uuid + " is done " + taskRecord.status);
                }
                return;
            } else {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Task: ref:" + task.toWireString() + ", UUID:" + taskRecord.uuid + " progress: " + taskRecord.progress);
                }
            }
        }
        if (System.currentTimeMillis() - beginTime > timeout) {
            final String msg = "Async " + timeout / 1000 + " seconds timeout for task " + task.toString();
            s_logger.warn(msg);
            task.cancel(c);
            throw new TimeoutException(msg);
        }
    }
}
Also used : Task(com.xensource.xenapi.Task) Event(com.xensource.xenapi.Event) EventBatch(com.xensource.xenapi.EventBatch) HashSet(java.util.HashSet) TimeoutException(java.util.concurrent.TimeoutException)

Example 12 with Task

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

the class CitrixResourceBase method cloudVDIcopy.

protected VDI cloudVDIcopy(final Connection conn, final VDI vdi, final SR sr, int wait) throws Exception {
    Task task = null;
    if (wait == 0) {
        wait = 2 * 60 * 60;
    }
    try {
        task = vdi.copyAsync(conn, sr);
        // poll every 1 seconds , timeout after 2 hours
        waitForTask(conn, task, 1000, (long) wait * 1000);
        checkForSuccess(conn, task);
        final VDI dvdi = Types.toVDI(task, conn);
        return dvdi;
    } finally {
        if (task != null) {
            try {
                task.destroy(conn);
            } catch (final Exception e) {
                s_logger.debug("unable to destroy task(" + task.toString() + ") on host(" + _host.getUuid() + ") due to " + e.toString());
            }
        }
    }
}
Also used : Task(com.xensource.xenapi.Task) VDI(com.xensource.xenapi.VDI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 13 with Task

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

the class CitrixResourceBase method callHostPluginAsync.

protected String callHostPluginAsync(final Connection conn, final String plugin, final String cmd, final int wait, final String... params) {
    final int timeout = wait * 1000;
    final Map<String, String> args = new HashMap<String, String>();
    Task task = null;
    try {
        for (int i = 0; i < params.length; i += 2) {
            args.put(params[i], params[i + 1]);
        }
        if (s_logger.isTraceEnabled()) {
            s_logger.trace("callHostPlugin executing for command " + cmd + " with " + getArgsString(args));
        }
        final Host host = Host.getByUuid(conn, _host.getUuid());
        task = host.callPluginAsync(conn, plugin, cmd, args);
        // poll every 1 seconds
        waitForTask(conn, task, 1000, timeout);
        checkForSuccess(conn, task);
        final String result = task.getResult(conn);
        if (s_logger.isTraceEnabled()) {
            s_logger.trace("callHostPlugin Result: " + result);
        }
        return result.replace("<value>", "").replace("</value>", "").replace("\n", "");
    } catch (final Types.HandleInvalid e) {
        s_logger.warn("callHostPlugin failed for cmd: " + cmd + " with args " + getArgsString(args) + " due to HandleInvalid clazz:" + e.clazz + ", handle:" + e.handle);
    } catch (final XenAPIException e) {
        s_logger.warn("callHostPlugin failed for cmd: " + cmd + " with args " + getArgsString(args) + " due to " + e.toString(), e);
    } catch (final Exception e) {
        s_logger.warn("callHostPlugin failed for cmd: " + cmd + " with args " + getArgsString(args) + " due to " + e.getMessage(), e);
    } finally {
        if (task != null) {
            try {
                task.destroy(conn);
            } catch (final Exception e1) {
                s_logger.debug("unable to destroy task(" + task.toString() + ") on host(" + _host.getUuid() + ") due to " + e1.toString());
            }
        }
    }
    return null;
}
Also used : Types(com.xensource.xenapi.Types) Task(com.xensource.xenapi.Task) HashMap(java.util.HashMap) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 14 with Task

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

the class XenServer610WrapperTest method testXenServer610MigrateVolumeCommandWrapper.

@Test
public void testXenServer610MigrateVolumeCommandWrapper() {
    final String uuid = "206b21a7-c6ec-40e2-b5e2-f861b9612f04";
    final Connection conn = Mockito.mock(Connection.class);
    final SR destinationPool = Mockito.mock(SR.class);
    final VDI srcVolume = Mockito.mock(VDI.class);
    final Task task = Mockito.mock(Task.class);
    final long volumeId = 1l;
    final String volumePath = "206b21a7-c6ec-40e2-b5e2-f861b9612f04";
    final StoragePool pool = Mockito.mock(StoragePool.class);
    final int timeout = 120;
    final Map<String, String> other = new HashMap<String, String>();
    other.put("live", "true");
    final MigrateVolumeCommand createStorageCommand = new MigrateVolumeCommand(volumeId, volumePath, pool, timeout);
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(xenServer610Resource.getConnection()).thenReturn(conn);
    when(pool.getUuid()).thenReturn(uuid);
    when(xenServer610Resource.getStorageRepository(conn, uuid)).thenReturn(destinationPool);
    when(xenServer610Resource.getVDIbyUuid(conn, volumePath)).thenReturn(srcVolume);
    try {
        when(srcVolume.poolMigrateAsync(conn, destinationPool, other)).thenReturn(task);
    } catch (final BadServerResponse e) {
        fail(e.getMessage());
    } catch (final XenAPIException e) {
        fail(e.getMessage());
    } catch (final XmlRpcException e) {
        fail(e.getMessage());
    }
    when(xenServer610Resource.getMigrateWait()).thenReturn(120);
    final Answer answer = wrapper.execute(createStorageCommand, xenServer610Resource);
    verify(xenServer610Resource, times(1)).getConnection();
    //        try {
    //            verify(xenServer610Resource, times(1)).waitForTask(conn, task, 1000, timeout);
    //            verify(xenServer610Resource, times(1)).checkForSuccess(conn, task);
    //        } catch (final XenAPIException e) {
    //            fail(e.getMessage());
    //        } catch (final XmlRpcException e) {
    //            fail(e.getMessage());
    //        } catch (final TimeoutException e) {
    //            fail(e.getMessage());
    //        }
    assertFalse(answer.getResult());
}
Also used : MigrateVolumeCommand(com.cloud.agent.api.storage.MigrateVolumeCommand) Task(com.xensource.xenapi.Task) StoragePool(com.cloud.storage.StoragePool) BadServerResponse(com.xensource.xenapi.Types.BadServerResponse) HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Answer(com.cloud.agent.api.Answer) VDI(com.xensource.xenapi.VDI) XmlRpcException(org.apache.xmlrpc.XmlRpcException) SR(com.xensource.xenapi.SR) Test(org.junit.Test)

Example 15 with Task

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

the class XenServer610MigrateWithStorageSendCommandWrapper method execute.

@Override
public Answer execute(final MigrateWithStorageSendCommand command, final XenServer610Resource xenServer610Resource) {
    final Connection connection = xenServer610Resource.getConnection();
    final VirtualMachineTO vmSpec = command.getVirtualMachine();
    final List<Pair<VolumeTO, Object>> volumeToSr = command.getVolumeToSr();
    final List<Pair<NicTO, Object>> nicToNetwork = command.getNicToNetwork();
    final Map<String, String> token = command.getToken();
    final String vmName = vmSpec.getName();
    Task task = null;
    try {
        // In a cluster management server setup, the migrate with storage receive and send
        // commands and answers may have to be forwarded to another management server. This
        // happens when the host/resource on which the command has to be executed is owned
        // by the second management server. The serialization/deserialization of the command
        // and answers fails as the xapi SR and Network class type isn't understand by the
        // agent attache. Seriliaze the SR and Network objects here to a string and pass in
        // the answer object. It'll be deserialzed and object created in migrate with
        // storage send command execution.
        Gson gson = new Gson();
        final Map<String, String> other = new HashMap<String, String>();
        other.put("live", "true");
        // Create the vdi map which tells what volumes of the vm need to go
        // on which sr on the destination.
        final Map<VDI, SR> vdiMap = new HashMap<VDI, SR>();
        for (final Pair<VolumeTO, Object> entry : volumeToSr) {
            if (entry.second() instanceof SR) {
                final SR sr = (SR) entry.second();
                final VDI vdi = xenServer610Resource.getVDIbyUuid(connection, entry.first().getPath());
                vdiMap.put(vdi, sr);
            } else {
                throw new CloudRuntimeException("The object " + entry.second() + " passed is not of type SR.");
            }
        }
        final Set<VM> vms = VM.getByNameLabel(connection, vmSpec.getName());
        VM vmToMigrate = null;
        if (vms != null) {
            vmToMigrate = vms.iterator().next();
        }
        // Create the vif map.
        final Map<VIF, Network> vifMap = new HashMap<VIF, Network>();
        for (final Pair<NicTO, Object> entry : nicToNetwork) {
            if (entry.second() instanceof Network) {
                final Network network = (Network) entry.second();
                final VIF vif = xenServer610Resource.getVifByMac(connection, vmToMigrate, entry.first().getMac());
                vifMap.put(vif, network);
            } else {
                throw new CloudRuntimeException("The object " + entry.second() + " passed is not of type Network.");
            }
        }
        // Check migration with storage is possible.
        task = vmToMigrate.assertCanMigrateAsync(connection, token, true, vdiMap, vifMap, other);
        try {
            // poll every 1 seconds.
            final long timeout = xenServer610Resource.getMigrateWait() * 1000L;
            xenServer610Resource.waitForTask(connection, task, 1000, timeout);
            xenServer610Resource.checkForSuccess(connection, task);
        } catch (final Types.HandleInvalid e) {
            s_logger.error("Error while checking if vm " + vmName + " can be migrated.", e);
            throw new CloudRuntimeException("Error while checking if vm " + vmName + " can be migrated.", e);
        }
        // Migrate now.
        task = vmToMigrate.migrateSendAsync(connection, token, true, vdiMap, vifMap, other);
        try {
            // poll every 1 seconds.
            final long timeout = xenServer610Resource.getMigrateWait() * 1000L;
            xenServer610Resource.waitForTask(connection, task, 1000, timeout);
            xenServer610Resource.checkForSuccess(connection, task);
        } catch (final Types.HandleInvalid e) {
            s_logger.error("Error while migrating vm " + vmName, e);
            throw new CloudRuntimeException("Error while migrating vm " + vmName, e);
        }
        final Set<VolumeTO> volumeToSet = null;
        return new MigrateWithStorageSendAnswer(command, volumeToSet);
    } catch (final CloudRuntimeException e) {
        s_logger.error("Migration of vm " + vmName + " with storage failed due to " + e.toString(), e);
        return new MigrateWithStorageSendAnswer(command, e);
    } catch (final Exception e) {
        s_logger.error("Migration of vm " + vmName + " with storage failed due to " + e.toString(), e);
        return new MigrateWithStorageSendAnswer(command, e);
    } finally {
        if (task != null) {
            try {
                task.destroy(connection);
            } catch (final Exception e) {
                s_logger.debug("Unable to destroy task " + task.toString() + " on host " + xenServer610Resource.getHost().getUuid() + " due to " + e.toString());
            }
        }
    }
}
Also used : Types(com.xensource.xenapi.Types) Task(com.xensource.xenapi.Task) MigrateWithStorageSendAnswer(com.cloud.agent.api.MigrateWithStorageSendAnswer) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) VolumeTO(com.cloud.agent.api.to.VolumeTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.xensource.xenapi.Network) VDI(com.xensource.xenapi.VDI) Pair(com.cloud.utils.Pair) SR(com.xensource.xenapi.SR) NicTO(com.cloud.agent.api.to.NicTO) Connection(com.xensource.xenapi.Connection) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM)

Aggregations

Task (com.xensource.xenapi.Task)21 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)17 XenAPIException (com.xensource.xenapi.Types.XenAPIException)16 XmlRpcException (org.apache.xmlrpc.XmlRpcException)16 InternalErrorException (com.cloud.exception.InternalErrorException)15 VDI (com.xensource.xenapi.VDI)14 SR (com.xensource.xenapi.SR)13 Connection (com.xensource.xenapi.Connection)12 Types (com.xensource.xenapi.Types)10 TimeoutException (java.util.concurrent.TimeoutException)8 NfsTO (com.cloud.agent.api.to.NfsTO)7 IOException (java.io.IOException)7 MalformedURLException (java.net.MalformedURLException)7 URI (java.net.URI)7 URISyntaxException (java.net.URISyntaxException)7 HashMap (java.util.HashMap)7 ConfigurationException (javax.naming.ConfigurationException)7 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)7 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)7 SAXException (org.xml.sax.SAXException)7