Program: 79453
Project: 79453
Buggy source:
/** 
 * Looks up the state of all nodes in the management plane, and checks if the master is still ok. If it's not then determines which node should be promoted to master. If it is ourself, then promotes.
 */
protected void checkMaster(boolean initializing){
  ManagementPlaneSyncRecord memento=loadManagementPlaneSyncRecord(false);
  if (getNodeState() == ManagementNodeState.FAILED) {
    return;
  }
  String currMasterNodeId=memento.getMasterNodeId();
  ManagementNodeSyncRecord currMasterNodeRecord=memento.getManagementNodes().get(currMasterNodeId);
  ManagementNodeSyncRecord ownNodeRecord=memento.getManagementNodes().get(ownNodeId);
  ManagementNodeSyncRecord newMasterNodeRecord=null;
  boolean demotingSelfInFavourOfOtherMaster=false;
  if (currMasterNodeRecord != null && currMasterNodeRecord.getStatus() == ManagementNodeState.MASTER && isHeartbeatOk(currMasterNodeRecord,ownNodeRecord)) {
    if (ownNodeId.equals(currMasterNodeId)) {
      if (LOG.isTraceEnabled())       LOG.trace("String_Node_Str",currMasterNodeRecord.toVerboseString());
      return;
    }
 else {
      if (ownNodeRecord != null && ownNodeRecord.getStatus() == ManagementNodeState.MASTER) {
        LOG.error("String_Node_Str" + ownNodeId + "String_Node_Str"+ currMasterNodeId);
        newMasterNodeRecord=currMasterNodeRecord;
        demotingSelfInFavourOfOtherMaster=true;
      }
 else {
        if (LOG.isTraceEnabled())         LOG.trace("String_Node_Str",currMasterNodeRecord.toVerboseString());
        return;
      }
    }
  }
 else   if (ownNodeRecord == null || !isHeartbeatOk(ownNodeRecord,ownNodeRecord)) {
    if (ownNodeRecord == null) {
      LOG.error("String_Node_Str" + ownNodeId + "String_Node_Str"+ "String_Node_Str"+ currMasterNodeId+ "String_Node_Str");
    }
 else {
      LOG.error("String_Node_Str" + ownNodeId + "String_Node_Str"+ "String_Node_Str"+ currMasterNodeId+ "String_Node_Str"+ "String_Node_Str"+ ownNodeRecord.toVerboseString());
    }
    return;
  }
 else   if (ownNodeId.equals(currMasterNodeId)) {
    LOG.warn("String_Node_Str" + ownNodeId + "String_Node_Str"+ "String_Node_Str"+ ownNodeRecord.toVerboseString());
    return;
  }
  if (demotingSelfInFavourOfOtherMaster) {
    LOG.debug("String_Node_Str" + ownNodeRecord.toVerboseString() + "String_Node_Str"+ newMasterNodeRecord.toVerboseString());
    demoteToStandby(BrooklynFeatureEnablement.isEnabled(BrooklynFeatureEnablement.FEATURE_DEFAULT_STANDBY_IS_HOT_PROPERTY));
    return;
  }
  newMasterNodeRecord=masterChooser.choose(memento,heartbeatTimeout,ownNodeId);
  String newMasterNodeId=(newMasterNodeRecord == null) ? null : newMasterNodeRecord.getNodeId();
  URI newMasterNodeUri=(newMasterNodeRecord == null) ? null : newMasterNodeRecord.getUri();
  boolean weAreNewMaster=ownNodeId.equals(newMasterNodeId);
  if (LOG.isDebugEnabled()) {
    LOG.debug("String_Node_Str",new Object[]{(newMasterNodeRecord == null ? "String_Node_Str" : newMasterNodeRecord.toVerboseString()),(currMasterNodeRecord == null ? currMasterNodeId + "String_Node_Str" : currMasterNodeRecord.toVerboseString()),memento,ownNodeRecord.toVerboseString(),heartbeatTimeout});
  }
  if (!initializing) {
    String message="String_Node_Str" + ownNodeId + "String_Node_Str";
    if (weAreNewMaster)     message+="String_Node_Str";
 else     message+="String_Node_Str";
    message+=currMasterNodeId + "String_Node_Str" + (currMasterNodeRecord == null ? "String_Node_Str" : currMasterNodeRecord.getRemoteTimestamp())+ "String_Node_Str"+ "String_Node_Str"+ (newMasterNodeId == null ? "String_Node_Str" : (weAreNewMaster ? "String_Node_Str" : "String_Node_Str") + newMasterNodeId + "String_Node_Str"+ newMasterNodeRecord.getRemoteTimestamp()+ "String_Node_Str"+ (newMasterNodeUri != null ? "String_Node_Str" + newMasterNodeUri : "String_Node_Str"));
    LOG.warn(message);
  }
  if (weAreNewMaster) {
    promoteToMaster();
  }
}

