Search in sources :

Example 6 with T2

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

the class GridQueryProcessor method prepareChangeOnNotStartedCache.

/**
     * Prepare operation on non-started cache.
     *
     * @param op Operation.
     * @param schema Known cache schema.
     * @return Result: nop flag, error.
     */
private T2<Boolean, SchemaOperationException> prepareChangeOnNotStartedCache(SchemaAbstractOperation op, QuerySchema schema) {
    boolean nop = false;
    SchemaOperationException err = null;
    // Build table and index maps.
    Map<String, QueryEntity> tblMap = new HashMap<>();
    Map<String, T2<QueryEntity, QueryIndex>> idxMap = new HashMap<>();
    for (QueryEntity entity : schema.entities()) {
        String tblName = entity.getTableName();
        QueryEntity oldEntity = tblMap.put(tblName, entity);
        if (oldEntity != null) {
            err = new SchemaOperationException("Invalid schema state (duplicate table found): " + tblName);
            break;
        }
        for (QueryIndex entityIdx : entity.getIndexes()) {
            String idxName = entityIdx.getName();
            T2<QueryEntity, QueryIndex> oldIdxEntity = idxMap.put(idxName, new T2<>(entity, entityIdx));
            if (oldIdxEntity != null) {
                err = new SchemaOperationException("Invalid schema state (duplicate index found): " + idxName);
                break;
            }
        }
        if (err != null)
            break;
    }
    // Now check whether operation can be applied to schema.
    if (op instanceof SchemaIndexCreateOperation) {
        SchemaIndexCreateOperation op0 = (SchemaIndexCreateOperation) op;
        String idxName = op0.indexName();
        T2<QueryEntity, QueryIndex> oldIdxEntity = idxMap.get(idxName);
        if (oldIdxEntity == null) {
            String tblName = op0.tableName();
            QueryEntity oldEntity = tblMap.get(tblName);
            if (oldEntity == null)
                err = new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, tblName);
            else {
                for (String fieldName : op0.index().getFields().keySet()) {
                    Set<String> oldEntityFields = new HashSet<>(oldEntity.getFields().keySet());
                    for (Map.Entry<String, String> alias : oldEntity.getAliases().entrySet()) {
                        oldEntityFields.remove(alias.getKey());
                        oldEntityFields.add(alias.getValue());
                    }
                    if (!oldEntityFields.contains(fieldName)) {
                        err = new SchemaOperationException(SchemaOperationException.CODE_COLUMN_NOT_FOUND, fieldName);
                        break;
                    }
                }
            }
        } else {
            if (op0.ifNotExists())
                nop = true;
            else
                err = new SchemaOperationException(SchemaOperationException.CODE_INDEX_EXISTS, idxName);
        }
    } else if (op instanceof SchemaIndexDropOperation) {
        SchemaIndexDropOperation op0 = (SchemaIndexDropOperation) op;
        String idxName = op0.indexName();
        T2<QueryEntity, QueryIndex> oldIdxEntity = idxMap.get(idxName);
        if (oldIdxEntity == null) {
            if (op0.ifExists())
                nop = true;
            else
                err = new SchemaOperationException(SchemaOperationException.CODE_INDEX_NOT_FOUND, idxName);
        }
    } else
        err = new SchemaOperationException("Unsupported operation: " + op);
    return new T2<>(nop, err);
}
Also used : SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SchemaIndexCreateOperation(org.apache.ignite.internal.processors.query.schema.operation.SchemaIndexCreateOperation) SchemaIndexDropOperation(org.apache.ignite.internal.processors.query.schema.operation.SchemaIndexDropOperation) QueryEntity(org.apache.ignite.cache.QueryEntity) QueryIndex(org.apache.ignite.cache.QueryIndex) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) T2(org.apache.ignite.internal.util.typedef.T2) HashSet(java.util.HashSet) GridBoundedConcurrentLinkedHashSet(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashSet)

Example 7 with T2

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

the class IgniteCachePeekModesAbstractTest method offheapKeysCount.

/**
     * @param nodeIdx Node index.
     * @param part Cache partition.
     * @return Tuple with number of primary and backup keys (one or both will be zero).
     */
private T2<Integer, Integer> offheapKeysCount(int nodeIdx, int part) throws IgniteCheckedException {
    GridCacheContext ctx = ((IgniteEx) ignite(nodeIdx)).context().cache().internalCache(DEFAULT_CACHE_NAME).context();
    // Swap and offheap are disabled for near cache.
    IgniteCacheOffheapManager offheapManager = ctx.isNear() ? ctx.near().dht().context().offheap() : ctx.offheap();
    //First count entries...
    int cnt = (int) offheapManager.entriesCount(part);
    GridCacheAffinityManager affinity = ctx.affinity();
    AffinityTopologyVersion topVer = affinity.affinityTopologyVersion();
    //And then find out whether they are primary or backup ones.
    int primaryCnt = 0;
    int backupCnt = 0;
    if (affinity.primaryByPartition(ctx.localNode(), part, topVer))
        primaryCnt = cnt;
    else if (affinity.backupByPartition(ctx.localNode(), part, topVer))
        backupCnt = cnt;
    return new T2<>(primaryCnt, backupCnt);
}
Also used : AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) T2(org.apache.ignite.internal.util.typedef.T2)

