Beispiel #1
0
 private static void addAddress(final Address address) {
   Address addr = address;
   EntityWrapper<Address> db = EntityWrapper.get(Address.class);
   try {
     addr =
         db.getUnique(
             new Address() {
               {
                 this.setDisplayName(address.getName());
               }
             });
     addr.setOwner(address.getOwner());
     db.commit();
   } catch (RuntimeException e) {
     db.rollback();
     LOG.error(e, e);
   } catch (EucalyptusCloudException e) {
     try {
       db.add(address);
       db.commit();
     } catch (Exception e1) {
       db.rollback();
     }
   }
 }
Beispiel #2
0
 @Override
 public boolean equals(final Object o) {
   if (this == o) return true;
   if (!(o instanceof Address)) return false;
   Address address = (Address) o;
   if (!this.getDisplayName().equals(address.getDisplayName())) return false;
   return true;
 }
 private static void clearAddressCachedState(final Address addr) {
   try {
     if (!addr.isPending()) {
       addr.unassign().clearPending();
     }
   } catch (final Exception t) {
     LOG.trace(t, t);
   }
 }
 public AssignAddressCallback(Address address) {
   super(
       new AssignAddressType(
           address.getStateUuid(),
           address.getName(),
           address.getInstanceAddress(),
           address.getInstanceId()));
   this.address = address;
   this.vm = lookupVm();
 }
 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);
     }
   }
 }
 private static void markAsAllocated(
     final Cluster cluster, final ClusterAddressInfo addrInfo, final Address address) {
   try {
     if (!address.isPending()) {
       for (final VmInstance vm : VmInstances.list(VmState.RUNNING)) {
         if (addrInfo.getInstanceIp().equals(vm.getPrivateAddress())
             && VmState.RUNNING.equals(vm.getState())) {
           LOG.warn(
               "Out of band address state change: "
                   + LogUtil.dumpObject(addrInfo)
                   + " address="
                   + address
                   + " vm="
                   + vm);
           //              if ( !address.isAllocated( ) ) {
           //                address.pendingAssignment( ).assign( vm ).clearPending( );
           //              } else {
           //                address.assign( vm ).clearPending( );
           //              }
           //              clearOrphan( addrInfo );
           return;
         }
       }
     }
   } catch (final IllegalStateException e) {
     LOG.error(e);
   }
 }
Beispiel #7
0
 private static void removeAddress(final String ipAddress) {
   try {
     Addresses.getInstance().disable(ipAddress);
   } catch (NoSuchElementException e1) {
     LOG.debug(e1);
   }
   EntityWrapper<Address> db = EntityWrapper.get(Address.class);
   try {
     Address searchAddr = new Address(ipAddress);
     searchAddr.setOwner(null);
     Address dbAddr = db.getUnique(searchAddr);
     db.delete(dbAddr);
     db.commit();
   } catch (Exception e) {
     Logs.extreme().error(e, e);
     db.rollback();
   }
 }
 protected static void loadStoredAddresses() {
   final Address clusterAddr = new Address();
   final EntityTransaction db = Entities.get(Address.class);
   try {
     for (Address addr : Entities.query(clusterAddr)) {
       if (!Addresses.getInstance().contains(addr.getName())) {
         try {
           addr.init();
         } catch (Exception ex) {
           LOG.error(ex, ex);
         }
       }
     }
     db.commit();
   } catch (final Exception e) {
     LOG.debug(e, e);
     db.rollback();
   }
 }
  public Address allocateNext(final OwnerFullName userId) throws NotEnoughResourcesException {
    int numSystemReserved = 0;
    try {
      ConfigurableProperty p =
          PropertyDirectory.getPropertyEntry("cloud.addresses.systemreservedpublicaddresses");
      if (p != null) numSystemReserved = Integer.parseInt(p.getValue());
    } catch (IllegalAccessException e) {
      LOG.error("Can't find the 'systemreservedpublicaddresses' property");
      numSystemReserved = 0;
    }
    if ((Addresses.getInstance().listDisabledValues().size() - numSystemReserved) < 1) {
      throw new NotEnoughResourcesException(ExceptionList.ERR_SYS_INSUFFICIENT_ADDRESS_CAPACITY);
    }

    Predicate<Address> predicate = RestrictedTypes.filterPrivileged();
    final Address addr = Addresses.getInstance().enableFirst(predicate).allocate(userId);

    LOG.debug("Allocated address for public addressing: " + addr.toString());
    if (addr == null) {
      LOG.debug(LogUtil.header(Addresses.getInstance().toString()));
      throw new NotEnoughResourcesException(ExceptionList.ERR_SYS_INSUFFICIENT_ADDRESS_CAPACITY);
    }
    return 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);
     }
   }
 }
