Program: 105589
Project: 105589
Buggy source:
/** 
 * enableVlanNetwork creates a Network object, Vlan object, and thereby a tagged PIF object in Xapi. In XenServer, VLAN is added by - Create a network, which is unique cluster wide. - Find the PIF that you want to create the VLAN on. - Create a VLAN using the network and the PIF.  As a result of this operation, a tagged PIF object is also created. Here is a list of problems with clustered Xapi implementation that we are trying to circumvent. - There can be multiple Networks with the same name-label so searching using name-label is not unique. - There are no other ways to search for Networks other than listing all of them which is not efficient in our implementation because we can have over 4000 VLAN networks. - In a clustered situation, it's possible for both hosts to detect that the Network is missing and both creates it.  This causes a lot of problems as one host may be using one Network and another may be using a different network for their VMs.  This causes problems in migration because the VMs are logically attached to different networks in Xapi's database but in reality, they are attached to the same network. To work around these problems, we do the following. - When creating the VLAN network, we name it as VLAN-UUID of the Network it is created on-VLAN Tag.  Because VLAN tags is unique with one particular network, this is a unique name-label to quickly retrieve the the VLAN network with when we need it again. - When we create the VLAN network, we add a timestamp and a random number as a tag into the network.  Then instead of creating VLAN on that network, we actually retrieve the Network again and this time uses the VLAN network with lowest timestamp or lowest random number as the VLAN network.  This allows VLAN creation to happen on multiple hosts concurrently but even if two VLAN networks were created with the same name, only one of them is used. One cavaet about this approach is that it relies on the timestamp to be relatively accurate among different hosts.
 * @param conn Xapi Connection
 * @param tag VLAN tag
 * @param network network on this host to create the VLAN on.
 * @return VLAN Network created.
 * @throws XenAPIException
 * @throws XmlRpcException
 */
protected Network enableVlanNetwork(Connection conn,long tag,XsLocalNetwork network) throws XenAPIException, XmlRpcException {
  Network vlanNetwork=null;
  String oldName="String_Node_Str" + Long.toString(tag);
  String newName="String_Node_Str" + network.getNetworkRecord(conn).uuid + "String_Node_Str"+ tag;
  XsLocalNetwork vlanNic=getNetworkByName(conn,newName);
  if (vlanNic == null) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + oldName);
    }
    vlanNic=getNetworkByName(conn,oldName);
    if (vlanNic != null) {
      s_logger.info("String_Node_Str" + oldName + "String_Node_Str"+ newName);
      vlanNic.getNetwork().setNameLabel(conn,newName);
    }
  }
  if (vlanNic == null) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + tag + "String_Node_Str"+ _host.ip);
    }
    Network.Record nwr=new Network.Record();
    nwr.nameLabel=newName;
    nwr.tags=new HashSet<String>();
    nwr.tags.add(generateTimeStamp());
    vlanNetwork=Network.create(conn,nwr);
    vlanNic=getNetworkByName(conn,newName);
  }
  PIF nPif=network.getPif(conn);
  PIF.Record nPifr=network.getPifRecord(conn);
  vlanNetwork=vlanNic.getNetwork();
  if (vlanNic.getPif(conn) != null) {
    return vlanNetwork;
  }
  if (s_logger.isDebugEnabled()) {
    s_logger.debug("String_Node_Str" + tag + "String_Node_Str"+ _host.ip+ "String_Node_Str"+ nPifr.device);
  }
  VLAN vlan=VLAN.create(conn,nPif,tag,vlanNetwork);
  VLAN.Record vlanr=vlan.getRecord(conn);
  if (s_logger.isDebugEnabled()) {
    s_logger.debug("String_Node_Str" + tag + "String_Node_Str"+ vlanr.uuid);
  }
  return vlanNetwork;
}

Fixed version (ground truth):
/** 
 * enableVlanNetwork creates a Network object, Vlan object, and thereby a tagged PIF object in Xapi. In XenServer, VLAN is added by - Create a network, which is unique cluster wide. - Find the PIF that you want to create the VLAN on. - Create a VLAN using the network and the PIF.  As a result of this operation, a tagged PIF object is also created. Here is a list of problems with clustered Xapi implementation that we are trying to circumvent. - There can be multiple Networks with the same name-label so searching using name-label is not unique. - There are no other ways to search for Networks other than listing all of them which is not efficient in our implementation because we can have over 4000 VLAN networks. - In a clustered situation, it's possible for both hosts to detect that the Network is missing and both creates it.  This causes a lot of problems as one host may be using one Network and another may be using a different network for their VMs.  This causes problems in migration because the VMs are logically attached to different networks in Xapi's database but in reality, they are attached to the same network. To work around these problems, we do the following. - When creating the VLAN network, we name it as VLAN-UUID of the Network it is created on-VLAN Tag.  Because VLAN tags is unique with one particular network, this is a unique name-label to quickly retrieve the the VLAN network with when we need it again. - When we create the VLAN network, we add a timestamp and a random number as a tag into the network.  Then instead of creating VLAN on that network, we actually retrieve the Network again and this time uses the VLAN network with lowest timestamp or lowest random number as the VLAN network.  This allows VLAN creation to happen on multiple hosts concurrently but even if two VLAN networks were created with the same name, only one of them is used. One cavaet about this approach is that it relies on the timestamp to be relatively accurate among different hosts.
 * @param conn Xapi Connection
 * @param tag VLAN tag
 * @param network network on this host to create the VLAN on.
 * @return VLAN Network created.
 * @throws XenAPIException
 * @throws XmlRpcException
 */
protected Network enableVlanNetwork(Connection conn,long tag,XsLocalNetwork network) throws XenAPIException, XmlRpcException {
  Network vlanNetwork=null;
  String oldName="String_Node_Str" + Long.toString(tag);
  String newName="String_Node_Str" + network.getNetworkRecord(conn).uuid + "String_Node_Str"+ tag;
  XsLocalNetwork vlanNic=getNetworkByName(conn,newName);
  if (vlanNic == null) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + oldName);
    }
    vlanNic=getNetworkByName(conn,oldName);
    if (vlanNic != null) {
      s_logger.info("String_Node_Str" + oldName + "String_Node_Str"+ newName);
      vlanNic.getNetwork().setNameLabel(conn,newName);
    }
  }
  if (vlanNic == null) {
    if (s_logger.isDebugEnabled()) {
      s_logger.debug("String_Node_Str" + tag + "String_Node_Str"+ _host.ip);
    }
    Network.Record nwr=new Network.Record();
    nwr.nameLabel=newName;
    nwr.tags=new HashSet<String>();
    nwr.tags.add(generateTimeStamp());
    vlanNetwork=Network.create(conn,nwr);
    vlanNic=getNetworkByName(conn,newName);
    if (vlanNic == null) {
      throw new CloudRuntimeException("String_Node_Str" + newName);
    }
  }
  PIF nPif=network.getPif(conn);
  PIF.Record nPifr=network.getPifRecord(conn);
  vlanNetwork=vlanNic.getNetwork();
  if (vlanNic.getPif(conn) != null) {
    return vlanNetwork;
  }
  if (s_logger.isDebugEnabled()) {
    s_logger.debug("String_Node_Str" + tag + "String_Node_Str"+ _host.ip+ "String_Node_Str"+ nPifr.device);
  }
  VLAN vlan=VLAN.create(conn,nPif,tag,vlanNetwork);
  VLAN.Record vlanr=vlan.getRecord(conn);
  if (s_logger.isDebugEnabled()) {
    s_logger.debug("String_Node_Str" + tag + "String_Node_Str"+ vlanr.uuid);
  }
  return vlanNetwork;
}