Program: 106792
Project: 106792
Buggy source:
@Override @DB public Cluster updateCluster(Cluster clusterToUpdate,String clusterType,String hypervisor,String allocationState,String managedstate){
  ClusterVO cluster=(ClusterVO)clusterToUpdate;
  boolean doUpdate=false;
  if (hypervisor != null && !hypervisor.isEmpty()) {
    Hypervisor.HypervisorType hypervisorType=Hypervisor.HypervisorType.getType(hypervisor);
    if (hypervisorType == null) {
      s_logger.error("String_Node_Str" + hypervisor + "String_Node_Str");
      throw new InvalidParameterValueException("String_Node_Str" + hypervisor + "String_Node_Str");
    }
 else {
      cluster.setHypervisorType(hypervisor);
      doUpdate=true;
    }
  }
  Cluster.ClusterType newClusterType=null;
  if (clusterType != null && !clusterType.isEmpty()) {
    try {
      newClusterType=Cluster.ClusterType.valueOf(clusterType);
    }
 catch (    IllegalArgumentException ex) {
      throw new InvalidParameterValueException("String_Node_Str" + clusterType + "String_Node_Str");
    }
    if (newClusterType == null) {
      s_logger.error("String_Node_Str" + clusterType + "String_Node_Str");
      throw new InvalidParameterValueException("String_Node_Str" + clusterType + "String_Node_Str");
    }
 else {
      cluster.setClusterType(newClusterType);
      doUpdate=true;
    }
  }
  Grouping.AllocationState newAllocationState=null;
  if (allocationState != null && !allocationState.isEmpty()) {
    try {
      newAllocationState=Grouping.AllocationState.valueOf(allocationState);
    }
 catch (    IllegalArgumentException ex) {
      throw new InvalidParameterValueException("String_Node_Str" + allocationState + "String_Node_Str");
    }
    if (newAllocationState == null) {
      s_logger.error("String_Node_Str" + allocationState + "String_Node_Str");
      throw new InvalidParameterValueException("String_Node_Str" + allocationState + "String_Node_Str");
    }
 else {
      cluster.setAllocationState(newAllocationState);
      doUpdate=true;
    }
  }
  Managed.ManagedState newManagedState=null;
  Managed.ManagedState oldManagedState=cluster.getManagedState();
  if (managedstate != null && !managedstate.isEmpty()) {
    try {
      newManagedState=Managed.ManagedState.valueOf(managedstate);
    }
 catch (    IllegalArgumentException ex) {
      throw new InvalidParameterValueException("String_Node_Str" + managedstate + "String_Node_Str");
    }
    if (newManagedState == null) {
      s_logger.error("String_Node_Str" + managedstate + "String_Node_Str");
      throw new InvalidParameterValueException("String_Node_Str" + managedstate + "String_Node_Str");
    }
 else {
      doUpdate=true;
    }
  }
  if (doUpdate) {
    Transaction txn=Transaction.currentTxn();
    try {
      txn.start();
      _clusterDao.update(cluster.getId(),cluster);
      txn.commit();
    }
 catch (    Exception e) {
      s_logger.error("String_Node_Str" + e.getMessage(),e);
      throw new CloudRuntimeException("String_Node_Str");
    }
  }
  if (newManagedState != null && !newManagedState.equals(oldManagedState)) {
    Transaction txn=Transaction.currentTxn();
    if (newManagedState.equals(Managed.ManagedState.Unmanaged)) {
      boolean success=true;
      try {
        txn.start();
        cluster.setManagedState(Managed.ManagedState.PrepareUnmanaged);
        _clusterDao.update(cluster.getId(),cluster);
        txn.commit();
        List<HostVO> hosts=_hostDao.listBy(cluster.getId(),cluster.getPodId(),cluster.getDataCenterId());
        for (        HostVO host : hosts) {
          if (!host.getStatus().equals(Status.Down) && !host.getStatus().equals(Status.Disconnected) && !host.getStatus().equals(Status.Up)&& !host.getStatus().equals(Status.Alert)) {
            String msg="String_Node_Str" + host.getPrivateIpAddress() + "String_Node_Str"+ host.getStatus().toString()+ "String_Node_Str";
            throw new CloudRuntimeException("String_Node_Str" + msg);
          }
        }
        for (        HostVO host : hosts) {
          if (host.getStatus().equals(Status.Up)) {
            _agentMgr.disconnect(host.getId());
          }
        }
        int retry=10;
        for (int i=0; i < retry; i++) {
          success=true;
          try {
            Thread.sleep(20 * 1000);
          }
 catch (          Exception e) {
          }
          hosts=_hostDao.listBy(cluster.getId(),cluster.getPodId(),cluster.getDataCenterId());
          for (          HostVO host : hosts) {
            if (!host.getStatus().equals(Status.Down) && !host.getStatus().equals(Status.Disconnected) && !host.getStatus().equals(Status.Alert)) {
              success=false;
              break;
            }
          }
          if (success == true) {
            break;
          }
        }
        if (success == false) {
          throw new CloudRuntimeException("String_Node_Str");
        }
      }
  finally {
        if (success == false) {
          txn.start();
          cluster.setManagedState(success ? Managed.ManagedState.Unmanaged : Managed.ManagedState.PrepareUnmanagedError);
          _clusterDao.update(cluster.getId(),cluster);
          txn.commit();
        }
      }
    }
 else     if (newManagedState.equals(Managed.ManagedState.Managed)) {
      txn.start();
      cluster.setManagedState(Managed.ManagedState.Managed);
      _clusterDao.update(cluster.getId(),cluster);
      txn.commit();
    }
  }
  return cluster;
}

