protected void createApps() { for (EntitySpec<? extends Application> spec : appSpecsToManage) { Application app = managementContext.getEntityManager().createEntity(spec); apps.add(app); } for (String blueprint : yamlAppsToManage) { Application app = EntityManagementUtils.createUnstarted(managementContext, blueprint); // Note: BrooklynAssemblyTemplateInstantiator automatically puts applications under management. apps.add(app); } }
@Override public Collection<Entity> getEntities() { if (isInitialManagementContextReal()) { return initialManagementContext.getEntityManager().getEntities(); } else { return Collections.emptyList(); } }
@Override protected T customReadBody(String type, Map<Object, Object> values, JsonParser jp, DeserializationContext ctxt) throws IOException { Optional<String> entityId = Optional.fromNullable((String) values.get("entityId")); Optional<Entity> entity = Optional.fromNullable(entityId.isPresent() ? null: mgmt.getEntityManager().getEntity(entityId.get())); String id = (String) values.get("id"); return getInstanceFromId(entity, id); } protected Optional<String> getEntityId(T value) {
@Test public void testEntityManagerQueriesGiveProxies() { EntityManager entityManager = mgmt.getEntityManager(); Application retrievedApp = (Application) entityManager.getEntity(app.getId()); TestEntity retrievedEntity = (TestEntity) entityManager.getEntity(entity.getId()); assertIsProxy(retrievedApp); assertIsProxy(retrievedEntity); Collection<Entity> entities = entityManager.getEntities(); for (Entity e : entities) { assertIsProxy(e); } assertEquals(ImmutableSet.copyOf(entities), ImmutableSet.of(app, entity)); }
@Test public void testGetEntities() { TestApplication app2 = mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class)); TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); TestEntity child = entity.createAndManageChild(EntitySpec.create(TestEntity.class)); Asserts.assertEqualsIgnoringOrder(entityManager.getEntitiesInApplication(app), ImmutableList.of(app, entity, child)); Asserts.assertEqualsIgnoringOrder(entityManager.getEntities(), ImmutableList.of(app, entity, child, app2)); Asserts.assertEqualsIgnoringOrder(entityManager.findEntities(Predicates.instanceOf(TestApplication.class)), ImmutableList.of(app, app2)); Asserts.assertEqualsIgnoringOrder(entityManager.findEntitiesInApplication(app, Predicates.instanceOf(TestApplication.class)), ImmutableList.of(app)); }
mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class)); fail(); } catch (Exception e) { TestEntity e3 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); assertEquals(ImmutableSet.copyOf(mgmt.getEntityManager().getEntities()), ImmutableSet.of(app, e1, e3));
final TestApplication app = mgmt().getEntityManager().createEntity(EntitySpec.create(TestApplication.class)); final TestApplication newApp = (TestApplication) mgmt().getEntityManager().getEntity(app.getId()); final RecordingStaticUsageListener newListener = RecordingStaticUsageListener.getLastInstance(); assertNotSame(origListener, newListener);
@Test(groups="Broken") public void testStartsThenChildFailsButWithQuorumCausesAppToSucceed() throws Exception { TestApplication app = mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class) .configure(StartableApplication.UP_QUORUM_CHECK, QuorumCheck.QuorumChecks.atLeastOne()) .configure(StartableApplication.RUNNING_QUORUM_CHECK, QuorumCheck.QuorumChecks.atLeastOne()) .child(EntitySpec.create(TestEntity.class)) .child(EntitySpec.create(TestEntity.class))); TestEntity child = (TestEntity) Iterables.get(app.getChildren(), 0); app.start(ImmutableList.<Location>of()); assertUpAndRunningEventually(app); for (Entity childr : app.getChildren()) { EntityAsserts.assertAttributeEquals(childr, TestEntity.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING); } ServiceStateLogic.ServiceNotUpLogic.updateNotUpIndicator(child, "myIndicator", "Simulate not-up of child"); assertHealthContinually(app, Lifecycle.RUNNING, true); mgmt.getEntityManager().unmanage(app); }
@Override public Collection<Entity> findEntities(Predicate<? super Entity> filter) { if (isInitialManagementContextReal()) { return initialManagementContext.getEntityManager().findEntities(filter); } else { return Collections.emptyList(); } }
@Override public void run() { ((EntityInternal)application).destroy(); mgmt.getEntityManager().unmanage(application); } });
@Override public Collection<Entity> getEntitiesInApplication(Application application) { if (isInitialManagementContextReal()) { return initialManagementContext.getEntityManager().getEntitiesInApplication(application); } else { return Collections.emptyList(); } }
@Override public List<DockerHostLocation> filterLocations(List<DockerHostLocation> locations, Entity entity) { List<DockerHostLocation> available = Lists.newArrayList(); // Select hosts that satisfy the affinity rules for (DockerHostLocation machine : locations) { Optional<List<String>> entityRules = Optional.fromNullable(entity.config().get(DockerHost.DOCKER_HOST_AFFINITY_RULES)); Optional<List<String>> hostRules = Optional.fromNullable(machine.getOwner().config().get(DockerHost.DOCKER_HOST_AFFINITY_RULES)); Optional<List<String>> infrastructureRules = Optional.fromNullable(machine.getOwner().getInfrastructure().config().get(DockerHost.DOCKER_HOST_AFFINITY_RULES)); Iterable<String> combined = Iterables.concat(Optional.presentInstances(ImmutableList.of(entityRules, hostRules, infrastructureRules))); AffinityRules rules = AffinityRules.rulesFor(entity).parse(combined); Iterable<Entity> entities = getBrooklynManagementContext().getEntityManager().findEntities(EntityPredicates.locationsIncludes(machine)); if (Iterables.isEmpty(entities)) { if (rules.allowEmptyLocations()) { available.add(machine); } } else { Iterable<Entity> filtered = Iterables.filter(entities, rules); if (Iterables.size(filtered) == Iterables.size(entities)) { available.add(machine); } } } if (LOG.isDebugEnabled()) { LOG.debug("Available Docker hosts: {}", Iterables.toString(available)); } return available; }
@Override public void run() { if (release) Entities.destroyCatching(entity); else mgmt.getEntityManager().unmanage(entity); } });
@Test(groups = "Integration") private void testReadAndWriteDifferentRouters() { MongoDBShardedDeployment deployment = makeAndStartDeployment(); EntityAsserts.assertAttributeEqualsEventually(deployment, Startable.SERVICE_UP, true); MongoDBRouter router1 = (MongoDBRouter) Iterables.get(deployment.getRouterCluster().getMembers(), 0); MongoDBRouter router2 = (MongoDBRouter) Iterables.get(deployment.getRouterCluster().getMembers(), 1); EntityAsserts.assertAttributeEqualsEventually(router1, Startable.SERVICE_UP, true); EntityAsserts.assertAttributeEqualsEventually(router2, Startable.SERVICE_UP, true); String documentId = MongoDBTestHelper.insert(router1, "meaning-of-life", 42); DBObject docOut = MongoDBTestHelper.getById(router2, documentId); Assert.assertEquals(docOut.get("meaning-of-life"), 42); for (Entity entity : Iterables.filter(app.getManagementContext().getEntityManager().getEntitiesInApplication(app), AbstractMongoDBServer.class)) { EntityAsserts.assertAttributeEqualsEventually(entity, Startable.SERVICE_UP, true); } }
@Override public <T extends Entity> T createEntity(EntitySpec<T> spec) { if (isInitialManagementContextReal()) { return initialManagementContext.getEntityManager().createEntity(spec); } else { throw new IllegalStateException("Non-deployment context "+this+" (with no initial management context supplied) is not valid for this operation."); } }
@Override public void run() { Set<FailingEntity> all = ImmutableSet.copyOf(Iterables.filter(mgmt.getEntityManager().getEntities(), FailingEntity.class)); Set<FailingEntity> replacements = Sets.difference(all, initialMembers); assertEquals(replacements.size(), 4); }}); }
@Override protected Entity getInstanceFromId(String id) { return mgmt.getEntityManager().getEntity(id); } }
@Test public void testConcurrentAddChild() throws Exception { final int NUM_TASKS = 100; List<ListenableFuture<?>> futures = Lists.newArrayList(); for (int i = 0; i < NUM_TASKS; i++) { ListenableFuture<?> future = executor.submit(new Runnable() { @Override public void run() { entity.addChild(EntitySpec.create(BasicEntity.class)); }}); futures.add(future); } Futures.allAsList(futures).get(); assertEquals(entity.getChildren().size(), NUM_TASKS); Asserts.assertEqualsIgnoringOrder(entity.getChildren(), mgmt.getEntityManager().findEntities(Predicates.instanceOf(BasicEntity.class))); }
public static void unmanage(Entity entity) { if (((EntityInternal)entity).getManagementSupport().isDeployed()) { ((EntityInternal)entity).getManagementContext().getEntityManager().unmanage(entity); } }
@Test public void testLoadEntityFromClasspath() throws Exception { String entityName = ExampleEntity.class.getName(); Object appSpec = loadApplicationFromClasspathOrParse(entityName); assertTrue(appSpec instanceof EntitySpec, "app="+appSpec); mgmt = LocalManagementContextForTests.newInstance(); app = (Application) mgmt.getEntityManager().createEntity((EntitySpec<?>)appSpec); Collection<Entity> entities = app.getChildren(); assertEquals(entities.size(), 1, "entities="+entities); assertTrue(Iterables.getOnlyElement(entities) instanceof ExampleEntity, "entities="+entities+"; ifs="+Iterables.getOnlyElement(entities).getClass().getInterfaces()); assertTrue(Iterables.getOnlyElement(entities) instanceof EntityProxy, "entities="+entities); }