-
Notifications
You must be signed in to change notification settings - Fork 1.4k
kvm: fix storage pool refcount race and false umount success #14189
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
bhouse-nexthop
wants to merge
3
commits into
apache:4.22
Choose a base branch
from
bhouse-nexthop:fix-nfs-storage-pool-refcount-and-umount
base: 4.22
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.
+304
−21
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
45 changes: 45 additions & 0 deletions
45
...s/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStoragePoolLocks.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,45 @@ | ||
| // 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 com.cloud.hypervisor.kvm.storage; | ||
|
|
||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| /** | ||
| * One monitor per storage pool uuid, shared by {@link KVMStoragePoolManager} and | ||
| * {@link LibvirtStorageAdaptor} so that both take the same lock for a pool. | ||
| * | ||
| * The adaptor holds it across the whole of createStoragePool and deleteStoragePool. The refcount | ||
| * alone cannot keep a pool alive: deleteStoragePool decides to tear the pool down when the count | ||
| * reaches zero and then destroys and unmounts it, and a createStoragePool that finds the still | ||
| * active pool and takes a reference in between would have it torn down underneath it. | ||
| * | ||
| * Whoever also takes the manager wide lock must take this one first. A teardown can sit in an | ||
| * umount for as long as the storage takes to answer, and a create of that pool that waited for it | ||
| * while holding the manager wide lock would hold up the creates of every other pool on the host. | ||
| * | ||
| * Entries are never removed, as there is one per pool the host has ever used. | ||
| */ | ||
| final class KVMStoragePoolLocks { | ||
| private static final ConcurrentHashMap<String, Object> LOCKS = new ConcurrentHashMap<>(); | ||
|
|
||
| private KVMStoragePoolLocks() { | ||
| } | ||
|
|
||
| static Object get(String uuid) { | ||
| return LOCKS.computeIfAbsent(uuid, k -> new Object()); | ||
| } | ||
| } |
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
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
77 changes: 77 additions & 0 deletions
77
...rvisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/KVMStoragePoolManagerTest.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,77 @@ | ||
| // 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 com.cloud.hypervisor.kvm.storage; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyBoolean; | ||
| import static org.mockito.ArgumentMatchers.anyInt; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
|
|
||
| import java.util.UUID; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
| import org.mockito.Mockito; | ||
|
|
||
| import com.cloud.storage.Storage.StoragePoolType; | ||
|
|
||
| public class KVMStoragePoolManagerTest { | ||
|
|
||
| @Test(timeout = 60000) | ||
| public void testCreateWaitingForPoolLockDoesNotHoldManagerLock() throws Exception { | ||
| /* | ||
| * A teardown of a pool holds that pool's lock, and can sit in an umount for as long as the | ||
| * storage takes to answer. A create of the same pool has to wait for it, but must do so | ||
| * without holding the manager wide lock, or the creates of every other pool on the host wait | ||
| * behind the umount too. | ||
| */ | ||
| final KVMStoragePoolManager manager = Mockito.mock(KVMStoragePoolManager.class, Mockito.CALLS_REAL_METHODS); | ||
| final KVMStoragePool pool = Mockito.mock(KVMStoragePool.class); | ||
| final String uuid = String.valueOf(UUID.randomUUID()); | ||
| Mockito.doReturn(pool).when(manager).createStoragePoolSynchronized(eq(uuid), any(), anyInt(), any(), any(), any(), any(), anyBoolean()); | ||
|
|
||
| final AtomicReference<KVMStoragePool> created = new AtomicReference<>(); | ||
| final Thread create; | ||
| synchronized (KVMStoragePoolLocks.get(uuid)) { | ||
| // the teardown in progress | ||
| create = new Thread(() -> created.set(manager.createStoragePool(uuid, "127.0.0.1", 0, "/export/primary", | ||
| null, StoragePoolType.NetworkFilesystem))); | ||
| create.start(); | ||
| while (create.getState() != Thread.State.BLOCKED) { | ||
| Assert.assertTrue("create finished while the pool's lock was held", create.isAlive()); | ||
| Thread.sleep(10); | ||
| } | ||
|
|
||
| final CountDownLatch managerLockTaken = new CountDownLatch(1); | ||
| final Thread otherPool = new Thread(() -> { | ||
| synchronized (manager) { | ||
| managerLockTaken.countDown(); | ||
| } | ||
| }); | ||
| otherPool.start(); | ||
| Assert.assertTrue("the manager wide lock was held by a create waiting for its pool's lock", | ||
| managerLockTaken.await(30, TimeUnit.SECONDS)); | ||
| Assert.assertNull(created.get()); | ||
| } | ||
|
|
||
| create.join(30000); | ||
| Assert.assertSame(pool, created.get()); | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.