Search in sources :

Example 1 with T2

use of org.apache.ignite.internal.util.typedef.T2 in project ignite by apache.

the class IgnitionEx method start0.

/**
     * Starts grid with given configuration.
     *
     * @param startCtx Start context.
     * @param failIfStarted Throw or not an exception if grid is already started.
     * @return Tuple with: grid instance and flag to indicate the instance is started by this call.
     *      So, when the new ignite instance is started the flag is {@code true}. If an existing instance is returned
     *      the flag is {@code false}.
     * @throws IgniteCheckedException If grid could not be started.
     */
private static T2<IgniteNamedInstance, Boolean> start0(GridStartContext startCtx, boolean failIfStarted) throws IgniteCheckedException {
    assert startCtx != null;
    String name = startCtx.config().getIgniteInstanceName();
    if (name != null && name.isEmpty())
        throw new IgniteCheckedException("Non default Ignite instances cannot have empty string name.");
    IgniteNamedInstance grid = new IgniteNamedInstance(name);
    IgniteNamedInstance old;
    if (name != null)
        old = grids.putIfAbsent(name, grid);
    else {
        synchronized (dfltGridMux) {
            old = dfltGrid;
            if (old == null)
                dfltGrid = grid;
        }
    }
    if (old != null)
        if (failIfStarted) {
            if (name == null)
                throw new IgniteCheckedException("Default Ignite instance has already been started.");
            else
                throw new IgniteCheckedException("Ignite instance with this name has already been started: " + name);
        } else
            return new T2<>(old, false);
    if (startCtx.config().getWarmupClosure() != null)
        startCtx.config().getWarmupClosure().apply(startCtx.config());
    startCtx.single(grids.size() == 1);
    boolean success = false;
    try {
        try {
            grid.start(startCtx);
        } catch (IgniteInterruptedCheckedException e) {
            if (grid.starterThreadInterrupted)
                Thread.interrupted();
            throw e;
        }
        notifyStateChange(name, STARTED);
        success = true;
    } finally {
        if (!success) {
            if (name != null)
                grids.remove(name, grid);
            else {
                synchronized (dfltGridMux) {
                    if (dfltGrid == grid)
                        dfltGrid = null;
                }
            }
            grid = null;
        }
    }
    if (grid == null)
        throw new IgniteCheckedException("Failed to start grid with provided configuration.");
    return new T2<>(grid, true);
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) T2(org.apache.ignite.internal.util.typedef.T2)

Example 2 with T2

use of org.apache.ignite.internal.util.typedef.T2 in project ignite by apache.

the class GridCachePartitionExchangeManager method createPartitionsSingleMessage.

/**
     * @param targetNode Target node.
     * @param exchangeId ID.
     * @param clientOnlyExchange Client exchange flag.
     * @param sndCounters {@code True} if need send partition update counters.
     * @return Message.
     */
public GridDhtPartitionsSingleMessage createPartitionsSingleMessage(ClusterNode targetNode, @Nullable GridDhtPartitionExchangeId exchangeId, boolean clientOnlyExchange, boolean sndCounters) {
    GridDhtPartitionsSingleMessage m = new GridDhtPartitionsSingleMessage(exchangeId, clientOnlyExchange, cctx.versions().last(), true);
    Map<Object, T2<Integer, Map<Integer, GridDhtPartitionState>>> dupData = new HashMap<>();
    for (GridCacheContext cacheCtx : cctx.cacheContexts()) {
        if (!cacheCtx.isLocal()) {
            GridDhtPartitionMap locMap = cacheCtx.topology().localPartitionMap();
            addPartitionMap(m, dupData, true, cacheCtx.cacheId(), locMap, cacheCtx.affinity().affinityCache().similarAffinityKey());
            if (sndCounters)
                m.partitionUpdateCounters(cacheCtx.cacheId(), cacheCtx.topology().updateCounters(true));
        }
    }
    for (GridClientPartitionTopology top : clientTops.values()) {
        if (m.partitions() != null && m.partitions().containsKey(top.cacheId()))
            continue;
        GridDhtPartitionMap locMap = top.localPartitionMap();
        addPartitionMap(m, dupData, true, top.cacheId(), locMap, top.similarAffinityKey());
        if (sndCounters)
            m.partitionUpdateCounters(top.cacheId(), top.updateCounters(true));
    }
    return m;
}
Also used : GridDhtPartitionMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionMap) GridDhtPartitionsSingleMessage(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsSingleMessage) HashMap(java.util.HashMap) GridDhtPartitionState(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtPartitionState) GridTimeoutObject(org.apache.ignite.internal.processors.timeout.GridTimeoutObject) T2(org.apache.ignite.internal.util.typedef.T2) GridClientPartitionTopology(org.apache.ignite.internal.processors.cache.distributed.dht.GridClientPartitionTopology)

