Repository navigation
framework: do not double-execute an async job after returning its queue item #14039
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rootnomad
wants to merge
5
commits into
apache:4.20
Choose a base branch
from
rootnomad:fix/asyncjob-double-execution
base: 4.20
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8c06db5
framework: do not double-execute an async job after returning its que…
rootnomad bc3cde8
framework: simplify executeQueueItem exception handling
rootnomad 095d9b3
framework: move the executeQueueItem deadlock TODO into its javadoc
rootnomad fd11c31
framework: clear the executing msid before returning a rejected job t…
rootnomad b38a0c7
framework: still run a rejected-dispatch job when the requeue also fails
rootnomad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...va/org/apache/cloudstack/framework/jobs/impl/AsyncJobManagerImplExecuteQueueItemTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| package org.apache.cloudstack.framework.jobs.impl; | ||
|
|
||
| import java.util.concurrent.RejectedExecutionException; | ||
|
|
||
| import org.apache.cloudstack.framework.jobs.dao.AsyncJobDao; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.InOrder; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.Mockito; | ||
| import org.mockito.Spy; | ||
| import org.mockito.junit.MockitoJUnitRunner; | ||
|
|
||
| import com.cloud.utils.exception.CloudRuntimeException; | ||
|
|
||
| @RunWith(MockitoJUnitRunner.Silent.class) | ||
| public class AsyncJobManagerImplExecuteQueueItemTest { | ||
|
|
||
| @Mock | ||
| AsyncJobDao _jobDao; | ||
| @Mock | ||
| SyncQueueManager _queueMgr; | ||
|
|
||
| @Spy | ||
| @InjectMocks | ||
| AsyncJobManagerImpl asyncJobManager = new AsyncJobManagerImpl(); | ||
|
|
||
| @Test | ||
| public void executeQueueItemDoesNotScheduleWhenTheJobUpdateFailsAndItemIsReturned() { | ||
| long contentId = 10L; | ||
| long itemId = 20L; | ||
| long jobId = 1L; | ||
|
|
||
| SyncQueueItemVO item = Mockito.mock(SyncQueueItemVO.class); | ||
| Mockito.when(item.getContentId()).thenReturn(contentId); | ||
| Mockito.when(item.getId()).thenReturn(itemId); | ||
|
|
||
| AsyncJobVO job = Mockito.mock(AsyncJobVO.class); | ||
| Mockito.when(job.getId()).thenReturn(jobId); | ||
| Mockito.when(_jobDao.findById(contentId)).thenReturn(job); | ||
|
|
||
| // Simulate the DB deadlock the catch block was written to survive. | ||
| Mockito.doThrow(new CloudRuntimeException("simulated DB deadlock")) | ||
| .when(_jobDao).update(Mockito.anyLong(), Mockito.any(AsyncJobVO.class)); | ||
|
|
||
| // Stub the executor path so we can assert whether it is reached (and avoid the real submit). | ||
| Mockito.doNothing().when(asyncJobManager).scheduleExecution(Mockito.any(AsyncJobVO.class)); | ||
|
|
||
| asyncJobManager.executeQueueItem(item, false); | ||
|
|
||
| // The queue item was returned for a later retry; the job must NOT also be scheduled now, or it | ||
| // would run twice (once here and once when the heartbeat re-dequeues the returned item). | ||
| Mockito.verify(_queueMgr).returnItem(itemId); | ||
| Mockito.verify(asyncJobManager, Mockito.never()).scheduleExecution(Mockito.any(AsyncJobVO.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void executeQueueItemClearsExecutingMsidBeforeReturningItemOnRejectedExecution() { | ||
| long contentId = 10L; | ||
| long itemId = 20L; | ||
| long jobId = 1L; | ||
|
|
||
| SyncQueueItemVO item = Mockito.mock(SyncQueueItemVO.class); | ||
| Mockito.when(item.getContentId()).thenReturn(contentId); | ||
| Mockito.when(item.getId()).thenReturn(itemId); | ||
|
|
||
| AsyncJobVO job = Mockito.mock(AsyncJobVO.class); | ||
| Mockito.when(job.getId()).thenReturn(jobId); | ||
| Mockito.when(_jobDao.findById(contentId)).thenReturn(job); | ||
|
|
||
| // Dispatch stamps the executing msid successfully, then the executor rejects the job. | ||
| Mockito.doThrow(new RejectedExecutionException("pool full")) | ||
| .when(asyncJobManager).scheduleExecution(Mockito.any(AsyncJobVO.class)); | ||
|
|
||
| asyncJobManager.executeQueueItem(item, false); | ||
|
|
||
| // The executing msid must be cleared while we still hold the item, before it goes back on the | ||
| // queue. Otherwise another management server could dequeue the item and stamp its own msid in | ||
| // the gap, and the clearing update would wipe it. | ||
| InOrder inOrder = Mockito.inOrder(job, _queueMgr); | ||
| inOrder.verify(job).setExecutingMsid(null); | ||
| inOrder.verify(_queueMgr).returnItem(itemId); | ||
| } | ||
|
|
||
| @Test | ||
| public void executeQueueItemStillSchedulesWhenRequeueAlsoFailsSoJobIsNotStranded() { | ||
| long contentId = 10L; | ||
| long itemId = 20L; | ||
| long jobId = 1L; | ||
|
|
||
| SyncQueueItemVO item = Mockito.mock(SyncQueueItemVO.class); | ||
| Mockito.when(item.getContentId()).thenReturn(contentId); | ||
| Mockito.when(item.getId()).thenReturn(itemId); | ||
|
|
||
| AsyncJobVO job = Mockito.mock(AsyncJobVO.class); | ||
| Mockito.when(job.getId()).thenReturn(jobId); | ||
| Mockito.when(_jobDao.findById(contentId)).thenReturn(job); | ||
|
|
||
| // Stamping the executing msid fails (the DB deadlock path), and returning the item also fails. | ||
| Mockito.doThrow(new CloudRuntimeException("simulated DB deadlock")) | ||
| .when(_jobDao).update(Mockito.anyLong(), Mockito.any(AsyncJobVO.class)); | ||
| Mockito.doThrow(new CloudRuntimeException("queue unavailable")) | ||
| .when(_queueMgr).returnItem(Mockito.anyLong()); | ||
| Mockito.doNothing().when(asyncJobManager).scheduleExecution(Mockito.any(AsyncJobVO.class)); | ||
|
|
||
| asyncJobManager.executeQueueItem(item, false); | ||
|
|
||
| // The requeue failed, so the job must still be scheduled once instead of sitting until cleanup. | ||
| Mockito.verify(_queueMgr).returnItem(itemId); | ||
| Mockito.verify(asyncJobManager).scheduleExecution(job); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we clear the server id before putting the item back? another server could pick it up in between and we would wipe its id
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thanks @Damans227, you're right that was a real cross-server race. I moved clearing the executing msid ahead of returning the item in fd11c31, so it is nulled while we still hold the item and another management server cannot dequeue it and have its msid wiped by our update. Added an InOrder test that fails on the old order and passes on the new one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that covers it, thanks