Skip to content

Commit 9f44909

Browse files
author
Anthony Xu
committed
use event instead of poll for xapi async call in XS 6.2 and above to reduce the pressure on XAPI
1 parent 0ae35dd commit 9f44909

3 files changed

Lines changed: 72 additions & 65 deletions

File tree

‎plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@
214214
import org.w3c.dom.NodeList;
215215
import org.xml.sax.InputSource;
216216
import org.xml.sax.SAXException;
217+
import java.util.concurrent.TimeoutException;
217218

218219
import javax.ejb.Local;
219220
import javax.naming.ConfigurationException;
@@ -664,16 +665,11 @@ protected String callHostPluginAsync(Connection conn, String plugin,
664665
+ " with args " + getArgsString(args)
665666
+ " due to HandleInvalid clazz:" + e.clazz + ", handle:"
666667
+ e.handle);
667-
} catch (XenAPIException e) {
668+
} catch (Exception e) {
668669
s_logger.warn(
669670
"callHostPlugin failed for cmd: " + cmd + " with args "
670671
+ getArgsString(args) + " due to " + e.toString(),
671672
e);
672-
} catch (XmlRpcException e) {
673-
s_logger.warn(
674-
"callHostPlugin failed for cmd: " + cmd + " with args "
675-
+ getArgsString(args) + " due to " + e.getMessage(),
676-
e);
677673
} finally {
678674
if (task != null) {
679675
try {
@@ -3310,7 +3306,7 @@ protected boolean isDmcEnabled(Connection conn, Host host) throws XenAPIExceptio
33103306
return false;
33113307
}
33123308

3313-
protected void waitForTask(Connection c, Task task, long pollInterval, long timeout) throws XenAPIException, XmlRpcException {
3309+
protected void waitForTask(Connection c, Task task, long pollInterval, long timeout) throws XenAPIException, XmlRpcException, TimeoutException {
33143310
long beginTime = System.currentTimeMillis();
33153311
if (s_logger.isTraceEnabled()) {
33163312
s_logger.trace("Task " + task.getNameLabel(c) + " (" + task.getUuid(c) + ") sent to " + c.getSessionReference() + " is pending completion with a " + timeout +
@@ -3328,7 +3324,7 @@ protected void waitForTask(Connection c, Task task, long pollInterval, long time
33283324
String msg = "Async " + timeout / 1000 + " seconds timeout for task " + task.toString();
33293325
s_logger.warn(msg);
33303326
task.cancel(c);
3331-
throw new Types.BadAsyncResult(msg);
3327+
throw new TimeoutException(msg);
33323328
}
33333329
}
33343330
}
@@ -3347,7 +3343,7 @@ protected void checkForSuccess(Connection c, Task task) throws XenAPIException,
33473343
}
33483344
}
33493345

3350-
void rebootVM(Connection conn, VM vm, String vmName) throws XmlRpcException {
3346+
void rebootVM(Connection conn, VM vm, String vmName) throws Exception {
33513347
Task task = null;
33523348
try {
33533349
task = vm.cleanRebootAsync(conn);
@@ -3403,7 +3399,7 @@ void shutdownVM(Connection conn, VM vm, String vmName) throws XmlRpcException {
34033399
//poll every 1 seconds , timeout after 10 minutes
34043400
waitForTask(conn, task, 1000, 10 * 60 * 1000);
34053401
checkForSuccess(conn, task);
3406-
} catch (Types.HandleInvalid e) {
3402+
} catch (TimeoutException e) {
34073403
if (vm.getPowerState(conn) == Types.VmPowerState.HALTED) {
34083404
task = null;
34093405
return;
@@ -3448,7 +3444,7 @@ void shutdownVM(Connection conn, VM vm, String vmName) throws XmlRpcException {
34483444
}
34493445
}
34503446

3451-
void startVM(Connection conn, Host host, VM vm, String vmName) throws XmlRpcException {
3447+
void startVM(Connection conn, Host host, VM vm, String vmName) throws Exception {
34523448
Task task = null;
34533449
try {
34543450
task = vm.startOnAsync(conn, host, false, true);
@@ -3463,7 +3459,7 @@ void startVM(Connection conn, Host host, VM vm, String vmName) throws XmlRpcExce
34633459
return;
34643460
}
34653461
throw new CloudRuntimeException("Start VM " + vmName + " catch HandleInvalid and VM is not in RUNNING state");
3466-
} catch (Types.BadAsyncResult e) {
3462+
} catch (TimeoutException e) {
34673463
if (vm.getPowerState(conn) == Types.VmPowerState.RUNNING) {
34683464
s_logger.debug("VM " + vmName + " is in Running status");
34693465
task = null;
@@ -3486,7 +3482,7 @@ void startVM(Connection conn, Host host, VM vm, String vmName) throws XmlRpcExce
34863482
}
34873483
}
34883484

3489-
private void migrateVM(Connection conn, Host destHost, VM vm, String vmName) throws XmlRpcException {
3485+
private void migrateVM(Connection conn, Host destHost, VM vm, String vmName) throws Exception {
34903486
Task task = null;
34913487
try {
34923488
Map<String, String> other = new HashMap<String, String>();
@@ -3519,7 +3515,7 @@ private void migrateVM(Connection conn, Host destHost, VM vm, String vmName) thr
35193515
}
35203516
}
35213517

3522-
protected VDI cloudVDIcopy(Connection conn, VDI vdi, SR sr, int wait) throws XenAPIException, XmlRpcException {
3518+
protected VDI cloudVDIcopy(Connection conn, VDI vdi, SR sr, int wait) throws Exception {
35233519
Task task = null;
35243520
if (wait == 0) {
35253521
wait = 2 * 60 * 60;
@@ -3568,7 +3564,7 @@ protected String callHostPluginAsync(Connection conn, String plugin, String cmd,
35683564
e.handle);
35693565
} catch (XenAPIException e) {
35703566
s_logger.warn("callHostPlugin failed for cmd: " + cmd + " with args " + getArgsString(args) + " due to " + e.toString(), e);
3571-
} catch (XmlRpcException e) {
3567+
} catch (Exception e) {
35723568
s_logger.warn("callHostPlugin failed for cmd: " + cmd + " with args " + getArgsString(args) + " due to " + e.getMessage(), e);
35733569
} finally {
35743570
if (task != null) {

‎plugins/hypervisors/xen/src/com/cloud/hypervisor/xen/resource/XenServer620Resource.java‎

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@
1919
import com.xensource.xenapi.Host;
2020
import com.xensource.xenapi.HostPatch;
2121
import com.xensource.xenapi.PoolPatch;
22+
import com.xensource.xenapi.Event;
23+
import com.xensource.xenapi.Task;
24+
import com.xensource.xenapi.Types;
25+
import com.xensource.xenapi.Types.XenAPIException;
26+
import org.apache.xmlrpc.XmlRpcException;
2227
import org.apache.cloudstack.hypervisor.xenserver.XenserverConfigs;
2328
import java.util.Map;
2429
import java.util.Set;
25-
30+
import java.util.HashSet;
2631
import javax.ejb.Local;
27-
2832
import org.apache.log4j.Logger;
29-
33+
import java.util.concurrent.TimeoutException;
3034
import com.cloud.agent.api.StartupRoutingCommand;
3135
import com.cloud.resource.ServerResource;
3236

@@ -105,4 +109,56 @@ protected void fillHostInfo(Connection conn, StartupRoutingCommand cmd) {
105109
}
106110
cmd.setHostDetails(details);
107111
}
112+
113+
114+
protected void waitForTask(Connection c, Task task, long pollInterval, long timeout) throws XenAPIException, XmlRpcException, TimeoutException {
115+
long beginTime = System.currentTimeMillis();
116+
if (s_logger.isTraceEnabled()) {
117+
s_logger.trace("Task " + task.getNameLabel(c) + " (" + task.getType(c) + ") sent to " + c.getSessionReference() + " is pending completion with a " + timeout +
118+
"ms timeout");
119+
}
120+
Set<String> classes = new HashSet<String>();
121+
classes.add("Task/" + task.toWireString());
122+
String token = "";
123+
Double t = new Double(timeout / 1000);
124+
while (true) {
125+
Map<?, ?> map = Event.properFrom(c, classes, token, t);
126+
token = (String)map.get("token");
127+
@SuppressWarnings("unchecked")
128+
Set<Event.Record> events = (Set<Event.Record>)map.get("events");
129+
if (events.size() == 0) {
130+
String msg = "Async " + timeout / 1000 + " seconds timeout for task " + task.toString();
131+
s_logger.warn(msg);
132+
task.cancel(c);
133+
throw new TimeoutException(msg);
134+
}
135+
for (Event.Record rec : events) {
136+
if (!(rec.snapshot instanceof Task.Record)) {
137+
if (s_logger.isDebugEnabled()) {
138+
s_logger.debug("Skipping over " + rec);
139+
}
140+
continue;
141+
}
142+
143+
Task.Record taskRecord = (Task.Record)rec.snapshot;
144+
145+
if (taskRecord.status != Types.TaskStatusType.PENDING) {
146+
if (s_logger.isDebugEnabled()) {
147+
s_logger.debug("Task is done " + taskRecord.status);
148+
}
149+
return;
150+
} else {
151+
s_logger.debug("Task is not done " + taskRecord);
152+
}
153+
}
154+
if (System.currentTimeMillis() - beginTime > timeout) {
155+
String msg = "Async " + timeout / 1000 + " seconds timeout for task " + task.toString();
156+
s_logger.warn(msg);
157+
task.cancel(c);
158+
throw new TimeoutException(msg);
159+
}
160+
}
161+
}
162+
163+
108164
}

‎plugins/hypervisors/xen/src/org/apache/cloudstack/hypervisor/xenserver/XenServerResourceNewBase.java‎

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,26 @@
2121
import java.util.HashSet;
2222
import java.util.Map;
2323
import java.util.Set;
24-
import java.util.concurrent.TimeoutException;
2524

2625
import org.apache.log4j.Logger;
2726
import org.apache.xmlrpc.XmlRpcException;
2827

2928
import com.xensource.xenapi.Connection;
30-
import com.xensource.xenapi.Event;
3129
import com.xensource.xenapi.Host;
3230
import com.xensource.xenapi.Pool;
33-
import com.xensource.xenapi.Task;
34-
import com.xensource.xenapi.Types;
3531
import com.xensource.xenapi.Types.XenAPIException;
3632
import com.xensource.xenapi.VM;
33+
import com.xensource.xenapi.Event;
3734

3835
import com.cloud.agent.api.Answer;
3936
import com.cloud.agent.api.ClusterSyncAnswer;
4037
import com.cloud.agent.api.ClusterSyncCommand;
4138
import com.cloud.agent.api.StartupCommand;
42-
import com.cloud.hypervisor.xen.resource.XenServer620SP1Resource;
4339
import com.cloud.utils.Pair;
4440
import com.cloud.utils.exception.CloudRuntimeException;
4541
import com.cloud.vm.VirtualMachine;
4642
import com.cloud.vm.VirtualMachineName;
43+
import com.cloud.hypervisor.xen.resource.XenServer620SP1Resource;
4744

4845
/**
4946
*
@@ -99,48 +96,6 @@ public StartupCommand[] initialize() throws IllegalArgumentException {
9996
return cmds;
10097
}
10198

102-
protected void waitForTask2(Connection c, Task task, long pollInterval, long timeout) throws XenAPIException, XmlRpcException, TimeoutException {
103-
long beginTime = System.currentTimeMillis();
104-
if (s_logger.isTraceEnabled()) {
105-
s_logger.trace("Task " + task.getNameLabel(c) + " (" + task.getType(c) + ") sent to " + c.getSessionReference() + " is pending completion with a " + timeout +
106-
"ms timeout");
107-
}
108-
Set<String> classes = new HashSet<String>();
109-
classes.add("Task/" + task.toString());
110-
String token = "";
111-
Double t = new Double(timeout / 1000);
112-
while (true) {
113-
Map<?, ?> map = Event.properFrom(c, classes, token, t);
114-
token = (String)map.get("token");
115-
@SuppressWarnings("unchecked")
116-
Set<Event.Record> events = (Set<Event.Record>)map.get("events");
117-
if (events.size() == 0) {
118-
String msg = "Async " + timeout / 1000 + " seconds timeout for task " + task.toString();
119-
s_logger.warn(msg);
120-
task.cancel(c);
121-
throw new TimeoutException(msg);
122-
}
123-
for (Event.Record rec : events) {
124-
if (!(rec.snapshot instanceof Task.Record)) {
125-
if (s_logger.isDebugEnabled()) {
126-
s_logger.debug("Skipping over " + rec);
127-
}
128-
continue;
129-
}
130-
131-
Task.Record taskRecord = (Task.Record)rec.snapshot;
132-
133-
if (taskRecord.status != Types.TaskStatusType.PENDING) {
134-
if (s_logger.isDebugEnabled()) {
135-
s_logger.debug("Task is done " + taskRecord.status);
136-
}
137-
return;
138-
} else {
139-
s_logger.debug("Task is not done " + taskRecord);
140-
}
141-
}
142-
}
143-
}
14499

145100
@Override
146101
protected Answer execute(final ClusterSyncCommand cmd) {

0 commit comments

Comments
 (0)