@Override public void connectSensors() { super.connectSensors(); connectServiceUpIsRunning(); HostAndPort hostAndPort = BrooklynAccessUtils.getBrooklynAccessibleAddress(this, sensors().get(DOCKER_REGISTRY_PORT)); sensors().set(Attributes.MAIN_URI, URI.create("https://" + hostAndPort + "/v2")); httpFeed = HttpFeed.builder() .entity(this) .period(Duration.seconds(3)) .baseUri(getAttribute(Attributes.MAIN_URI)) .poll( new HttpPollConfig<Boolean>(Attributes.SERVICE_UP) .onSuccess(Functions.constant(true)) .onFailureOrException(Functions.constant(false))) .poll( new HttpPollConfig<List<String>>(DOCKER_REGISTRY_CATALOG) .suburl("/_catalog") .onSuccess( Functionals.chain( HttpValueFunctions.jsonContents(), JsonFunctions.walk("repositories"), JsonFunctions.forEach(JsonFunctions.cast(String.class)))) .onFailureOrException(Functions.constant(Collections.<String>emptyList()))) .build(); }
public void testScanReverseKeysOnly() { persistence.mutate("A1", Functions.constant("A1".getBytes())); persistence.mutate("A2", Functions.constant("A2".getBytes())); persistence.mutate("A3", Functions.constant("A3".getBytes())); assertEquals(Lists.newArrayList("A3", "A2", "A1"), persistence.keyScanReverse("A", "B", 10)); assertEquals(Lists.newArrayList("A3", "A2"), persistence.keyScanReverse("A", "B", 2)); assertEquals(Lists.newArrayList("A2", "A1"), persistence.keyScanReverse("A", "A3", 5)); }
@Test public void testSubstituter() throws Exception { entity.setConfig(BrooklynConfigKeys.SUGGESTED_VERSION, "myversion"); String baseurl = "version=${version},type=${type},simpletype=${simpletype}"; Map<String, Object> subs = DownloadSubstituters.getBasicEntitySubstitutions(driver); DownloadTargets result = DownloadSubstituters.substituter(Functions.constant(baseurl), Functions.constant(subs)) .apply(new BasicDownloadRequirement(driver)); String expected = String.format( "version=%s,type=%s,simpletype=%s", "myversion", TestEntity.class.getName(), TestEntity.class.getSimpleName()); assertEquals(result.getPrimaryLocations(), ImmutableList.of(expected)); }
public void testDefensiveCopy() { byte[] input = "B".getBytes(); persistence.mutate("A", Functions.constant(input)); input[0]++; if (supportsDefensiveCopy) { assertTrue(Arrays.equals(persistence.get("A"), "B".getBytes())); } else { assertTrue(Arrays.equals(persistence.get("A"), input)); } }
private Map<String, Integer> nullMeasures() { Set<String> metrics = ImmutableSet.of( "tests", "test_failures", "test_errors", "skipped_tests", "test_success_density", "test_execution_time"); return Maps.asMap(metrics, Functions.<Integer>constant(null)); }
public void testFunctionInput() { persistence.mutate("A", Functions.constant("B".getBytes())); persistence.mutate( "A", new Function<byte[], byte[]>() { @Override public byte[] apply(byte[] fromPersistence) { assertTrue(Arrays.equals(fromPersistence, "B".getBytes())); return "C".getBytes(); } }); assertTrue(Arrays.equals(persistence.get("A"), "C".getBytes())); }
public void testScan() { persistence.mutate("A1", Functions.constant("A1".getBytes())); persistence.mutate("A2", Functions.constant("A2".getBytes())); persistence.mutate("A3", Functions.constant("A3".getBytes())); persistence.mutate("A2", Functions.constant((byte[]) null)); List<Entry<String, byte[]>> scanResult = persistence.scan("A", "B", 10); assertEquals(2, scanResult.size()); assertEquals("A1", scanResult.get(0).getKey()); assertEquals("A3", scanResult.get(1).getKey()); assertTrue(Arrays.equals("A1".getBytes(), scanResult.get(0).getValue())); assertTrue(Arrays.equals("A3".getBytes(), scanResult.get(1).getValue())); scanResult = persistence.scan("A1", "A3", 10); assertEquals(1, scanResult.size()); assertEquals("A1", scanResult.get(0).getKey()); scanResult = persistence.scan("A1", "A4", 1); assertEquals(1, scanResult.size()); assertEquals("A1", scanResult.get(0).getKey()); scanResult = persistence.scan("B", "Z", 10); assertEquals(0, scanResult.size()); scanResult = persistence.scan("A1", "A4", 0); assertEquals(0, scanResult.size()); }
@Override protected <R> ModelSchema<R> createSchema( ModelSchemaExtractionContext<R> extractionContext, ModelSchemaStore store, ModelType<R> type, List<ModelProperty<?>> properties, List<ModelSchemaAspect> aspects) { return new ModelManagedImplStructSchema<R>( type, properties, aspects, type.getConcreteClass(), delegateType, Functions.<NodeInitializer>constant(null)); }
public void testOnResultOf_chained() { Comparator<String> comparator = DECREASING_INTEGER.onResultOf(StringLengthFunction.StringLength); assertTrue(comparator.compare("to", "be") == 0); assertTrue(comparator.compare("not", "or") < 0); assertTrue(comparator.compare("to", "that") > 0); new EqualsTester() .addEqualityGroup( comparator, DECREASING_INTEGER.onResultOf(StringLengthFunction.StringLength)) .addEqualityGroup(DECREASING_INTEGER.onResultOf(Functions.constant(1))) .addEqualityGroup(Ordering.natural()) .testEquals(); reserializeAndAssert(comparator); assertEquals("Ordering.natural().reverse().onResultOf(StringLength)", comparator.toString()); }
@Override public void apply(final EntityLocal entity) { super.apply(entity); if (LOG.isDebugEnabled()) { LOG.debug("Adding SSH sensor {} to {}", name, entity); } Supplier<Map<String, String>> envSupplier = new Supplier<Map<String, String>>() { @Override public Map<String, String> get() { return MutableMap.copyOf( Strings.toStringMap(entity.getConfig(SoftwareProcess.SHELL_ENVIRONMENT), "")); } }; Supplier<String> commandSupplier = new Supplier<String>() { @Override public String get() { return makeCommandExecutingInDirectory(command, executionDir, entity); } }; SshPollConfig<T> pollConfig = new SshPollConfig<T>(sensor) .period(period) .env(envSupplier) .command(commandSupplier) .checkSuccess(SshValueFunctions.exitStatusEquals(0)) .onFailureOrException(Functions.constant((T) null)) .onSuccess( Functions.compose( new Function<String, T>() { @Override public T apply(String input) { return TypeCoercions.coerce(input, getType(type)); } }, SshValueFunctions.stdout())); SshFeed.builder().entity(entity).onlyIfServiceUp().poll(pollConfig).build(); }
@Before public void createFixture() throws Exception { URI profileURI = URI.createPlatformPluginURI( "org.eclipse.papyrus.uml.modelrepair.tests/resources/profile/myprofile.profile.uml", true); profile = UML2Util.load(modelSet.getResourceSet(), profileURI, UMLPackage.Literals.PROFILE); stereotype = profile.getOwnedStereotype("Stereo"); class1 = (Class) modelSet.getModel().getOwnedType("Class1"); fixture = houseKeeper.cleanUpLater( new StereotypeApplicationRepairSnippet(Functions.constant(profile)), "dispose", modelSet.getResourceSet()); fixture.start(modelSet.getResourceSet()); houseKeeper.setField( "zombies", fixture.getZombieStereotypes(modelSet.getModelResource(), modelSet.getModel())); }
public F setOnSuccess(T val) { return onSuccess(Functions.constant(val)); }
/** @author Nikolay Krasko */ public class ResolveSessionUtils { // This name is used as a key for the case when something has no name _due to a syntactic error_ // Example: fun (x: Int) = 5 // There's no name for this function in the PSI // The name contains a GUID to avoid clashes, if a clash happens, it's not a big deal: the code // does not compile anyway public static final Name NO_NAME_FOR_LAZY_RESOLVE = Name.identifier("no_name_in_PSI_for_lazy_resolve_3d19d79d_1ba9_4cd0_b7f5_b46aa3cd5d40"); private ResolveSessionUtils() {} @SuppressWarnings("unchecked") private static final BodyResolveContextForLazy EMPTY_CONTEXT = new BodyResolveContextForLazy((Function) Functions.constant(null)); private static class BodyResolveContextForLazy implements BodiesResolveContext { private final Function<JetDeclaration, JetScope> declaringScopes; private BodyResolveContextForLazy(@NotNull Function<JetDeclaration, JetScope> declaringScopes) { this.declaringScopes = declaringScopes; } @Override public Collection<JetFile> getFiles() { return Collections.emptySet(); } @Override public Map<JetClass, MutableClassDescriptor> getClasses() { return Collections.emptyMap(); } @Override public Map<JetObjectDeclaration, MutableClassDescriptor> getObjects() { return Collections.emptyMap(); } @Override public Map<JetProperty, PropertyDescriptor> getProperties() { return Collections.emptyMap(); } @Override public Map<JetNamedFunction, SimpleFunctionDescriptor> getFunctions() { return Collections.emptyMap(); } @Override public Function<JetDeclaration, JetScope> getDeclaringScopes() { return declaringScopes; } @Override public Map<JetScript, ScriptDescriptor> getScripts() { return Collections.emptyMap(); } @Override public Map<JetScript, WritableScope> getScriptScopes() { return Collections.emptyMap(); } @Override public void setTopDownAnalysisParameters(TopDownAnalysisParameters parameters) {} @Override public boolean completeAnalysisNeeded(@NotNull PsiElement element) { return true; } } public static @NotNull BindingContext resolveToExpression( @NotNull final ResolveSession resolveSession, @NotNull JetExpression expression) { final DelegatingBindingTrace trace = new DelegatingBindingTrace( resolveSession.getBindingContext(), "trace to resolve expression", expression); JetFile file = (JetFile) expression.getContainingFile(); @SuppressWarnings("unchecked") PsiElement topmostCandidateForAdditionalResolve = JetPsiUtil.getTopmostParentOfTypes( expression, JetNamedFunction.class, JetClassInitializer.class, JetProperty.class, JetDelegationSpecifierList.class); if (topmostCandidateForAdditionalResolve != null) { if (topmostCandidateForAdditionalResolve instanceof JetNamedFunction) { functionAdditionalResolve( resolveSession, (JetNamedFunction) topmostCandidateForAdditionalResolve, trace, file); } else if (topmostCandidateForAdditionalResolve instanceof JetClassInitializer) { initializerAdditionalResolve( resolveSession, (JetClassInitializer) topmostCandidateForAdditionalResolve, trace, file); } else if (topmostCandidateForAdditionalResolve instanceof JetProperty) { propertyAdditionalResolve( resolveSession, (JetProperty) topmostCandidateForAdditionalResolve, trace, file); } else if (topmostCandidateForAdditionalResolve instanceof JetDelegationSpecifierList) { delegationSpecifierAdditionalResolve( resolveSession, (JetDelegationSpecifierList) topmostCandidateForAdditionalResolve, trace, file); } else { assert false : "Invalid type of the topmost parent"; } return trace.getBindingContext(); } // Setup resolution scope explicitly if (trace.getBindingContext().get(BindingContext.RESOLUTION_SCOPE, expression) == null) { JetScope scope = getExpressionMemberScope(resolveSession, expression); if (scope != null) { trace.record(BindingContext.RESOLUTION_SCOPE, expression, scope); } } return trace.getBindingContext(); } private static void delegationSpecifierAdditionalResolve( final ResolveSession resolveSession, final JetDelegationSpecifierList specifier, DelegatingBindingTrace trace, JetFile file) { BodyResolver bodyResolver = createBodyResolverWithEmptyContext(trace, file, resolveSession.getModuleConfiguration()); JetClassOrObject classOrObject = (JetClassOrObject) specifier.getParent(); LazyClassDescriptor descriptor = (LazyClassDescriptor) resolveSession.resolveToDescriptor(classOrObject); // Activate resolving of supertypes descriptor.getTypeConstructor().getSupertypes(); bodyResolver.resolveDelegationSpecifierList( classOrObject, descriptor, descriptor.getUnsubstitutedPrimaryConstructor(), descriptor.getScopeForClassHeaderResolution(), descriptor.getScopeForMemberDeclarationResolution()); } private static void propertyAdditionalResolve( final ResolveSession resolveSession, final JetProperty jetProperty, DelegatingBindingTrace trace, JetFile file) { final JetScope propertyResolutionScope = resolveSession .getInjector() .getScopeProvider() .getResolutionScopeForDeclaration(jetProperty); BodyResolveContextForLazy bodyResolveContext = new BodyResolveContextForLazy( new Function<JetDeclaration, JetScope>() { @Override public JetScope apply(JetDeclaration declaration) { assert declaration.getParent() == jetProperty : "Must be called only for property accessors, but called for " + declaration; return propertyResolutionScope; } }); BodyResolver bodyResolver = createBodyResolver( trace, file, bodyResolveContext, resolveSession.getModuleConfiguration()); PropertyDescriptor descriptor = (PropertyDescriptor) resolveSession.resolveToDescriptor(jetProperty); JetExpression propertyInitializer = jetProperty.getInitializer(); if (propertyInitializer != null) { bodyResolver.resolvePropertyInitializer( jetProperty, descriptor, propertyInitializer, propertyResolutionScope); } bodyResolver.resolvePropertyAccessors(jetProperty, descriptor); } private static void functionAdditionalResolve( ResolveSession resolveSession, JetNamedFunction namedFunction, DelegatingBindingTrace trace, JetFile file) { BodyResolver bodyResolver = createBodyResolverWithEmptyContext(trace, file, resolveSession.getModuleConfiguration()); JetScope scope = resolveSession .getInjector() .getScopeProvider() .getResolutionScopeForDeclaration(namedFunction); FunctionDescriptor functionDescriptor = (FunctionDescriptor) resolveSession.resolveToDescriptor(namedFunction); bodyResolver.resolveFunctionBody(trace, namedFunction, functionDescriptor, scope); } private static boolean initializerAdditionalResolve( ResolveSession resolveSession, JetClassInitializer classInitializer, DelegatingBindingTrace trace, JetFile file) { BodyResolver bodyResolver = createBodyResolverWithEmptyContext(trace, file, resolveSession.getModuleConfiguration()); JetClassOrObject classOrObject = PsiTreeUtil.getParentOfType(classInitializer, JetClassOrObject.class); LazyClassDescriptor classOrObjectDescriptor = (LazyClassDescriptor) resolveSession.resolveToDescriptor(classOrObject); bodyResolver.resolveAnonymousInitializers( classOrObject, classOrObjectDescriptor.getUnsubstitutedPrimaryConstructor(), classOrObjectDescriptor.getScopeForPropertyInitializerResolution()); return true; } private static BodyResolver createBodyResolver( DelegatingBindingTrace trace, JetFile file, BodyResolveContextForLazy bodyResolveContext, ModuleConfiguration moduleConfiguration) { TopDownAnalysisParameters parameters = new TopDownAnalysisParameters( Predicates.<PsiFile>alwaysTrue(), false, true, Collections.<AnalyzerScriptParameter>emptyList()); InjectorForBodyResolve bodyResolve = new InjectorForBodyResolve( file.getProject(), parameters, trace, bodyResolveContext, moduleConfiguration); return bodyResolve.getBodyResolver(); } private static BodyResolver createBodyResolverWithEmptyContext( DelegatingBindingTrace trace, JetFile file, ModuleConfiguration moduleConfiguration) { return createBodyResolver(trace, file, EMPTY_CONTEXT, moduleConfiguration); } private static JetScope getExpressionResolutionScope( @NotNull ResolveSession resolveSession, @NotNull JetExpression expression) { ScopeProvider provider = resolveSession.getInjector().getScopeProvider(); JetDeclaration parentDeclaration = PsiTreeUtil.getParentOfType(expression, JetDeclaration.class); if (parentDeclaration == null) { return provider.getFileScopeWithAllImported((JetFile) expression.getContainingFile()); } return provider.getResolutionScopeForDeclaration(parentDeclaration); } public static JetScope getExpressionMemberScope( @NotNull ResolveSession resolveSession, @NotNull JetExpression expression) { DelegatingBindingTrace trace = new DelegatingBindingTrace( resolveSession.getBindingContext(), "trace to resolve a member scope of expression", expression); if (expression instanceof JetReferenceExpression) { QualifiedExpressionResolver qualifiedExpressionResolver = resolveSession.getInjector().getQualifiedExpressionResolver(); // In some type declaration if (expression.getParent() instanceof JetUserType) { JetUserType qualifier = ((JetUserType) expression.getParent()).getQualifier(); if (qualifier != null) { Collection<? extends DeclarationDescriptor> descriptors = qualifiedExpressionResolver.lookupDescriptorsForUserType( qualifier, getExpressionResolutionScope(resolveSession, expression), trace); for (DeclarationDescriptor descriptor : descriptors) { if (descriptor instanceof LazyPackageDescriptor) { return ((LazyPackageDescriptor) descriptor).getMemberScope(); } } } } // Inside import if (PsiTreeUtil.getParentOfType(expression, JetImportDirective.class, false) != null) { NamespaceDescriptor rootPackage = resolveSession.getPackageDescriptorByFqName(FqName.ROOT); assert rootPackage != null; if (expression.getParent() instanceof JetDotQualifiedExpression) { JetExpression element = ((JetDotQualifiedExpression) expression.getParent()).getReceiverExpression(); String name = ((JetFile) expression.getContainingFile()).getPackageName(); NamespaceDescriptor filePackage = name != null ? resolveSession.getPackageDescriptorByFqName(new FqName(name)) : rootPackage; assert filePackage != null : "File package should be already resolved and be found"; JetScope scope = filePackage.getMemberScope(); Collection<? extends DeclarationDescriptor> descriptors; if (element instanceof JetDotQualifiedExpression) { descriptors = qualifiedExpressionResolver.lookupDescriptorsForQualifiedExpression( (JetDotQualifiedExpression) element, rootPackage.getMemberScope(), scope, trace, false, false); } else { descriptors = qualifiedExpressionResolver.lookupDescriptorsForSimpleNameReference( (JetSimpleNameExpression) element, rootPackage.getMemberScope(), scope, trace, false, false, false); } for (DeclarationDescriptor descriptor : descriptors) { if (descriptor instanceof NamespaceDescriptor) { return ((NamespaceDescriptor) descriptor).getMemberScope(); } } } else { return rootPackage.getMemberScope(); } } // Inside package declaration JetNamespaceHeader namespaceHeader = PsiTreeUtil.getParentOfType(expression, JetNamespaceHeader.class, false); if (namespaceHeader != null) { NamespaceDescriptor packageDescriptor = resolveSession.getPackageDescriptorByFqName( namespaceHeader.getParentFqName((JetReferenceExpression) expression)); if (packageDescriptor != null) { return packageDescriptor.getMemberScope(); } } } return null; } @NotNull public static Collection<ClassDescriptor> getClassDescriptorsByFqName( @NotNull ResolveSession resolveSession, @NotNull FqName fqName) { return getClassOrObjectDescriptorsByFqName(resolveSession, fqName, false); } @NotNull public static Collection<ClassDescriptor> getClassOrObjectDescriptorsByFqName( @NotNull ResolveSession resolveSession, @NotNull FqName fqName, boolean includeObjectDeclarations) { if (fqName.isRoot()) { return Collections.emptyList(); } Collection<ClassDescriptor> classDescriptors = Lists.newArrayList(); FqName packageFqName = fqName.parent(); while (true) { NamespaceDescriptor packageDescriptor = resolveSession.getPackageDescriptorByFqName(packageFqName); if (packageDescriptor != null) { FqName classInPackagePath = new FqName(QualifiedNamesUtil.tail(packageFqName, fqName)); Collection<ClassDescriptor> descriptors = getClassOrObjectDescriptorsByFqName( packageDescriptor, classInPackagePath, includeObjectDeclarations); classDescriptors.addAll(descriptors); } if (packageFqName.isRoot()) { break; } else { packageFqName = packageFqName.parent(); } } return classDescriptors; } private static Collection<ClassDescriptor> getClassOrObjectDescriptorsByFqName( NamespaceDescriptor packageDescriptor, FqName path, boolean includeObjectDeclarations) { if (path.isRoot()) { return Collections.emptyList(); } Collection<JetScope> scopes = Arrays.asList(packageDescriptor.getMemberScope()); List<Name> names = path.pathSegments(); if (names.size() > 1) { for (Name subName : path.pathSegments().subList(0, names.size() - 1)) { Collection<JetScope> tempScopes = Lists.newArrayList(); for (JetScope scope : scopes) { ClassifierDescriptor classifier = scope.getClassifier(subName); if (classifier instanceof ClassDescriptorBase) { ClassDescriptorBase classDescriptor = (ClassDescriptorBase) classifier; tempScopes.add(classDescriptor.getUnsubstitutedInnerClassesScope()); } } scopes = tempScopes; } } Name shortName = path.shortName(); Collection<ClassDescriptor> resultClassifierDescriptors = Lists.newArrayList(); for (JetScope scope : scopes) { ClassifierDescriptor classifier = scope.getClassifier(shortName); if (classifier instanceof ClassDescriptor) { resultClassifierDescriptors.add((ClassDescriptor) classifier); } if (includeObjectDeclarations) { ClassDescriptor objectDescriptor = scope.getObjectDescriptor(shortName); if (objectDescriptor != null) { resultClassifierDescriptors.add(objectDescriptor); } } } return resultClassifierDescriptors; } @NotNull public static Name safeNameForLazyResolve(@NotNull JetNamed named) { Name name = named.getNameAsName(); return safeNameForLazyResolve(name); } @NotNull public static Name safeNameForLazyResolve(@Nullable Name name) { return name != null ? name : NO_NAME_FOR_LAZY_RESOLVE; } }
private static Map<String, MediaType> mapToMediaType(Set<String> services, MediaType type) { return Maps.<String, String, MediaType>transformValues( Maps.uniqueIndex(services, Functions.<String>identity()), Functions.constant(type)); }
@SuppressWarnings({"unchecked", "rawtypes"}) @Override protected void connectSensors() { // "cassandra" isn't really a protocol, but okay for now setAttribute( DATASTORE_URL, "cassandra://" + getAttribute(HOSTNAME) + ":" + getAttribute(THRIFT_PORT)); super.connectSensors(); jmxHelper = new JmxHelper(this); boolean retrieveUsageMetrics = getConfig(RETRIEVE_USAGE_METRICS); jmxFeed = JmxFeed.builder() .entity(this) .period(3000, TimeUnit.MILLISECONDS) .helper(jmxHelper) .pollAttribute( new JmxAttributePollConfig<Boolean>(SERVICE_UP_JMX) .objectName(storageServiceMBean) .attributeName("Initialized") .onSuccess(Functions.forPredicate(Predicates.notNull())) .onException(Functions.constant(false)) .suppressDuplicates(true)) .pollAttribute( new JmxAttributePollConfig<Set<BigInteger>>(TOKENS) .objectName(storageServiceMBean) .attributeName("TokenToEndpointMap") .onSuccess( new Function<Object, Set<BigInteger>>() { @Override public Set<BigInteger> apply(@Nullable Object arg) { Map input = (Map) arg; if (input == null || input.isEmpty()) return null; // FIXME does not work on aws-ec2, uses RFC1918 address Predicate<String> self = Predicates.in( ImmutableList.of( getAttribute(HOSTNAME), getAttribute(ADDRESS), getAttribute(SUBNET_ADDRESS), getAttribute(SUBNET_HOSTNAME))); Set<String> tokens = Maps.filterValues(input, self).keySet(); Set<BigInteger> result = Sets.newLinkedHashSet(); for (String token : tokens) { result.add(new BigInteger(token)); } return result; } }) .onException(Functions.<Set<BigInteger>>constant(null)) .suppressDuplicates(true)) .pollAttribute( new JmxAttributePollConfig<BigInteger>(TOKEN) .objectName(storageServiceMBean) .attributeName("TokenToEndpointMap") .onSuccess( new Function<Object, BigInteger>() { @Override public BigInteger apply(@Nullable Object arg) { Map input = (Map) arg; // TODO remove duplication from setting TOKENS if (input == null || input.isEmpty()) return null; // FIXME does not work on aws-ec2, uses RFC1918 address Predicate<String> self = Predicates.in( ImmutableList.of( getAttribute(HOSTNAME), getAttribute(ADDRESS), getAttribute(SUBNET_ADDRESS), getAttribute(SUBNET_HOSTNAME))); Set<String> tokens = Maps.filterValues(input, self).keySet(); String token = Iterables.getFirst(tokens, null); return (token != null) ? new BigInteger(token) : null; } }) .onException(Functions.<BigInteger>constant(null)) .suppressDuplicates(true)) .pollOperation( new JmxOperationPollConfig<String>(DATACENTER_NAME) .period(60, TimeUnit.SECONDS) .objectName(snitchMBean) .operationName("getDatacenter") .operationParams(ImmutableList.of(getBroadcastAddress())) .onException(Functions.<String>constant(null)) .suppressDuplicates(true)) .pollOperation( new JmxOperationPollConfig<String>(RACK_NAME) .period(60, TimeUnit.SECONDS) .objectName(snitchMBean) .operationName("getRack") .operationParams(ImmutableList.of(getBroadcastAddress())) .onException(Functions.<String>constant(null)) .suppressDuplicates(true)) .pollAttribute( new JmxAttributePollConfig<Integer>(PEERS) .objectName(storageServiceMBean) .attributeName("TokenToEndpointMap") .onSuccess( new Function<Object, Integer>() { @Override public Integer apply(@Nullable Object arg) { Map input = (Map) arg; if (input == null || input.isEmpty()) return 0; return input.size(); } }) .onException(Functions.constant(-1))) .pollAttribute( new JmxAttributePollConfig<Integer>(LIVE_NODE_COUNT) .objectName(storageServiceMBean) .attributeName("LiveNodes") .onSuccess( new Function<Object, Integer>() { @Override public Integer apply(@Nullable Object arg) { List input = (List) arg; if (input == null || input.isEmpty()) return 0; return input.size(); } }) .onException(Functions.constant(-1))) .pollAttribute( new JmxAttributePollConfig<Integer>(READ_ACTIVE) .objectName(readStageMBean) .attributeName("ActiveCount") .onException(Functions.constant((Integer) null)) .enabled(retrieveUsageMetrics)) .pollAttribute( new JmxAttributePollConfig<Long>(READ_PENDING) .objectName(readStageMBean) .attributeName("PendingTasks") .onException(Functions.constant((Long) null)) .enabled(retrieveUsageMetrics)) .pollAttribute( new JmxAttributePollConfig<Long>(READ_COMPLETED) .objectName(readStageMBean) .attributeName("CompletedTasks") .onException(Functions.constant((Long) null)) .enabled(retrieveUsageMetrics)) .pollAttribute( new JmxAttributePollConfig<Integer>(WRITE_ACTIVE) .objectName(mutationStageMBean) .attributeName("ActiveCount") .onException(Functions.constant((Integer) null)) .enabled(retrieveUsageMetrics)) .pollAttribute( new JmxAttributePollConfig<Long>(WRITE_PENDING) .objectName(mutationStageMBean) .attributeName("PendingTasks") .onException(Functions.constant((Long) null)) .enabled(retrieveUsageMetrics)) .pollAttribute( new JmxAttributePollConfig<Long>(WRITE_COMPLETED) .objectName(mutationStageMBean) .attributeName("CompletedTasks") .onException(Functions.constant((Long) null)) .enabled(retrieveUsageMetrics)) .build(); functionFeed = FunctionFeed.builder() .entity(this) .period(3000, TimeUnit.MILLISECONDS) .poll( new FunctionPollConfig<Long, Long>(THRIFT_PORT_LATENCY) .onException(Functions.constant((Long) null)) .callable( new Callable<Long>() { public Long call() { try { long start = System.currentTimeMillis(); Socket s = new Socket(getAttribute(Attributes.HOSTNAME), getThriftPort()); s.close(); long latency = System.currentTimeMillis() - start; computeServiceUp(); return latency; } catch (Exception e) { if (log.isDebugEnabled()) log.debug("Cassandra thrift port poll failure: " + e); setAttribute(SERVICE_UP, false); return null; } } public void computeServiceUp() { // this will wait an additional poll period after thrift port is up, // as the caller will not have set yet, but that will help ensure it is // really healthy! setAttribute( SERVICE_UP, getAttribute(THRIFT_PORT_LATENCY) != null && getAttribute(THRIFT_PORT_LATENCY) >= 0 && Boolean.TRUE.equals(getAttribute(SERVICE_UP_JMX))); } }) .enabled(retrieveUsageMetrics)) .build(); jmxMxBeanFeed = JavaAppUtils.connectMXBeanSensors(this); }
@Override public void abort(IJobUpdateKey key, AuditData auditData) throws UpdateStateException { unscopedChangeUpdateStatus( key, Functions.compose(createAuditedEvent(auditData), Functions.constant(ABORTED))); }
public void testDelete() { persistence.mutate("A", Functions.constant("A".getBytes())); assertNull(persistence.mutate("A", Functions.constant((byte[]) null))); assertNull(persistence.get("A")); }
@Test @SuppressWarnings("unchecked") public void testDependencies() throws Exception { TargetNode<?> hamcrestTargetNode = PrebuiltJarBuilder.createBuilder( BuildTargetFactory.newInstance("//third-party/hamcrest:hamcrest")) .setBinaryJar(Paths.get("third-party/hamcrest/hamcrest.jar")) .build(); TargetNode<?> guavaTargetNode = PrebuiltJarBuilder.createBuilder( BuildTargetFactory.newInstance("//third-party/guava:guava")) .setBinaryJar(Paths.get("third-party/guava/guava.jar")) .build(); TargetNode<?> baseTargetNode = JavaLibraryBuilder.createBuilder( BuildTargetFactory.newInstance("//java/com/example/base:base")) .addDep(guavaTargetNode.getBuildTarget()) .addSrc(Paths.get("java/com/example/base/Base.java")) .build(); TargetNode<?> baseGenruleTarget = GenruleBuilder.newGenruleBuilder( BuildTargetFactory.newInstance("//java/com/example/base:genrule")) .build(); TargetNode<?> baseInlineTestsTargetNode = JavaLibraryBuilder.createBuilder( BuildTargetFactory.newInstance("//java/com/example/base:tests")) .addDep(hamcrestTargetNode.getBuildTarget()) .addSrc(Paths.get("java/com/example/base/TestBase.java")) .addSrcTarget(baseGenruleTarget.getBuildTarget()) .build(); TargetNode<?> baseTestsTargetNode = JavaTestBuilder.createBuilder( BuildTargetFactory.newInstance("//javatests/com/example/base:base")) .addDep(baseTargetNode.getBuildTarget()) .addDep(hamcrestTargetNode.getBuildTarget()) .addSrc(Paths.get("javatests/com/example/base/Base.java")) .build(); IjModuleGraph moduleGraph = IjModuleGraphTest.createModuleGraph( ImmutableSet.of( hamcrestTargetNode, guavaTargetNode, baseTargetNode, baseGenruleTarget, baseInlineTestsTargetNode, baseTestsTargetNode), ImmutableMap.<TargetNode<?>, Path>of( baseInlineTestsTargetNode, Paths.get("buck-out/baseInlineTests.jar")), Functions.constant(Optional.<Path>absent())); IjLibrary hamcrestLibrary = IjModuleGraphTest.getLibraryForTarget(moduleGraph, hamcrestTargetNode); IjLibrary guavaLibrary = IjModuleGraphTest.getLibraryForTarget(moduleGraph, guavaTargetNode); IjModule baseModule = IjModuleGraphTest.getModuleForTarget(moduleGraph, baseTargetNode); IjModule baseTestModule = IjModuleGraphTest.getModuleForTarget(moduleGraph, baseTestsTargetNode); IjProjectTemplateDataPreparer dataPreparer = new IjProjectTemplateDataPreparer(javaPackageFinder, moduleGraph, filesystem); assertEquals( IjModuleGraphTest.getModuleForTarget(moduleGraph, baseInlineTestsTargetNode), IjModuleGraphTest.getModuleForTarget(moduleGraph, baseTargetNode)); DependencyEntryData.Builder dependencyEntryBuilder = DependencyEntryData.builder().setExported(false); assertThat( dataPreparer.getDependencies(baseModule), contains( allOf( hasProperty("type", equalTo(IjDependencyListBuilder.Type.LIBRARY)), hasProperty( "data", equalTo( Optional.of( dependencyEntryBuilder .setName(guavaLibrary.getName()) .setScope(IjDependencyListBuilder.Scope.COMPILE) .build())))), allOf( hasProperty("type", equalTo(IjDependencyListBuilder.Type.LIBRARY)), hasProperty( "data", equalTo( Optional.of( dependencyEntryBuilder .setName(hamcrestLibrary.getName()) .setScope(IjDependencyListBuilder.Scope.COMPILE) .build())))), allOf(hasProperty("type", equalTo(IjDependencyListBuilder.Type.SOURCE_FOLDER))), allOf( hasProperty("type", equalTo(IjDependencyListBuilder.Type.LIBRARY)), hasProperty( "data", equalTo( Optional.of( DependencyEntryData.builder() .setExported(true) .setName("library_java_com_example_base_tests") .setScope(IjDependencyListBuilder.Scope.PROVIDED) .build())))))); assertThat( dataPreparer.getDependencies(baseTestModule), contains( allOf( hasProperty("type", equalTo(IjDependencyListBuilder.Type.MODULE)), hasProperty( "data", equalTo( Optional.of( dependencyEntryBuilder .setName(baseModule.getName()) .setScope(IjDependencyListBuilder.Scope.TEST) .build())))), allOf( hasProperty("type", equalTo(IjDependencyListBuilder.Type.LIBRARY)), hasProperty( "data", equalTo( Optional.of( dependencyEntryBuilder .setName(hamcrestLibrary.getName()) .setScope(IjDependencyListBuilder.Scope.TEST) .build())))), allOf(hasProperty("type", equalTo(IjDependencyListBuilder.Type.SOURCE_FOLDER))))); }
public void connectSensors() { sensors().set(Attributes.MAIN_URI, URI.create(config().get(MESOS_URL))); HttpFeed.Builder httpFeedBuilder = HttpFeed.builder() .entity(this) .period(1, TimeUnit.MINUTES) .baseUri(sensors().get(Attributes.MAIN_URI)) .credentialsIfNotNull(config().get(MESOS_USERNAME), config().get(MESOS_PASSWORD)) .poll( HttpPollConfig.forSensor(SERVICE_UP) .suburl("/master/health") .onSuccess(HttpValueFunctions.responseCodeEquals(200)) .onFailureOrException(Functions.constant(Boolean.FALSE))) .poll( HttpPollConfig.forSensor(CLUSTER_NAME) .suburl("/master/state.json") .onSuccess(HttpValueFunctions.jsonContents("cluster", String.class)) .onFailureOrException(Functions.constant(""))) .poll( HttpPollConfig.forSensor(CLUSTER_ID) .suburl("/master/state.json") .onSuccess(HttpValueFunctions.jsonContents("id", String.class)) .onFailureOrException(Functions.constant(""))) .poll( HttpPollConfig.forSensor(MESOS_VERSION) .suburl("/master/state.json") .onSuccess(HttpValueFunctions.jsonContents("version", String.class)) .onFailureOrException(Functions.constant(""))) .poll( HttpPollConfig.forSensor(CPUS_TOTAL) .suburl("/system/stats.json") .onSuccess(HttpValueFunctions.jsonContents("cpus_total", Integer.class)) .onFailureOrException(Functions.constant(-1))) .poll( HttpPollConfig.forSensor(LOAD_1MIN) .suburl("/system/stats.json") .onSuccess(HttpValueFunctions.jsonContents("avg_load_1min", Double.class)) .onFailureOrException(Functions.constant(-1.0d))) .poll( HttpPollConfig.forSensor(LOAD_5MIN) .suburl("/system/stats.json") .onSuccess(HttpValueFunctions.jsonContents("avg_load_5min", Double.class)) .onFailureOrException(Functions.constant(-1.0d))) .poll( HttpPollConfig.forSensor(LOAD_15MIN) .suburl("/system/stats.json") .onSuccess(HttpValueFunctions.jsonContents("avg_load_15min", Double.class)) .onFailureOrException(Functions.constant(-1.0d))) .poll( HttpPollConfig.forSensor(MEMORY_FREE_BYTES) .suburl("/system/stats.json") .onSuccess(HttpValueFunctions.jsonContents("mem_free_bytes", Long.class)) .onFailureOrException(Functions.constant(-1L))) .poll( HttpPollConfig.forSensor(MEMORY_TOTAL_BYTES) .suburl("/system/stats.json") .onSuccess(HttpValueFunctions.jsonContents("mem_total_bytes", Long.class)) .onFailureOrException(Functions.constant(-1L))); httpFeed = httpFeedBuilder.build(); Duration scanInterval = config().get(SCAN_INTERVAL); HttpFeed.Builder scanBuilder = HttpFeed.builder() .entity(this) .period(scanInterval) .baseUri(sensors().get(Attributes.MAIN_URI)) .credentialsIfNotNull(config().get(MESOS_USERNAME), config().get(MESOS_PASSWORD)) .poll( HttpPollConfig.forSensor(MESOS_SLAVE_LIST) .description("Scan Cluster Slaves") .suburl("/master/state.json") .onSuccess( Functionals.chain( HttpValueFunctions.jsonContents(), JsonFunctions.walk("slaves"), new Function<JsonElement, List<String>>() { @Override public List<String> apply(JsonElement slaves) { try { return scanSlaves(slaves.getAsJsonArray()); } catch (UnknownHostException e) { throw Exceptions.propagate(e); } } })) .onFailureOrException(Functions.<List<String>>constant(null))) .poll( HttpPollConfig.forSensor(MESOS_FRAMEWORK_LIST) .description("Scan Cluster Frameworks") .suburl("/master/state.json") .onSuccess( Functionals.chain( HttpValueFunctions.jsonContents(), JsonFunctions.walk("frameworks"), new Function<JsonElement, List<String>>() { @Override public List<String> apply(JsonElement frameworks) { return scanFrameworks(frameworks.getAsJsonArray()); } })) .onFailureOrException(Functions.<List<String>>constant(null))); scanner = scanBuilder.build(); }
public F setOnException(T val) { return onException(Functions.constant(val)); }
@Override public void connectSensors() { super.connectSensors(); final String id = sensors().get(MESOS_SLAVE_ID); HttpFeed.Builder httpFeedBuilder = HttpFeed.builder() .entity(this) .period(30, TimeUnit.SECONDS) .baseUri(getMesosCluster().sensors().get(Attributes.MAIN_URI)) .credentialsIfNotNull( config().get(MesosCluster.MESOS_USERNAME), config().get(MesosCluster.MESOS_PASSWORD)) .poll( new HttpPollConfig<Long>(MEMORY_AVAILABLE) .suburl("/master/state.json") .onSuccess( Functionals.chain( HttpValueFunctions.jsonContents(), Functions.compose( MesosUtils.selectM( new Predicate<JsonElement>() { @Override public boolean apply(JsonElement input) { return input .getAsJsonObject() .get("id") .getAsString() .equals(id); } }), JsonFunctions.walk("slaves")), JsonFunctions.walkM("resources", "mem"), JsonFunctions.castM(Long.class))) .onFailureOrException(Functions.constant(-1L))) .poll( new HttpPollConfig<Double>(CPU_AVAILABLE) .suburl("/master/state.json") .onSuccess( Functionals.chain( HttpValueFunctions.jsonContents(), Functions.compose( MesosUtils.selectM( new Predicate<JsonElement>() { @Override public boolean apply(JsonElement input) { return input .getAsJsonObject() .get("id") .getAsString() .equals(id); } }), JsonFunctions.walk("slaves")), JsonFunctions.walkM("resources", "cpus"), JsonFunctions.castM(Double.class))) .onFailureOrException(Functions.constant(-1d))) .poll( new HttpPollConfig<Long>(DISK_AVAILABLE) .suburl("/master/state.json") .onSuccess( Functionals.chain( HttpValueFunctions.jsonContents(), Functions.compose( MesosUtils.selectM( new Predicate<JsonElement>() { @Override public boolean apply(JsonElement input) { return input .getAsJsonObject() .get("id") .getAsString() .equals(id); } }), JsonFunctions.walk("slaves")), JsonFunctions.walkM("resources", "disk"), JsonFunctions.castM(Long.class))) .onFailureOrException(Functions.constant(-1L))) .poll( new HttpPollConfig<Long>(MEMORY_USED) .suburl("/master/state.json") .onSuccess( Functionals.chain( HttpValueFunctions.jsonContents(), Functions.compose( MesosUtils.selectM( new Predicate<JsonElement>() { @Override public boolean apply(JsonElement input) { return input .getAsJsonObject() .get("id") .getAsString() .equals(id); } }), JsonFunctions.walk("slaves")), JsonFunctions.walkM("used_resources", "mem"), JsonFunctions.castM(Long.class))) .onFailureOrException(Functions.constant(-1L))) .poll( new HttpPollConfig<Double>(CPU_USED) .suburl("/master/state.json") .onSuccess( Functionals.chain( HttpValueFunctions.jsonContents(), Functions.compose( MesosUtils.selectM( new Predicate<JsonElement>() { @Override public boolean apply(JsonElement input) { return input .getAsJsonObject() .get("id") .getAsString() .equals(id); } }), JsonFunctions.walk("slaves")), JsonFunctions.walkM("used_resources", "cpus"), JsonFunctions.castM(Double.class))) .onFailureOrException(Functions.constant(-1d))) .poll( new HttpPollConfig<Long>(DISK_USED) .suburl("/master/state.json") .onSuccess( Functionals.chain( HttpValueFunctions.jsonContents(), Functions.compose( MesosUtils.selectM( new Predicate<JsonElement>() { @Override public boolean apply(JsonElement input) { return input .getAsJsonObject() .get("id") .getAsString() .equals(id); } }), JsonFunctions.walk("slaves")), JsonFunctions.walkM("used_resources", "disk"), JsonFunctions.castM(Long.class))) .onFailureOrException(Functions.constant(-1L))); httpFeed = httpFeedBuilder.build(); }
public void testBasicSetAndGet() { persistence.mutate("A", Functions.constant("A".getBytes())); assertTrue(Arrays.equals(persistence.get("A"), "A".getBytes())); }
public F setOnFailure(T val) { return onFailure(Functions.constant(val)); }
private static Map<String, Specialization> mapToSpecialisation( Set<String> services, Specialization spec) { return Maps.<String, String, Specialization>transformValues( Maps.uniqueIndex(services, Functions.<String>identity()), Functions.constant(spec)); }
public F setOnResult(T val) { return onResult(Functions.constant(val)); }
public void testOverwrite() { persistence.mutate("A", Functions.constant("A".getBytes())); assertTrue( Arrays.equals("B".getBytes(), persistence.mutate("A", Functions.constant("B".getBytes())))); assertTrue(Arrays.equals(persistence.get("A"), "B".getBytes())); }
public F setOnFailureOrException(T val) { return onFailureOrException(Functions.constant(val)); }
public static <T> Task<T> valueWhenAttributeReady( Entity source, AttributeSensor<T> sensor, T value) { return DependentConfiguration.<T, T>attributePostProcessedWhenReady( source, sensor, GroovyJavaMethods.truthPredicate(), Functions.constant(value)); }