Search in sources :

Example 71 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project java by kubernetes-client.

the class LegacyEventBroadcaster method recordToSink.

private void recordToSink(CoreV1Event event) throws InterruptedException {
    Optional<MutablePair<CoreV1Event, V1Patch>> eventAndPatch = this.eventCorrelator.correlate(event);
    if (!eventAndPatch.isPresent()) {
        // skip
        return;
    }
    CoreV1Event recordingEvent = eventAndPatch.get().getLeft();
    V1Patch patch = eventAndPatch.get().getRight();
    for (int retries = 0; retries < maxTriesPerEvent; retries++) {
        if (recordEvent(recordingEvent, patch, event.getCount() > 1)) {
            break;
        }
        Thread.sleep(sleepDuration.toMillis());
    }
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) V1Patch(io.kubernetes.client.custom.V1Patch) CoreV1Event(io.kubernetes.client.openapi.models.CoreV1Event)

Example 72 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project java by kubernetes-client.

the class DeltaFIFO method queueActionLocked.

/**
 * queueActionLocked appends to the delta list for the object. Caller must hold the lock.
 */
private void queueActionLocked(DeltaType actionType, KubernetesObject obj) {
    String id = this.keyOf(obj);
    Deque<MutablePair<DeltaType, KubernetesObject>> deltas = items.get(id);
    if (deltas == null) {
        deltas = new LinkedList<>();
        deltas.add(new MutablePair(actionType, obj));
    } else {
        deltas.add(new MutablePair<DeltaType, KubernetesObject>(actionType, obj));
    }
    Deque<MutablePair<DeltaType, KubernetesObject>> combinedDeltaList = combineDeltas(deltas);
    boolean exist = items.containsKey(id);
    if (combinedDeltaList != null && combinedDeltaList.size() > 0) {
        if (!exist) {
            this.queue.add(id);
        }
        this.items.put(id, combinedDeltaList);
        notEmpty.signalAll();
    } else {
        this.items.remove(id);
    }
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) KubernetesObject(io.kubernetes.client.common.KubernetesObject)

Example 73 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project pinot by linkedin.

the class Cube method sortDimensionOrder.

/**
   * Sort dimensions according to their cost, which is the sum of the error for aggregating all its children rows.
   * Dimensions with larger error is sorted in the front of the list.
   * The order among the dimensions that belong to the same hierarchical group will be maintained. An example of
   * a hierarchical group {continent, country}. The cost of a group is the average of member costs.
   * @throws Exception An exception is thrown if OLAP database cannot be connected.
   */