Fixed version (ground truth):
@Override @DB public Cluster updateCluster(Cluster clusterToUpdate,String clusterType,String hypervisor,String allocationState,String managedstate){
  ClusterVO cluster=(ClusterVO)clusterToUpdate;
  boolean doUpdate=false;
  if (hypervisor != null && !hypervisor.isEmpty()) {
    Hypervisor.HypervisorType hypervisorType=Hypervisor.HypervisorType.getType(hypervisor);
    if (hypervisorType == null) {
      s_logger.error("String_Node_Str" + hypervisor + "String_Node_Str");
      throw new InvalidParameterValueException("String_Node_Str" + hypervisor + "String_Node_Str");
    }
 else {
      cluster.setHypervisorType(hypervisor);
      doUpdate=true;
    }
  }
  Cluster.ClusterType newClusterType=null;
  if (clusterType != null && !clusterType.isEmpty()) {
    try {
      newClusterType=Cluster.ClusterType.valueOf(clusterType);
    }
 catch (    IllegalArgumentException ex) {
      throw new InvalidParameterValueException("String_Node_Str" + clusterType + "String_Node_Str");
    }
    if (newClusterType == null) {
      s_logger.error("String_Node_Str" + clusterType + "String_Node_Str");
      throw new InvalidParameterValueException("String_Node_Str" + clusterType + "String_Node_Str");
    }
 else {
      cluster.setClusterType(newClusterType);
      doUpdate=true;
    }
  }
  Grouping.AllocationState newAllocationState=null;
  if (allocationState != null && !allocationState.isEmpty()) {
    try {
      newAllocationState=Grouping.AllocationState.valueOf(allocationState);
    }
 catch (    IllegalArgumentException ex) {
      throw new InvalidParameterValueException("String_Node_Str" + allocationState + "String_Node_Str");
    }
    if (newAllocationState == null) {
      s_logger.error("String_Node_Str" + allocationState + "String_Node_Str");
      throw new InvalidParameterValueException("String_Node_Str" + allocationState + "String_Node_Str");
    }
 else {
      cluster.setAllocationState(newAllocationState);
      doUpdate=true;
    }
  }
  Managed.ManagedState newManagedState=null;
  Managed.ManagedState oldManagedState=cluster.getManagedState();
  if (managedstate != null && !managedstate.isEmpty()) {
    try {
      newManagedState=Managed.ManagedState.valueOf(managedstate);
    }
 catch (    IllegalArgumentException ex) {
      throw new InvalidParameterValueException("String_Node_Str" + managedstate + "String_Node_Str");
    }
    if (newManagedState == null) {
      s_logger.error("String_Node_Str" + managedstate + "String_Node_Str");
      throw new InvalidParameterValueException("String_Node_Str" + managedstate + "String_Node_Str");
    }
 else {
      doUpdate=true;
    }
  }
  if (doUpdate) {
    Transaction txn=Transaction.currentTxn();
    try {
      txn.start();
      _clusterDao.update(cluster.getId(),cluster);
      txn.commit();
    }
 catch (    Exception e) {
      s_logger.error("String_Node_Str" + e.getMessage(),e);
      throw new CloudRuntimeException("String_Node_Str");
    }
  }
  if (newManagedState != null && !newManagedState.equals(oldManagedState)) {
    Transaction txn=Transaction.currentTxn();
    if (newManagedState.equals(Managed.ManagedState.Unmanaged)) {
      boolean success=false;
      try {
        txn.start();
        cluster.setManagedState(Managed.ManagedState.PrepareUnmanaged);
        _clusterDao.update(cluster.getId(),cluster);
        txn.commit();
        List<HostVO> hosts=_hostDao.listBy(Host.Type.Routing,cluster.getId(),cluster.getPodId(),cluster.getDataCenterId());
        for (        HostVO host : hosts) {
          if (host.getType().equals(Host.Type.Routing) && !host.getStatus().equals(Status.Down) && !host.getStatus().equals(Status.Disconnected)&& !host.getStatus().equals(Status.Up)&& !host.getStatus().equals(Status.Alert)) {
            String msg="String_Node_Str" + host.getPrivateIpAddress() + "String_Node_Str"+ host.getStatus().toString()+ "String_Node_Str";
            throw new CloudRuntimeException("String_Node_Str" + msg);
          }
        }
        for (        HostVO host : hosts) {
          if (host.getStatus().equals(Status.Up)) {
            _agentMgr.disconnect(host.getId());
          }
        }
        int retry=10;
        boolean lsuccess=true;
        for (int i=0; i < retry; i++) {
          lsuccess=true;
          try {
            Thread.sleep(20 * 1000);
          }
 catch (          Exception e) {
          }
          hosts=_hostDao.listBy(Host.Type.Routing,cluster.getId(),cluster.getPodId(),cluster.getDataCenterId());
          for (          HostVO host : hosts) {
            if (!host.getStatus().equals(Status.Down) && !host.getStatus().equals(Status.Disconnected) && !host.getStatus().equals(Status.Alert)) {
              lsuccess=false;
              break;
            }
          }
          if (lsuccess == true) {
            success=true;
            break;
          }
        }
        if (success == false) {
          throw new CloudRuntimeException("String_Node_Str");
        }
      }
  finally {
        txn.start();
        cluster.setManagedState(success ? Managed.ManagedState.Unmanaged : Managed.ManagedState.PrepareUnmanagedError);
        _clusterDao.update(cluster.getId(),cluster);
        txn.commit();
      }
    }
 else     if (newManagedState.equals(Managed.ManagedState.Managed)) {
      txn.start();
      cluster.setManagedState(Managed.ManagedState.Managed);
      _clusterDao.update(cluster.getId(),cluster);
      txn.commit();
    }
  }
  return cluster;
}