if (lh.isPresent() && entity().sensors().get(Attributes.SUBNET_HOSTNAME) == null) { entity().sensors().set(Attributes.SUBNET_HOSTNAME, lh.get()); if (la.isPresent() && entity().sensors().get(Attributes.SUBNET_ADDRESS) == null) { entity().sensors().set(Attributes.SUBNET_ADDRESS, la.get()); if (entity().sensors().get(Attributes.HOSTNAME) == null) { entity().sensors().set(Attributes.HOSTNAME, machine.getAddress().getHostName()); if (entity().sensors().get(Attributes.ADDRESS) == null) { entity().sensors().set(Attributes.ADDRESS, machine.getAddress().getHostAddress()); entity().sensors().set(Attributes.SSH_ADDRESS, sshAddress);
ProvisioningTaskState provisioningState = entity().sensors().get(AttributesInternal.INTERNAL_PROVISIONING_TASK_STATE); log.warn("When stopping {}, timed out after {} waiting for the machine to finish provisioning - machine may we left running", entity(), maxWait); machine = Maybe.ofDisallowingNull(entity().sensors().get(INTERNAL_PROVISIONED_MACHINE)); entity().sensors().remove(AttributesInternal.INTERNAL_PROVISIONING_TASK_STATE); entity().sensors().remove(INTERNAL_PROVISIONED_MACHINE); Exceptions.propagate(e); entity().sensors().set(SoftwareProcess.SERVICE_UP, false); ServiceStateLogic.setExpectedState(entity(), Lifecycle.STOPPED);
final int counter = i+1; EntityInternal entity = (EntityInternal) Iterables.get(initialMembers, i); entity.sensors().emit(HASensors.ENTITY_FAILED, new FailureDescriptor(entity, "simulate failure")); if (i <= 3) { Asserts.succeedsEventually(new Runnable() {
final TestEntity e1 = (TestEntity) Iterables.get(initialMembers, 0); e1.sensors().emit(HASensors.ENTITY_FAILED, new FailureDescriptor(e1, "simulate failure"));
Iterable<FailingEntity> children1 = Iterables.filter(cluster.getChildren(), FailingEntity.class); assertEquals(Iterables.size(children1), 1); assertEquals(Iterables.getOnlyElement(children1).sensors().get(TestEntity.SERVICE_UP), Boolean.TRUE);
assertEquals(testEntity.sensors().get(MyTestEntity.MY_SENSOR), "myVal");
ProvisioningTaskState provisioningState = entity.sensors().get(AttributesInternal.INTERNAL_PROVISIONING_TASK_STATE); if (machine.isAbsent() && provisioningState == ProvisioningTaskState.RUNNING) { LOG.warn("Entity {} was provisioning; VM may have been left running", entity); ProvisioningTaskState terminationState = entity.sensors().get(AttributesInternal.INTERNAL_TERMINATION_TASK_STATE); if (machine.isAbsent() && terminationState == ProvisioningTaskState.RUNNING) { LOG.warn("Entity {} was terminating; VM may have been left running", entity);
final TestEntity e1 = (TestEntity) Iterables.get(initialMembers, 0); e1.sensors().emit(HASensors.ENTITY_FAILED, new FailureDescriptor(e1, "simulate failure"));
newApp.getManagementContext().getSubscriptionManager().subscribe(e1, HASensors.ENTITY_RECOVERED, eventListener); e1.sensors().emit(HASensors.ENTITY_FAILED, new FailureDescriptor(e1, "simulate failure"));
@Override protected <T> void emit(Sensor<T> sensor, Object val) { checkState(entity != null, "entity must first be set"); if (val == Entities.UNCHANGED) { return; } if (val == Entities.REMOVE) { ((EntityInternal)entity).sensors().remove((AttributeSensor<T>) sensor); return; } T newVal = TypeCoercions.coerce(val, sensor.getTypeToken()); Maybe<T> published = Maybe.of(newVal); if (sensor instanceof AttributeSensor) { AttributeSensor<T> attribute = (AttributeSensor<T>)sensor; if (Boolean.TRUE.equals(suppressDuplicates)) { DeduplicatingAttributeModifier<T> modifier = DeduplicatingAttributeModifier.create(newVal); entity.sensors().modify(attribute, modifier); published = modifier.getLastValue(); } else { entity.sensors().set(attribute, newVal); } } else { entity.sensors().emit(sensor, newVal); } if (published!=null && published.isPresent()) { highlightActionPublishSensor(sensor, published.get()); } }
private void testTransformerResolvesResolvableValues(int portStart, int portCount) { // Note: The test gets progressively slower with iterations, probably due to the GC triggering much more frequently. // There's no memory leak, but doesn't seem right to be putting so much pressure on the GC with such a simple test. AttributeSensor<Integer> sourceSensor = Sensors.newIntegerSensor("port"); AttributeSensor<String> targetSensor = Sensors.newStringSensor("port.transformed"); app.enrichers().add(EnricherSpec.create(Transformer.class) .configure(Transformer.SOURCE_SENSOR, sourceSensor) .configure(Transformer.TARGET_SENSOR, targetSensor) .configure(Transformer.TARGET_VALUE, // Can only use the inner-most sensor, but including the // wrapping formatStrings amplifies the resolving effort, making // a bug more probable to manifest. BrooklynDslCommon.formatString("%s", BrooklynDslCommon.formatString("%d", BrooklynDslCommon.attributeWhenReady("port"))))); int failures = 0; for (int port = portStart; port < portStart + portCount; port++) { app.sensors().set(sourceSensor, port); try { EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, Integer.toString(port)); } catch (Exception e) { failures++; LOG.warn("Assertion failed, port=" + port + ", transformed sensor is " + app.sensors().get(targetSensor), e); } } assertEquals(failures, 0, failures + " assertion failures while transforming sensor; see logs for detailed errors"); }
if (!(location instanceof LocalhostMachineProvisioningLocation)) log.info("Starting {}, obtaining a new location instance in {} with ports {}", new Object[]{entity(), location, flags.get("inboundPorts")}); entity().sensors().set(SoftwareProcess.PROVISIONING_LOCATION, location); Transition expectedState = entity().sensors().get(Attributes.SERVICE_STATE_EXPECTED); throw new IllegalStateException("Provisioning aborted before even begun for "+entity()+" in "+location+" (presumably by a concurrent call to stop"); entity().sensors().set(AttributesInternal.INTERNAL_PROVISIONING_TASK_STATE, ProvisioningTaskState.RUNNING); entity().sensors().set(INTERNAL_PROVISIONED_MACHINE, machine); } finally { entity().sensors().remove(AttributesInternal.INTERNAL_PROVISIONING_TASK_STATE);
assertEquals(newEntity.sensors().get(CreateUserPolicy.VM_USER_CREDENTIALS), creds);
@Test public void testChildUsesDefaultsFromParent() throws Exception { addCatalogItems( "brooklyn.catalog:", " itemType: template", " items:", " - id: template-with-top-level-params", " item:", " brooklyn.parameters:", " - name: test.parameter", " description: myDescription", " type: String", " default: myDefaultParamVal", " services:", " - type: "+TestEntity.class.getName(), " brooklyn.config:", " " + TestEntity.ATTRIBUTE_AND_CONF_STRING.getName() + ": $brooklyn:config(\"test.parameter\")" ); String yaml = Joiner.on("\n").join( "services:", "- type: template-with-top-level-params"); Entity app = createStartWaitAndLogApplication(yaml); TestEntity entity = (TestEntity) Iterables.getOnlyElement(app.getChildren()); assertEquals(entity.sensors().get(TestEntity.ATTRIBUTE_AND_CONF_STRING), "myDefaultParamVal"); }
@Test(groups={"Integration", "Broken"}) // has a 1 second wait public void testDoesNotOnFireWhenFailToReplaceMember() throws Exception { app.subscriptions().subscribe(null, ServiceReplacer.ENTITY_REPLACEMENT_FAILED, eventListener); final DynamicCluster cluster = app.createAndManageChild(EntitySpec.create(DynamicCluster.class) .configure(DynamicCluster.MEMBER_SPEC, EntitySpec.create(FailingEntity.class) .configure(FailingEntity.FAIL_ON_START_CONDITION, predicateOnlyTrueForCallAtOrAfter(2))) .configure(DynamicCluster.INITIAL_SIZE, 1) .configure(DynamicCluster.QUARANTINE_FAILED_ENTITIES, true)); app.start(ImmutableList.<Location>of(loc)); cluster.policies().add(PolicySpec.create(ServiceReplacer.class) .configure(ServiceReplacer.FAILURE_SENSOR_TO_MONITOR, HASensors.ENTITY_FAILED) .configure(ServiceReplacer.SET_ON_FIRE_ON_FAILURE, false)); final Set<Entity> initialMembers = ImmutableSet.copyOf(cluster.getMembers()); final TestEntity e1 = (TestEntity) Iterables.get(initialMembers, 0); e1.sensors().emit(HASensors.ENTITY_FAILED, new FailureDescriptor(e1, "simulate failure")); // Configured to not mark cluster as on fire Asserts.succeedsContinually(new Runnable() { @Override public void run() { assertNotEquals(cluster.getAttribute(Attributes.SERVICE_STATE_ACTUAL), Lifecycle.ON_FIRE, "Unexpected ON_FIRE state"); }}); // And will have received notification event about it assertEventuallyHasEntityReplacementFailedEvent(cluster); }
@Test public void testReplacesFailedMember() throws Exception { final DynamicCluster cluster = app.createAndManageChild(EntitySpec.create(DynamicCluster.class) .configure(DynamicCluster.MEMBER_SPEC, EntitySpec.create(TestEntity.class)) .configure(DynamicCluster.INITIAL_SIZE, 3)); app.start(ImmutableList.<Location>of(loc)); cluster.policies().add(PolicySpec.create(ServiceReplacer.class) .configure(ServiceReplacer.FAILURE_SENSOR_TO_MONITOR, HASensors.ENTITY_FAILED)); final Set<Entity> initialMembers = ImmutableSet.copyOf(cluster.getMembers()); final TestEntity e1 = (TestEntity) Iterables.get(initialMembers, 1); e1.sensors().emit(HASensors.ENTITY_FAILED, new FailureDescriptor(e1, "simulate failure")); // Expect e1 to be replaced Asserts.succeedsEventually(new Runnable() { @Override public void run() { Set<Entity> newMembers = Sets.difference(ImmutableSet.copyOf(cluster.getMembers()), initialMembers); Set<Entity> removedMembers = Sets.difference(initialMembers, ImmutableSet.copyOf(cluster.getMembers())); assertEquals(removedMembers, ImmutableSet.of(e1)); assertEquals(newMembers.size(), 1); assertEquals(((TestEntity)Iterables.getOnlyElement(newMembers)).getCallHistory(), ImmutableList.of("start")); assertEquals(e1.getCallHistory(), ImmutableList.of("start", "stop")); assertFalse(Entities.isManaged(e1)); }}); }
@Test public void testConcurrentSetAttribute() throws Exception { final int NUM_TASKS = Math.min(500 * Runtime.getRuntime().availableProcessors(), 1000); List<ListenableFuture<?>> futures = Lists.newArrayList(); for (int i = 0; i < NUM_TASKS; i++) { final AttributeSensor<Integer> nextSensor = Sensors.newIntegerSensor("EntityConcurrencyTest.exampleSensor"+i); final int val = i; ListenableFuture<?> future = executor.submit(new Runnable() { @Override public void run() { entity.sensors().set(nextSensor, val); }}); futures.add(future); } Futures.allAsList(futures).get(); for (int i = 0; i < NUM_TASKS; i++) { AttributeSensor<Integer> nextSensor = Sensors.newIntegerSensor("EntityConcurrencyTest.exampleSensor"+i); assertEquals(entity.sensors().get(nextSensor), (Integer)i, "i="+i); } }