Search in sources :

Example 61 with Function

use of com.google.common.base.Function in project druid by druid-io.

the class MergeTaskBase method isReady.

/**
   * Checks pre-existing segments in "context" to confirm that this merge query is valid. Specifically, confirm that
   * we are operating on every segment that overlaps the chosen interval.
   */
@Override
public boolean isReady(TaskActionClient taskActionClient) throws Exception {
    // Try to acquire lock
    if (!super.isReady(taskActionClient)) {
        return false;
    } else {
        final Function<DataSegment, String> toIdentifier = new Function<DataSegment, String>() {

            @Override
            public String apply(DataSegment dataSegment) {
                return dataSegment.getIdentifier();
            }
        };
        final Set<String> current = ImmutableSet.copyOf(Iterables.transform(taskActionClient.submit(new SegmentListUsedAction(getDataSource(), getInterval(), null)), toIdentifier));
        final Set<String> requested = ImmutableSet.copyOf(Iterables.transform(segments, toIdentifier));
        final Set<String> missingFromRequested = Sets.difference(current, requested);
        if (!missingFromRequested.isEmpty()) {
            throw new ISE("Merge is invalid: current segment(s) are not in the requested set: %s", Joiner.on(", ").join(missingFromRequested));
        }
        final Set<String> missingFromCurrent = Sets.difference(requested, current);
        if (!missingFromCurrent.isEmpty()) {
            throw new ISE("Merge is invalid: requested segment(s) are not in the current set: %s", Joiner.on(", ").join(missingFromCurrent));
        }
        return true;
    }
}
Also used : Function(com.google.common.base.Function) ISE(io.druid.java.util.common.ISE) SegmentListUsedAction(io.druid.indexing.common.actions.SegmentListUsedAction) DataSegment(io.druid.timeline.DataSegment)

Example 62 with Function

use of com.google.common.base.Function in project druid by druid-io.

the class MergeTaskBase method run.

@Override
public TaskStatus run(TaskToolbox toolbox) throws Exception {
    final TaskLock myLock = Iterables.getOnlyElement(getTaskLocks(toolbox));
    final ServiceEmitter emitter = toolbox.getEmitter();
    final ServiceMetricEvent.Builder builder = new ServiceMetricEvent.Builder();
    final DataSegment mergedSegment = computeMergedSegment(getDataSource(), myLock.getVersion(), segments);
    final File taskDir = toolbox.getTaskWorkDir();
    try {
        final long startTime = System.currentTimeMillis();
        log.info("Starting merge of id[%s], segments: %s", getId(), Lists.transform(segments, new Function<DataSegment, String>() {

            @Override
            public String apply(DataSegment input) {
                return input.getIdentifier();
            }
        }));
        // download segments to merge
        final Map<DataSegment, File> gettedSegments = toolbox.fetchSegments(segments);
        // merge files together
        final File fileToUpload = merge(toolbox, gettedSegments, new File(taskDir, "merged"));
        emitter.emit(builder.build("merger/numMerged", segments.size()));
        emitter.emit(builder.build("merger/mergeTime", System.currentTimeMillis() - startTime));
        log.info("[%s] : Merged %d segments in %,d millis", mergedSegment.getDataSource(), segments.size(), System.currentTimeMillis() - startTime);
        long uploadStart = System.currentTimeMillis();
        // Upload file
        final DataSegment uploadedSegment = toolbox.getSegmentPusher().push(fileToUpload, mergedSegment);
        emitter.emit(builder.build("merger/uploadTime", System.currentTimeMillis() - uploadStart));
        emitter.emit(builder.build("merger/mergeSize", uploadedSegment.getSize()));
        toolbox.publishSegments(ImmutableList.of(uploadedSegment));
        return TaskStatus.success(getId());
    } catch (Exception e) {
        log.makeAlert(e, "Exception merging[%s]", mergedSegment.getDataSource()).addData("interval", mergedSegment.getInterval()).emit();
        return TaskStatus.failure(getId());
    }
}
Also used : ServiceEmitter(com.metamx.emitter.service.ServiceEmitter) Function(com.google.common.base.Function) TaskLock(io.druid.indexing.common.TaskLock) ServiceMetricEvent(com.metamx.emitter.service.ServiceMetricEvent) DataSegment(io.druid.timeline.DataSegment) File(java.io.File)