private static Dimensions sortDimensionOrder(List<DimNameValueCostEntry> costSet, Dimensions dimensions, int topDimension, List<List<String>> hierarchy) throws Exception {
    List<MutablePair<String, Double>> dimensionCostPairs = new ArrayList<>();
    Map<String, Double> dimNameToCost = new HashMap<>();
    for (DimNameValueCostEntry dimNameValueCostEntry : costSet) {
        double cost = dimNameValueCostEntry.getCost();
        if (dimNameToCost.containsKey(dimNameValueCostEntry.getDimName())) {
            cost += dimNameToCost.get(dimNameValueCostEntry.getDimName());
        }
        dimNameToCost.put(dimNameValueCostEntry.getDimName(), cost);
    }
    // Given one dimension name D, returns the hierarchical dimension to which D belong.
    Map<String, HierarchicalDimension> hierarchicalDimensionMap = new HashMap<>();
    Set<String> availableDimensionKeySet = new HashSet<>(dimensions.allDimensions());
    // dimensions of the dataset.
    for (List<String> suggestedHierarchyList : hierarchy) {
        if (suggestedHierarchyList == null || suggestedHierarchyList.size() < 2) {
            continue;
        }
        List<String> actualHierarchy = new ArrayList<>();
        for (String dimension : suggestedHierarchyList) {
            if (availableDimensionKeySet.contains(dimension)) {
                actualHierarchy.add(dimension);
            }
        }
        if (actualHierarchy.size() > 1) {
            HierarchicalDimension hierarchicalDimension = new HierarchicalDimension();
            hierarchicalDimension.hierarchy = actualHierarchy;
            for (String dimension : actualHierarchy) {
                hierarchicalDimensionMap.put(dimension, hierarchicalDimension);
            }
            hierarchicalDimension.index = dimensionCostPairs.size();
            dimensionCostPairs.add(new MutablePair<>(actualHierarchy.get(0), .0));
        }
    }
    // cost among all the children in that hierarchy.
    for (int i = 0; i < dimensions.size(); ++i) {
        String dimension = dimensions.get(i);
        double cost = 0d;
        if (dimNameToCost.containsKey(dimension)) {
            cost += dimNameToCost.get(dimension);
        }
        if (hierarchicalDimensionMap.containsKey(dimension)) {
            HierarchicalDimension hierarchicalDimension = hierarchicalDimensionMap.get(dimension);
            MutablePair<String, Double> costOfDimensionPair = dimensionCostPairs.get(hierarchicalDimension.index);
            // The max cost of children will be the cost of a group
            costOfDimensionPair.right = Math.max(cost, costOfDimensionPair.right);
        } else {
            // The dimension does not belong to any hierarchy
            MutablePair<String, Double> costOfDimensionPair = new MutablePair<>(dimension, cost);
            dimensionCostPairs.add(costOfDimensionPair);
        }
    }
    // Sort dimensions according to their costs in a descending order
    dimensionCostPairs.sort((new DimensionCostPairSorter()).reversed());
    // If there exists a huge gap (e.g., 1/10 of cost) between two cost pairs, then we chop of the dimensions because
    // pairs with small costs does not provide useful information
    // Invariance to keep: cutOffPairIdx <= number of dimensionCostPairs
    int cutOffPairIdx = 1;
    if (dimensionCostPairs.size() > 1) {
        double cutOffCost = dimensionCostPairs.get(0).getRight() / 10d;
        for (; cutOffPairIdx < dimensionCostPairs.size(); ++cutOffPairIdx) {
            double curCost = dimensionCostPairs.get(cutOffPairIdx).getRight();
            if (Double.compare(cutOffCost, curCost) > 0) {
                break;
            }
        }
    } else {
        cutOffPairIdx = 0;
    }
    // Create a new Dimension instance whose dimensions follow the calculated order
    ArrayList<String> newDimensions = new ArrayList<>();
    int pairIdx = 0;
    for (MutablePair<String, Double> dimensionCostPair : dimensionCostPairs) {
        StringBuilder sb = new StringBuilder("  Dimension: ");
        if (hierarchicalDimensionMap.containsKey(dimensionCostPair.getLeft())) {
            HierarchicalDimension hierarchicalDimension = hierarchicalDimensionMap.get(dimensionCostPair.getLeft());
            if (pairIdx <= cutOffPairIdx) {
                newDimensions.addAll(hierarchicalDimension.hierarchy);
            }
            sb.append(hierarchicalDimension.hierarchy);
        } else {
            // The dimension does not belong to any hierarchy
            if (pairIdx <= cutOffPairIdx) {
                newDimensions.add(dimensionCostPair.getLeft());
            }
            sb.append(dimensionCostPair.getLeft());
        }
        sb.append(", Cost: ");
        sb.append(dimensionCostPair.getRight());
        LOG.info(sb.toString());
        ++pairIdx;
    }
    return new Dimensions(newDimensions.subList(0, Math.min(topDimension, newDimensions.size())));
}
Also used : ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MutablePair(org.apache.commons.lang3.tuple.MutablePair) HashSet(java.util.HashSet)

Example 74 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project pulsar by yahoo.

the class Consumer method sendMessages.

/**
     * Dispatch a list of entries to the consumer.
     *
     * @return a promise that can be use to track when all the data has been written into the socket
     */
