Search in sources :

Example 31 with Multimap

use of com.google.common.collect.Multimap in project cdap by caskdata.

the class HBaseQueueDebugger method scanQueue.

/**
   * Only works for {@link co.cask.cdap.data2.transaction.queue.hbase.ShardedHBaseQueueStrategy}.
   */
public QueueStatistics scanQueue(final QueueName queueName, @Nullable Long consumerGroupId) throws Exception {
    HBaseConsumerStateStore stateStore;
    try {
        stateStore = queueAdmin.getConsumerStateStore(queueName);
    } catch (IllegalStateException e) {
        throw new NotFoundException(queueName);
    }
    TransactionExecutor txExecutor = Transactions.createTransactionExecutor(txExecutorFactory, stateStore);
    Multimap<Long, QueueBarrier> barriers = txExecutor.execute(new TransactionExecutor.Function<HBaseConsumerStateStore, Multimap<Long, QueueBarrier>>() {

        @Override
        public Multimap<Long, QueueBarrier> apply(HBaseConsumerStateStore input) throws Exception {
            return input.getAllBarriers();
        }
    }, stateStore);
    printProgress("Got %d barriers\n", barriers.size());
    QueueStatistics stats = new QueueStatistics();
    if (consumerGroupId != null) {
        barriers = Multimaps.filterKeys(barriers, Predicates.equalTo(consumerGroupId));
    }
    for (Map.Entry<Long, Collection<QueueBarrier>> entry : barriers.asMap().entrySet()) {
        long groupId = entry.getKey();
        Collection<QueueBarrier> groupBarriers = entry.getValue();
        printProgress("Scanning barriers for group %d\n", groupId);
        int currentSection = 1;
        PeekingIterator<QueueBarrier> barrierIterator = Iterators.peekingIterator(groupBarriers.iterator());
        while (barrierIterator.hasNext()) {
            QueueBarrier start = barrierIterator.next();
            QueueBarrier end = barrierIterator.hasNext() ? barrierIterator.peek() : null;
            printProgress("Scanning section %d/%d...\n", currentSection, groupBarriers.size());
            scanQueue(txExecutor, stateStore, queueName, start, end, stats);
            printProgress("Current results: %s\n", stats.getReport(showTxTimestampOnly()));
            currentSection++;
        }
        printProgress("Scanning complete");
    }
    System.out.printf("Results for queue %s: %s\n", queueName.toString(), stats.getReport(showTxTimestampOnly()));
    return stats;
}
Also used : NotFoundException(co.cask.cdap.common.NotFoundException) QueueBarrier(co.cask.cdap.data2.transaction.queue.hbase.QueueBarrier) TransactionExecutor(org.apache.tephra.TransactionExecutor) TransactionNotInProgressException(org.apache.tephra.TransactionNotInProgressException) TransactionFailureException(org.apache.tephra.TransactionFailureException) NotFoundException(co.cask.cdap.common.NotFoundException) HBaseConsumerStateStore(co.cask.cdap.data2.transaction.queue.hbase.HBaseConsumerStateStore) Multimap(com.google.common.collect.Multimap) Collection(java.util.Collection) Map(java.util.Map)

Example 32 with Multimap

use of com.google.common.collect.Multimap in project killbill by killbill.

the class FixedAndRecurringInvoiceItemGenerator method safetyBounds.