Example 3 with T2

use of org.apache.ignite.internal.util.typedef.T2 in project ignite by apache.

the class GridDhtPartitionTopologyImpl method update.

/** {@inheritDoc} */
@SuppressWarnings({ "MismatchedQueryAndUpdateOfCollection" })
@Override
public GridDhtPartitionMap update(@Nullable GridDhtPartitionExchangeId exchId, GridDhtPartitionFullMap partMap, @Nullable Map<Integer, T2<Long, Long>> cntrMap) {
    if (log.isDebugEnabled())
        log.debug("Updating full partition map [exchId=" + exchId + ", parts=" + fullMapString() + ']');
    assert partMap != null;
    lock.writeLock().lock();
    try {
        if (stopping)
            return null;
        if (cntrMap != null) {
            // update local map partition counters
            for (Map.Entry<Integer, T2<Long, Long>> e : cntrMap.entrySet()) {
                T2<Long, Long> cntr = this.cntrMap.get(e.getKey());
                if (cntr == null || cntr.get2() < e.getValue().get2())
                    this.cntrMap.put(e.getKey(), e.getValue());
            }
            // update local counters in partitions
            for (int i = 0; i < locParts.length(); i++) {
                GridDhtLocalPartition part = locParts.get(i);
                if (part == null)
                    continue;
                T2<Long, Long> cntr = cntrMap.get(part.id());
                if (cntr != null)
                    part.updateCounter(cntr.get2());
            }
        }
        //if need skip
        if (exchId != null && lastExchangeId != null && lastExchangeId.compareTo(exchId) >= 0) {
            if (log.isDebugEnabled())
                log.debug("Stale exchange id for full partition map update (will ignore) [lastExchId=" + lastExchangeId + ", exchId=" + exchId + ']');
            return null;
        }
        if (node2part != null && node2part.compareTo(partMap) >= 0) {
            if (log.isDebugEnabled())
                log.debug("Stale partition map for full partition map update (will ignore) [lastExchId=" + lastExchangeId + ", exchId=" + exchId + ", curMap=" + node2part + ", newMap=" + partMap + ']');
            return null;
        }
        long updateSeq = this.updateSeq.incrementAndGet();
        if (exchId != null)
            lastExchangeId = exchId;
        if (node2part != null) {
            for (GridDhtPartitionMap part : node2part.values()) {
                GridDhtPartitionMap newPart = partMap.get(part.nodeId());
                // then we keep the newer value.
                if (newPart != null && (newPart.updateSequence() < part.updateSequence() || (cctx.startTopologyVersion().compareTo(newPart.topologyVersion()) > 0))) {
                    if (log.isDebugEnabled())
                        log.debug("Overriding partition map in full update map [exchId=" + exchId + ", curPart=" + mapString(part) + ", newPart=" + mapString(newPart) + ']');
                    partMap.put(part.nodeId(), part);
                }
            }
            // Remove entry if node left.
            for (Iterator<UUID> it = partMap.keySet().iterator(); it.hasNext(); ) {
                UUID nodeId = it.next();
                if (!cctx.discovery().alive(nodeId)) {
                    if (log.isDebugEnabled())
                        log.debug("Removing left node from full map update [nodeId=" + nodeId + ", partMap=" + partMap + ']');
                    it.remove();
                }
            }
        }
        node2part = partMap;
        Map<Integer, Set<UUID>> p2n = new HashMap<>(cctx.affinity().partitions(), 1.0f);
        for (Map.Entry<UUID, GridDhtPartitionMap> e : partMap.entrySet()) {
            for (Integer p : e.getValue().keySet()) {
                Set<UUID> ids = p2n.get(p);
                if (ids == null)
                    // Initialize HashSet to size 3 in anticipation that there won't be
                    // more than 3 nodes per partitions.
                    p2n.put(p, ids = U.newHashSet(3));
                ids.add(e.getKey());
            }
        }
        part2node = p2n;
        boolean changed = false;
        AffinityTopologyVersion affVer = cctx.affinity().affinityTopologyVersion();
        GridDhtPartitionMap nodeMap = partMap.get(cctx.localNodeId());
        if (nodeMap != null && cctx.shared().database().persistenceEnabled()) {
            for (Map.Entry<Integer, GridDhtPartitionState> e : nodeMap.entrySet()) {
                int p = e.getKey();
                GridDhtPartitionState state = e.getValue();
                if (state == MOVING) {
                    GridDhtLocalPartition locPart = locParts.get(p);
                    assert locPart != null;
                    if (locPart.state() == OWNING) {
                        locPart.moving();
                        changed = true;
                    }
                    if (cntrMap != null) {
                        T2<Long, Long> cntr = cntrMap.get(p);
                        if (cntr != null && cntr.get2() > locPart.updateCounter())
                            locPart.updateCounter(cntr.get2());
                    }
                }
            }
        }
        if (!affVer.equals(AffinityTopologyVersion.NONE) && affVer.compareTo(topVer) >= 0) {
            List<List<ClusterNode>> aff = cctx.affinity().assignments(topVer);
            changed |= checkEvictions(updateSeq, aff);
            updateRebalanceVersion(aff);
        }
        consistencyCheck();
        if (log.isDebugEnabled())
            log.debug("Partition map after full update: " + fullMapString());
        if (changed)
            cctx.shared().exchange().scheduleResendPartitions();
        return changed ? localPartitionMap() : null;
    } finally {
        lock.writeLock().unlock();
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridDhtPartitionMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionMap) GridAtomicLong(org.apache.ignite.internal.util.GridAtomicLong) List(java.util.List) ArrayList(java.util.ArrayList) UUID(java.util.UUID) Map(java.util.Map) HashMap(java.util.HashMap) GridDhtPartitionFullMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionFullMap) GridDhtPartitionMap(org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionMap) T2(org.apache.ignite.internal.util.typedef.T2)