Example 8 with T2

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

the class IgniteCachePeekModesAbstractTest method swapKeys.

/**
     * @param nodeIdx Node index.
     * @return Tuple with primary and backup keys.
     */
private T2<List<Integer>, List<Integer>> swapKeys(int nodeIdx) {
    // TODO: GG-11148.
    //        SwapSpaceSpi swap = ignite(nodeIdx).configuration().getSwapSpaceSpi();
    //
    //        IgniteSpiCloseableIterator<KeyCacheObject> it = swap.keyIterator(SPACE_NAME, null);
    IgniteSpiCloseableIterator<KeyCacheObject> it = new GridEmptyCloseableIterator<>();
    assertNotNull(it);
    Affinity aff = ignite(nodeIdx).affinity(DEFAULT_CACHE_NAME);
    ClusterNode node = ignite(nodeIdx).cluster().localNode();
    List<Integer> primary = new ArrayList<>();
    List<Integer> backups = new ArrayList<>();
    CacheObjectContext coctx = ((IgniteEx) ignite(nodeIdx)).context().cache().internalCache(DEFAULT_CACHE_NAME).context().cacheObjectContext();
    while (it.hasNext()) {
        Integer key = it.next().value(coctx, false);
        if (aff.isPrimary(node, key))
            primary.add(key);
        else {
            assertTrue(aff.isBackup(node, key));
            backups.add(key);
        }
    }
    return new T2<>(primary, backups);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteEx(org.apache.ignite.internal.IgniteEx) ArrayList(java.util.ArrayList) GridEmptyCloseableIterator(org.apache.ignite.internal.util.GridEmptyCloseableIterator) Affinity(org.apache.ignite.cache.affinity.Affinity) T2(org.apache.ignite.internal.util.typedef.T2)

Example 9 with T2

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

the class GridCacheNearMultiNodeSelfTest method mapKeys.

/** @param cnt Count. */
private Map<UUID, T2<Set<Integer>, Set<Integer>>> mapKeys(int cnt) {
    Affinity<Object> aff = affinity(0);
    //Mapping primary and backup keys on node
    Map<UUID, T2<Set<Integer>, Set<Integer>>> map = new HashMap<>();
    for (int i = 0; i < GRID_CNT; i++) {
        IgniteEx grid = grid(i);
        map.put(grid.cluster().localNode().id(), new T2<Set<Integer>, Set<Integer>>(new HashSet<Integer>(), new HashSet<Integer>()));
    }
    for (int key = 1; key <= cnt; key++) {
        Integer part = aff.partition(key);
        assert part != null;
        Collection<ClusterNode> nodes = aff.mapPartitionToPrimaryAndBackups(part);
        ClusterNode primary = F.first(nodes);
        map.get(primary.id()).get1().add(key);
        if (mapDebug)
            info("Mapped key to primary node [key=" + key + ", node=" + U.toShortString(primary));
        for (ClusterNode n : nodes) {
            if (n != primary) {
                map.get(n.id()).get2().add(key);
                if (mapDebug)
                    info("Mapped key to backup node [key=" + key + ", node=" + U.toShortString(n));
            }
        }
    }
    return map;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx) UUID(java.util.UUID) T2(org.apache.ignite.internal.util.typedef.T2) HashSet(java.util.HashSet)

Example 10 with T2

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

the class GridCacheNearMultiNodeSelfTest method testReadThrough.

/** @throws Exception If failed. */
public void testReadThrough() throws Exception {
    ClusterNode loc = grid(0).localNode();
    info("Local node: " + U.toShortString(loc));
    IgniteCache<Integer, String> near = jcache(0);
    int cnt = 10;
    Map<UUID, T2<Set<Integer>, Set<Integer>>> mapKeys = mapKeys(cnt);
    for (int key = 1; key <= cnt; key++) {
        String s = near.get(key);
        info("Read key [key=" + key + ", val=" + s + ']');
        assert s != null;
    }
    info("Read all keys.");
    for (int key = 1; key <= cnt; key++) {
        ClusterNode n = primaryNode(key);
        info("Primary node for key [key=" + key + ", node=" + U.toShortString(n) + ']');
        assert n != null;
        assert mapKeys.get(n.id()).get1().contains(key);
        GridCacheAdapter<Integer, String> dhtCache = dht(G.ignite(n.id()));
        String s = dhtCache.localPeek(key, null, null);
        assert s != null : "Value is null for key: " + key;
        assertEquals(s, Integer.toString(key));
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UUID(java.util.UUID) 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