@VisibleForTesting
void safetyBounds(final Iterable<InvoiceItem> resultingItems, final Multimap<UUID, LocalDate> createdItemsPerDayPerSubscription, final InternalTenantContext internalCallContext) throws InvoiceApiException {
    // See https://github.com/killbill/killbill/issues/664
    if (config.isSanitySafetyBoundEnabled(internalCallContext)) {
        final Map<UUID, Multimap<LocalDate, InvoiceItem>> fixedItemsPerDateAndSubscription = new HashMap<UUID, Multimap<LocalDate, InvoiceItem>>();
        final Map<UUID, Multimap<Range<LocalDate>, InvoiceItem>> recurringItemsPerServicePeriodAndSubscription = new HashMap<UUID, Multimap<Range<LocalDate>, InvoiceItem>>();
        for (final InvoiceItem resultingItem : resultingItems) {
            if (resultingItem.getInvoiceItemType() == InvoiceItemType.FIXED) {
                if (fixedItemsPerDateAndSubscription.get(resultingItem.getSubscriptionId()) == null) {
                    fixedItemsPerDateAndSubscription.put(resultingItem.getSubscriptionId(), LinkedListMultimap.<LocalDate, InvoiceItem>create());
                }
                fixedItemsPerDateAndSubscription.get(resultingItem.getSubscriptionId()).put(resultingItem.getStartDate(), resultingItem);
                final Collection<InvoiceItem> resultingInvoiceItems = fixedItemsPerDateAndSubscription.get(resultingItem.getSubscriptionId()).get(resultingItem.getStartDate());
                if (resultingInvoiceItems.size() > 1) {
                    throw new InvoiceApiException(ErrorCode.UNEXPECTED_ERROR, String.format("SAFETY BOUND TRIGGERED Multiple FIXED items for subscriptionId='%s', startDate='%s', resultingItems=%s", resultingItem.getSubscriptionId(), resultingItem.getStartDate(), resultingInvoiceItems));
                }
            } else if (resultingItem.getInvoiceItemType() == InvoiceItemType.RECURRING) {
                if (recurringItemsPerServicePeriodAndSubscription.get(resultingItem.getSubscriptionId()) == null) {
                    recurringItemsPerServicePeriodAndSubscription.put(resultingItem.getSubscriptionId(), LinkedListMultimap.<Range<LocalDate>, InvoiceItem>create());
                }
                final Range<LocalDate> interval = Range.<LocalDate>closedOpen(resultingItem.getStartDate(), resultingItem.getEndDate());
                recurringItemsPerServicePeriodAndSubscription.get(resultingItem.getSubscriptionId()).put(interval, resultingItem);
                final Collection<InvoiceItem> resultingInvoiceItems = recurringItemsPerServicePeriodAndSubscription.get(resultingItem.getSubscriptionId()).get(interval);
                if (resultingInvoiceItems.size() > 1) {
                    throw new InvoiceApiException(ErrorCode.UNEXPECTED_ERROR, String.format("SAFETY BOUND TRIGGERED Multiple RECURRING items for subscriptionId='%s', startDate='%s', endDate='%s', resultingItems=%s", resultingItem.getSubscriptionId(), resultingItem.getStartDate(), resultingItem.getEndDate(), resultingInvoiceItems));
                }
            }
        }
    }
    // Trigger an exception if we create too many invoice items for a subscription on a given day
    if (config.getMaxDailyNumberOfItemsSafetyBound(internalCallContext) == -1) {
        // Safety bound disabled
        return;
    }
    for (final InvoiceItem invoiceItem : resultingItems) {
        if (invoiceItem.getSubscriptionId() != null) {
            final LocalDate resultingItemCreationDay = trackInvoiceItemCreatedDay(invoiceItem, createdItemsPerDayPerSubscription, internalCallContext);
            final Collection<LocalDate> creationDaysForSubscription = createdItemsPerDayPerSubscription.get(invoiceItem.getSubscriptionId());
            int i = 0;
            for (final LocalDate creationDayForSubscription : creationDaysForSubscription) {
                if (creationDayForSubscription.compareTo(resultingItemCreationDay) == 0) {
                    i++;
                    if (i > config.getMaxDailyNumberOfItemsSafetyBound(internalCallContext)) {
                        // Proposed items have already been logged
                        throw new InvoiceApiException(ErrorCode.UNEXPECTED_ERROR, String.format("SAFETY BOUND TRIGGERED subscriptionId='%s', resultingItem=%s", invoiceItem.getSubscriptionId(), invoiceItem));
                    }
                }
            }
        }
    }
}
Also used : FixedPriceInvoiceItem(org.killbill.billing.invoice.model.FixedPriceInvoiceItem) RecurringInvoiceItem(org.killbill.billing.invoice.model.RecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) HashMap(java.util.HashMap) Range(com.google.common.collect.Range) LocalDate(org.joda.time.LocalDate) Multimap(com.google.common.collect.Multimap) LinkedListMultimap(com.google.common.collect.LinkedListMultimap) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) Collection(java.util.Collection) UUID(java.util.UUID) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 33 with Multimap