Example 63 with Function

use of com.google.common.base.Function in project druid by druid-io.

the class ActionBasedUsedSegmentChecker method findUsedSegments.

@Override
public Set<DataSegment> findUsedSegments(Set<SegmentIdentifier> identifiers) throws IOException {
    // Group by dataSource
    final Map<String, Set<SegmentIdentifier>> identifiersByDataSource = Maps.newTreeMap();
    for (SegmentIdentifier identifier : identifiers) {
        if (!identifiersByDataSource.containsKey(identifier.getDataSource())) {
            identifiersByDataSource.put(identifier.getDataSource(), Sets.<SegmentIdentifier>newHashSet());
        }
        identifiersByDataSource.get(identifier.getDataSource()).add(identifier);
    }
    final Set<DataSegment> retVal = Sets.newHashSet();
    for (Map.Entry<String, Set<SegmentIdentifier>> entry : identifiersByDataSource.entrySet()) {
        final List<Interval> intervals = JodaUtils.condenseIntervals(Iterables.transform(entry.getValue(), new Function<SegmentIdentifier, Interval>() {

            @Override
            public Interval apply(SegmentIdentifier input) {
                return input.getInterval();
            }
        }));
        final List<DataSegment> usedSegmentsForIntervals = taskActionClient.submit(new SegmentListUsedAction(entry.getKey(), null, intervals));
        for (DataSegment segment : usedSegmentsForIntervals) {
            if (identifiers.contains(SegmentIdentifier.fromDataSegment(segment))) {
                retVal.add(segment);
            }
        }
    }
    return retVal;
}
Also used : Set(java.util.Set) SegmentIdentifier(io.druid.segment.realtime.appenderator.SegmentIdentifier) DataSegment(io.druid.timeline.DataSegment) Function(com.google.common.base.Function) SegmentListUsedAction(io.druid.indexing.common.actions.SegmentListUsedAction) Map(java.util.Map) Interval(org.joda.time.Interval)

Example 64 with Function

use of com.google.common.base.Function in project core-java by SpineEventEngine.

the class EventBusShould method allow_enrichment_configuration_at_runtime_if_enricher_not_set_previously.

@Test
public void allow_enrichment_configuration_at_runtime_if_enricher_not_set_previously() {
    setUp(null);
    assertNull(eventBus.getEnricher());
    final Class<ProjectId> eventFieldClass = ProjectId.class;
    final Class<String> enrichmentFieldClass = String.class;
    final Function<ProjectId, String> function = new Function<ProjectId, String>() {

        @Override
        public String apply(@Nullable ProjectId input) {
            checkNotNull(input);
            return input.toString();
        }
    };
    eventBus.addFieldEnrichment(eventFieldClass, enrichmentFieldClass, function);
    final EventEnricher enricher = eventBus.getEnricher();
    assertNotNull(enricher);
}
Also used : Function(com.google.common.base.Function) EventEnricher(io.spine.server.event.enrich.EventEnricher) ProjectId(io.spine.test.event.ProjectId) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Example 65 with Function

use of com.google.common.base.Function in project intellij-community by JetBrains.

the class PyRemotePackageManagerImpl method getPythonProcessOutput.