Example 4 with T2

use of org.apache.ignite.internal.util.typedef.T2 in project ignite by apache.

the class CacheContinuousQueryHandler method getOrCreatePartitionRecovery.

/**
     * @param ctx Context.
     * @param partId Partition id.
     * @param topVer Topology version for current operation.
     * @return Partition recovery.
     */
@NotNull
private CacheContinuousQueryPartitionRecovery getOrCreatePartitionRecovery(GridKernalContext ctx, int partId, AffinityTopologyVersion topVer) {
    assert topVer != null && topVer.topologyVersion() > 0 : topVer;
    CacheContinuousQueryPartitionRecovery rec = rcvs.get(partId);
    if (rec == null) {
        T2<Long, Long> partCntrs = null;
        Map<UUID, Map<Integer, T2<Long, Long>>> initUpdCntrsPerNode = this.initUpdCntrsPerNode;
        if (initUpdCntrsPerNode != null) {
            GridCacheContext<K, V> cctx = cacheContext(ctx);
            GridCacheAffinityManager aff = cctx.affinity();
            for (ClusterNode node : aff.nodesByPartition(partId, topVer)) {
                Map<Integer, T2<Long, Long>> map = initUpdCntrsPerNode.get(node.id());
                if (map != null) {
                    partCntrs = map.get(partId);
                    break;
                }
            }
        } else if (initUpdCntrs != null)
            partCntrs = initUpdCntrs.get(partId);
        rec = new CacheContinuousQueryPartitionRecovery(ctx.log(CU.CONTINUOUS_QRY_LOG_CATEGORY), topVer, partCntrs != null ? partCntrs.get2() : null);
        CacheContinuousQueryPartitionRecovery oldRec = rcvs.putIfAbsent(partId, rec);
        if (oldRec != null)
            rec = oldRec;
    }
    return rec;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridCacheAffinityManager(org.apache.ignite.internal.processors.cache.GridCacheAffinityManager) UUID(java.util.UUID) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) T2(org.apache.ignite.internal.util.typedef.T2) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with T2

