Search in sources :

Example 1 with Ordering

use of com.google.common.collect.Ordering in project druid by druid-io.

the class TaskLockbox method syncFromStorage.

/**
   * Wipe out our current in-memory state and resync it from our bundled {@link io.druid.indexing.overlord.TaskStorage}.
   */
public void syncFromStorage() {
    giant.lock();
    try {
        // Load stuff from taskStorage first. If this fails, we don't want to lose all our locks.
        final Set<String> storedActiveTasks = Sets.newHashSet();
        final List<Pair<Task, TaskLock>> storedLocks = Lists.newArrayList();
        for (final Task task : taskStorage.getActiveTasks()) {
            storedActiveTasks.add(task.getId());
            for (final TaskLock taskLock : taskStorage.getLocks(task.getId())) {
                storedLocks.add(Pair.of(task, taskLock));
            }
        }
        // Sort locks by version, so we add them back in the order they were acquired.
        final Ordering<Pair<Task, TaskLock>> byVersionOrdering = new Ordering<Pair<Task, TaskLock>>() {

            @Override
            public int compare(Pair<Task, TaskLock> left, Pair<Task, TaskLock> right) {
                // The second compare shouldn't be necessary, but, whatever.
                return ComparisonChain.start().compare(left.rhs.getVersion(), right.rhs.getVersion()).compare(left.lhs.getId(), right.lhs.getId()).result();
            }
        };
        running.clear();
        activeTasks.clear();
        activeTasks.addAll(storedActiveTasks);
        // Bookkeeping for a log message at the end
        int taskLockCount = 0;
        for (final Pair<Task, TaskLock> taskAndLock : byVersionOrdering.sortedCopy(storedLocks)) {
            final Task task = taskAndLock.lhs;
            final TaskLock savedTaskLock = taskAndLock.rhs;
            if (savedTaskLock.getInterval().toDurationMillis() <= 0) {
                // "Impossible", but you never know what crazy stuff can be restored from storage.
                log.warn("WTF?! Got lock with empty interval for task: %s", task.getId());
                continue;
            }
            final Optional<TaskLock> acquiredTaskLock = tryLock(task, savedTaskLock.getInterval(), Optional.of(savedTaskLock.getVersion()));
            if (acquiredTaskLock.isPresent() && savedTaskLock.getVersion().equals(acquiredTaskLock.get().getVersion())) {
                taskLockCount++;
                log.info("Reacquired lock on interval[%s] version[%s] for task: %s", savedTaskLock.getInterval(), savedTaskLock.getVersion(), task.getId());
            } else if (acquiredTaskLock.isPresent()) {
                taskLockCount++;
                log.info("Could not reacquire lock on interval[%s] version[%s] (got version[%s] instead) for task: %s", savedTaskLock.getInterval(), savedTaskLock.getVersion(), acquiredTaskLock.get().getVersion(), task.getId());
            } else {
                log.info("Could not reacquire lock on interval[%s] version[%s] for task: %s", savedTaskLock.getInterval(), savedTaskLock.getVersion(), task.getId());
            }
        }
        log.info("Synced %,d locks for %,d activeTasks from storage (%,d locks ignored).", taskLockCount, activeTasks.size(), storedLocks.size() - taskLockCount);
    } finally {
        giant.unlock();
    }
}
Also used : Task(io.druid.indexing.common.task.Task) TaskLock(io.druid.indexing.common.TaskLock) Ordering(com.google.common.collect.Ordering) Pair(io.druid.java.util.common.Pair)

Example 2 with Ordering

use of com.google.common.collect.Ordering in project druid by druid-io.

the class ChainedExecutionQueryRunner method run.

