Skip to content

Commit 89f21cf

Browse files
shwstpprdhslove
authored andcommitted
server: fix orphan db transaction issue (apache#11095)
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 9a563d2 commit 89f21cf

2 files changed

Lines changed: 44 additions & 24 deletions

File tree

server/src/main/java/com/cloud/alert/AlertManagerImpl.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@
8989
import com.cloud.utils.component.ManagerBase;
9090
import com.cloud.utils.concurrency.NamedThreadFactory;
9191
import com.cloud.utils.db.SearchCriteria;
92+
import com.cloud.utils.db.Transaction;
93+
import com.cloud.utils.db.TransactionCallbackNoReturn;
94+
import com.cloud.utils.db.TransactionStatus;
95+
9296
import org.jetbrains.annotations.Nullable;
9397

9498
public class AlertManagerImpl extends ManagerBase implements AlertManager, Configurable {
@@ -303,8 +307,13 @@ protected void recalculateHostCapacities() {
303307
Math.min(CapacityManager.CapacityCalculateWorkers.value(), hostIds.size())));
304308
for (Long hostId : hostIds) {
305309
futures.put(hostId, executorService.submit(() -> {
306-
final HostVO host = hostDao.findById(hostId);
307-
_capacityMgr.updateCapacityForHost(host);
310+
Transaction.execute(new TransactionCallbackNoReturn() {
311+
@Override
312+
public void doInTransactionWithoutResult(TransactionStatus status) {
313+
final HostVO host = hostDao.findById(hostId);
314+
_capacityMgr.updateCapacityForHost(host);
315+
}
316+
});
308317
return null;
309318
}));
310319
}
@@ -329,13 +338,18 @@ protected void recalculateStorageCapacities() {
329338
Math.min(CapacityManager.CapacityCalculateWorkers.value(), storagePoolIds.size())));
330339
for (Long poolId: storagePoolIds) {
331340
futures.put(poolId, executorService.submit(() -> {
332-
final StoragePoolVO pool = _storagePoolDao.findById(poolId);
333-
long disk = _capacityMgr.getAllocatedPoolCapacity(pool, null);
334-
if (pool.isShared()) {
335-
_storageMgr.createCapacityEntry(pool, Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED, disk);
336-
} else {
337-
_storageMgr.createCapacityEntry(pool, Capacity.CAPACITY_TYPE_LOCAL_STORAGE, disk);
338-
}
341+
Transaction.execute(new TransactionCallbackNoReturn() {
342+
@Override
343+
public void doInTransactionWithoutResult(TransactionStatus status) {
344+
final StoragePoolVO pool = _storagePoolDao.findById(poolId);
345+
long disk = _capacityMgr.getAllocatedPoolCapacity(pool, null);
346+
if (pool.isShared()) {
347+
_storageMgr.createCapacityEntry(pool, Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED, disk);
348+
} else {
349+
_storageMgr.createCapacityEntry(pool, Capacity.CAPACITY_TYPE_LOCAL_STORAGE, disk);
350+
}
351+
}
352+
});
339353
return null;
340354
}));
341355
}

server/src/main/java/com/cloud/storage/StorageManagerImpl.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@
265265
import com.cloud.utils.db.SearchCriteria.Op;
266266
import com.cloud.utils.db.Transaction;
267267
import com.cloud.utils.db.TransactionCallbackNoReturn;
268+
import com.cloud.utils.db.TransactionCallbackWithExceptionNoReturn;
268269
import com.cloud.utils.db.TransactionLegacy;
269270
import com.cloud.utils.db.TransactionStatus;
270271
import com.cloud.utils.exception.CloudRuntimeException;
@@ -1842,22 +1843,27 @@ public void connectHostsToPool(DataStore primaryStore, List<Long> hostIds, Scope
18421843
if (exceptionOccurred.get()) {
18431844
return null;
18441845
}
1845-
HostVO host = _hostDao.findById(hostId);
1846-
try {
1847-
connectHostToSharedPool(host, primaryStore.getId());
1848-
poolHostIds.add(hostId);
1849-
} catch (Exception e) {
1850-
if (handleExceptionsPartially && e.getCause() instanceof StorageConflictException) {
1851-
exceptionOccurred.set(true);
1852-
throw e;
1853-
}
1854-
logger.warn("Unable to establish a connection between {} and {}", host, primaryStore, e);
1855-
String reason = getStoragePoolMountFailureReason(e.getMessage());
1856-
if (handleExceptionsPartially && reason != null) {
1857-
exceptionOccurred.set(true);
1858-
throw new CloudRuntimeException(reason);
1846+
Transaction.execute(new TransactionCallbackWithExceptionNoReturn<Exception>() {
1847+
@Override
1848+
public void doInTransactionWithoutResult(TransactionStatus status) throws Exception {
1849+
HostVO host = _hostDao.findById(hostId);
1850+
try {
1851+
connectHostToSharedPool(host, primaryStore.getId());
1852+
poolHostIds.add(hostId);
1853+
} catch (Exception e) {
1854+
if (handleExceptionsPartially && e.getCause() instanceof StorageConflictException) {
1855+
exceptionOccurred.set(true);
1856+
throw e;
1857+
}
1858+
logger.warn("Unable to establish a connection between {} and {}", host, primaryStore, e);
1859+
String reason = getStoragePoolMountFailureReason(e.getMessage());
1860+
if (handleExceptionsPartially && reason != null) {
1861+
exceptionOccurred.set(true);
1862+
throw new CloudRuntimeException(reason);
1863+
}
1864+
}
18591865
}
1860-
}
1866+
});
18611867
return null;
18621868
}));
18631869
}

0 commit comments

Comments
 (0)