Skip to content

Commit c24e4ee

Browse files
authored
server: fix orphan db transaction issue (apache#11095)
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 3b54194 commit c24e4ee

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

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 {
@@ -290,8 +294,13 @@ protected void recalculateHostCapacities() {
290294
Math.min(CapacityManager.CapacityCalculateWorkers.value(), hostIds.size())));
291295
for (Long hostId : hostIds) {
292296
futures.put(hostId, executorService.submit(() -> {
293-
final HostVO host = hostDao.findById(hostId);
294-
_capacityMgr.updateCapacityForHost(host);
297+
Transaction.execute(new TransactionCallbackNoReturn() {
298+
@Override
299+
public void doInTransactionWithoutResult(TransactionStatus status) {
300+
final HostVO host = hostDao.findById(hostId);
301+
_capacityMgr.updateCapacityForHost(host);
302+
}
303+
});
295304
return null;
296305
}));
297306
}
@@ -316,13 +325,18 @@ protected void recalculateStorageCapacities() {
316325
Math.min(CapacityManager.CapacityCalculateWorkers.value(), storagePoolIds.size())));
317326
for (Long poolId: storagePoolIds) {
318327
futures.put(poolId, executorService.submit(() -> {
319-
final StoragePoolVO pool = _storagePoolDao.findById(poolId);
320-
long disk = _capacityMgr.getAllocatedPoolCapacity(pool, null);
321-
if (pool.isShared()) {
322-
_storageMgr.createCapacityEntry(pool, Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED, disk);
323-
} else {
324-
_storageMgr.createCapacityEntry(pool, Capacity.CAPACITY_TYPE_LOCAL_STORAGE, disk);
325-
}
328+
Transaction.execute(new TransactionCallbackNoReturn() {
329+
@Override
330+
public void doInTransactionWithoutResult(TransactionStatus status) {
331+
final StoragePoolVO pool = _storagePoolDao.findById(poolId);
332+
long disk = _capacityMgr.getAllocatedPoolCapacity(pool, null);
333+
if (pool.isShared()) {
334+
_storageMgr.createCapacityEntry(pool, Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED, disk);
335+
} else {
336+
_storageMgr.createCapacityEntry(pool, Capacity.CAPACITY_TYPE_LOCAL_STORAGE, disk);
337+
}
338+
}
339+
});
326340
return null;
327341
}));
328342
}

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

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@
257257
import com.cloud.utils.db.SearchCriteria.Op;
258258
import com.cloud.utils.db.Transaction;
259259
import com.cloud.utils.db.TransactionCallbackNoReturn;
260+
import com.cloud.utils.db.TransactionCallbackWithExceptionNoReturn;
260261
import com.cloud.utils.db.TransactionLegacy;
261262
import com.cloud.utils.db.TransactionStatus;
262263
import com.cloud.utils.exception.CloudRuntimeException;
@@ -1591,22 +1592,27 @@ public void connectHostsToPool(DataStore primaryStore, List<Long> hostIds, Scope
15911592
if (exceptionOccurred.get()) {
15921593
return null;
15931594
}
1594-
HostVO host = _hostDao.findById(hostId);
1595-
try {
1596-
connectHostToSharedPool(host, primaryStore.getId());
1597-
poolHostIds.add(hostId);
1598-
} catch (Exception e) {
1599-
if (handleExceptionsPartially && e.getCause() instanceof StorageConflictException) {
1600-
exceptionOccurred.set(true);
1601-
throw e;
1602-
}
1603-
logger.warn("Unable to establish a connection between {} and {}", host, primaryStore, e);
1604-
String reason = getStoragePoolMountFailureReason(e.getMessage());
1605-
if (handleExceptionsPartially && reason != null) {
1606-
exceptionOccurred.set(true);
1607-
throw new CloudRuntimeException(reason);
1595+
Transaction.execute(new TransactionCallbackWithExceptionNoReturn<Exception>() {
1596+
@Override
1597+
public void doInTransactionWithoutResult(TransactionStatus status) throws Exception {
1598+
HostVO host = _hostDao.findById(hostId);
1599+
try {
1600+
connectHostToSharedPool(host, primaryStore.getId());
1601+
poolHostIds.add(hostId);
1602+
} catch (Exception e) {
1603+
if (handleExceptionsPartially && e.getCause() instanceof StorageConflictException) {
1604+
exceptionOccurred.set(true);
1605+
throw e;
1606+
}
1607+
logger.warn("Unable to establish a connection between {} and {}", host, primaryStore, e);
1608+
String reason = getStoragePoolMountFailureReason(e.getMessage());
1609+
if (handleExceptionsPartially && reason != null) {
1610+
exceptionOccurred.set(true);
1611+
throw new CloudRuntimeException(reason);
1612+
}
1613+
}
16081614
}
1609-
}
1615+
});
16101616
return null;
16111617
}));
16121618
}

0 commit comments

Comments
 (0)