Search in sources :

Example 21 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class CacheScanPartitionQueryFallbackSelfTest method remotePartition.

/**
     * @param cctx Cctx.
     * @return Remote partition.
     */
private IgniteBiTuple<Integer, UUID> remotePartition(final GridCacheContext cctx) {
    ClusterNode node = F.first(cctx.kernalContext().grid().cluster().forRemotes().nodes());
    GridCacheAffinityManager affMgr = cctx.affinity();
    AffinityTopologyVersion topVer = affMgr.affinityTopologyVersion();
    Set<Integer> parts = affMgr.primaryPartitions(node.id(), topVer);
    return new IgniteBiTuple<>(F.first(parts), node.id());
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple)

Example 22 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class IgniteCacheClientNodeChangingTopologyTest method findKeys.

/**
     * Tries to find keys for two partitions: for one partition assignment should not change after node join,
     * for another primary node should change.
     *
     * @param ignite Ignite.
     * @param nodes Current nodes.
     * @return Found keys.
     */
private IgniteBiTuple<Integer, Integer> findKeys(Ignite ignite, ClusterNode... nodes) {
    ClusterNode newNode = new TcpDiscoveryNode();
    GridTestUtils.setFieldValue(newNode, "consistentId", getTestIgniteInstanceName(4));
    GridTestUtils.setFieldValue(newNode, "id", UUID.randomUUID());
    List<ClusterNode> topNodes = new ArrayList<>();
    Collections.addAll(topNodes, nodes);
    topNodes.add(newNode);
    DiscoveryEvent discoEvt = new DiscoveryEvent(newNode, "", EventType.EVT_NODE_JOINED, newNode);
    final long topVer = ignite.cluster().topologyVersion();
    GridAffinityFunctionContextImpl ctx = new GridAffinityFunctionContextImpl(topNodes, null, discoEvt, new AffinityTopologyVersion(topVer + 1), 1);
    AffinityFunction affFunc = ignite.cache(DEFAULT_CACHE_NAME).getConfiguration(CacheConfiguration.class).getAffinity();
    List<List<ClusterNode>> newAff = affFunc.assignPartitions(ctx);
    List<List<ClusterNode>> curAff = ((IgniteKernal) ignite).context().cache().internalCache(DEFAULT_CACHE_NAME).context().affinity().assignments(new AffinityTopologyVersion(topVer));
    Integer key1 = null;
    Integer key2 = null;
    Affinity<Integer> aff = ignite.affinity(DEFAULT_CACHE_NAME);
    for (int i = 0; i < curAff.size(); i++) {
        if (key1 == null) {
            List<ClusterNode> oldNodes = curAff.get(i);
            List<ClusterNode> newNodes = newAff.get(i);
            if (oldNodes.equals(newNodes))
                key1 = findKey(aff, i);
        }
        if (key2 == null) {
            ClusterNode oldPrimary = F.first(curAff.get(i));
            ClusterNode newPrimary = F.first(newAff.get(i));
            if (!oldPrimary.equals(newPrimary))
                key2 = findKey(aff, i);
        }
        if (key1 != null && key2 != null)
            break;
    }
    if (key1 == null || key2 == null)
        fail("Failed to find nodes required for test.");
    return new IgniteBiTuple<>(key1, key2);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ArrayList(java.util.ArrayList) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridAffinityFunctionContextImpl(org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl) List(java.util.List) ArrayList(java.util.ArrayList) AffinityFunction(org.apache.ignite.cache.affinity.AffinityFunction) TcpDiscoveryNode(org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 23 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class DmlStatementsProcessor method doInsert.

/**
     * Execute INSERT statement plan.
     * @param cursor Cursor to take inserted data from.
     * @param pageSize Batch size for streaming, anything <= 0 for single page operations.
     * @return Number of items affected.
     * @throws IgniteCheckedException if failed, particularly in case of duplicate keys.
     */
@SuppressWarnings({ "unchecked", "ConstantConditions" })
private long doInsert(UpdatePlan plan, Iterable<List<?>> cursor, int pageSize) throws IgniteCheckedException {
    GridH2RowDescriptor desc = plan.tbl.rowDescriptor();
    GridCacheContext cctx = desc.context();
    // If we have just one item to put, just do so
    if (plan.rowsNum == 1) {
        IgniteBiTuple t = rowToKeyValue(cctx, cursor.iterator().next(), plan);
        if (cctx.cache().putIfAbsent(t.getKey(), t.getValue()))
            return 1;
        else
            throw new IgniteSQLException("Duplicate key during INSERT [key=" + t.getKey() + ']', IgniteQueryErrorCode.DUPLICATE_KEY);
    } else {
        Map<Object, EntryProcessor<Object, Object, Boolean>> rows = plan.isLocSubqry ? new LinkedHashMap<Object, EntryProcessor<Object, Object, Boolean>>(plan.rowsNum) : new LinkedHashMap<Object, EntryProcessor<Object, Object, Boolean>>();
        // Keys that failed to INSERT due to duplication.
        List<Object> duplicateKeys = new ArrayList<>();
        int resCnt = 0;
        SQLException resEx = null;
        Iterator<List<?>> it = cursor.iterator();
        while (it.hasNext()) {
            List<?> row = it.next();
            final IgniteBiTuple t = rowToKeyValue(cctx, row, plan);
            rows.put(t.getKey(), new InsertEntryProcessor(t.getValue()));
            if (!it.hasNext() || (pageSize > 0 && rows.size() == pageSize)) {
                PageProcessingResult pageRes = processPage(cctx, rows);
                resCnt += pageRes.cnt;
                duplicateKeys.addAll(F.asList(pageRes.errKeys));
                if (pageRes.ex != null) {
                    if (resEx == null)
                        resEx = pageRes.ex;
                    else
                        resEx.setNextException(pageRes.ex);
                }
                rows.clear();
            }
        }
        if (!F.isEmpty(duplicateKeys)) {
            String msg = "Failed to INSERT some keys because they are already in cache " + "[keys=" + duplicateKeys + ']';
            SQLException dupEx = new SQLException(msg, null, IgniteQueryErrorCode.DUPLICATE_KEY);
            if (resEx == null)
                resEx = dupEx;
            else
                resEx.setNextException(dupEx);
        }
        if (resEx != null)
            throw new IgniteSQLException(resEx);
        return resCnt;
    }
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) ArrayList(java.util.ArrayList) EntryProcessor(javax.cache.processor.EntryProcessor) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) BinaryObject(org.apache.ignite.binary.BinaryObject) List(java.util.List) ArrayList(java.util.ArrayList)

Example 24 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class DmlStatementsProcessor method streamUpdateQuery.

/**
     * Perform given statement against given data streamer. Only rows based INSERT and MERGE are supported
     * as well as key bound UPDATE and DELETE (ones with filter {@code WHERE _key = ?}).
     *
     * @param streamer Streamer to feed data to.
     * @param stmt Statement.
     * @param args Statement arguments.
     * @return Number of rows in given statement for INSERT and MERGE, {@code 1} otherwise.
     * @throws IgniteCheckedException if failed.
     */
@SuppressWarnings({ "unchecked", "ConstantConditions" })
long streamUpdateQuery(IgniteDataStreamer streamer, PreparedStatement stmt, Object[] args) throws IgniteCheckedException {
    args = U.firstNotNull(args, X.EMPTY_OBJECT_ARRAY);
    Prepared p = GridSqlQueryParser.prepared(stmt);
    assert p != null;
    UpdatePlan plan = UpdatePlanBuilder.planForStatement(p, null);
    if (!F.eq(streamer.cacheName(), plan.tbl.rowDescriptor().context().name()))
        throw new IgniteSQLException("Cross cache streaming is not supported, please specify cache explicitly" + " in connection options", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
    if (plan.mode == UpdateMode.INSERT && plan.rowsNum > 0) {
        assert plan.isLocSubqry;
        final GridCacheContext cctx = plan.tbl.rowDescriptor().context();
        QueryCursorImpl<List<?>> cur;
        final ArrayList<List<?>> data = new ArrayList<>(plan.rowsNum);
        final GridQueryFieldsResult res = idx.queryLocalSqlFields(idx.schema(cctx.name()), plan.selectQry, F.asList(args), null, false, 0, null);
        QueryCursorImpl<List<?>> stepCur = new QueryCursorImpl<>(new Iterable<List<?>>() {

            @Override
            public Iterator<List<?>> iterator() {
                try {
                    return new GridQueryCacheObjectsIterator(res.iterator(), idx.objectContext(), cctx.keepBinary());
                } catch (IgniteCheckedException e) {
                    throw new IgniteException(e);
                }
            }
        }, null);
        data.addAll(stepCur.getAll());
        cur = new QueryCursorImpl<>(new Iterable<List<?>>() {

            @Override
            public Iterator<List<?>> iterator() {
                return data.iterator();
            }
        }, null);
        if (plan.rowsNum == 1) {
            IgniteBiTuple t = rowToKeyValue(cctx, cur.iterator().next(), plan);
            streamer.addData(t.getKey(), t.getValue());
            return 1;
        }
        Map<Object, Object> rows = new LinkedHashMap<>(plan.rowsNum);
        for (List<?> row : cur) {
            final IgniteBiTuple t = rowToKeyValue(cctx, row, plan);
            rows.put(t.getKey(), t.getValue());
        }
        streamer.addData(rows);
        return rows.size();
    } else
        throw new IgniteSQLException("Only tuple based INSERT statements are supported in streaming mode", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Prepared(org.h2.command.Prepared) ArrayList(java.util.ArrayList) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) GridQueryFieldsResult(org.apache.ignite.internal.processors.query.GridQueryFieldsResult) LinkedHashMap(java.util.LinkedHashMap) GridBoundedConcurrentLinkedHashMap(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashMap) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) IgniteSingletonIterator(org.apache.ignite.internal.util.lang.IgniteSingletonIterator) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) BinaryObject(org.apache.ignite.binary.BinaryObject) UpdatePlan(org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan)

Example 25 with IgniteBiTuple

use of org.apache.ignite.lang.IgniteBiTuple in project ignite by apache.

the class DmlStatementsProcessor method doMerge.

/**
     * Execute MERGE statement plan.
     * @param cursor Cursor to take inserted data from.
     * @param pageSize Batch size to stream data from {@code cursor}, anything <= 0 for single page operations.
     * @return Number of items affected.
     * @throws IgniteCheckedException if failed.
     */
@SuppressWarnings("unchecked")
private long doMerge(UpdatePlan plan, Iterable<List<?>> cursor, int pageSize) throws IgniteCheckedException {
    GridH2RowDescriptor desc = plan.tbl.rowDescriptor();
    GridCacheContext cctx = desc.context();
    // If we have just one item to put, just do so
    if (plan.rowsNum == 1) {
        IgniteBiTuple t = rowToKeyValue(cctx, cursor.iterator().next(), plan);
        cctx.cache().put(t.getKey(), t.getValue());
        return 1;
    } else {
        int resCnt = 0;
        Map<Object, Object> rows = new LinkedHashMap<>();
        for (Iterator<List<?>> it = cursor.iterator(); it.hasNext(); ) {
            List<?> row = it.next();
            IgniteBiTuple t = rowToKeyValue(cctx, row, plan);
            rows.put(t.getKey(), t.getValue());
            if ((pageSize > 0 && rows.size() == pageSize) || !it.hasNext()) {
                cctx.cache().putAll(rows);
                resCnt += rows.size();
                if (it.hasNext())
                    rows.clear();
            }
        }
        return resCnt;
    }
}
Also used : GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) BinaryObject(org.apache.ignite.binary.BinaryObject) List(java.util.List) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) GridBoundedConcurrentLinkedHashMap(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashMap)

Aggregations

IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)64 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)35 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 IgniteException (org.apache.ignite.IgniteException)17 ArrayList (java.util.ArrayList)14 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)13 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)13 ClusterNode (org.apache.ignite.cluster.ClusterNode)11 HashMap (java.util.HashMap)10 UUID (java.util.UUID)10 IOException (java.io.IOException)9 List (java.util.List)9 Map (java.util.Map)8 CacheStorePartialUpdateException (org.apache.ignite.internal.processors.cache.CacheStorePartialUpdateException)8 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)8 LinkedHashMap (java.util.LinkedHashMap)7 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Event (org.apache.ignite.events.Event)6 IgfsPath (org.apache.ignite.igfs.IgfsPath)6