Program: 105424
Project: 105424
Buggy source:
@DB protected ExternalLoadBalancerDeviceVO allocateLoadBalancerForNetwork(final Network guestConfig) throws InsufficientCapacityException {
  boolean retry=true;
  boolean tryLbProvisioning=false;
  ExternalLoadBalancerDeviceVO lbDevice=null;
  long physicalNetworkId=guestConfig.getPhysicalNetworkId();
  NetworkOfferingVO offering=_networkOfferingDao.findById(guestConfig.getNetworkOfferingId());
  String provider=_ntwkSrvcProviderDao.getProviderForServiceInNetwork(guestConfig.getId(),Service.Lb);
  while (retry) {
    GlobalLock deviceMapLock=GlobalLock.getInternLock("String_Node_Str");
    try {
      if (deviceMapLock.lock(120)) {
        try {
          final boolean dedicatedLB=offering.getDedicatedLB();
          try {
            lbDevice=Transaction.execute(new TransactionCallbackWithException<ExternalLoadBalancerDeviceVO,InsufficientCapacityException>(){
              @Override public ExternalLoadBalancerDeviceVO doInTransaction(              TransactionStatus status) throws InsufficientCapacityException {
                ExternalLoadBalancerDeviceVO lbDevice=findSuitableLoadBalancerForNetwork(guestConfig,dedicatedLB);
                long lbDeviceId=lbDevice.getId();
                NetworkExternalLoadBalancerVO networkLB=new NetworkExternalLoadBalancerVO(guestConfig.getId(),lbDeviceId);
                _networkExternalLBDao.persist(networkLB);
                lbDevice.setAllocationState(dedicatedLB ? LBDeviceAllocationState.Dedicated : LBDeviceAllocationState.Shared);
                _externalLoadBalancerDeviceDao.update(lbDeviceId,lbDevice);
                return lbDevice;
              }
            }
);
            tryLbProvisioning=false;
            retry=false;
          }
 catch (          InsufficientCapacityException exception) {
            if (tryLbProvisioning) {
              retry=false;
              s_logger.warn("String_Node_Str");
              throw exception;
            }
 else {
              tryLbProvisioning=true;
            }
          }
        }
  finally {
          deviceMapLock.unlock();
        }
      }
    }
  finally {
      deviceMapLock.releaseRef();
    }
    if (tryLbProvisioning) {
      List<ExternalLoadBalancerDeviceVO> providerLbDevices=_externalLoadBalancerDeviceDao.listByProviderAndDeviceAllocationState(physicalNetworkId,provider,LBDeviceAllocationState.Provider);
      if ((providerLbDevices != null) && (!providerLbDevices.isEmpty())) {
        for (        ExternalLoadBalancerDeviceVO lbProviderDevice : providerLbDevices) {
          if (lbProviderDevice.getState() == LBDeviceState.Enabled) {
            DataCenterIpAddressVO dcPrivateIp=_dcDao.allocatePrivateIpAddress(guestConfig.getDataCenterId(),lbProviderDevice.getUuid());
            if (dcPrivateIp == null) {
              throw new InsufficientNetworkCapacityException("String_Node_Str" + guestConfig.getDataCenterId() + "String_Node_Str",DataCenter.class,guestConfig.getDataCenterId());
            }
            Pod pod=_podDao.findById(dcPrivateIp.getPodId());
            String lbIP=dcPrivateIp.getIpAddress();
            String netmask=NetUtils.getCidrNetmask(pod.getCidrSize());
            String gateway=pod.getGateway();
            CreateLoadBalancerApplianceCommand lbProvisionCmd=new CreateLoadBalancerApplianceCommand(lbIP,netmask,gateway);
            CreateLoadBalancerApplianceAnswer createLbAnswer=null;
            try {
              createLbAnswer=(CreateLoadBalancerApplianceAnswer)_agentMgr.easySend(lbProviderDevice.getHostId(),lbProvisionCmd);
              if (createLbAnswer == null || !createLbAnswer.getResult()) {
                s_logger.error("String_Node_Str" + lbProviderDevice.getId());
                continue;
              }
            }
 catch (            Exception agentException) {
              s_logger.error("String_Node_Str" + lbProviderDevice.getId() + "String_Node_Str"+ agentException.getMessage());
              continue;
            }
            String username=createLbAnswer.getUsername();
            String password=createLbAnswer.getPassword();
            String publicIf=createLbAnswer.getPublicInterface();
            String privateIf=createLbAnswer.getPrivateInterface();
            String dedicatedLb=offering.getDedicatedLB() ? "String_Node_Str" : "String_Node_Str";
            String capacity=Long.toString(lbProviderDevice.getCapacity());
            PublicIp publicIp=_ipAddrMgr.assignPublicIpAddress(guestConfig.getDataCenterId(),null,_accountMgr.getSystemAccount(),VlanType.VirtualNetwork,null,null,false);
            String publicIPNetmask=publicIp.getVlanNetmask();
            String publicIPgateway=publicIp.getVlanGateway();
            String publicIPVlanTag=publicIp.getVlanTag();
            String publicIP=publicIp.getAddress().toString();
            String url="String_Node_Str" + lbIP + "String_Node_Str"+ publicIf+ "String_Node_Str"+ privateIf+ "String_Node_Str"+ dedicatedLb+ "String_Node_Str"+ "String_Node_Str"+ publicIP+ "String_Node_Str"+ publicIPNetmask+ "String_Node_Str"+ capacity+ "String_Node_Str"+ publicIPVlanTag+ "String_Node_Str"+ publicIPgateway;
            ExternalLoadBalancerDeviceVO lbAppliance=null;
            try {
              lbAppliance=addExternalLoadBalancer(physicalNetworkId,url,username,password,createLbAnswer.getDeviceName(),createLbAnswer.getServerResource(),false,false,null,null);
            }
 catch (            Exception e) {
              s_logger.error("String_Node_Str" + e.getMessage() + "String_Node_Str");
            }
            if (lbAppliance != null) {
              ExternalLoadBalancerDeviceVO managedLb=_externalLoadBalancerDeviceDao.findById(lbAppliance.getId());
              managedLb.setIsManagedDevice(true);
              managedLb.setParentHostId(lbProviderDevice.getHostId());
              _externalLoadBalancerDeviceDao.update(lbAppliance.getId(),managedLb);
            }
 else {
              DestroyLoadBalancerApplianceCommand lbDeleteCmd=new DestroyLoadBalancerApplianceCommand(lbIP);
              DestroyLoadBalancerApplianceAnswer answer=null;
              try {
                answer=(DestroyLoadBalancerApplianceAnswer)_agentMgr.easySend(lbProviderDevice.getHostId(),lbDeleteCmd);
                if (answer == null || !answer.getResult()) {
                  s_logger.warn("String_Node_Str");
                }
 else {
                  _dcDao.releasePrivateIpAddress(lbIP,guestConfig.getDataCenterId(),null);
                  _ipAddrMgr.disassociatePublicIpAddress(publicIp.getId(),_accountMgr.getSystemUser().getId(),_accountMgr.getSystemAccount());
                }
              }
 catch (              Exception e) {
                s_logger.warn("String_Node_Str" + guestConfig.getId() + "String_Node_Str"+ e.getMessage());
              }
            }
          }
        }
      }
    }
  }
  return lbDevice;
}

