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());
}
}
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);
}
}
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())));
}
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;
}
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);
}
Aggregations