protected void addLocations(RebindContext rebindContext, EntityMemento memento) { for (String id : memento.getLocations()) { Location loc = rebindContext.getLocation(id); if (loc != null) { ((EntityInternal)entity).addLocations(ImmutableList.of(loc)); } else { LOG.warn("Location not found; discarding location {} of entity {}({})", new Object[] {id, memento.getType(), memento.getId()}); } } }
@Override public String addRegion(String location) { Preconditions.checkNotNull(location, "location"); Location l = getManagementContext().getLocationRegistry().resolve(location); addLocations(Arrays.asList(l)); Entity e = addCluster(l); ((EntityInternal)e).addLocations(Arrays.asList(l)); if (e instanceof Startable) { Task<?> task = e.invoke(Startable.START, ImmutableMap.of("locations", ImmutableList.of(l))); task.getUnchecked(); } return e.getId(); }
@Override public void start(Collection<? extends Location> locations) { Preconditions.checkNotNull(locations, "locations must be supplied"); Preconditions.checkArgument(locations.size() >= 1, "One or more location must be supplied"); addLocations(locations); setAttribute(SERVICE_STATE, Lifecycle.STARTING); try { Map<Entity, Task<?>> tasks = Maps.newLinkedHashMap(); for (Location it : locations) { Entity e = addCluster(it); // FIXME: this is a quick workaround to ensure that the location is available to any membership change // listeners (notably AbstractDeoDnsService). A more robust mechanism is required; see ENGR-???? // for ideas and discussion. ((EntityInternal)e).addLocations(Arrays.asList(it)); if (e instanceof Startable) { Task task = e.invoke(Startable.START, ImmutableMap.of("locations", ImmutableList.of(it))); tasks.put(e, task); } } waitForTasksOnStart(tasks); setAttribute(SERVICE_STATE, Lifecycle.RUNNING); setAttribute(SERVICE_UP, true); } catch (Exception e) { setAttribute(SERVICE_STATE, Lifecycle.ON_FIRE); throw Exceptions.propagate(e); } }
DynamicTasks.queue("pre-start", new Runnable() { public void run() { MachineLocation machine = machineS.get(); log.info("Starting {} on machine {}", entity(), machine); entity().addLocations(ImmutableList.of((Location)machine)); // elsewhere we rely on (public) hostname being set _after_ subnet_hostname // (to prevent the tiny possibility of races resulting in hostname being returned // simply because subnet is still being looked up) Optional<String> lh = Machines.getSubnetHostname(machine); Optional<String> la = Machines.getSubnetIp(machine); if (lh.isPresent()) entity().setAttribute(Attributes.SUBNET_HOSTNAME, lh.get()); if (la.isPresent()) entity().setAttribute(Attributes.SUBNET_ADDRESS, la.get()); entity().setAttribute(Attributes.HOSTNAME, machine.getAddress().getHostName()); entity().setAttribute(Attributes.ADDRESS, machine.getAddress().getHostAddress()); preStartCustom(machine); }}); }