@Override
public Sequence<T> run(final Query<T> query, final Map<String, Object> responseContext) {
    final int priority = BaseQuery.getContextPriority(query, 0);
    final Ordering ordering = query.getResultOrdering();
    return new BaseSequence<T, Iterator<T>>(new BaseSequence.IteratorMaker<T, Iterator<T>>() {

        @Override
        public Iterator<T> make() {
            // Make it a List<> to materialize all of the values (so that it will submit everything to the executor)
            ListenableFuture<List<Iterable<T>>> futures = Futures.allAsList(Lists.newArrayList(Iterables.transform(queryables, new Function<QueryRunner<T>, ListenableFuture<Iterable<T>>>() {

                @Override
                public ListenableFuture<Iterable<T>> apply(final QueryRunner<T> input) {
                    if (input == null) {
                        throw new ISE("Null queryRunner! Looks to be some segment unmapping action happening");
                    }
                    return exec.submit(new AbstractPrioritizedCallable<Iterable<T>>(priority) {

                        @Override
                        public Iterable<T> call() throws Exception {
                            try {
                                Sequence<T> result = input.run(query, responseContext);
                                if (result == null) {
                                    throw new ISE("Got a null result! Segments are missing!");
                                }
                                List<T> retVal = Sequences.toList(result, Lists.<T>newArrayList());
                                if (retVal == null) {
                                    throw new ISE("Got a null list of results! WTF?!");
                                }
                                return retVal;
                            } catch (QueryInterruptedException e) {
                                throw Throwables.propagate(e);
                            } catch (Exception e) {
                                log.error(e, "Exception with one of the sequences!");
                                throw Throwables.propagate(e);
                            }
                        }
                    });
                }
            })));
            queryWatcher.registerQuery(query, futures);
            try {
                final Number timeout = query.getContextValue(QueryContextKeys.TIMEOUT, (Number) null);
                return new MergeIterable<>(ordering.nullsFirst(), timeout == null ? futures.get() : futures.get(timeout.longValue(), TimeUnit.MILLISECONDS)).iterator();
            } catch (InterruptedException e) {
                log.warn(e, "Query interrupted, cancelling pending results, query id [%s]", query.getId());
                futures.cancel(true);
                throw new QueryInterruptedException(e);
            } catch (CancellationException e) {
                throw new QueryInterruptedException(e);
            } catch (TimeoutException e) {
                log.info("Query timeout, cancelling pending results for query id [%s]", query.getId());
                futures.cancel(true);
                throw new QueryInterruptedException(e);
            } catch (ExecutionException e) {
                throw Throwables.propagate(e.getCause());
            }
        }

        @Override
        public void cleanup(Iterator<T> tIterator) {
        }
    });
}
Also used : MergeIterable(io.druid.java.util.common.guava.MergeIterable) BaseSequence(io.druid.java.util.common.guava.BaseSequence) Sequence(io.druid.java.util.common.guava.Sequence) BaseSequence(io.druid.java.util.common.guava.BaseSequence) CancellationException(java.util.concurrent.CancellationException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) Function(com.google.common.base.Function) MergeIterable(io.druid.java.util.common.guava.MergeIterable) CancellationException(java.util.concurrent.CancellationException) Ordering(com.google.common.collect.Ordering) Iterator(java.util.Iterator) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ISE(io.druid.java.util.common.ISE) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with Ordering

use of com.google.common.collect.Ordering in project druid by druid-io.

the class DefaultLimitSpec method makeComparator.