use of com.google.common.collect.Multimap in project killbill by killbill.

the class TestAdmin method fixPaymentState.

private void fixPaymentState(final Payment payment, final String lastSuccessPaymentState, final String currentPaymentStateName, final TransactionStatus transactionStatus) throws KillBillClientException {
    //
    // We do not expose the endpoint in the client API on purpose since this should only be accessed using special permission ADMIN_CAN_FIX_DATA
    // for when there is a need to fix payment state.
    //
    final String uri = "/1.0/kb/admin/payments/" + payment.getPaymentId().toString() + "/transactions/" + payment.getTransactions().get(0).getTransactionId().toString();
    final AdminPaymentJson body = new AdminPaymentJson(lastSuccessPaymentState, currentPaymentStateName, transactionStatus.toString());
    final Multimap result = HashMultimap.create();
    result.put(KillBillHttpClient.AUDIT_OPTION_CREATED_BY, createdBy);
    result.put(KillBillHttpClient.AUDIT_OPTION_REASON, reason);
    result.put(KillBillHttpClient.AUDIT_OPTION_COMMENT, comment);
    killBillHttpClient.doPut(uri, body, result);
}
Also used : Multimap(com.google.common.collect.Multimap) HashMultimap(com.google.common.collect.HashMultimap) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) AdminPaymentJson(org.killbill.billing.jaxrs.json.AdminPaymentJson)

Example 34 with Multimap

use of com.google.common.collect.Multimap in project hbase by apache.

the class LoadIncrementalHFiles method groupOrSplitPhase.

/**
   * @param table the table to load into
   * @param pool the ExecutorService
   * @param queue the queue for LoadQueueItem
   * @param startEndKeys start and end keys
   * @return A map that groups LQI by likely bulk load region targets and Set of missing hfiles.
   */
private Pair<Multimap<ByteBuffer, LoadQueueItem>, Set<String>> groupOrSplitPhase(final Table table, ExecutorService pool, Deque<LoadQueueItem> queue, final Pair<byte[][], byte[][]> startEndKeys) throws IOException {
    // <region start key, LQI> need synchronized only within this scope of this
    // phase because of the puts that happen in futures.
    Multimap<ByteBuffer, LoadQueueItem> rgs = HashMultimap.create();
    final Multimap<ByteBuffer, LoadQueueItem> regionGroups = Multimaps.synchronizedMultimap(rgs);
    Set<String> missingHFiles = new HashSet<>();
    Pair<Multimap<ByteBuffer, LoadQueueItem>, Set<String>> pair = new Pair<>(regionGroups, missingHFiles);
    // drain LQIs and figure out bulk load groups
    Set<Future<Pair<List<LoadQueueItem>, String>>> splittingFutures = new HashSet<>();
    while (!queue.isEmpty()) {
        final LoadQueueItem item = queue.remove();
        final Callable<Pair<List<LoadQueueItem>, String>> call = new Callable<Pair<List<LoadQueueItem>, String>>() {

            @Override
            public Pair<List<LoadQueueItem>, String> call() throws Exception {
                Pair<List<LoadQueueItem>, String> splits = groupOrSplit(regionGroups, item, table, startEndKeys);
                return splits;
            }
        };
        splittingFutures.add(pool.submit(call));
    }
    // we can attempt the atomic loads.
    for (Future<Pair<List<LoadQueueItem>, String>> lqis : splittingFutures) {
        try {
            Pair<List<LoadQueueItem>, String> splits = lqis.get();
            if (splits != null) {
                if (splits.getFirst() != null) {
                    queue.addAll(splits.getFirst());
                } else {
                    missingHFiles.add(splits.getSecond());
                }
            }
        } catch (ExecutionException e1) {
            Throwable t = e1.getCause();
            if (t instanceof IOException) {
                LOG.error("IOException during splitting", e1);
                // would have been thrown if not parallelized,
                throw (IOException) t;
            }
            LOG.error("Unexpected execution exception during splitting", e1);
            throw new IllegalStateException(t);
        } catch (InterruptedException e1) {
            LOG.error("Unexpected interrupted exception during splitting", e1);
            throw (InterruptedIOException) new InterruptedIOException().initCause(e1);
        }
    }
    return pair;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) Set(java.util.Set) HashSet(java.util.HashSet) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) ClientServiceCallable(org.apache.hadoop.hbase.client.ClientServiceCallable) Callable(java.util.concurrent.Callable) HashMultimap(com.google.common.collect.HashMultimap) Multimap(com.google.common.collect.Multimap) Future(java.util.concurrent.Future) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) Pair(org.apache.hadoop.hbase.util.Pair)