Fixed version (ground truth):
/** 
 * Looks up the state of all nodes in the management plane, and checks if the master is still ok. If it's not then determines which node should be promoted to master. If it is ourself, then promotes.
 */
protected void checkMaster(boolean initializing){
  ManagementPlaneSyncRecord memento=loadManagementPlaneSyncRecord(false);
  if (getNodeState() == ManagementNodeState.FAILED) {
    return;
  }
  String currMasterNodeId=memento.getMasterNodeId();
  ManagementNodeSyncRecord currMasterNodeRecord=memento.getManagementNodes().get(currMasterNodeId);
  ManagementNodeSyncRecord ownNodeRecord=memento.getManagementNodes().get(ownNodeId);
  ManagementNodeSyncRecord newMasterNodeRecord=null;
  boolean demotingSelfInFavourOfOtherMaster=false;
  if (currMasterNodeRecord != null && currMasterNodeRecord.getStatus() == ManagementNodeState.MASTER && isHeartbeatOk(currMasterNodeRecord,ownNodeRecord)) {
    if (ownNodeId.equals(currMasterNodeId)) {
      if (LOG.isTraceEnabled())       LOG.trace("String_Node_Str",currMasterNodeRecord.toVerboseString());
      return;
    }
 else {
      if (ownNodeRecord != null && ownNodeRecord.getStatus() == ManagementNodeState.MASTER) {
        LOG.error("String_Node_Str" + ownNodeId + "String_Node_Str"+ currMasterNodeId);
        newMasterNodeRecord=currMasterNodeRecord;
        demotingSelfInFavourOfOtherMaster=true;
      }
 else {
        if (LOG.isTraceEnabled())         LOG.trace("String_Node_Str",currMasterNodeRecord.toVerboseString());
        return;
      }
    }
  }
 else   if (ownNodeRecord == null || !isHeartbeatOk(ownNodeRecord,ownNodeRecord)) {
    if (ownNodeRecord == null) {
      LOG.error("String_Node_Str" + ownNodeId + "String_Node_Str"+ "String_Node_Str"+ currMasterNodeId+ "String_Node_Str");
    }
 else {
      LOG.error("String_Node_Str" + ownNodeId + "String_Node_Str"+ "String_Node_Str"+ currMasterNodeId+ "String_Node_Str"+ "String_Node_Str"+ ownNodeRecord.toVerboseString());
    }
    return;
  }
 else   if (ownNodeId.equals(currMasterNodeId)) {
    LOG.warn("String_Node_Str" + ownNodeId + "String_Node_Str"+ "String_Node_Str"+ ownNodeRecord.toVerboseString());
    return;
  }
  if (demotingSelfInFavourOfOtherMaster) {
    LOG.debug("String_Node_Str" + ownNodeRecord.toVerboseString() + "String_Node_Str"+ newMasterNodeRecord.toVerboseString());
    demoteToStandby(BrooklynFeatureEnablement.isEnabled(BrooklynFeatureEnablement.FEATURE_DEFAULT_STANDBY_IS_HOT_PROPERTY));
    return;
  }
 else {
    LOG.debug("String_Node_Str" + currMasterNodeRecord);
  }
  newMasterNodeRecord=masterChooser.choose(memento,heartbeatTimeout,ownNodeId);
  String newMasterNodeId=(newMasterNodeRecord == null) ? null : newMasterNodeRecord.getNodeId();
  URI newMasterNodeUri=(newMasterNodeRecord == null) ? null : newMasterNodeRecord.getUri();
  boolean weAreNewMaster=ownNodeId.equals(newMasterNodeId);
  if (LOG.isDebugEnabled()) {
    LOG.debug("String_Node_Str",new Object[]{(newMasterNodeRecord == null ? "String_Node_Str" : newMasterNodeRecord.toVerboseString()),(currMasterNodeRecord == null ? currMasterNodeId + "String_Node_Str" : currMasterNodeRecord.toVerboseString()),memento,ownNodeRecord.toVerboseString(),heartbeatTimeout});
  }
  if (!initializing) {
    String message="String_Node_Str" + ownNodeId + "String_Node_Str";
    if (weAreNewMaster)     message+="String_Node_Str";
 else     message+="String_Node_Str";
    message+=currMasterNodeId + "String_Node_Str" + (currMasterNodeRecord == null ? "String_Node_Str" : currMasterNodeRecord.getRemoteTimestamp())+ "String_Node_Str"+ "String_Node_Str"+ (newMasterNodeId == null ? "String_Node_Str" : (weAreNewMaster ? "String_Node_Str" : "String_Node_Str") + newMasterNodeId + "String_Node_Str"+ newMasterNodeRecord.getRemoteTimestamp()+ "String_Node_Str"+ (newMasterNodeUri != null ? "String_Node_Str" + newMasterNodeUri : "String_Node_Str"));
    LOG.warn(message);
  }
  if (weAreNewMaster) {
    promoteToMaster();
  }
}