public static void handleOrphan(Cluster cluster, ClusterAddressInfo address) {
   Integer orphanCount = 1;
   orphanCount = orphans.putIfAbsent(address, orphanCount);
   orphanCount = (orphanCount == null) ? 1 : orphanCount;
   orphans.put(address, orphanCount + 1);
   EventRecord.caller(
           ClusterState.class,
           EventType.ADDRESS_STATE,
           "Updated orphaned public ip address: "
               + LogUtil.dumpObject(address)
               + " count="
               + orphanCount)
       .debug();
   if (orphanCount > AddressingConfiguration.getInstance().getMaxKillOrphans()) {
     EventRecord.caller(
             ClusterState.class,
             EventType.ADDRESS_STATE,
             "Unassigning orphaned public ip address: "
                 + LogUtil.dumpObject(address)
                 + " count="
                 + orphanCount)
         .warn();
     try {
       final Address addr = Addresses.getInstance().lookup(address.getAddress());
       if (addr.isPending()) {
         try {
           addr.clearPending();
         } catch (Exception ex) {
         }
       }
       try {
         if (addr.isAssigned() && "0.0.0.0".equals(address.getInstanceIp())) {
           addr.unassign().clearPending();
           if (addr.isSystemOwned()) {
             addr.release();
           }
         } else if (addr.isAssigned() && !"0.0.0.0".equals(address.getInstanceIp())) {
           AsyncRequests.newRequest(new UnassignAddressCallback(address))
               .sendSync(cluster.getConfiguration());
           if (addr.isSystemOwned()) {
             addr.release();
           }
         } else if (!addr.isAssigned() && addr.isAllocated() && addr.isSystemOwned()) {
           addr.release();
         }
       } catch (ExecutionException ex) {
         if (!addr.isAssigned() && addr.isAllocated() && addr.isSystemOwned()) {
           addr.release();
         }
       }
     } catch (InterruptedException ex) {
       Exceptions.maybeInterrupted(ex);
     } catch (NoSuchElementException ex) {
     } finally {
       orphans.remove(address);
     }
   }
 }
 private static void ensureAllocated(final Address addr, final VmInstance vm) {
   long lastUpdate = addr.lastUpdateMillis();
   if (lastUpdate > 60L * 1000 * AddressingConfiguration.getInstance().getOrphanGrace()) {
     if (!addr.isAllocated() && !addr.isPending()) {
       try {
         if (!addr.isAssigned() && !addr.isPending()) {
           addr.pendingAssignment();
           try {
             addr.assign(vm).clearPending();
           } catch (final Exception e1) {
             LOG.debug(e1, e1);
           }
         }
       } catch (final Exception e1) {
         LOG.debug(e1, e1);
       }
     } else if (!addr.isAssigned()) {
       try {
         addr.assign(vm).clearPending();
       } catch (final Exception e1) {
         LOG.debug(e1, e1);
       }
     } else {
       LOG.debug("Address usage checked: " + addr);
     }
   }
 }
 public void update(final Cluster cluster, final List<ClusterAddressInfo> ccList) {
   Helper.loadStoredAddresses();
   for (final ClusterAddressInfo addrInfo : ccList) {
     try {
       final Address address = Helper.lookupOrCreate(cluster, addrInfo);
       if (address.isAssigned() && !addrInfo.hasMapping() && !address.isPending()) {
         if (Principals.nobodyFullName().equals(address.getOwner())) {
           Helper.markAsAllocated(cluster, addrInfo, address);
         }
         try {
           final VmInstance vm = VmInstances.lookupByPrivateIp(addrInfo.getInstanceIp());
           clearOrphan(addrInfo);
         } catch (final NoSuchElementException e) {
           try {
             final VmInstance vm = VmInstances.lookup(address.getInstanceId());
             clearOrphan(addrInfo);
           } catch (final NoSuchElementException ex) {
             InetAddress addr = null;
             try {
               addr = Inet4Address.getByName(addrInfo.getInstanceIp());
             } catch (final UnknownHostException e1) {
               LOG.debug(e1, e1);
             }
             if ((addr == null) || !addr.isLoopbackAddress()) {
               handleOrphan(cluster, addrInfo);
             }
           }
         }
       } else if (address.isAllocated()
           && Principals.nobodyFullName().equals(address.getOwner())
           && !address.isPending()) {
         Helper.markAsAllocated(cluster, addrInfo, address);
       }
     } catch (final Exception e) {
       LOG.debug(e, e);
     }
   }
 }