List of usage examples for com.google.common.collect Iterables tryFind
public static <T> Optional<T> tryFind(Iterable<T> iterable, Predicate<? super T> predicate)
From source file:org.apache.druid.indexing.kafka.supervisor.KafkaSupervisor.java
public KafkaSupervisor(final TaskStorage taskStorage, final TaskMaster taskMaster, final IndexerMetadataStorageCoordinator indexerMetadataStorageCoordinator, final KafkaIndexTaskClientFactory taskClientFactory, final ObjectMapper mapper, final KafkaSupervisorSpec spec, final RowIngestionMetersFactory rowIngestionMetersFactory) { this.taskStorage = taskStorage; this.taskMaster = taskMaster; this.indexerMetadataStorageCoordinator = indexerMetadataStorageCoordinator; this.sortingMapper = mapper.copy().configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true); this.spec = spec; this.emitter = spec.getEmitter(); this.monitorSchedulerConfig = spec.getMonitorSchedulerConfig(); this.rowIngestionMetersFactory = rowIngestionMetersFactory; this.dataSource = spec.getDataSchema().getDataSource(); this.ioConfig = spec.getIoConfig(); this.tuningConfig = spec.getTuningConfig(); this.taskTuningConfig = KafkaTuningConfig.copyOf(this.tuningConfig); this.supervisorId = StringUtils.format("KafkaSupervisor-%s", dataSource); this.exec = Execs.singleThreaded(supervisorId); this.scheduledExec = Execs.scheduledSingleThreaded(supervisorId + "-Scheduler-%d"); this.reportingExec = Execs.scheduledSingleThreaded(supervisorId + "-Reporting-%d"); int workerThreads = (this.tuningConfig.getWorkerThreads() != null ? this.tuningConfig.getWorkerThreads() : Math.min(10, this.ioConfig.getTaskCount())); this.workerExec = MoreExecutors .listeningDecorator(Execs.multiThreaded(workerThreads, supervisorId + "-Worker-%d")); log.info("Created worker pool with [%d] threads for dataSource [%s]", workerThreads, this.dataSource); this.taskInfoProvider = new TaskInfoProvider() { @Override//from ww w . j a va 2s . c o m public TaskLocation getTaskLocation(final String id) { Preconditions.checkNotNull(id, "id"); Optional<TaskRunner> taskRunner = taskMaster.getTaskRunner(); if (taskRunner.isPresent()) { Optional<? extends TaskRunnerWorkItem> item = Iterables.tryFind( taskRunner.get().getRunningTasks(), (Predicate<TaskRunnerWorkItem>) taskRunnerWorkItem -> id .equals(taskRunnerWorkItem.getTaskId())); if (item.isPresent()) { return item.get().getLocation(); } } else { log.error("Failed to get task runner because I'm not the leader!"); } return TaskLocation.unknown(); } @Override public Optional<TaskStatus> getTaskStatus(String id) { return taskStorage.getStatus(id); } }; this.futureTimeoutInSeconds = Math.max(MINIMUM_FUTURE_TIMEOUT_IN_SECONDS, tuningConfig.getChatRetries() * (tuningConfig.getHttpTimeout().getStandardSeconds() + KafkaIndexTaskClient.MAX_RETRY_WAIT_SECONDS)); int chatThreads = (this.tuningConfig.getChatThreads() != null ? this.tuningConfig.getChatThreads() : Math.min(10, this.ioConfig.getTaskCount() * this.ioConfig.getReplicas())); this.taskClient = taskClientFactory.build(taskInfoProvider, dataSource, chatThreads, this.tuningConfig.getHttpTimeout(), this.tuningConfig.getChatRetries()); log.info("Created taskClient with dataSource[%s] chatThreads[%d] httpTimeout[%s] chatRetries[%d]", dataSource, chatThreads, this.tuningConfig.getHttpTimeout(), this.tuningConfig.getChatRetries()); }
From source file:org.killbill.billing.plugin.analytics.reports.ReportsUserApi.java
private void addMissingValueForDateIfNeeded(final LocalDate curDate, final List<XY> dataForPivot) { final XY valueForCurrentDate = Iterables.tryFind(dataForPivot, new Predicate<XY>() { @Override// w ww .j av a2 s.c om public boolean apply(final XY xy) { return xy.getxDate().compareTo(curDate) == 0; } }).orNull(); if (valueForCurrentDate == null) { dataForPivot.add(new XY(curDate, (float) 0)); } }
From source file:com.thinkbiganalytics.feedmgr.service.template.RegisteredTemplateService.java
/** * Return a registered template object that is populated for use with updating in Kylo * * @param registeredTemplateRequest the request to get a registered template * @return a RegisteredTemplate object mapping either one already defined in Kylo, or a new template that maps to one in NiFi *///from ww w .ja v a 2 s. c o m public RegisteredTemplate getRegisteredTemplateForUpdate(RegisteredTemplateRequest registeredTemplateRequest) { if (registeredTemplateRequest.isTemplateEdit()) { this.accessController.checkPermission(AccessController.SERVICES, FeedServicesAccessControl.EDIT_TEMPLATES); } RegisteredTemplate registeredTemplate = null; //attempt to find the template as a Service RegisteredTemplateRequest serviceLevelRequest = new RegisteredTemplateRequest(registeredTemplateRequest); //editing a feed will run as a service account serviceLevelRequest.setFeedEdit(true); RegisteredTemplate template = findRegisteredTemplate(serviceLevelRequest); boolean canEdit = true; if (template != null && StringUtils.isNotBlank(template.getId()) && registeredTemplateRequest.isTemplateEdit()) { canEdit = checkTemplatePermission(template.getId(), TemplateAccessControl.EDIT_TEMPLATE); } if (canEdit) { registeredTemplate = template; if (registeredTemplate == null) { registeredTemplate = nifiTemplateToRegisteredTemplate( registeredTemplateRequest.getNifiTemplateId()); } if (registeredTemplate == null) { //throw exception } else { if (StringUtils.isBlank(registeredTemplate.getId()) && template != null && StringUtils.isNotBlank(template.getId())) { registeredTemplate.setId(template.getId()); } Set<PortDTO> ports = null; // fetch ports for this template try { if (registeredTemplate.getNifiTemplate() != null) { ports = nifiRestClient.getPortsForTemplate(registeredTemplate.getNifiTemplate()); } else { ports = nifiRestClient.getPortsForTemplate(registeredTemplate.getNifiTemplateId()); } } catch (NifiComponentNotFoundException notFoundException) { syncNiFiTemplateId(registeredTemplate); ports = nifiRestClient.getPortsForTemplate(registeredTemplate.getNifiTemplateId()); } if (ports == null) { ports = new HashSet<>(); } List<PortDTO> outputPorts = ports.stream().filter(portDTO -> portDTO != null && NifiConstants.NIFI_PORT_TYPE.OUTPUT_PORT.name().equalsIgnoreCase(portDTO.getType())) .collect(Collectors.toList()); List<PortDTO> inputPorts = ports.stream().filter(portDTO -> portDTO != null && NifiConstants.NIFI_PORT_TYPE.INPUT_PORT.name().equalsIgnoreCase(portDTO.getType())) .collect(Collectors.toList()); registeredTemplate.setReusableTemplate(inputPorts != null && !inputPorts.isEmpty()); List<ReusableTemplateConnectionInfo> reusableTemplateConnectionInfos = registeredTemplate .getReusableTemplateConnections(); List<ReusableTemplateConnectionInfo> updatedConnectionInfo = new ArrayList<>(); for (final PortDTO port : outputPorts) { ReusableTemplateConnectionInfo reusableTemplateConnectionInfo = null; if (reusableTemplateConnectionInfos != null && !reusableTemplateConnectionInfos.isEmpty()) { reusableTemplateConnectionInfo = Iterables.tryFind(reusableTemplateConnectionInfos, reusableTemplateConnectionInfo1 -> reusableTemplateConnectionInfo1 .getFeedOutputPortName().equalsIgnoreCase(port.getName())) .orNull(); } if (reusableTemplateConnectionInfo == null) { reusableTemplateConnectionInfo = new ReusableTemplateConnectionInfo(); reusableTemplateConnectionInfo.setFeedOutputPortName(port.getName()); } updatedConnectionInfo.add(reusableTemplateConnectionInfo); } registeredTemplate.setReusableTemplateConnections(updatedConnectionInfo); registeredTemplate.initializeProcessors(); ensureRegisteredTemplateInputProcessors(registeredTemplate); } } return registeredTemplate; }
From source file:cc.arduino.contributions.packages.ContributionsIndexer.java
public ContributedPlatform getPlatformByFolder(final File folder) { com.google.common.base.Optional<ContributedPlatform> platformOptional = Iterables .tryFind(getInstalledPlatforms(), contributedPlatform -> { assert contributedPlatform.getInstalledFolder() != null; return FileUtils.isSubDirectory(contributedPlatform.getInstalledFolder(), folder); });/* ww w .j av a 2 s . c o m*/ return platformOptional.orNull(); }
From source file:org.apache.brooklyn.cloudfoundry.location.CloudFoundryLocation.java
private boolean implementsInterface(Entity entity, Class<?> type) { return Iterables.tryFind(Arrays.asList(entity.getClass().getInterfaces()), Predicates.assignableFrom(type)) .isPresent();//from ww w .j ava 2 s .co m }
From source file:org.apache.brooklyn.entity.database.mysql.InitSlaveTaskBody.java
private MySqlNode getSnapshotNode() { String snapshotNodeId = cluster.getConfig(MySqlCluster.REPLICATION_PREFERRED_SOURCE); if (snapshotNodeId != null) { Optional<Entity> preferredNode = Iterables.tryFind(cluster.getMembers(), EntityPredicates.idEqualTo(snapshotNodeId)); if (preferredNode.isPresent()) { return (MySqlNode) preferredNode.get(); } else {// w w w .j a v a 2 s.c o m log.warn("MySql cluster " + this + " configured with preferred snapshot node " + snapshotNodeId + " but it's not a member. Defaulting to a random slave."); } } return getRandomSlave(); }
From source file:com.eucalyptus.simpleworkflow.stateful.TimeoutManager.java
private void timeoutDecisionTasksAndWorkflows() { final Set<NotifyTaskList> taskLists = Sets.newHashSet(); try {/*from w w w . j a va 2s. com*/ final long now = System.currentTimeMillis(); for (final WorkflowExecution workflowExecution : workflowExecutions.listTimedOut(now, Functions.<WorkflowExecution>identity())) { try (final WorkflowLock lock = WorkflowLock.lock(workflowExecution.getOwnerAccountNumber(), workflowExecution.getDomainUuid(), workflowExecution.getDisplayName())) { workflowExecutions.withRetries().updateByExample(workflowExecution, workflowExecution.getOwner(), workflowExecution.getDisplayName(), new Function<WorkflowExecution, Void>() { @Override public Void apply(final WorkflowExecution workflowExecution) { final Date timeout = workflowExecution.calculateNextTimeout(); if (timeout != null) { if (workflowExecution.isWorkflowTimedOut(now, getWorkflowExecutionDurationMillis())) { workflowExecution.closeWorkflow(WorkflowExecution.CloseStatus.Timed_Out, WorkflowHistoryEvent.create(workflowExecution, new WorkflowExecutionTimedOutEventAttributes() .withTimeoutType("START_TO_CLOSE") .withChildPolicy( workflowExecution.getChildPolicy()))); } else { // decision task timed out final List<WorkflowHistoryEvent> events = workflowExecution .getWorkflowHistory(); final List<WorkflowHistoryEvent> reverseEvents = Lists.reverse(events); final WorkflowHistoryEvent scheduled = Iterables.find(reverseEvents, CollectionUtils.propertyPredicate("DecisionTaskScheduled", WorkflowExecutions.WorkflowHistoryEventStringFunctions.EVENT_TYPE)); final Optional<WorkflowHistoryEvent> previousStarted = Iterables .tryFind(reverseEvents, CollectionUtils.propertyPredicate( "DecisionTaskStarted", WorkflowExecutions.WorkflowHistoryEventStringFunctions.EVENT_TYPE)); workflowExecution.addHistoryEvent(WorkflowHistoryEvent.create( workflowExecution, new DecisionTaskTimedOutEventAttributes() .withTimeoutType("START_TO_CLOSE") .withScheduledEventId(scheduled.getEventId()) .withStartedEventId(previousStarted.transform( WorkflowExecutions.WorkflowHistoryEventLongFunctions.EVENT_ID) .orNull()))); workflowExecution.addHistoryEvent(WorkflowHistoryEvent.create( workflowExecution, new DecisionTaskScheduledEventAttributes() .withTaskList(new TaskList() .withName(workflowExecution.getTaskList())) .withStartToCloseTimeout( String.valueOf(workflowExecution .getTaskStartToCloseTimeout())))); workflowExecution.setDecisionStatus(Pending); workflowExecution.setDecisionTimestamp(new Date()); addToNotifyLists(taskLists, workflowExecution); } } return null; } }); } catch (final SwfMetadataException e) { if (!handleException(e)) { logger.error("Error processing workflow execution/decision task timeout: " + workflowExecution.getDisplayName(), e); } } } } catch (final SwfMetadataException e) { logger.error("Error processing workflow execution/decision task timeouts", e); } notifyLists(taskLists); }
From source file:brooklyn.entity.mesos.task.marathon.MarathonTaskImpl.java
@Override public void start(Collection<? extends Location> locs) { addLocations(locs);//from ww w . j a v a 2s . co m List<Location> locations = MutableList.copyOf(Locations.getLocationsCheckingAncestors(locs, this)); super.start(locations); ServiceStateLogic.setExpectedState(this, Lifecycle.STARTING); // Only start new application for managed tasks boolean managed = Optional.fromNullable(sensors().get(MANAGED)).or(true); if (managed) { Entity entity = getRunningEntity(); entity.sensors().set(DockerContainer.CONTAINER, this); entity.sensors().set(MesosAttributes.MESOS_CLUSTER, getMesosCluster()); MarathonFramework marathon = (MarathonFramework) sensors().get(FRAMEWORK); marathon.sensors().get(MesosFramework.FRAMEWORK_TASKS).addMember(this); String id = sensors().get(APPLICATION_ID); Map<String, Object> flags = getMarathonFlags(entity); try { LOG.debug("Starting task {} on {} with flags: {}", new Object[] { id, marathon, Joiner.on(",").withKeyValueSeparator("=").join(flags) }); marathon.startApplication(id, flags); } catch (Exception e) { ServiceStateLogic.setExpectedState(this, Lifecycle.ON_FIRE); throw Exceptions.propagate(e); } // Waiting for TASK_RUNNING status and get hostname Task<?> running = DependentConfiguration.attributeWhenReady(this, MesosTask.TASK_STATE, Predicates.equalTo(MesosTask.TaskState.TASK_RUNNING.toString())); DynamicTasks.queueIfPossible(running).orSubmitAndBlock(this).asTask() .getUnchecked(Duration.FIVE_MINUTES); Task<?> hostname = DependentConfiguration.attributeWhenReady(this, Attributes.HOSTNAME, Predicates.notNull()); DynamicTasks.queueIfPossible(hostname).orSubmitAndBlock(this).asTask() .getUnchecked(Duration.FIVE_MINUTES); LOG.info("Task {} running on {} successfully", id, getHostname()); String containerId = null; MesosSlave slave = getMesosCluster().getMesosSlave(getHostname()); String ps = slave.execCommand(BashCommands.sudo("docker ps --no-trunc --filter=name=mesos-* -q")); Iterable<String> containers = Iterables.filter( Splitter.on(CharMatcher.anyOf("\r\n")).omitEmptyStrings().split(ps), StringPredicates.matchesRegex("[a-z0-9]{64}")); for (String each : containers) { String env = slave.execCommand(BashCommands .sudo("docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' " + each)); Optional<String> found = Iterables.tryFind(Splitter.on(CharMatcher.anyOf("\r\n")).split(env), Predicates.equalTo("MARATHON_APP_ID=" + sensors().get(APPLICATION_ID))); if (found.isPresent()) { containerId = each; break; } } sensors().set(DockerContainer.CONTAINER_ID, containerId); entity.sensors().set(DockerContainer.CONTAINER_ID, containerId); // Set network configuration if using Calico SDN if (SdnUtils.isSdnProvider(getMesosCluster(), "CalicoModule")) { CalicoModule provider = ((CalicoModule) getMesosCluster().sensors().get(MesosCluster.SDN_PROVIDER)); List<String> networks = Lists.newArrayList(entity.getApplicationId()); Collection<String> extra = entity.config().get(SdnAttributes.NETWORK_LIST); if (extra != null) networks.addAll(extra); sensors().set(SdnAttributes.ATTACHED_NETWORKS, networks); entity.sensors().set(SdnAttributes.ATTACHED_NETWORKS, networks); Set<String> addresses = Sets.newHashSet(); for (String networkId : networks) { SdnUtils.createNetwork(provider, networkId); InetAddress address = provider.attachNetwork(slave, entity, containerId, networkId); addresses.add(address.getHostAddress().toString()); if (networkId.equals(entity.getApplicationId())) { sensors().set(Attributes.SUBNET_ADDRESS, address.getHostAddress()); sensors().set(Attributes.SUBNET_HOSTNAME, address.getHostAddress()); entity.sensors().set(Attributes.SUBNET_ADDRESS, address.getHostAddress()); entity.sensors().set(Attributes.SUBNET_HOSTNAME, address.getHostAddress()); } } sensors().set(DockerContainer.CONTAINER_ADDRESSES, addresses); entity.sensors().set(DockerContainer.CONTAINER_ADDRESSES, addresses); } // Look up mapped ports for entity DockerUtils.getContainerPorts(entity); createLocation(flags); } ServiceStateLogic.setExpectedState(this, Lifecycle.RUNNING); }
From source file:clocker.mesos.entity.task.marathon.MarathonTaskImpl.java
@Override public void start(Collection<? extends Location> locs) { addLocations(locs);/*from w ww . j a v a 2s . c o m*/ List<Location> locations = MutableList.copyOf(Locations.getLocationsCheckingAncestors(locs, this)); super.start(locations); ServiceStateLogic.setExpectedState(this, Lifecycle.STARTING); // Only start new application for managed tasks boolean managed = Optional.fromNullable(sensors().get(MANAGED)).or(true); if (managed) { Entity entity = getRunningEntity(); entity.sensors().set(DockerContainer.CONTAINER, this); entity.sensors().set(MesosAttributes.MESOS_CLUSTER, getMesosCluster()); MarathonFramework marathon = (MarathonFramework) sensors().get(FRAMEWORK); marathon.sensors().get(MesosFramework.FRAMEWORK_TASKS).addMember(this); String id = sensors().get(APPLICATION_ID); Map<String, Object> flags = getMarathonFlags(entity); try { LOG.debug("Starting task {} on {} with flags: {}", new Object[] { id, marathon, Joiner.on(",").withKeyValueSeparator("=").join(flags) }); marathon.startApplication(id, flags); } catch (Exception e) { ServiceStateLogic.setExpectedState(this, Lifecycle.ON_FIRE); throw Exceptions.propagate(e); } // Waiting for TASK_RUNNING status and get hostname Task<?> running = DependentConfiguration.attributeWhenReady(this, MesosTask.TASK_STATE, Predicates.equalTo(MesosTask.TaskState.TASK_RUNNING.toString())); DynamicTasks.queueIfPossible(running).orSubmitAndBlock(this).asTask() .getUnchecked(Duration.FIVE_MINUTES); Task<?> hostname = DependentConfiguration.attributeWhenReady(this, Attributes.HOSTNAME, Predicates.notNull()); DynamicTasks.queueIfPossible(hostname).orSubmitAndBlock(this).asTask() .getUnchecked(Duration.FIVE_MINUTES); LOG.info("Task {} running on {} successfully", id, getHostname()); String containerId = null; MesosSlave slave = getMesosCluster().getMesosSlave(getHostname()); String ps = slave.execCommand(BashCommands.sudo("docker ps --no-trunc --filter=name=mesos-* -q")); Iterable<String> containers = Iterables.filter( Splitter.on(CharMatcher.anyOf("\r\n")).omitEmptyStrings().split(ps), StringPredicates.matchesRegex("[a-z0-9]{64}")); for (String each : containers) { String env = slave.execCommand(BashCommands .sudo("docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' " + each)); Optional<String> found = Iterables.tryFind(Splitter.on(CharMatcher.anyOf("\r\n")).split(env), Predicates.equalTo("MARATHON_APP_ID=" + sensors().get(APPLICATION_ID))); if (found.isPresent()) { containerId = each; break; } } sensors().set(DockerContainer.DOCKER_CONTAINER_ID, containerId); entity.sensors().set(DockerContainer.DOCKER_CONTAINER_ID, containerId); // Set network configuration if using Calico SDN if (SdnUtils.isSdnProvider(getMesosCluster(), "CalicoModule")) { CalicoModule provider = ((CalicoModule) getMesosCluster().sensors().get(MesosCluster.SDN_PROVIDER)); List<String> networks = Lists.newArrayList(entity.getApplicationId()); Collection<String> extra = entity.config().get(SdnAttributes.NETWORK_LIST); if (extra != null) networks.addAll(extra); sensors().set(SdnAttributes.ATTACHED_NETWORKS, networks); entity.sensors().set(SdnAttributes.ATTACHED_NETWORKS, networks); Set<String> addresses = Sets.newHashSet(); for (String networkId : networks) { SdnUtils.createNetwork(provider, networkId); InetAddress address = provider.attachNetwork(slave, entity, containerId, networkId); addresses.add(address.getHostAddress().toString()); if (networkId.equals(entity.getApplicationId())) { sensors().set(Attributes.SUBNET_ADDRESS, address.getHostAddress()); sensors().set(Attributes.SUBNET_HOSTNAME, address.getHostAddress()); entity.sensors().set(Attributes.SUBNET_ADDRESS, address.getHostAddress()); entity.sensors().set(Attributes.SUBNET_HOSTNAME, address.getHostAddress()); } } sensors().set(DockerContainer.CONTAINER_ADDRESSES, addresses); entity.sensors().set(DockerContainer.CONTAINER_ADDRESSES, addresses); } // Look up mapped ports for entity DockerUtils.getContainerPorts(entity); createLocation(flags); } ServiceStateLogic.setExpectedState(this, Lifecycle.RUNNING); }
From source file:brooklyn.entity.nosql.cassandra.CassandraFabricImpl.java
protected boolean calculateServiceUp() { Optional<Entity> upNode = Iterables.tryFind(getMembers(), EntityPredicates.attributeEqualTo(SERVICE_UP, Boolean.TRUE)); return upNode.isPresent(); }