private Ordering<Row> makeComparator(List<DimensionSpec> dimensions, List<AggregatorFactory> aggs, List<PostAggregator> postAggs) {
    Ordering<Row> ordering = new Ordering<Row>() {

        @Override
        public int compare(Row left, Row right) {
            return Longs.compare(left.getTimestampFromEpoch(), right.getTimestampFromEpoch());
        }
    };
    Map<String, DimensionSpec> dimensionsMap = Maps.newHashMap();
    for (DimensionSpec spec : dimensions) {
        dimensionsMap.put(spec.getOutputName(), spec);
    }
    Map<String, AggregatorFactory> aggregatorsMap = Maps.newHashMap();
    for (final AggregatorFactory agg : aggs) {
        aggregatorsMap.put(agg.getName(), agg);
    }
    Map<String, PostAggregator> postAggregatorsMap = Maps.newHashMap();
    for (PostAggregator postAgg : postAggs) {
        postAggregatorsMap.put(postAgg.getName(), postAgg);
    }
    for (OrderByColumnSpec columnSpec : columns) {
        String columnName = columnSpec.getDimension();
        Ordering<Row> nextOrdering = null;
        if (postAggregatorsMap.containsKey(columnName)) {
            nextOrdering = metricOrdering(columnName, postAggregatorsMap.get(columnName).getComparator());
        } else if (aggregatorsMap.containsKey(columnName)) {
            nextOrdering = metricOrdering(columnName, aggregatorsMap.get(columnName).getComparator());
        } else if (dimensionsMap.containsKey(columnName)) {
            nextOrdering = dimensionOrdering(columnName, columnSpec.getDimensionComparator());
        }
        if (nextOrdering == null) {
            throw new ISE("Unknown column in order clause[%s]", columnSpec);
        }
        switch(columnSpec.getDirection()) {
            case DESCENDING:
                nextOrdering = nextOrdering.reverse();
        }
        ordering = ordering.compound(nextOrdering);
    }
    return ordering;
}
Also used : DimensionSpec(io.druid.query.dimension.DimensionSpec) PostAggregator(io.druid.query.aggregation.PostAggregator) Ordering(com.google.common.collect.Ordering) ISE(io.druid.java.util.common.ISE) Row(io.druid.data.input.Row) AggregatorFactory(io.druid.query.aggregation.AggregatorFactory)

Example 4 with Ordering

use of com.google.common.collect.Ordering in project immutables by immutables.

the class SourceOrdering method getAllAccessorsProvider.

/**
   * While we have {@link SourceOrdering}, there's still a problem: We have inheritance hierarchy
   * and
   * we want to have all defined or inherited accessors returned as members of target type, like
   * {@link Elements#getAllMembers(TypeElement)}, but we need to have them properly and stably
   * sorted.
   * This implementation doesn't try to correctly resolve order for accessors inherited from
   * different supertypes(interfaces), just something that stable and reasonable wrt source ordering
   * without handling complex cases.
   * @param elements the elements utility
   * @param types the types utility
   * @param type the type to traverse
   * @return provider of all accessors in source order and mapping
   */
public static AccessorProvider getAllAccessorsProvider(final Elements elements, final Types types, final TypeElement type) {
    class CollectedOrdering extends Ordering<Element> {

        class Intratype {

            Ordering<String> ordering;

            int rank;
        }

        final Map<String, Intratype> accessorOrderings = Maps.newLinkedHashMap();

        final List<TypeElement> linearizedTypes = Lists.newArrayList();

        final Predicate<String> accessorNotYetInOrderings = Predicates.not(Predicates.in(accessorOrderings.keySet()));

        final ArrayListMultimap<String, TypeElement> accessorMapping = ArrayListMultimap.create();

        CollectedOrdering() {
            traverse(type);
            traverseObjectForInterface();
        }

        private void traverseObjectForInterface() {
            if (type.getKind() == ElementKind.INTERFACE) {
                traverse(elements.getTypeElement(Object.class.getName()));
            }
        }

        void traverse(@Nullable TypeElement element) {
            if (element == null || isJavaLangObject(element)) {
                return;
            }
            collectEnclosing(element);
            traverse(asTypeElement(element.getSuperclass()));
            for (TypeMirror implementedInterface : element.getInterfaces()) {
                traverse(asTypeElement(implementedInterface));
            }
        }

        @Nullable
        TypeElement asTypeElement(TypeMirror type) {
            if (type.getKind() == TypeKind.DECLARED) {
                return (TypeElement) ((DeclaredType) type).asElement();
            }
            return null;
        }

        void collectEnclosing(TypeElement type) {
            FluentIterable<String> accessorsInType = FluentIterable.from(SourceOrdering.getEnclosedElements(type)).filter(IsParameterlessNonstaticNonobject.PREDICATE).transform(ToSimpleName.FUNCTION);
            for (String accessor : accessorsInType) {
                accessorMapping.put(accessor, type);
            }
            List<String> accessors = accessorsInType.filter(accessorNotYetInOrderings).toList();
            Intratype intratype = new Intratype();
            intratype.rank = linearizedTypes.size();
            intratype.ordering = Ordering.explicit(accessors);
            for (String name : accessors) {
                accessorOrderings.put(name, intratype);
            }
            linearizedTypes.add(type);
        }

        @Override
        public int compare(Element left, Element right) {
            String leftKey = ToSimpleName.FUNCTION.apply(left);
            String rightKey = ToSimpleName.FUNCTION.apply(right);
            Intratype leftIntratype = accessorOrderings.get(leftKey);
            Intratype rightIntratype = accessorOrderings.get(rightKey);
            if (leftIntratype == null || rightIntratype == null) {
                // FIXME figure out why it happens (null)
                return Boolean.compare(leftIntratype == null, rightIntratype == null);
            }
            return leftIntratype == rightIntratype ? leftIntratype.ordering.compare(leftKey, rightKey) : Integer.compare(leftIntratype.rank, rightIntratype.rank);
        }
    }
    final CollectedOrdering ordering = new CollectedOrdering();
    final ImmutableList<ExecutableElement> sortedList = ordering.immutableSortedCopy(disambiguateMethods(ElementFilter.methodsIn(elements.getAllMembers(type))));
    return new AccessorProvider() {

        ImmutableListMultimap<String, TypeElement> accessorMapping = ImmutableListMultimap.copyOf(ordering.accessorMapping);

        @Override
        public ImmutableListMultimap<String, TypeElement> accessorMapping() {
            return accessorMapping;
        }

        @Override
        public ImmutableList<ExecutableElement> get() {
            return sortedList;
        }
    };
}
Also used : TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) Predicate(com.google.common.base.Predicate) TypeMirror(javax.lang.model.type.TypeMirror) Ordering(com.google.common.collect.Ordering) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) Nullable(javax.annotation.Nullable)

