private Entity getMember(String memberId) { Group cluster = (Group)entity(); try { return Iterables.find(cluster.getMembers(), EntityPredicates.idEqualTo(memberId)); } catch (NoSuchElementException e) { throw new IllegalStateException(memberId + " is not an ID of brooklyn node in this cluster"); } }
private MySqlNode getSnapshotNode() { String snapshotNodeId = cluster.getConfig(MySqlCluster.REPLICATION_PREFERRED_SOURCE); if (snapshotNodeId != null) { Optional<Entity> preferredNode = Iterables.tryFind(cluster.getMembers(), EntityPredicates.idEqualTo(snapshotNodeId)); if (preferredNode.isPresent()) { return (MySqlNode) preferredNode.get(); } else { log.warn("MySql cluster " + this + " configured with preferred snapshot node " + snapshotNodeId + " but it's not a member. Defaulting to a random slave."); } } return getRandomSlave(); }
predicate = EntityPredicates.idEqualTo(entity.getId()); } else if (verb.equalsIgnoreCase(APPLICATION)) { predicate = EntityPredicates.applicationIdEqualTo(entity.getApplicationId()); predicate = EntityPredicates.idEqualTo(parameter); } else if (verb.equalsIgnoreCase(APPLICATION)) { predicate = EntityPredicates.applicationIdEqualTo(parameter);
protected void onServerPoolMemberRemoved(final Entity member) { Map<Entity, String> nodes = MutableMap.copyOf(sensors().get(ETCD_CLUSTER_NODES)); Duration timeout = config().get(BrooklynConfigKeys.START_TIMEOUT); String name = nodes.get(member); if (nodes.containsKey(member)) { synchronized (clusterMutex) { Optional<Entity> otherNode = Iterables.tryFind(nodes.keySet(), Predicates.and( Predicates.instanceOf(EtcdNode.class), EntityPredicates.attributeEqualTo(EtcdNode.ETCD_NODE_HAS_JOINED_CLUSTER, Boolean.TRUE), Predicates.not(EntityPredicates.idEqualTo(member.getId())))); if (otherNode.isPresent()) { boolean ended = Entities.invokeEffectorWithArgs(this, otherNode.get(), EtcdNode.LEAVE_ETCD_CLUSTER, name).blockUntilEnded(timeout); if (!ended) { LOG.warn("Timeout invoking leaveCluster for {} on {}", member, otherNode.get()); } } removeNode(member, name); } } }
private boolean isReplicationInfoValid(ReplicationSnapshot replicationSnapshot) { MySqlNode master = getMaster(); String dataDir = Strings.nullToEmpty(master.getConfig(MySqlNode.DATA_DIR)); if (!checkFileExistsOnEntity(master, Os.mergePathsUnix(dataDir, replicationSnapshot.getBinLogName()))) { return false; } if (replicationSnapshot.getEntityId() != null) { Optional<Entity> snapshotSlave = Iterables.tryFind(cluster.getChildren(), EntityPredicates.idEqualTo(replicationSnapshot.getEntityId())); if (!snapshotSlave.isPresent()) { log.info("MySql cluster " + cluster + " missing node " + replicationSnapshot.getEntityId() + " with last snapshot " + replicationSnapshot.getSnapshotPath() + ". Will generate new snapshot."); return false; } if (!checkFileExistsOnEntity(snapshotSlave.get(), replicationSnapshot.getSnapshotPath())) { log.info("MySql cluster " + cluster + ", node " + snapshotSlave.get() + " missing replication snapshot " + replicationSnapshot.getSnapshotPath() + ". Will generate new snapshot."); return false; } } return true; }
@Test public void testGroupWithMatchingFilterReturnsOnlyMatchingMembers() throws Exception { group.setEntityFilter(EntityPredicates.idEqualTo(e1.getId())); assertEqualsIgnoringOrder(group.getMembers(), ImmutableList.of(e1)); }
@Test public void testEntityCreatingItsEnricherDoesNotReCreateItUnlessUniqueTagDifferent() throws Exception { TestEntity e1 = origApp.createAndManageChild(EntitySpec.create(TestEntity.class, MyTestEntityWithEnricher.class)); Collection<Enricher> e1e = e1.getEnrichers(); log.info("enrichers1: "+e1e); Entities.dumpInfo(e1); assertEquals(e1e.size(), 5); newApp = rebind(); Entity e2 = Iterables.getOnlyElement( Entities.descendantsAndSelf(newApp, EntityPredicates.idEqualTo(e1.getId())) ); Collection<Enricher> e2e = e2.getEnrichers(); log.info("enrichers2: "+e2e); Entities.dumpInfo(e2); assertEquals(e2e.size(), e1e.size()+1); }
@Test public void testFailureGeneratingMementoStillPersistsOtherEntities() throws Exception { MyEntity origE = origApp.createAndManageChild(EntitySpec.create(MyEntity.class)); MyEntity origFailingE = origApp.createAndManageChild(EntitySpec.create(MyEntity.class) .impl(MyEntityFailingImpl.class) .configure(MyEntityFailingImpl.FAIL_ON_GENERATE_MEMENTO, true)); newApp = rebind(); MyEntity newE = (MyEntity) Iterables.find(newApp.getChildren(), EntityPredicates.idEqualTo(origE.getId())); Optional<Entity> newFailingE = Iterables.tryFind(newApp.getChildren(), EntityPredicates.idEqualTo(origFailingE.getId())); // Expect origFailingE to never have been persisted, but origE to have worked assertNotNull(newE); assertFalse(newFailingE.isPresent(), "newFailedE="+newFailingE); }
/** * @deprecated since 0.7; support for rebinding old-style entities is deprecated */ @Deprecated @Test public void testHandlesOldStyleEntity() throws Exception { MyOldStyleEntity origE = new MyOldStyleEntity(MutableMap.of("confName", "myval"), origApp); Entities.manage(origE); newApp = rebind(); MyOldStyleEntity newE = (MyOldStyleEntity) Iterables.find(newApp.getChildren(), EntityPredicates.idEqualTo(origE.getId())); assertEquals(newE.getConfig(MyOldStyleEntity.CONF_NAME), "myval"); }
@Override public void onCallback(String childId, boolean success) { Optional<Entity> child = Iterables.tryFind(getChildren(), EntityPredicates.idEqualTo(childId)); if (child.isPresent()) { ((AsyncEntity)child.get()).onCallback(success); } else { LOG.warn("Child not found with resourceId '"+childId+"'; not injecting state from callback"); } Optional<Entity> unstartedVm = Iterables.tryFind(getChildren(), EntityPredicates.attributeSatisfies(Attributes.SERVICE_STATE_EXPECTED, new Predicate<Lifecycle.Transition>() { @Override public boolean apply(Transition input) { return input == null || input.getState() == Lifecycle.STARTING; }})); if (!unstartedVm.isPresent()) { // No VMs are still starting; we are finished starting ServiceStateLogic.ServiceNotUpLogic.clearNotUpIndicator(this, START.getName()); ServiceStateLogic.setExpectedState(this, Lifecycle.RUNNING); } }
@Test public void testReplacesEntityInSameZone() throws Exception { cluster.config().set(DynamicCluster.AVAILABILITY_ZONE_NAMES, ImmutableList.of("zone1", "zone2")); cluster.start(ImmutableList.of(loc)); cluster.resize(4); List<String> locsUsed = getLocationNames(getLocationsOf(cluster.getMembers())); Asserts.assertEqualsIgnoringOrder(locsUsed, ImmutableList.of("zone1", "zone1", "zone2", "zone2")); String idToRemove = Iterables.getFirst(cluster.getMembers(), null).getId(); String idAdded = cluster.replaceMember(idToRemove); locsUsed = getLocationNames(getLocationsOf(cluster.getMembers())); Asserts.assertEqualsIgnoringOrder(locsUsed, ImmutableList.of("zone1", "zone1", "zone2", "zone2")); assertNull(Iterables.find(cluster.getMembers(), EntityPredicates.idEqualTo(idToRemove), null)); assertNotNull(Iterables.find(cluster.getMembers(), EntityPredicates.idEqualTo(idAdded), null)); }
@Test public void testGroupRemovesUnmanagedEntity() throws Exception { group.setEntityFilter(EntityPredicates.idEqualTo(e1.getId())); assertEqualsIgnoringOrder(group.getMembers(), ImmutableSet.of(e1)); Entities.unmanage(e1); Asserts.succeedsEventually(new Runnable() { @Override public void run() { assertEqualsIgnoringOrder(group.getMembers(), ImmutableSet.of()); }}); }
@Test public void testReplacesEntityInSameZone() throws Exception { cluster.config().set(DynamicCluster.ENABLE_AVAILABILITY_ZONES, true); cluster.start(ImmutableList.of(multiLoc)); cluster.resize(4); List<String> locsUsed = getLocationNames(getLocationsOf(cluster.getMembers(), Predicates.instanceOf(MachineProvisioningLocation.class))); Asserts.assertEqualsIgnoringOrder(locsUsed, ImmutableList.of("loc1", "loc1", "loc2", "loc2")); String idToRemove = Iterables.getFirst(cluster.getMembers(), null).getId(); String idAdded = cluster.replaceMember(idToRemove); locsUsed = getLocationNames(getLocationsOf(cluster.getMembers(), Predicates.instanceOf(MachineProvisioningLocation.class))); Asserts.assertEqualsIgnoringOrder(locsUsed, ImmutableList.of("loc1", "loc1", "loc2", "loc2")); assertNull(Iterables.find(cluster.getMembers(), EntityPredicates.idEqualTo(idToRemove), null)); assertNotNull(Iterables.find(cluster.getMembers(), EntityPredicates.idEqualTo(idAdded), null)); }
Entity sourceEntity = Iterables.find(cluster.getMembers(), EntityPredicates.idEqualTo(replicationSnapshot.getEntityId())); String dumpId = FilenameUtils.removeExtension(replicationSnapshot.getSnapshotPath()); copyDumpAsync(sourceEntity, slave, replicationSnapshot.getSnapshotPath(), dumpId);
@SuppressWarnings("unchecked") protected T rebind(RebindOptions options) throws Exception { if (newApp != null || newManagementContext != null) { throw new IllegalStateException("already rebound - use switchOriginalToNewManagementContext() if you are trying to rebind multiple times"); } options = RebindOptions.create(options); if (options.classLoader == null) options.classLoader(classLoader); if (options.mementoDir == null) options.mementoDir(mementoDir); if (options.origManagementContext == null) options.origManagementContext(origManagementContext); if (options.newManagementContext == null) options.newManagementContext(createNewManagementContext(options.mementoDir, options.additionalProperties)); if (options.applicationChooserOnRebind == null && origApp != null) options.applicationChooserOnRebind(EntityPredicates.idEqualTo(origApp.getId())); RebindTestUtils.stopPersistence(options.origManagementContext); newManagementContext = options.newManagementContext; newApp = (T) RebindTestUtils.rebind(options); return newApp; }