public Pair<ChannelPromise, Integer> sendMessages(final List<Entry> entries) {
    final ChannelHandlerContext ctx = cnx.ctx();
    final MutablePair<ChannelPromise, Integer> sentMessages = new MutablePair<ChannelPromise, Integer>();
    final ChannelPromise writePromise = ctx.newPromise();
    sentMessages.setLeft(writePromise);
    if (entries.isEmpty()) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] List of messages is empty, triggering write future immediately for consumerId {}", subscription, consumerId);
        }
        writePromise.setSuccess();
        sentMessages.setRight(0);
        return sentMessages;
    }
    try {
        sentMessages.setRight(updatePermitsAndPendingAcks(entries));
    } catch (PulsarServerException pe) {
        log.warn("[{}] [{}] consumer doesn't support batch-message {}", subscription, consumerId, cnx.getRemoteEndpointProtocolVersion());
        subscription.markTopicWithBatchMessagePublished();
        sentMessages.setRight(0);
        // disconnect consumer: it will update dispatcher's availablePermits and resend pendingAck-messages of this
        // consumer to other consumer
        disconnect();
        return sentMessages;
    }
    ctx.channel().eventLoop().execute(() -> {
        for (int i = 0; i < entries.size(); i++) {
            Entry entry = entries.get(i);
            PositionImpl pos = (PositionImpl) entry.getPosition();
            MessageIdData.Builder messageIdBuilder = MessageIdData.newBuilder();
            MessageIdData messageId = messageIdBuilder.setLedgerId(pos.getLedgerId()).setEntryId(pos.getEntryId()).build();
            ByteBuf metadataAndPayload = entry.getDataBuffer();
            // skip checksum by incrementing reader-index if consumer-client doesn't support checksum verification
            if (cnx.getRemoteEndpointProtocolVersion() < ProtocolVersion.v6.getNumber()) {
                readChecksum(metadataAndPayload);
            }
            // stats
            msgOut.recordEvent(metadataAndPayload.readableBytes());
            if (log.isDebugEnabled()) {
                log.debug("[{}] Sending message to consumerId {}, entry id {}", subscription, consumerId, pos.getEntryId());
            }
            // We only want to pass the "real" promise on the last entry written
            ChannelPromise promise = ctx.voidPromise();
            if (i == (entries.size() - 1)) {
                promise = writePromise;
            }
            ctx.write(Commands.newMessage(consumerId, messageId, metadataAndPayload), promise);
            messageId.recycle();
            messageIdBuilder.recycle();
        }
        ctx.flush();
    });
    return sentMessages;
}
Also used : PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Entry(org.apache.bookkeeper.mledger.Entry) MessageIdData(com.yahoo.pulsar.common.api.proto.PulsarApi.MessageIdData) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf)

Example 75 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project gatk by broadinstitute.

the class BAQ method calculateQueryRange.

/**
     * Determine the appropriate start and stop offsets in the reads for the bases given the cigar string
     * @param read
     * @return
     */
private final Pair<Integer, Integer> calculateQueryRange(final GATKRead read) {
    int queryStart = -1, queryStop = -1;
    int readI = 0;
    // iterate over the cigar elements to determine the start and stop of the read bases for the BAQ calculation
    for (CigarElement elt : read.getCigarElements()) {
        switch(elt.getOperator()) {
            // cannot handle these
            case N:
                return null;
            // ignore pads, hard clips, and deletions
            case H:
            // ignore pads, hard clips, and deletions
            case P:
            // ignore pads, hard clips, and deletions
            case D:
                break;
            case I:
            case S:
            case M:
            case EQ:
            case X:
                int prev = readI;
                readI += elt.getLength();
                if (elt.getOperator() != CigarOperator.S) {
                    if (queryStart == -1) {
                        queryStart = prev;
                    }
                    queryStop = readI;
                }
                // queryStart or queryStop
                break;
            default:
                throw new GATKException("BUG: Unexpected CIGAR element " + elt + " in read " + read.getName());
        }
    }
    if (queryStop == queryStart) {
        //System.err.printf("WARNING -- read is completely clipped away: " + read.format());
        return null;
    }
    return new MutablePair<>(queryStart, queryStop);
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) GATKException(org.broadinstitute.hellbender.exceptions.GATKException) CigarElement(htsjdk.samtools.CigarElement)

Aggregations

MutablePair (org.apache.commons.lang3.tuple.MutablePair)116 Pair (org.apache.commons.lang3.tuple.Pair)49 ArrayList (java.util.ArrayList)32 Test (org.junit.Test)32 HashMap (java.util.HashMap)29 Message (com.microsoft.azure.sdk.iot.device.Message)27 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)27 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)23 List (java.util.List)20 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)17 Map (java.util.Map)14 IOException (java.io.IOException)13 WalkPosition (org.openbw.bwapi4j.WalkPosition)9 MiniTile (bwem.tile.MiniTile)8 DeviceOperations (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations)8 AreaId (bwem.area.typedef.AreaId)7 Collectors (java.util.stream.Collectors)7 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)7 HashSet (java.util.HashSet)6 TileImpl (bwem.tile.TileImpl)5