Example 35 with Multimap

use of com.google.common.collect.Multimap in project cassandra by apache.

the class NetworkTopologyStrategy method calculateNaturalEndpoints.

/**
     * calculate endpoints in one pass through the tokens by tracking our progress in each DC.
     */
public List<InetAddress> calculateNaturalEndpoints(Token searchToken, TokenMetadata tokenMetadata) {
    // we want to preserve insertion order so that the first added endpoint becomes primary
    Set<InetAddress> replicas = new LinkedHashSet<>();
    Set<Pair<String, String>> seenRacks = new HashSet<>();
    Topology topology = tokenMetadata.getTopology();
    // all endpoints in each DC, so we can check when we have exhausted all the members of a DC
    Multimap<String, InetAddress> allEndpoints = topology.getDatacenterEndpoints();
    // all racks in a DC so we can check when we have exhausted all racks in a DC
    Map<String, Multimap<String, InetAddress>> racks = topology.getDatacenterRacks();
    assert !allEndpoints.isEmpty() && !racks.isEmpty() : "not aware of any cluster members";
    int dcsToFill = 0;
    Map<String, DatacenterEndpoints> dcs = new HashMap<>(datacenters.size() * 2);
    // Create a DatacenterEndpoints object for each non-empty DC.
    for (Map.Entry<String, Integer> en : datacenters.entrySet()) {
        String dc = en.getKey();
        int rf = en.getValue();
        int nodeCount = sizeOrZero(allEndpoints.get(dc));
        if (rf <= 0 || nodeCount <= 0)
            continue;
        DatacenterEndpoints dcEndpoints = new DatacenterEndpoints(rf, sizeOrZero(racks.get(dc)), nodeCount, replicas, seenRacks);
        dcs.put(dc, dcEndpoints);
        ++dcsToFill;
    }
    Iterator<Token> tokenIter = TokenMetadata.ringIterator(tokenMetadata.sortedTokens(), searchToken, false);
    while (dcsToFill > 0 && tokenIter.hasNext()) {
        Token next = tokenIter.next();
        InetAddress ep = tokenMetadata.getEndpoint(next);
        Pair<String, String> location = topology.getLocation(ep);
        DatacenterEndpoints dcEndpoints = dcs.get(location.left);
        if (dcEndpoints != null && dcEndpoints.addEndpointAndCheckIfDone(ep, location))
            --dcsToFill;
    }
    return new ArrayList<>(replicas);
}
Also used : Token(org.apache.cassandra.dht.Token) Topology(org.apache.cassandra.locator.TokenMetadata.Topology) Multimap(com.google.common.collect.Multimap) InetAddress(java.net.InetAddress) Pair(org.apache.cassandra.utils.Pair)

Aggregations

Multimap (com.google.common.collect.Multimap)61 HashMultimap (com.google.common.collect.HashMultimap)25 List (java.util.List)25 Test (org.junit.Test)20 Map (java.util.Map)17 ImmutableList (com.google.common.collect.ImmutableList)15 Collection (java.util.Collection)14 Set (java.util.Set)14 ImmutableMap (com.google.common.collect.ImmutableMap)13 HashMap (java.util.HashMap)13 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)12 ImmutableSet (com.google.common.collect.ImmutableSet)11 ArrayList (java.util.ArrayList)11 Collectors (java.util.stream.Collectors)11 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)10 Nullable (javax.annotation.Nullable)10 IOException (java.io.IOException)9 Stream (java.util.stream.Stream)9 InetAddress (java.net.InetAddress)8 Objects (java.util.Objects)8