Skip to content

Commit 537bf7c

Browse files
committed
add job path to help associate an API job to related internal job. Reviewed-By: Self
1 parent 414b1cb commit 537bf7c

14 files changed

Lines changed: 96 additions & 74 deletions

File tree

api/src/org/apache/cloudstack/context/CallContext.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ public static CallContext register(User callingUser, Account callingAccount, Str
131131

132132
protected static CallContext register(User callingUser, Account callingAccount, Long userId, Long accountId, String contextId) {
133133
/*
134+
<<<<<<< HEAD
134135
Unit tests will have multiple times of setup/tear-down call to this, remove assertions to all unit test to run
136+
=======
137+
>>>>>>> a7a8a19... BUG-ID: CS-19295: add job path to help associate an API job to related internal job. Reviewed-By: Self
135138
assert s_currentContext.get() == null : "There's a context already so what does this new register context mean? " + s_currentContext.get().toString();
136139
if (s_currentContext.get() != null) { // FIXME: This should be removed soon. I added this check only to surface all the places that have this problem.
137140
throw new CloudRuntimeException("There's a context already so what does this new register context mean? " + s_currentContext.get().toString());
@@ -154,6 +157,14 @@ protected static CallContext register(User callingUser, Account callingAccount,
154157
return callingContext;
155158
}
156159

160+
public static CallContext registerPlaceHolderContext() {
161+
CallContext context = new CallContext(0, 0, UUID.randomUUID().toString());
162+
s_currentContext.set(context);
163+
164+
s_currentContextStack.get().push(context);
165+
return context;
166+
}
167+
157168
public static CallContext register(User callingUser, Account callingAccount) {
158169
return register(callingUser, callingAccount, UUID.randomUUID().toString());
159170
}

api/src/org/apache/cloudstack/context/CallContextListener.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ public class CallContextListener implements ManagedContextListener<Object> {
3232

3333
@Override
3434
public Object onEnterContext(boolean reentry) {
35-
if (!reentry) {
35+
if (!reentry && CallContext.current() == null) {
3636
CallContext.registerSystemCallContextOnceOnly();
3737
}
38-
3938
return null;
4039
}
4140

engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4436,7 +4436,7 @@ public Object[] doInTransaction(TransactionStatus status) {
44364436
workJob.setStep(VmWorkJobVO.Step.Starting);
44374437
workJob.setVmType(VirtualMachine.Type.Instance);
44384438
workJob.setVmInstanceId(vm.getId());
4439-
workJob.setRelated(AsyncJobExecutionContext.getOriginJobContextId());
4439+
workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
44404440

44414441
// save work context info (there are some duplications)
44424442
VmWorkStart workInfo = new VmWorkStart(callingUser.getId(), callingAccount.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER);
@@ -4493,7 +4493,7 @@ public Object[] doInTransaction(TransactionStatus status) {
44934493
workJob.setStep(VmWorkJobVO.Step.Prepare);
44944494
workJob.setVmType(VirtualMachine.Type.Instance);
44954495
workJob.setVmInstanceId(vm.getId());
4496-
workJob.setRelated(AsyncJobExecutionContext.getOriginJobContextId());
4496+
workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
44974497

44984498
// save work context info (there are some duplications)
44994499
VmWorkStop workInfo = new VmWorkStop(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, cleanup);
@@ -4549,7 +4549,7 @@ public Object[] doInTransaction(TransactionStatus status) {
45494549
workJob.setStep(VmWorkJobVO.Step.Prepare);
45504550
workJob.setVmType(VirtualMachine.Type.Instance);
45514551
workJob.setVmInstanceId(vm.getId());
4552-
workJob.setRelated(AsyncJobExecutionContext.getOriginJobContextId());
4552+
workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
45534553

45544554
// save work context info (there are some duplications)
45554555
VmWorkReboot workInfo = new VmWorkReboot(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, params);
@@ -4604,7 +4604,7 @@ public Object[] doInTransaction(TransactionStatus status) {
46044604
workJob.setUserId(user.getId());
46054605
workJob.setVmType(VirtualMachine.Type.Instance);
46064606
workJob.setVmInstanceId(vm.getId());
4607-
workJob.setRelated(AsyncJobExecutionContext.getOriginJobContextId());
4607+
workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
46084608

46094609
// save work context info (there are some duplications)
46104610
VmWorkMigrate workInfo = new VmWorkMigrate(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, srcHostId, dest);
@@ -4656,7 +4656,7 @@ public Object[] doInTransaction(TransactionStatus status) {
46564656
workJob.setUserId(user.getId());
46574657
workJob.setVmType(VirtualMachine.Type.Instance);
46584658
workJob.setVmInstanceId(vm.getId());
4659-
workJob.setRelated(AsyncJobExecutionContext.getOriginJobContextId());
4659+
workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
46604660

46614661
// save work context info (there are some duplications)
46624662
VmWorkMigrateAway workInfo = new VmWorkMigrateAway(user.getId(), account.getId(), vm.getId(), VirtualMachineManagerImpl.VM_WORK_JOB_HANDLER, srcHostId);
@@ -4713,7 +4713,7 @@ public Object[] doInTransaction(TransactionStatus status) {
47134713
workJob.setUserId(user.getId());
47144714
workJob.setVmType(VirtualMachine.Type.Instance);
47154715
workJob.setVmInstanceId(vm.getId());
4716-
workJob.setRelated(AsyncJobExecutionContext.getOriginJobContextId());
4716+
workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
47174717

47184718
// save work context info (there are some duplications)
47194719
VmWorkMigrateWithStorage workInfo = new VmWorkMigrateWithStorage(user.getId(), account.getId(), vm.getId(),
@@ -4770,7 +4770,7 @@ public Object[] doInTransaction(TransactionStatus status) {
47704770
workJob.setUserId(user.getId());
47714771
workJob.setVmType(VirtualMachine.Type.Instance);
47724772
workJob.setVmInstanceId(vm.getId());
4773-
workJob.setRelated(AsyncJobExecutionContext.getOriginJobContextId());
4773+
workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
47744774

47754775
// save work context info (there are some duplications)
47764776
VmWorkMigrateForScale workInfo = new VmWorkMigrateForScale(user.getId(), account.getId(), vm.getId(),
@@ -4827,7 +4827,7 @@ public Object[] doInTransaction(TransactionStatus status) {
48274827
workJob.setUserId(user.getId());
48284828
workJob.setVmType(VirtualMachine.Type.Instance);
48294829
workJob.setVmInstanceId(vm.getId());
4830-
workJob.setRelated(AsyncJobExecutionContext.getOriginJobContextId());
4830+
workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
48314831

48324832
// save work context info (there are some duplications)
48334833
VmWorkStorageMigration workInfo = new VmWorkStorageMigration(user.getId(), account.getId(), vm.getId(),
@@ -4883,7 +4883,7 @@ public Object[] doInTransaction(TransactionStatus status) {
48834883
workJob.setUserId(user.getId());
48844884
workJob.setVmType(VirtualMachine.Type.Instance);
48854885
workJob.setVmInstanceId(vm.getId());
4886-
workJob.setRelated(AsyncJobExecutionContext.getOriginJobContextId());
4886+
workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
48874887

48884888
// save work context info (there are some duplications)
48894889
VmWorkAddVmToNetwork workInfo = new VmWorkAddVmToNetwork(user.getId(), account.getId(), vm.getId(),
@@ -4937,7 +4937,7 @@ public Object[] doInTransaction(TransactionStatus status) {
49374937
workJob.setUserId(user.getId());
49384938
workJob.setVmType(VirtualMachine.Type.Instance);
49394939
workJob.setVmInstanceId(vm.getId());
4940-
workJob.setRelated(AsyncJobExecutionContext.getOriginJobContextId());
4940+
workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
49414941

49424942
// save work context info (there are some duplications)
49434943
VmWorkRemoveNicFromVm workInfo = new VmWorkRemoveNicFromVm(user.getId(), account.getId(), vm.getId(),
@@ -4991,7 +4991,7 @@ public Object[] doInTransaction(TransactionStatus status) {
49914991
workJob.setUserId(user.getId());
49924992
workJob.setVmType(VirtualMachine.Type.Instance);
49934993
workJob.setVmInstanceId(vm.getId());
4994-
workJob.setRelated(AsyncJobExecutionContext.getOriginJobContextId());
4994+
workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
49954995

49964996
// save work context info (there are some duplications)
49974997
VmWorkRemoveVmFromNetwork workInfo = new VmWorkRemoveVmFromNetwork(user.getId(), account.getId(), vm.getId(),
@@ -5047,7 +5047,7 @@ public Object[] doInTransaction(TransactionStatus status) {
50475047
workJob.setUserId(user.getId());
50485048
workJob.setVmType(VirtualMachine.Type.Instance);
50495049
workJob.setVmInstanceId(vm.getId());
5050-
workJob.setRelated(AsyncJobExecutionContext.getOriginJobContextId());
5050+
workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
50515051

50525052
// save work context info (there are some duplications)
50535053
VmWorkReconfigure workInfo = new VmWorkReconfigure(user.getId(), account.getId(), vm.getId(),

engine/orchestration/src/com/cloud/vm/VmWorkJobDispatcher.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,14 @@ public void runJob(AsyncJob job) {
9696
return;
9797
}
9898

99-
CallContext.register(work.getUserId(), work.getAccountId(), job.getRelated());
99+
CallContext.register(work.getUserId(), work.getAccountId());
100100

101-
Pair<JobInfo.Status, String> result = handler.handleVmWorkJob(work);
102-
_asyncJobMgr.completeAsyncJob(job.getId(), result.first(), 0, result.second());
101+
try {
102+
Pair<JobInfo.Status, String> result = handler.handleVmWorkJob(work);
103+
_asyncJobMgr.completeAsyncJob(job.getId(), result.first(), 0, result.second());
104+
} finally {
105+
CallContext.unregister();
106+
}
103107
} finally {
104108
if (s_logger.isDebugEnabled())
105109
s_logger.debug("Done with run of VM work job: " + cmd + " for VM " + work.getVmId() + ", job origin: " + job.getRelated());
@@ -109,8 +113,6 @@ public void runJob(AsyncJob job) {
109113

110114
RuntimeException ex = new RuntimeException("Job failed due to exception " + e.getMessage());
111115
_asyncJobMgr.completeAsyncJob(job.getId(), JobInfo.Status.FAILED, 0, _asyncJobMgr.marshallResultObject(ex));
112-
} finally {
113-
CallContext.unregister();
114116
}
115117
}
116118
}

framework/jobs/src/org/apache/cloudstack/framework/jobs/AsyncJobExecutionContext.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,11 @@ public static void setCurrentExecutionContext(AsyncJobExecutionContext currentCo
204204
s_currentExectionContext.set(currentContext);
205205
}
206206

207-
public static String getOriginJobContextId() {
208-
return String.valueOf(CallContext.current().getContextId());
207+
public static String getOriginJobId() {
208+
AsyncJobExecutionContext context = AsyncJobExecutionContext.getCurrentExecutionContext();
209+
if (context != null && context.getJob() != null)
210+
return "Job-" + context.getJob().getId();
211+
212+
return "";
209213
}
210214
}

framework/jobs/src/org/apache/cloudstack/framework/jobs/impl/AsyncJobManagerImpl.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
import javax.naming.ConfigurationException;
3535

3636
import org.apache.log4j.Logger;
37+
import org.apache.log4j.NDC;
3738

3839
import org.apache.cloudstack.api.ApiErrorCode;
40+
import org.apache.cloudstack.context.CallContext;
3941
import org.apache.cloudstack.framework.config.ConfigDepot;
4042
import org.apache.cloudstack.framework.config.ConfigKey;
4143
import org.apache.cloudstack.framework.config.Configurable;
@@ -428,6 +430,24 @@ private long getJobRunNumber() {
428430

429431
private Runnable getExecutorRunnable(final AsyncJob job) {
430432
return new ManagedContextRunnable() {
433+
434+
@Override
435+
public void run() {
436+
// register place-holder context to avoid installing system account call context
437+
if (CallContext.current() == null)
438+
CallContext.registerPlaceHolderContext();
439+
440+
if (job.getRelated() != null && !job.getRelated().isEmpty())
441+
NDC.push("Job-" + job.getRelated() + "/" + "Job-" + job.getId());
442+
else
443+
NDC.push("Job-" + job.getId());
444+
try {
445+
super.run();
446+
} finally {
447+
NDC.pop();
448+
}
449+
}
450+
431451
@Override
432452
protected void runInContext() {
433453
long runNumber = getJobRunNumber();

framework/jobs/src/org/apache/cloudstack/framework/jobs/impl/AsyncJobVO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public class AsyncJobVO implements AsyncJob, JobInfo {
132132

133133
public AsyncJobVO() {
134134
uuid = UUID.randomUUID().toString();
135-
related = UUID.randomUUID().toString();
135+
related = "";
136136
status = Status.IN_PROGRESS;
137137
}
138138

framework/managed-context/src/main/java/org/apache/cloudstack/managed/context/ManagedContextRunnable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static ManagedContext initializeGlobalContext(ManagedContext context) {
4242
}
4343

4444
@Override
45-
final public void run() {
45+
public void run() {
4646
getContext().runWithContext(new Runnable() {
4747
@Override
4848
public void run() {

server/src/com/cloud/api/ApiAsyncJobDispatcher.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.apache.cloudstack.framework.jobs.AsyncJobDispatcher;
3737
import org.apache.cloudstack.framework.jobs.AsyncJobManager;
3838
import org.apache.cloudstack.jobs.JobInfo;
39-
import org.apache.cloudstack.managed.context.ManagedContext;
4039

4140
import com.cloud.user.Account;
4241
import com.cloud.user.User;
@@ -54,23 +53,12 @@ public class ApiAsyncJobDispatcher extends AdapterBase implements AsyncJobDispat
5453
private AsyncJobManager _asyncJobMgr;
5554
@Inject
5655
private EntityManager _entityMgr;
57-
@Inject
58-
ManagedContext _managedContext;
5956

6057
public ApiAsyncJobDispatcher() {
6158
}
6259

6360
@Override
6461
public void runJob(final AsyncJob job) {
65-
_managedContext.runWithContext(new Runnable() {
66-
@Override
67-
public void run() {
68-
runJobInContext(job);
69-
}
70-
});
71-
}
72-
73-
protected void runJobInContext(AsyncJob job) {
7462
BaseAsyncCmd cmdObj = null;
7563
try {
7664
Class<?> cmdClass = Class.forName(job.getCmd());
@@ -106,7 +94,7 @@ protected void runJobInContext(AsyncJob job) {
10694
accountObject = _entityMgr.findById(Account.class, Long.parseLong(acctIdStr));
10795
}
10896

109-
CallContext.register(user, accountObject, job.getRelated());
97+
CallContext.register(user, accountObject);
11098
try {
11199
// dispatch could ultimately queue the job
112100
_dispatcher.dispatch(cmdObj, params, true);

server/src/com/cloud/api/ApiServer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,9 +522,9 @@ private String queueCommand(BaseCmd cmdObj, Map<String, String> params) throws E
522522
params.put("cmdEventType", asyncCmd.getEventType().toString());
523523

524524
Long instanceId = (objectId == null) ? asyncCmd.getInstanceId() : objectId;
525-
AsyncJobVO job =
526-
new AsyncJobVO(ctx.getContextId(), callerUserId, caller.getId(), cmdObj.getClass().getName(), ApiGsonHelper.getBuilder().create().toJson(params),
527-
instanceId, asyncCmd.getInstanceType() != null ? asyncCmd.getInstanceType().toString() : null);
525+
AsyncJobVO job = new AsyncJobVO("", callerUserId, caller.getId(), cmdObj.getClass().getName(),
526+
ApiGsonHelper.getBuilder().create().toJson(params), instanceId,
527+
asyncCmd.getInstanceType() != null ? asyncCmd.getInstanceType().toString() : null);
528528
job.setDispatcher(_asyncDispatcher.getName());
529529

530530
long jobId = _asyncMgr.submitAsyncJob(job);

0 commit comments

Comments
 (0)