Search in sources :

Example 26 with MutablePair

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

the class EventLogger method observe.

public MutablePair<CoreV1Event, V1Patch> observe(CoreV1Event event, String key) {
    OffsetDateTime now = OffsetDateTime.now();
    EventLog lastObserved = this.eventCache.getIfPresent(key);
    V1Patch patch = null;
    if (lastObserved != null && lastObserved.count != null && lastObserved.count > 0) {
        event.setCount(lastObserved.count + 1);
        event.setFirstTimestamp(lastObserved.firstTimestamp);
        event.getMetadata().setName(lastObserved.name);
        event.getMetadata().setResourceVersion(lastObserved.resourceVersion);
        patch = buildEventPatch(event.getCount(), event.getMessage(), now);
    }
    EventLog log = new EventLog();
    log.count = event.getCount();
    log.firstTimestamp = event.getFirstTimestamp();
    log.name = event.getMetadata().getName();
    log.resourceVersion = event.getMetadata().getResourceVersion();
    this.eventCache.put(key, log);
    return new MutablePair<>(event, patch);
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) OffsetDateTime(java.time.OffsetDateTime) V1Patch(io.kubernetes.client.custom.V1Patch)

Example 27 with MutablePair

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

the class DeltaFIFO method keyOf.

// KeyOf exposes f's keyFunc, but also detects the key of a Deltas object or
// DeletedFinalStateUnknown objects.
private String keyOf(KubernetesObject obj) {
    KubernetesObject innerObj = obj;
    if (obj instanceof Deque) {
        Deque<MutablePair<DeltaType, KubernetesObject>> deltas = (Deque<MutablePair<DeltaType, KubernetesObject>>) obj;
        if (deltas.size() == 0) {
            throw new NoSuchElementException("0 length Deltas object; can't get key");
        }
        innerObj = deltas.peekLast().getRight();
    }
    if (innerObj instanceof DeletedFinalStateUnknown) {
        return ((DeletedFinalStateUnknown) innerObj).key;
    }
    return keyFunc.apply(innerObj);
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) KubernetesObject(io.kubernetes.client.common.KubernetesObject) Deque(java.util.Deque) NoSuchElementException(java.util.NoSuchElementException)

Example 28 with MutablePair

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

the class DeltaFIFO method syncKeyLocked.

/**
 * Add Sync delta. Caller must hold the lock.
 */
private void syncKeyLocked(String key) {
    KubernetesObject obj = this.knownObjects.getByKey(key);
    if (obj == null) {
        return;
    }
    String id = this.keyOf(obj);
    Deque<MutablePair<DeltaType, KubernetesObject>> deltas = this.items.get(id);
    if (deltas != null && !(CollectionUtils.isEmpty(deltas))) {
        return;
    }
    this.queueActionLocked(DeltaType.Sync, obj);
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) KubernetesObject(io.kubernetes.client.common.KubernetesObject)

Example 29 with MutablePair

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

the class DeltaFIFOTest method testDeltaFIFOBasic.

@Test
public void testDeltaFIFOBasic() throws InterruptedException {
    Deque<MutablePair<DeltaFIFO.DeltaType, KubernetesObject>> receivingDeltas = new LinkedList<>();
    V1Pod foo1 = new V1Pod().metadata(new V1ObjectMeta().name("foo1").namespace("default"));
    Cache cache = new Cache();
    DeltaFIFO deltaFIFO = new DeltaFIFO(Caches::deletionHandlingMetaNamespaceKeyFunc, cache);
    MutablePair<DeltaFIFO.DeltaType, KubernetesObject> receivingDelta;
    // basic add operation
    deltaFIFO.add(foo1);
    cache.add(foo1);
    deltaFIFO.pop((deltas) -> {
        MutablePair<DeltaFIFO.DeltaType, KubernetesObject> delta = deltas.peekFirst();
        receivingDeltas.add(delta);
    });
    receivingDelta = receivingDeltas.peekFirst();
    receivingDeltas.removeFirst();
    assertEquals(foo1, receivingDelta.getRight());
    assertEquals(DeltaFIFO.DeltaType.Added, receivingDelta.getLeft());
    // basic update operation
    deltaFIFO.update(foo1);
    cache.update(foo1);
    deltaFIFO.pop((deltas) -> {
        MutablePair<DeltaFIFO.DeltaType, KubernetesObject> delta = deltas.peekFirst();
        receivingDeltas.add(delta);
    });
    receivingDelta = receivingDeltas.peekFirst();
    receivingDeltas.removeFirst();
    assertEquals(foo1, receivingDelta.getRight());
    assertEquals(DeltaFIFO.DeltaType.Updated, receivingDelta.getLeft());
    // basic delete operation
    deltaFIFO.delete(foo1);
    cache.delete(foo1);
    deltaFIFO.pop((deltas) -> {
        MutablePair<DeltaFIFO.DeltaType, KubernetesObject> delta = deltas.peekFirst();
        receivingDeltas.add(delta);
    });
    receivingDelta = receivingDeltas.peekFirst();
    receivingDeltas.removeFirst();
    assertEquals(foo1, receivingDelta.getRight());
    assertEquals(DeltaFIFO.DeltaType.Deleted, receivingDelta.getLeft());
    // basic sync operation
    deltaFIFO.replace(Arrays.asList(foo1), "0");
    cache.replace(Arrays.asList(foo1), "0");
    deltaFIFO.pop((deltas) -> {
        MutablePair<DeltaFIFO.DeltaType, KubernetesObject> delta = deltas.peekFirst();
        receivingDeltas.add(delta);
    });
    receivingDelta = receivingDeltas.peekFirst();
    receivingDeltas.removeFirst();
    assertEquals(foo1, receivingDelta.getRight());
    assertEquals(DeltaFIFO.DeltaType.Sync, receivingDelta.getLeft());
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) KubernetesObject(io.kubernetes.client.common.KubernetesObject) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 30 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project BWAPI4J by OpenBW.

