@Test public void testKeepsReachableAdjuncts() throws Exception { MyPolicy policy = origApp.policies().add(PolicySpec.create(MyPolicy.class)); MyEnricher enricher = origApp.enrichers().add(EnricherSpec.create(MyEnricher.class)); Feed feed = origApp.feeds().add(new MyFeed()); // Double-check we have the state we expected for the assertions that it is unmodified! BrooklynMementoRawData origData = getRawData(); assertTrue(origData.getPolicies().containsKey(policy.getId())); assertTrue(origData.getEnrichers().containsKey(enricher.getId())); assertTrue(origData.getFeeds().containsKey(feed.getId())); assertTransformIsNoop(origData); } }
for (Feed feed : entity.feeds().getFeeds()) { builder.feeds.add(feed.getId());
Assert.assertTrue(RecordingSshjTool.connectionCount.get()>0); Collection<Feed> origFeeds = ((EntityInternal)origNginx).feeds().getFeeds(); LOG.info("feeds before rebind are: "+origFeeds); Assert.assertTrue(origFeeds.size() >= 1); Collection<Feed> newFeeds = ((EntityInternal)newNginx).feeds().getFeeds(); LOG.info("feeds after rebind are: "+newFeeds); Assert.assertTrue(newFeeds.size() >= 1);
objectsToRebindFinal.addAll( ((EntityInternal)bo).feeds().getFeeds() );
instance = Entities.deproxy(instance); result.entity(instance.getId(), serializer.toString(newObjectMemento(instance))); for (Feed instanceAdjunct: ((EntityInternal)instance).feeds().getFeeds()) { result.feed(instanceAdjunct.getId(), serializer.toString(newObjectMemento(instanceAdjunct)));
EntityAsserts.assertAttributeEqualsEventually(origEntity, SENSOR_INT, 200); EntityAsserts.assertAttributeEqualsEventually(origEntity, SENSOR_STRING, "{\"foo\":\"myfoo\"}"); assertEquals(origEntity.feeds().getFeeds().size(), 1); origManagementContext.getRebindManager().forcePersistNow(); Collection<Feed> newFeeds = newEntity.feeds().getFeeds(); assertEquals(newFeeds.size(), 1);
EntityAsserts.assertAttributeEqualsEventually(origEntity, SENSOR_INT, 200); EntityAsserts.assertAttributeEqualsEventually(origEntity, SENSOR_STRING, "{\"foo\":\"myfoo\"}"); assertEquals(origEntity.feeds().getFeeds().size(), 1); TestEntity newEntity = (TestEntity) Iterables.getOnlyElement(newApp.getChildren()); Collection<Feed> newFeeds = newEntity.feeds().getFeeds(); assertEquals(newFeeds.size(), 1);
MyFeed newFeed = (MyFeed) Iterables.find(newApp.feeds().getFeeds(), Predicates.instanceOf(MyFeed.class)); assertEquals(newFeed.config().get(MyFeed.KEY_1), "myval1"); assertEquals(newFeed.config().get(MyFeed.KEY_2), "myval2");
@Override public void init() { super.init(); // By calling feeds().add(...), it will persist the feed, and rebind it functionFeed = feeds().add(FunctionFeed.builder() .entity(this) .period(Duration.millis(10)) .uniqueTag("MyserviceWithFeeds-functionFeed") .poll(new FunctionPollConfig<Integer, Integer>(COUNTER) .suppressDuplicates(true) .onException(Functions.constant(-1)) .callable(new Callable<Integer>() { @Override public Integer call() { if (!Entities.isManaged(MyServiceWithFeedsImpl.this)) { feedCalledWhenNotManaged = true; throw new IllegalStateException("Entity "+MyServiceWithFeedsImpl.this+" is not managed in feed.call"); } Integer oldVal = sensors().get(COUNTER); return (oldVal == null ? 0 : oldVal) + 1; } })) .build()); subscribeToServiceState(); }
@Override public void addFeeds(RebindContext rebindContext, EntityMemento memento) { for (String feedId : memento.getFeeds()) { AbstractFeed feed = (AbstractFeed) rebindContext.lookup().lookupFeed(feedId); if (feed != null) { try { entity.feeds().add(feed); } catch (Exception e) { rebindContext.getExceptionHandler().onAddFeedFailed(entity, feed, e); } try { if (!rebindContext.isReadOnly(feed)) { feed.start(); } } catch (Exception e) { rebindContext.getExceptionHandler().onRebindFailed(BrooklynObjectType.ENTITY, entity, e); } } else { LOG.warn("Feed not found; discarding feed {} of entity {}({})", new Object[] {feedId, memento.getType(), memento.getId()}); } } }
private void addReferencedObjects(DeltaCollector deltaCollector) { MutableSet<BrooklynObject> referencedObjects = MutableSet.of(); // collect references for (Entity entity : deltaCollector.entities) { // FIXME How to let the policy/location tell us about changes? Don't do this every time! for (Location location : entity.getLocations()) { Collection<Location> findLocationsInHierarchy = TreeUtils.findLocationsInHierarchy(location); referencedObjects.addAll(findLocationsInHierarchy); } if (persistPoliciesEnabled) { referencedObjects.addAll(entity.policies()); } if (persistEnrichersEnabled) { referencedObjects.addAll(entity.enrichers()); } if (persistFeedsEnabled) { referencedObjects.addAll(((EntityInternal)entity).feeds().getFeeds()); } } for (BrooklynObject instance : referencedObjects) { deltaCollector.addIfNotRemoved(instance); } }
@Test(groups="Integration") public void testHotStandbyDoesNotStartFeedsRebindingManyTimesWithAnotherFeedGenerator() throws Exception { HaMgmtNode n1 = createMaster(Duration.PRACTICALLY_FOREVER); TestApplication app = createFirstAppAndPersist(n1); TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class).impl(MyEntityWithNewFeedsEachTimeImpl.class)); forcePersistNow(n1); Assert.assertTrue(entity.feeds().getFeeds().size() == 4, "Feeds: "+entity.feeds().getFeeds()); final HaMgmtNode hsb = createHotStandby(Duration.millis(10)); Repeater.create("until 10 rebinds").every(Duration.millis(100)).until( new Callable<Boolean>() { @Override public Boolean call() throws Exception { return ((RebindManagerImpl)hsb.mgmt.getRebindManager()).getReadOnlyRebindCount() >= 10; } }).runRequiringTrue(); // make sure not too many tasks (allowing 5 for rebind etc; currently just 2) RebindTestFixture.waitForTaskCountToBecome(hsb.mgmt, 5); }
@Test public void testFeedDeDupe() throws Exception { testPollsFunctionRepeatedlyToSetAttribute(); entity.addFeed(feed); log.info("Feed 0 is: "+feed); Feed feed0 = feed; testPollsFunctionRepeatedlyToSetAttribute(); entity.addFeed(feed); log.info("Feed 1 is: "+feed); Feed feed1 = feed; Assert.assertFalse(feed1==feed0); FeedSupport feeds = ((EntityInternal)entity).feeds(); Assert.assertEquals(feeds.getFeeds().size(), 1, "Wrong feed count: "+feeds.getFeeds()); // a couple extra checks, compared to the de-dupe test in other *FeedTest classes Feed feedAdded = Iterables.getOnlyElement(feeds.getFeeds()); Assert.assertTrue(feedAdded==feed1); Assert.assertFalse(feedAdded==feed0); }
@Test public void testHotStandbyDoesNotStartFeeds() throws Exception { HaMgmtNode n1 = createMaster(Duration.PRACTICALLY_FOREVER); TestApplication app = createFirstAppAndPersist(n1); TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class).impl(MyEntityWithFunctionFeedImpl.class)); forcePersistNow(n1); Assert.assertTrue(entity.feeds().getFeeds().size() > 0, "Feeds: "+entity.feeds().getFeeds()); for (Feed feed : entity.feeds().getFeeds()) { assertTrue(feed.isRunning(), "Feed expected running, but it is non-running"); } HaMgmtNode n2 = createHotStandby(Duration.PRACTICALLY_FOREVER); TestEntity entityRO = (TestEntity) n2.mgmt.lookup(entity.getId(), Entity.class); Assert.assertTrue(entityRO.feeds().getFeeds().size() > 0, "Feeds: "+entity.feeds().getFeeds()); for (Feed feedRO : entityRO.feeds().getFeeds()) { assertFalse(feedRO.isRunning(), "Feed expected non-active, but it is running"); } }
@Test(groups="Integration") public void testSshFeedRegisteredInStartIsPersisted() throws Exception { LocalhostMachineProvisioningLocation origLoc = origApp.newLocalhostProvisioningLocation(); SshMachineLocation origMachine = origLoc.obtain(); TestEntity origEntity = origApp.createAndManageChild(EntitySpec.create(TestEntity.class).impl(MyEntityWithSshFeedImpl.class) .location(origMachine)); origApp.start(ImmutableList.<Location>of()); EntityAsserts.assertAttributeEqualsEventually(origEntity, SENSOR_INT, 0); assertEquals(origEntity.feeds().getFeeds().size(), 1); newApp = rebind(); TestEntity newEntity = (TestEntity) Iterables.getOnlyElement(newApp.getChildren()); Collection<Feed> newFeeds = newEntity.feeds().getFeeds(); assertEquals(newFeeds.size(), 1); // Expect the feed to still be polling newEntity.sensors().set(SENSOR_INT, null); EntityAsserts.assertAttributeEqualsEventually(newEntity, SENSOR_INT, 0); }
protected void runJmxFeedIsPersisted(boolean preCreateJmxHelper) throws Exception { TestEntity origEntity = origApp.createAndManageChild(EntitySpec.create(TestEntity.class).impl(MyEntityWithJmxFeedImpl.class) .configure(MyEntityWithJmxFeedImpl.PRE_CREATE_JMX_HELPER, preCreateJmxHelper)); origApp.start(ImmutableList.<Location>of()); jmxService = new JmxService(origEntity); GeneralisedDynamicMBean mbean = jmxService.registerMBean(MutableMap.of(JMX_ATTRIBUTE_NAME, "myval"), OBJECT_NAME); EntityAsserts.assertAttributeEqualsEventually(origEntity, SENSOR_STRING, "myval"); assertEquals(origEntity.feeds().getFeeds().size(), 1); newApp = rebind(); TestEntity newEntity = (TestEntity) Iterables.getOnlyElement(newApp.getChildren()); Collection<Feed> newFeeds = newEntity.feeds().getFeeds(); assertEquals(newFeeds.size(), 1); // Expect the feed to still be polling newEntity.sensors().set(SENSOR_STRING, null); EntityAsserts.assertAttributeEqualsEventually(newEntity, SENSOR_STRING, "myval"); }
@Test public void testConcurrentAddFeed() 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.feeds().addFeed(new MyFeed()); }}); futures.add(future); } Futures.allAsList(futures).get(); assertEquals(entity.feeds().getFeeds().size(), NUM_TASKS); } private static class MyFeed extends AbstractFeed {
@Override public void run() { entity.feeds().addFeed(new MyFeed()); }}); futures.add(future);