@Override public Collection<Entity> getEntities() { if (isInitialManagementContextReal()) { return initialManagementContext.getEntityManager().getEntities(); } else { return Collections.emptyList(); } }
@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)); }
@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 public void run() { Set<FailingEntity> all = ImmutableSet.copyOf(Iterables.filter(mgmt.getEntityManager().getEntities(), FailingEntity.class)); Set<FailingEntity> replacements = Sets.difference(all, initialMembers); Set<?> replacementMembers = Sets.intersection(ImmutableSet.of(cluster.getMembers()), replacements); assertTrue(replacementMembers.isEmpty()); assertEquals(replacements.size(), counter); }}); } else {
@Override public Entity call() { EntityInternal entity = entity(); Collection<Entity> entitiesToSearch = entity.getManagementContext().getEntityManager().getEntities(); Optional<Entity> result = Iterables.tryFind(entitiesToSearch, EntityPredicates.configEqualTo(TAG, tag)); if (result.isPresent()) { return result.get(); } else { throw new NoSuchElementException("No entity matching id " + tag+" in "+entitiesToSearch); } }}) .build();
@SuppressWarnings({ "rawtypes", "unchecked" }) private List<NginxController> findNginxs() { ImmutableList result = ImmutableList.copyOf(Iterables.filter(app.getManagementContext().getEntityManager().getEntities(), Predicates.instanceOf(NginxController.class))); return result; }
private Entity find(String desiredComponentId) { return Iterables.tryFind(mgmt().getEntityManager().getEntities(), EntityPredicates.configEqualTo(BrooklynCampConstants.PLAN_ID, desiredComponentId)).orNull(); }
protected String verboseDumpToString() { Iterable<MockContainerEntity> containers = Iterables.filter(app.getManagementContext().getEntityManager().getEntities(), MockContainerEntity.class); Iterable<MockItemEntity> items = Iterables.filter(app.getManagementContext().getEntityManager().getEntities(), MockItemEntity.class);
@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 testFailureRebindingEntityWhenFailFast() throws Exception { RebindFailureMode danglingRefFailureMode = RebindManager.RebindFailureMode.CONTINUE; RebindFailureMode rebindFailureMode = RebindManager.RebindFailureMode.FAIL_FAST; MyEntity origFailingE = origApp.createAndManageChild(EntitySpec.create(MyEntity.class) .impl(MyEntityFailingImpl.class) .configure(MyEntityFailingImpl.FAIL_ON_REBIND, true)); ManagementContext newManagementContext = LocalManagementContextForTests.newInstance(); EntityManager newEntityManager = newManagementContext.getEntityManager(); RecordingRebindExceptionHandler exceptionHandler = new RecordingRebindExceptionHandler(danglingRefFailureMode, rebindFailureMode); try { newApp = rebind(RebindOptions.create().newManagementContext(newManagementContext).exceptionHandler(exceptionHandler)); fail(); } catch (Exception e) { assertFailureRebindingError(e); Assert.assertTrue(e.toString().toLowerCase().contains("rebinding entity"), "Wrong error: "+e); } // exception handler should have been told about failure assertEquals(toIds(exceptionHandler.rebindFailures.keySet()), ImmutableSet.of(origFailingE.getId())); // entities will not have been managed assertEquals(toIds(newEntityManager.getEntities()), ImmutableSet.of()); }
@Test public void testFailureRebindingEntityWhenFailAtEnd() throws Exception { RebindFailureMode danglingRefFailureMode = RebindManager.RebindFailureMode.CONTINUE; RebindFailureMode rebindFailureMode = RebindManager.RebindFailureMode.FAIL_AT_END; MyEntity origFailingE = origApp.createAndManageChild(EntitySpec.create(MyEntity.class) .impl(MyEntityFailingImpl.class) .configure(MyEntityFailingImpl.FAIL_ON_REBIND, true)); ManagementContext newManagementContext = LocalManagementContextForTests.newInstance(); EntityManager newEntityManager = newManagementContext.getEntityManager(); RecordingRebindExceptionHandler exceptionHandler = new RecordingRebindExceptionHandler(danglingRefFailureMode, rebindFailureMode); try { newApp = rebind(RebindOptions.create().newManagementContext(newManagementContext).exceptionHandler(exceptionHandler)); fail(); } catch (Exception e) { assertFailureRebindingError(e); Assert.assertTrue(e.toString().toLowerCase().contains("rebinding entity"), "Wrong error: "+e); } // exception handler should have been told about failure assertEquals(toIds(exceptionHandler.rebindFailures.keySet()), ImmutableSet.of(origFailingE.getId())); // Expect that on failure will have continued with rebind, and then report all problems assertEquals(toIds(newEntityManager.getEntities()), ImmutableSet.of(origApp.getId(), origFailingE.getId())); }
@Test public void testHotStandbySeesChangesToNameConfigAndSensorValue() throws Exception { HaMgmtNode n1 = createMaster(Duration.PRACTICALLY_FOREVER); TestApplication app = createFirstAppAndPersist(n1); HaMgmtNode n2 = createHotStandby(Duration.PRACTICALLY_FOREVER); assertEquals(n2.mgmt.getApplications().size(), 1); Application appRO = n2.mgmt.lookup(app.getId(), Application.class); Assert.assertNotNull(appRO); assertEquals(appRO.getChildren().size(), 0); // test changes app.setDisplayName("First App Renamed"); app.config().set(TestEntity.CONF_NAME, "first-app-renamed"); app.sensors().set(TestEntity.SEQUENCE, 4); appRO = expectRebindSequenceNumber(n1, n2, app, 4, true); assertEquals(n2.mgmt.getEntityManager().getEntities().size(), 1); assertEquals(appRO.getDisplayName(), "First App Renamed"); assertEquals(appRO.getConfig(TestEntity.CONF_NAME), "first-app-renamed"); // and change again for good measure! app.setDisplayName("First App"); app.config().set(TestEntity.CONF_NAME, "first-app-restored"); app.sensors().set(TestEntity.SEQUENCE, 5); appRO = expectRebindSequenceNumber(n1, n2, app, 5, true); assertEquals(n2.mgmt.getEntityManager().getEntities().size(), 1); assertEquals(appRO.getDisplayName(), "First App"); assertEquals(appRO.getConfig(TestEntity.CONF_NAME), "first-app-restored"); }
@Test public void testFailureRebindingEntityWhenContinue() throws Exception { RebindFailureMode danglingRefFailureMode = RebindManager.RebindFailureMode.CONTINUE; RebindFailureMode rebindFailureMode = RebindManager.RebindFailureMode.CONTINUE; MyEntity origFailingE = origApp.createAndManageChild(EntitySpec.create(MyEntity.class) .impl(MyEntityFailingImpl.class) .configure(MyEntityFailingImpl.FAIL_ON_REBIND, true)); ManagementContext newManagementContext = LocalManagementContextForTests.newInstance(); EntityManager newEntityManager = newManagementContext.getEntityManager(); RecordingRebindExceptionHandler exceptionHandler = new RecordingRebindExceptionHandler(danglingRefFailureMode, rebindFailureMode); newApp = rebind(RebindOptions.create().newManagementContext(newManagementContext).exceptionHandler(exceptionHandler)); // exception handler should have been told about failure assertEquals(toIds(exceptionHandler.rebindFailures.keySet()), ImmutableSet.of(origFailingE.getId())); // TODO How should brooklyn indicate that this entity's rebind failed? What can we assert? assertEquals(toIds(newEntityManager.getEntities()), ImmutableSet.of(origApp.getId(), origFailingE.getId())); }
@Override public void setEntity(EntityLocal entity) { assert entity instanceof Group; super.setEntity(entity); subscriptions().subscribe(null, Startable.SERVICE_UP, handler); for (Entity e : ((EntityInternal) entity).getManagementContext().getEntityManager().getEntities()) { if (Objects.equal(e.getApplicationId(), entity.getApplicationId())) updateMembership(e); } }
assertEquals(n1.ha.getNodeState(), ManagementNodeState.FAILED); Assert.assertTrue(n1.mgmt.getApplications().isEmpty(), "n1 should have had no apps; instead had: "+n1.mgmt.getApplications()); Assert.assertTrue(n1.mgmt.getEntityManager().getEntities().isEmpty(), "n1 should have had no entities; instead had: "+n1.mgmt.getEntityManager().getEntities()); Assert.assertTrue(n1.mgmt.getLocationManager().getLocations().isEmpty(), "n1 should have had no locations; instead had: "+n1.mgmt.getLocationManager().getLocations());
assertEquals(app2RO.getConfig(TestEntity.CONF_NAME), "second-app"); assertEquals(n2.mgmt.getEntityManager().getEntities().size(), 3); assertEquals(n2.mgmt.getEntityManager().getEntities().size(), 1); assertEquals(appRO.getChildren().size(), 0); assertEquals(n2.mgmt.getApplications().size(), 1);
Iterable<Entity> entities = Iterables.filter(getManagementContext().getEntityManager().getEntities(), StubUtils.sameInfrastructure(this)); Set<Application> applications = ImmutableSet.copyOf(Iterables.transform(entities, new Function<Entity, Application>() { @Override
/** clears all managed items from the management context; same items destroyed as in the course of a rebind cycle */ protected void clearManagedItems(ManagementTransitionMode mode) { // start with the root applications for (Application app: managementContext.getApplications()) { if (((EntityInternal)app).getManagementSupport().isDeployed()) { ((LocalEntityManager)((EntityInternal)app).getManagementContext().getEntityManager()).unmanage(app, mode); } } // for active management, call above will remove recursively at present, // but for read-only, and if we stop recursively, go through them all for (Entity entity: managementContext.getEntityManager().getEntities()) { ((LocalEntityManager)managementContext.getEntityManager()).unmanage(entity, mode); } // again, for locations, call unmanage on parents first for (Location loc: managementContext.getLocationManager().getLocations()) { if (loc.getParent()==null) ((LocationManagerInternal)managementContext.getLocationManager()).unmanage(loc, mode); } for (Location loc: managementContext.getLocationManager().getLocations()) { ((LocationManagerInternal)managementContext.getLocationManager()).unmanage(loc, mode); } ((BasicBrooklynCatalog)managementContext.getCatalog()).reset(CatalogDto.newEmptyInstance("<reset-by-ha-status-change>")); }
for (Location instance: mgmt.getLocationManager().getLocations()) result.location(instance.getId(), serializer.toString(newObjectMemento(instance))); for (Entity instance: mgmt.getEntityManager().getEntities()) { instance = Entities.deproxy(instance); result.entity(instance.getId(), serializer.toString(newObjectMemento(instance)));
/** * Walks the contents of a ManagementContext, to create a corresponding memento. */ protected static BrooklynMemento newBrooklynMemento(ManagementContext managementContext) { BrooklynMementoImpl.Builder builder = BrooklynMementoImpl.builder(); for (Application app : managementContext.getApplications()) { builder.applicationId(app.getId()); } for (Entity entity : managementContext.getEntityManager().getEntities()) { builder.entity(((EntityInternal)entity).getRebindSupport().getMemento()); } for (Location location : managementContext.getLocationManager().getLocations()) { builder.location(((LocationInternal)location).getRebindSupport().getMemento()); if (location.getParent() == null) { builder.topLevelLocationId(location.getId()); } } BrooklynMemento result = builder.build(); MementoValidators.validateMemento(result); return result; } }