Beispiel #11
0
 public void init() { // Should only EVER be called externally after loading from the db
   this.atomicState = new AtomicMarkableReference<State>(State.unallocated, false);
   this.transition = this.QUIESCENT;
   this.getOwner(); // ensure to initialize
   if (this.instanceAddress == null || this.instanceId == null) {
     this.instanceAddress = UNASSIGNED_INSTANCEADDR;
     this.instanceUuid = UNASSIGNED_INSTANCEUUID;
     this.instanceId = UNASSIGNED_INSTANCEID;
   }
   if (Principals.nobodyFullName().equals(super.getOwner())) {
     this.atomicState.set(State.unallocated, true);
     this.instanceAddress = UNASSIGNED_INSTANCEADDR;
     this.instanceUuid = UNASSIGNED_INSTANCEUUID;
     this.instanceId = UNASSIGNED_INSTANCEID;
     Addresses.getInstance().registerDisabled(this);
     this.atomicState.set(State.unallocated, false);
   } else if (!this.instanceId.equals(UNASSIGNED_INSTANCEID)) {
     this.atomicState.set(State.assigned, true);
     Addresses.getInstance().register(this);
     this.atomicState.set(State.assigned, false);
   } else {
     this.atomicState.set(State.allocated, true);
     if (this.isSystemOwned()) {
       Addresses.getInstance().registerDisabled(this);
       this.setOwner(Principals.nobodyFullName());
       this.instanceAddress = UNASSIGNED_INSTANCEADDR;
       this.instanceUuid = UNASSIGNED_INSTANCEUUID;
       this.instanceId = UNASSIGNED_INSTANCEID;
       Address.removeAddress(this.getDisplayName());
       this.atomicState.set(State.unallocated, false);
     } else {
       Addresses.getInstance().register(this);
       this.atomicState.set(State.allocated, false);
     }
   }
   LOG.debug("Initialized address: " + this.toString());
 }
 protected static Address lookupOrCreate(
     final Cluster cluster, final ClusterAddressInfo addrInfo) {
   Address addr = null;
   VmInstance vm = null;
   try {
     addr = Addresses.getInstance().lookupDisabled(addrInfo.getAddress());
     LOG.trace("Found address in the inactive set cache: " + addr);
   } catch (final NoSuchElementException e1) {
     try {
       addr = Addresses.getInstance().lookup(addrInfo.getAddress());
       LOG.trace("Found address in the active set cache: " + addr);
     } catch (final NoSuchElementException e) {
     }
   }
   if (addrInfo.hasMapping()) {
     vm =
         Helper.maybeFindVm(
             addr != null ? addr.getInstanceId() : null,
             addrInfo.getAddress(),
             addrInfo.getInstanceIp());
     if ((addr != null) && (vm != null)) {
       Helper.ensureAllocated(addr, vm);
       clearOrphan(addrInfo);
     } else if (addr != null && !addr.isPending() && vm != null && VmStateSet.DONE.apply(vm)) {
       handleOrphan(cluster, addrInfo);
     } else if ((addr != null && addr.isAssigned() && !addr.isPending()) && (vm == null)) {
       handleOrphan(cluster, addrInfo);
     } else if ((addr == null) && (vm != null)) {
       addr =
           new Address(
               Principals.systemFullName(),
               addrInfo.getAddress(),
               vm.getInstanceUuid(),
               vm.getInstanceId(),
               vm.getPrivateAddress());
       clearOrphan(addrInfo);
     } else if ((addr == null) && (vm == null)) {
       addr = new Address(addrInfo.getAddress(), cluster.getPartition());
       handleOrphan(cluster, addrInfo);
     }
   } else {
     if ((addr != null) && addr.isAssigned() && !addr.isPending()) {
       handleOrphan(cluster, addrInfo);
     } else if ((addr != null)
         && !addr.isAssigned()
         && !addr.isPending()
         && addr.isSystemOwned()) {
       try {
         addr.release();
       } catch (final Exception ex) {
         LOG.error(ex);
       }
     } else if ((addr != null) && Address.Transition.system.equals(addr.getTransition())) {
       handleOrphan(cluster, addrInfo);
     } else if (addr == null) {
       addr = new Address(addrInfo.getAddress(), cluster.getPartition());
       Helper.clearVmState(addrInfo);
     }
   }
   return addr;
 }
 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);
     }
   }
 }