use of org.apache.ignite.internal.util.typedef.T2 in project ignite by apache.

the class DataStructuresProcessor method removeDataStructure.

/**
     * @param c Closure.
     * @param name Data structure name.
     * @param type Data structure type.
     * @param afterRmv Optional closure to run after data structure removed.
     * @throws IgniteCheckedException If failed.
     */
private <T> void removeDataStructure(final IgniteOutClosureX<T> c, String name, DataStructureType type, @Nullable final IgniteInClosureX<T> afterRmv) throws IgniteCheckedException {
    Map<String, DataStructureInfo> dsMap = utilityCache.get(DATA_STRUCTURES_KEY);
    if (dsMap == null || !dsMap.containsKey(name))
        return;
    final DataStructureInfo dsInfo = new DataStructureInfo(name, type, null);
    IgniteCheckedException err = validateDataStructure(dsMap, dsInfo, false);
    if (err != null)
        throw err;
    retryTopologySafe(new IgniteOutClosureX<Void>() {

        @Override
        public Void applyx() throws IgniteCheckedException {
            try (GridNearTxLocal tx = utilityCache.txStartEx(PESSIMISTIC, REPEATABLE_READ)) {
                T2<Boolean, IgniteCheckedException> res = utilityCache.invoke(DATA_STRUCTURES_KEY, new RemoveDataStructureProcessor(dsInfo)).get();
                IgniteCheckedException err = res.get2();
                if (err != null)
                    throw err;
                assert res.get1() != null;
                boolean exists = res.get1();
                if (!exists)
                    return null;
                T rmvInfo = c.applyx();
                tx.commit();
                if (afterRmv != null && rmvInfo != null)
                    afterRmv.applyx(rmvInfo);
                return null;
            }
        }
    });
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) EVT_NODE_LEFT(org.apache.ignite.events.EventType.EVT_NODE_LEFT) SET(org.apache.ignite.internal.processors.datastructures.DataStructuresProcessor.DataStructureType.SET) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) T2(org.apache.ignite.internal.util.typedef.T2)

Aggregations

T2 (org.apache.ignite.internal.util.typedef.T2)64 ArrayList (java.util.ArrayList)25 HashMap (java.util.HashMap)24 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 Map (java.util.Map)15 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)15 ClusterNode (org.apache.ignite.cluster.ClusterNode)14 UUID (java.util.UUID)13 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)13 Ignite (org.apache.ignite.Ignite)13 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)12 List (java.util.List)11 HashSet (java.util.HashSet)10 ConcurrentMap (java.util.concurrent.ConcurrentMap)10 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 CacheException (javax.cache.CacheException)7 CacheEntryEvent (javax.cache.event.CacheEntryEvent)7 Set (java.util.Set)5