Fixed version (ground truth):
@DB protected ExternalLoadBalancerDeviceVO allocateLoadBalancerForNetwork(final Network guestConfig) throws InsufficientCapacityException {
  boolean retry=true;
  boolean tryLbProvisioning=false;
  ExternalLoadBalancerDeviceVO lbDevice=null;
  long physicalNetworkId=guestConfig.getPhysicalNetworkId();
  NetworkOfferingVO offering=_networkOfferingDao.findById(guestConfig.getNetworkOfferingId());
  String provider=_ntwkSrvcProviderDao.getProviderForServiceInNetwork(guestConfig.getId(),Service.Lb);
  while (retry) {
    GlobalLock deviceMapLock=GlobalLock.getInternLock("String_Node_Str");
    try {
      if (deviceMapLock.lock(120)) {
        try {
          final boolean dedicatedLB=offering.getDedicatedLB();
          try {
            lbDevice=Transaction.execute(new TransactionCallbackWithException<ExternalLoadBalancerDeviceVO,InsufficientCapacityException>(){
              @Override public ExternalLoadBalancerDeviceVO doInTransaction(              TransactionStatus status) throws InsufficientCapacityException {
                ExternalLoadBalancerDeviceVO lbDevice=findSuitableLoadBalancerForNetwork(guestConfig,dedicatedLB);
                long lbDeviceId=lbDevice.getId();
                NetworkExternalLoadBalancerVO networkLB=new NetworkExternalLoadBalancerVO(guestConfig.getId(),lbDeviceId);
                _networkExternalLBDao.persist(networkLB);
                lbDevice.setAllocationState(dedicatedLB ? LBDeviceAllocationState.Dedicated : LBDeviceAllocationState.Shared);
                _externalLoadBalancerDeviceDao.update(lbDeviceId,lbDevice);
                return lbDevice;
              }
            }
);
            tryLbProvisioning=false;
            retry=false;
          }
 catch (          InsufficientCapacityException exception) {
            if (tryLbProvisioning) {
              retry=false;
              s_logger.warn("String_Node_Str");
              throw exception;
            }
 else {
              tryLbProvisioning=true;
            }
          }
        }
  finally {
          deviceMapLock.unlock();
        }
      }
    }
  finally {
      deviceMapLock.releaseRef();
    }
    if (tryLbProvisioning) {
      List<ExternalLoadBalancerDeviceVO> providerLbDevices=_externalLoadBalancerDeviceDao.listByProviderAndDeviceAllocationState(physicalNetworkId,provider,LBDeviceAllocationState.Provider);
      if ((providerLbDevices != null) && (!providerLbDevices.isEmpty())) {
        for (        ExternalLoadBalancerDeviceVO lbProviderDevice : providerLbDevices) {
          if (lbProviderDevice.getState() == LBDeviceState.Enabled) {
            DataCenterIpAddressVO dcPrivateIp=_dcDao.allocatePrivateIpAddress(guestConfig.getDataCenterId(),lbProviderDevice.getUuid());
            if (dcPrivateIp == null) {
              throw new InsufficientNetworkCapacityException("String_Node_Str" + guestConfig.getDataCenterId() + "String_Node_Str",DataCenter.class,guestConfig.getDataCenterId());
            }
            Pod pod=_podDao.findById(dcPrivateIp.getPodId());
            String lbIP=dcPrivateIp.getIpAddress();
            String netmask=NetUtils.getCidrNetmask(pod.getCidrSize());
            String gateway=pod.getGateway();
            CreateLoadBalancerApplianceCommand lbProvisionCmd=new CreateLoadBalancerApplianceCommand(lbIP,netmask,gateway);
            CreateLoadBalancerApplianceAnswer createLbAnswer=null;
            try {
              createLbAnswer=(CreateLoadBalancerApplianceAnswer)_agentMgr.easySend(lbProviderDevice.getHostId(),lbProvisionCmd);
              if (createLbAnswer == null || !createLbAnswer.getResult()) {
                s_logger.error("String_Node_Str" + lbProviderDevice.getId());
                continue;
              }
            }
 catch (            Exception agentException) {
              s_logger.error("String_Node_Str" + lbProviderDevice.getId() + "String_Node_Str"+ agentException.getMessage());
              continue;
            }
            String username=createLbAnswer.getUsername();
            String password=createLbAnswer.getPassword();
            String publicIf=createLbAnswer.getPublicInterface();
            String privateIf=createLbAnswer.getPrivateInterface();
            String dedicatedLb=offering.getDedicatedLB() ? "String_Node_Str" : "String_Node_Str";
            String capacity=Long.toString(lbProviderDevice.getCapacity());
            PublicIp publicIp=_ipAddrMgr.assignPublicIpAddress(guestConfig.getDataCenterId(),null,_accountMgr.getSystemAccount(),VlanType.VirtualNetwork,null,null,false);
            String publicIPNetmask=publicIp.getVlanNetmask();
            String publicIPgateway=publicIp.getVlanGateway();
            String publicIP=publicIp.getAddress().toString();
            String publicIPVlanTag="String_Node_Str";
            try {
              publicIPVlanTag=BroadcastDomainType.getValue(publicIp.getVlanTag());
            }
 catch (            URISyntaxException e) {
              s_logger.error("String_Node_Str" + e.getMessage());
            }
            String url="String_Node_Str" + lbIP + "String_Node_Str"+ publicIf+ "String_Node_Str"+ privateIf+ "String_Node_Str"+ dedicatedLb+ "String_Node_Str"+ "String_Node_Str"+ publicIP+ "String_Node_Str"+ publicIPNetmask+ "String_Node_Str"+ capacity+ "String_Node_Str"+ publicIPVlanTag+ "String_Node_Str"+ publicIPgateway;
            ExternalLoadBalancerDeviceVO lbAppliance=null;
            try {
              lbAppliance=addExternalLoadBalancer(physicalNetworkId,url,username,password,createLbAnswer.getDeviceName(),createLbAnswer.getServerResource(),false,false,null,null);
            }
 catch (            Exception e) {
              s_logger.error("String_Node_Str" + e.getMessage() + "String_Node_Str");
            }
            if (lbAppliance != null) {
              ExternalLoadBalancerDeviceVO managedLb=_externalLoadBalancerDeviceDao.findById(lbAppliance.getId());
              managedLb.setIsManagedDevice(true);
              managedLb.setParentHostId(lbProviderDevice.getHostId());
              _externalLoadBalancerDeviceDao.update(lbAppliance.getId(),managedLb);
            }
 else {
              DestroyLoadBalancerApplianceCommand lbDeleteCmd=new DestroyLoadBalancerApplianceCommand(lbIP);
              DestroyLoadBalancerApplianceAnswer answer=null;
              try {
                answer=(DestroyLoadBalancerApplianceAnswer)_agentMgr.easySend(lbProviderDevice.getHostId(),lbDeleteCmd);
                if (answer == null || !answer.getResult()) {
                  s_logger.warn("String_Node_Str");
                }
 else {
                  _dcDao.releasePrivateIpAddress(lbIP,guestConfig.getDataCenterId(),null);
                  _ipAddrMgr.disassociatePublicIpAddress(publicIp.getId(),_accountMgr.getSystemUser().getId(),_accountMgr.getSystemAccount());
                }
              }
 catch (              Exception e) {
                s_logger.warn("String_Node_Str" + guestConfig.getId() + "String_Node_Str"+ e.getMessage());
              }
            }
          }
        }
      }
    }
  }
  return lbDevice;
}