Example 5 with Ordering

use of com.google.common.collect.Ordering in project error-prone by google.

the class RefasterRuleBuilderScanner method extractRules.

public static Collection<? extends CodeTransformer> extractRules(ClassTree tree, Context context) {
    ClassSymbol sym = ASTHelpers.getSymbol(tree);
    RefasterRuleBuilderScanner scanner = new RefasterRuleBuilderScanner(context);
    // visit abstract methods first
    List<MethodTree> methods = new Ordering<MethodTree>() {

        @Override
        public int compare(MethodTree l, MethodTree r) {
            return Boolean.compare(l.getModifiers().getFlags().contains(Modifier.ABSTRACT), r.getModifiers().getFlags().contains(Modifier.ABSTRACT));
        }
    }.reverse().immutableSortedCopy(Iterables.filter(tree.getMembers(), MethodTree.class));
    scanner.visit(methods, null);
    UTemplater templater = new UTemplater(context);
    List<UType> types = templater.templateTypes(sym.type.getTypeArguments());
    return scanner.createMatchers(Iterables.filter(types, UTypeVar.class), sym.getQualifiedName().toString(), UTemplater.annotationMap(sym));
}
Also used : MethodTree(com.sun.source.tree.MethodTree) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Ordering(com.google.common.collect.Ordering)

Aggregations

Ordering (com.google.common.collect.Ordering)10 List (java.util.List)4 Function (com.google.common.base.Function)2 Strings (com.google.common.base.Strings)2 ImmutableList (com.google.common.collect.ImmutableList)2 Account (com.google.gerrit.reviewdb.client.Account)2 AccountSshKey (com.google.gerrit.reviewdb.client.AccountSshKey)2 AllUsersName (com.google.gerrit.server.config.AllUsersName)2 GitRepositoryManager (com.google.gerrit.server.git.GitRepositoryManager)2 MetaDataUpdate (com.google.gerrit.server.git.MetaDataUpdate)2 Inject (com.google.inject.Inject)2 Provider (com.google.inject.Provider)2 ISE (io.druid.java.util.common.ISE)2 MergeIterable (io.druid.java.util.common.guava.MergeIterable)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Density (com.android.resources.Density)1 ImageComponent (com.android.tools.idea.ui.ImageComponent)1 CompilerUtils.defineClass (com.facebook.presto.bytecode.CompilerUtils.defineClass)1 BytecodeExpressions.constantClass (com.facebook.presto.bytecode.expression.BytecodeExpressions.constantClass)1