the class Graph method createChokePoints.

// ----------------------------------------------------------------------
// //////////////////////////////////////////////////////////////////////
// Creates a new Area for each pair (top, miniTiles) in AreasList (See Area::top() and
// Area::miniTiles())
public void createChokePoints(final List<StaticBuilding> staticBuildings, final List<Mineral> minerals, final List<MutablePair<MutablePair<AreaId, AreaId>, WalkPosition>> rawFrontier) {
    Index newIndex = new Index(0);
    final List<Neutral> blockingNeutrals = new ArrayList<>();
    for (final StaticBuilding s : staticBuildings) {
        if (s.isBlocking()) {
            blockingNeutrals.add(s);
        }
    }
    for (final Mineral m : minerals) {
        if (m.isBlocking()) {
            blockingNeutrals.add(m);
        }
    }
    // Note: pseudoChokePointsToCreate is only used for pre-allocating the GetChokePoints array
    // size.
    // This number will highly likely be very small. There is no reason to set a minimum size.
    // int pseudoChokePointsToCreate = 0;
    // for (final Neutral blockingNeutral : blockingNeutrals) {
    // if (blockingNeutral.getNextStacked() == null) {
    // ++pseudoChokePointsToCreate;
    // }
    // }
    // 1) size the matrix
    initializeChokePointsMatrix(this.chokePointsMatrix, getAreaCount());
    // 2) Dispatch the global raw frontier between all the relevant pairs of areas:
    final java.util.Map<MutablePair<AreaId, AreaId>, List<WalkPosition>> rawFrontierByAreaPair = createRawFrontierByAreaPairMap(rawFrontier);
    // 3) For each pair of areas (A, B):
    for (final java.util.Map.Entry<MutablePair<AreaId, AreaId>, List<WalkPosition>> entry : rawFrontierByAreaPair.entrySet()) {
        MutablePair<AreaId, AreaId> rawleft = entry.getKey();
        final List<WalkPosition> rawFrontierAB = entry.getValue();
        // Because our dispatching preserved order,
        // and because Map::m_RawFrontier was populated in descending order of the altitude (see
        // Map::computeAreas),
        // we know that rawFrontierAB is also ordered the same way, but let's check it:
        {
            final List<Altitude> altitudes = new ArrayList<>();
            for (final WalkPosition w : rawFrontierAB) {
                altitudes.add(getMap().getData().getMiniTile(w).getAltitude());
            }
            // bwem_assert(is_sorted(altitudes.rbegin(), altitudes.rend()));
            for (int i = 1; i < altitudes.size(); ++i) {
                final int prev = altitudes.get(i - 1).intValue();
                final int curr = altitudes.get(i).intValue();
                if (prev < curr) {
                    throw new IllegalStateException();
                }
            }
        }
        // 3.1) Use that information to efficiently cluster rawFrontierAB in one or several
        // chokepoints.
        // Each cluster will be populated starting with the center of a chokepoint (max altitude)
        // and finishing with the ends (min altitude).
        final int clusterMinDist = (int) Math.sqrt(BwemExt.lake_max_miniTiles);
        final List<List<WalkPosition>> clusters = new ArrayList<>();
        for (final WalkPosition w : rawFrontierAB) {
            boolean added = false;
            for (final List<WalkPosition> cluster : clusters) {
                final int distToFront = BwemExt.queenWiseDist(cluster.get(0), w);
                final int distToBack = BwemExt.queenWiseDist(cluster.get(cluster.size() - 1), w);
                if (Math.min(distToFront, distToBack) <= clusterMinDist) {
                    if (distToFront < distToBack) {
                        cluster.add(0, w);
                    } else {
                        cluster.add(w);
                    }
                    added = true;
                    break;
                }
            }
            if (!added) {
                final List<WalkPosition> list = new ArrayList<>();
                list.add(w);
                clusters.add(list);
            }
        }
        // 3.2) Create one Chokepoint for each cluster:
        final AreaId a = rawleft.getLeft();
        final AreaId b = rawleft.getRight();
        // getChokePoints(a, b).reserve(clusters.size() + pseudoChokePointsToCreate);
        for (final List<WalkPosition> cluster : clusters) {
            getChokePoints(a, b).add(new ChokePointImpl(this, newIndex, getArea(a), getArea(b), cluster));
            newIndex = newIndex.add(1);
        }
    }
    // 4) Create one Chokepoint for each pair of blocked areas, for each blocking Neutral:
    for (final Neutral blockingNeutral : blockingNeutrals) {
        if (blockingNeutral.getNextStacked() == null) {
            // in the case where several neutrals are stacked, we only consider the top
            final List<Area> blockedAreas = blockingNeutral.getBlockedAreas();
            for (final Area blockedAreaA : blockedAreas) for (final Area blockedAreaB : blockedAreas) {
                if (blockedAreaB.equals(blockedAreaA)) {
                    // breaks symmetry
                    break;
                }
                final WalkPosition center = getMap().breadthFirstSearch(blockingNeutral.getCenter().toWalkPosition(), // findCond
                args -> {
                    Object ttile = args[0];
                    if (!(ttile instanceof MiniTile)) {
                        throw new IllegalArgumentException();
                    }
                    MiniTile miniTile = (MiniTile) ttile;
                    return miniTile.isWalkable();
                }, // visitCond
                args -> true);
                final List<WalkPosition> list = new ArrayList<>();
                list.add(center);
                getChokePoints(blockedAreaA, blockedAreaB).add(new ChokePointImpl(this, newIndex, blockedAreaA, blockedAreaB, list, blockingNeutral));
                newIndex = newIndex.add(1);
            }
        }
    }
    // 5) Set the references to the freshly created Chokepoints:
    for (int loopA = 1; loopA <= getAreaCount(); ++loopA) for (int loopB = 1; loopB < loopA; ++loopB) {
        final AreaId a = new AreaId(loopA);
        final AreaId b = new AreaId(loopB);
        if (!getChokePoints(a, b).isEmpty()) {
            ((AreaInitializer) getArea(a)).addChokePoints(getArea(b), getChokePoints(a, b));
            ((AreaInitializer) getArea(b)).addChokePoints(getArea(a), getChokePoints(a, b));
            this.chokePoints.addAll(getChokePoints(a, b));
        }
    }
}
Also used : StaticBuilding(bwem.unit.StaticBuilding) Utils(bwem.util.Utils) Altitude(bwem.typedef.Altitude) MutableInt(org.apache.commons.lang3.mutable.MutableInt) PriorityQueue(java.util.PriorityQueue) Map(bwem.map.Map) HashMap(java.util.HashMap) TilePosition(org.openbw.bwapi4j.TilePosition) Pair(org.openbw.bwapi4j.util.Pair) ArrayList(java.util.ArrayList) TerrainData(bwem.map.TerrainData) Area(bwem.area.Area) MutablePair(org.apache.commons.lang3.tuple.MutablePair) GroupId(bwem.area.typedef.GroupId) AreaId(bwem.area.typedef.AreaId) CPPath(bwem.typedef.CPPath) Index(bwem.typedef.Index) StaticBuilding(bwem.unit.StaticBuilding) Position(org.openbw.bwapi4j.Position) Tile(bwem.tile.Tile) Neutral(bwem.unit.Neutral) AreaInitializer(bwem.area.AreaInitializer) WalkPosition(org.openbw.bwapi4j.WalkPosition) Mineral(bwem.unit.Mineral) TileImpl(bwem.tile.TileImpl) List(java.util.List) Geyser(bwem.unit.Geyser) AreaInitializerImpl(bwem.area.AreaInitializerImpl) BwemExt(bwem.util.BwemExt) Queue(java.util.Queue) Comparator(java.util.Comparator) MiniTile(bwem.tile.MiniTile) Neutral(bwem.unit.Neutral) ArrayList(java.util.ArrayList) AreaId(bwem.area.typedef.AreaId) MiniTile(bwem.tile.MiniTile) Index(bwem.typedef.Index) MutablePair(org.apache.commons.lang3.tuple.MutablePair) ArrayList(java.util.ArrayList) List(java.util.List) Mineral(bwem.unit.Mineral) Area(bwem.area.Area) WalkPosition(org.openbw.bwapi4j.WalkPosition) Map(bwem.map.Map) HashMap(java.util.HashMap)

Aggregations

MutablePair (org.apache.commons.lang3.tuple.MutablePair)113 Pair (org.apache.commons.lang3.tuple.Pair)46 Test (org.junit.Test)32 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)28 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)19 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 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)7 HashSet (java.util.HashSet)6 TileImpl (bwem.tile.TileImpl)5 Mineral (bwem.unit.Mineral)5