Skip to content

Commit b38dbc6

Browse files
sudisonAnthony Xu
authored andcommitted
CLOUDSTACK-6245: the security group rule is lagging behind the rules in DB, due to there is a worker thread launched inside a transaction Reviewed-by: Alex
(cherry picked from commit d4fdc18) Signed-off-by: Animesh Chaturvedi <animesh@apache.org> Conflicts: server/src/com/cloud/network/security/SecurityGroupManagerImpl.java
1 parent 2276a39 commit b38dbc6

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

‎server/src/com/cloud/network/security/SecurityGroupManagerImpl.java‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ private List<SecurityGroupRuleVO> authorizeSecurityGroupRule(final Long security
716716
final Integer startPortOrTypeFinal = startPortOrType;
717717
final Integer endPortOrCodeFinal = endPortOrCode;
718718
final String protocolFinal = protocol;
719-
return Transaction.execute(new TransactionCallback<List<SecurityGroupRuleVO>>() {
719+
List<SecurityGroupRuleVO> newRules = Transaction.execute(new TransactionCallback<List<SecurityGroupRuleVO>>() {
720720
@Override
721721
public List<SecurityGroupRuleVO> doInTransaction(TransactionStatus status) {
722722
// Prevents other threads/management servers from creating duplicate security rules
@@ -761,9 +761,6 @@ public List<SecurityGroupRuleVO> doInTransaction(TransactionStatus status) {
761761
if (s_logger.isDebugEnabled()) {
762762
s_logger.debug("Added " + newRules.size() + " rules to security group " + securityGroup.getName());
763763
}
764-
final ArrayList<Long> affectedVms = new ArrayList<Long>();
765-
affectedVms.addAll(_securityGroupVMMapDao.listVmIdsBySecurityGroup(securityGroup.getId()));
766-
scheduleRulesetUpdateToHosts(affectedVms, true, null);
767764
return newRules;
768765
} catch (Exception e) {
769766
s_logger.warn("Exception caught when adding security group rules ", e);
@@ -776,6 +773,15 @@ public List<SecurityGroupRuleVO> doInTransaction(TransactionStatus status) {
776773
}
777774
});
778775

776+
try {
777+
final ArrayList<Long> affectedVms = new ArrayList<Long>();
778+
affectedVms.addAll(_securityGroupVMMapDao.listVmIdsBySecurityGroup(securityGroup.getId()));
779+
scheduleRulesetUpdateToHosts(affectedVms, true, null);
780+
} catch (Exception e) {
781+
s_logger.debug("can't update rules on host, ignore", e);
782+
}
783+
784+
return newRules;
779785
}
780786

781787
@Override
@@ -815,7 +821,8 @@ private boolean revokeSecurityGroupRule(final Long id, SecurityRuleType type) {
815821
SecurityGroup securityGroup = _securityGroupDao.findById(rule.getSecurityGroupId());
816822
_accountMgr.checkAccess(caller, AccessType.OperateEntry, true, securityGroup);
817823

818-
return Transaction.execute(new TransactionCallback<Boolean>() {
824+
long securityGroupId = rule.getSecurityGroupId();
825+
Boolean result = Transaction.execute(new TransactionCallback<Boolean>() {
819826
@Override
820827
public Boolean doInTransaction(TransactionStatus status) {
821828
SecurityGroupVO groupHandle = null;
@@ -831,10 +838,6 @@ public Boolean doInTransaction(TransactionStatus status) {
831838
_securityGroupRuleDao.remove(id);
832839
s_logger.debug("revokeSecurityGroupRule succeeded for security rule id: " + id);
833840

834-
final ArrayList<Long> affectedVms = new ArrayList<Long>();
835-
affectedVms.addAll(_securityGroupVMMapDao.listVmIdsBySecurityGroup(groupHandle.getId()));
836-
scheduleRulesetUpdateToHosts(affectedVms, true, null);
837-
838841
return true;
839842
} catch (Exception e) {
840843
s_logger.warn("Exception caught when deleting security rules ", e);
@@ -846,6 +849,16 @@ public Boolean doInTransaction(TransactionStatus status) {
846849
}
847850
}
848851
});
852+
853+
try {
854+
final ArrayList<Long> affectedVms = new ArrayList<Long>();
855+
affectedVms.addAll(_securityGroupVMMapDao.listVmIdsBySecurityGroup(securityGroupId));
856+
scheduleRulesetUpdateToHosts(affectedVms, true, null);
857+
} catch (Exception e) {
858+
s_logger.debug("Can't update rules for host, ignore", e);
859+
}
860+
861+
return result;
849862
}
850863

851864
@Override

0 commit comments

Comments
 (0)