@NotNull
@Override
protected ProcessOutput getPythonProcessOutput(@NotNull String helperPath, @NotNull List<String> args, boolean askForSudo, boolean showProgress, @Nullable final String workingDir) throws ExecutionException {
    final Sdk sdk = getSdk();
    final String homePath = sdk.getHomePath();
    if (homePath == null) {
        throw new ExecutionException("Cannot find Python interpreter for SDK " + sdk.getName());
    }
    final SdkAdditionalData sdkData = sdk.getSdkAdditionalData();
    if (sdkData instanceof PyRemoteSdkAdditionalDataBase) {
        //remote interpreter
        final PythonRemoteInterpreterManager manager = PythonRemoteInterpreterManager.getInstance();
        RemoteSdkCredentials remoteSdkCredentials;
        if (CaseCollector.useRemoteCredentials((PyRemoteSdkAdditionalDataBase) sdkData)) {
            try {
                remoteSdkCredentials = ((RemoteSdkAdditionalData) sdkData).getRemoteSdkCredentials(false);
            } catch (InterruptedException e) {
                LOG.error(e);
                remoteSdkCredentials = null;
            } catch (ExecutionException e) {
                throw analyzeException(e, helperPath, args);
            }
            if (manager != null && remoteSdkCredentials != null) {
                if (askForSudo) {
                    askForSudo = !manager.ensureCanWrite(null, remoteSdkCredentials, remoteSdkCredentials.getInterpreterPath());
                }
            } else {
                throw new PyExecutionException(PythonRemoteInterpreterManager.WEB_DEPLOYMENT_PLUGIN_IS_DISABLED, helperPath, args);
            }
        }
        if (manager != null) {
            final List<String> cmdline = new ArrayList<>();
            cmdline.add(homePath);
            cmdline.add(RemoteFile.detectSystemByPath(homePath).createRemoteFile(helperPath).getPath());
            cmdline.addAll(Collections2.transform(args, new Function<String, String>() {

                @Override
                public String apply(@Nullable String input) {
                    return quoteIfNeeded(input);
                }
            }));
            ProcessOutput processOutput;
            do {
                final PyRemoteSdkAdditionalDataBase remoteSdkAdditionalData = (PyRemoteSdkAdditionalDataBase) sdkData;
                final PyRemotePathMapper pathMapper = manager.setupMappings(null, remoteSdkAdditionalData, null);
                try {
                    processOutput = PyRemoteProcessStarterManagerUtil.getManager(remoteSdkAdditionalData).executeRemoteProcess(null, ArrayUtil.toStringArray(cmdline), workingDir, manager, remoteSdkAdditionalData, pathMapper, askForSudo, true);
                } catch (InterruptedException e) {
                    throw new ExecutionException(e);
                }
                if (askForSudo && processOutput.getStderr().contains("sudo: 3 incorrect password attempts")) {
                    continue;
                }
                break;
            } while (true);
            return processOutput;
        } else {
            throw new PyExecutionException(PythonRemoteInterpreterManager.WEB_DEPLOYMENT_PLUGIN_IS_DISABLED, helperPath, args);
        }
    } else {
        throw new PyExecutionException("Invalid remote SDK", helperPath, args);
    }
}
Also used : ArrayList(java.util.ArrayList) PyRemoteSdkAdditionalDataBase(com.jetbrains.python.remote.PyRemoteSdkAdditionalDataBase) PythonRemoteInterpreterManager(com.jetbrains.python.remote.PythonRemoteInterpreterManager) PyRemotePathMapper(com.jetbrains.python.remote.PyRemotePathMapper) Function(com.google.common.base.Function) ProcessOutput(com.intellij.execution.process.ProcessOutput) Sdk(com.intellij.openapi.projectRoots.Sdk) ExecutionException(com.intellij.execution.ExecutionException) Nullable(org.jetbrains.annotations.Nullable) RemoteSdkAdditionalData(com.intellij.remote.RemoteSdkAdditionalData) SdkAdditionalData(com.intellij.openapi.projectRoots.SdkAdditionalData) RemoteSdkCredentials(com.intellij.remote.RemoteSdkCredentials) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Function (com.google.common.base.Function)616 List (java.util.List)138 ArrayList (java.util.ArrayList)114 Nullable (javax.annotation.Nullable)103 Map (java.util.Map)97 IOException (java.io.IOException)89 HashMap (java.util.HashMap)78 Test (org.junit.Test)73 ImmutableList (com.google.common.collect.ImmutableList)49 File (java.io.File)46 Set (java.util.Set)46 Collection (java.util.Collection)35 ImmutableMap (com.google.common.collect.ImmutableMap)34 DateTime (org.joda.time.DateTime)33 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)29 HashSet (java.util.HashSet)27 Iterator (java.util.Iterator)27 LinkedList (java.util.LinkedList)26 ExecutionException (java.util.concurrent.ExecutionException)24 Collectors (java.util.stream.Collectors)15