Search in sources :

Example 1 with Query

use of org.apache.ignite.cache.query.Query in project camel by apache.

the class IgniteCacheProducer method doQuery.

@SuppressWarnings("unchecked")
private void doQuery(Message in, Message out, Exchange exchange) {
    Query<Object> query = in.getHeader(IgniteConstants.IGNITE_CACHE_QUERY, Query.class);
    if (query == null) {
        try {
            query = in.getMandatoryBody(Query.class);
        } catch (InvalidPayloadException e) {
            exchange.setException(e);
            return;
        }
    }
    final QueryCursor<Object> cursor = cache.query(query);
    out.setBody(cursor.iterator());
    exchange.addOnCompletion(new Synchronization() {

        @Override
        public void onFailure(Exchange exchange) {
            cursor.close();
        }

        @Override
        public void onComplete(Exchange exchange) {
            cursor.close();
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) Query(org.apache.ignite.cache.query.Query) InvalidPayloadException(org.apache.camel.InvalidPayloadException) Synchronization(org.apache.camel.spi.Synchronization)

Example 2 with Query

use of org.apache.ignite.cache.query.Query in project ignite by apache.

the class IgniteRepositoryQuery method execute.

/**
 * {@inheritDoc}
 */
@Override
public Object execute(Object[] prmtrs) {
    Query qry = prepareQuery(prmtrs);
    QueryCursor qryCursor = cache.query(qry);
    return transformQueryCursor(prmtrs, qryCursor);
}
Also used : Query(org.apache.ignite.cache.query.Query) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) SqlQuery(org.apache.ignite.cache.query.SqlQuery) RepositoryQuery(org.springframework.data.repository.query.RepositoryQuery) QueryCursor(org.apache.ignite.cache.query.QueryCursor)

Example 3 with Query

use of org.apache.ignite.cache.query.Query in project ignite by apache.

the class ReliabilityTest method testFailover.

/**
 * Thin clint failover.
 */
@Test
public void testFailover() throws Exception {
    if (isPartitionAware())
        return;
    final int CLUSTER_SIZE = 3;
    try (LocalIgniteCluster cluster = LocalIgniteCluster.start(CLUSTER_SIZE);
        IgniteClient client = Ignition.startClient(getClientConfiguration().setReconnectThrottlingRetries(// Disable throttling.
        0).setAddresses(cluster.clientAddresses().toArray(new String[CLUSTER_SIZE])))) {
        final Random rnd = new Random();
        final ClientCache<Integer, String> cache = client.getOrCreateCache(new ClientCacheConfiguration().setName("testFailover").setCacheMode(CacheMode.REPLICATED));
        // Simple operation failover: put/get
        assertOnUnstableCluster(cluster, () -> {
            Integer key = rnd.nextInt();
            String val = key.toString();
            cachePut(cache, key, val);
            String cachedVal = cache.get(key);
            assertEquals(val, cachedVal);
        });
        cache.clear();
        // Composite operation failover: query
        Map<Integer, String> data = IntStream.rangeClosed(1, 1000).boxed().collect(Collectors.toMap(i -> i, i -> String.format("String %s", i)));
        assertOnUnstableCluster(cluster, () -> {
            cache.putAll(data);
            Query<Cache.Entry<Integer, String>> qry = new ScanQuery<Integer, String>().setPageSize(data.size() / 10);
            try {
                try (QueryCursor<Cache.Entry<Integer, String>> cur = cache.query(qry)) {
                    List<Cache.Entry<Integer, String>> res = cur.getAll();
                    assertEquals("Unexpected number of entries", data.size(), res.size());
                    Map<Integer, String> act = res.stream().collect(Collectors.toMap(Cache.Entry::getKey, Cache.Entry::getValue));
                    assertEquals("Unexpected entries", data, act);
                }
            } catch (ClientConnectionException ignored) {
            // QueryCursor.getAll always executes on the same channel where the cursor is open,
            // so failover is not possible, and the call will fail when connection drops.
            }
        });
        // Client fails if all nodes go down
        cluster.close();
        boolean igniteUnavailable = false;
        try {
            cachePut(cache, 1, "1");
        } catch (ClientConnectionException ex) {
            igniteUnavailable = true;
            Throwable[] suppressed = ex.getSuppressed();
            assertEquals(CLUSTER_SIZE - 1, suppressed.length);
            assertTrue(Stream.of(suppressed).allMatch(t -> t instanceof ClientConnectionException));
        }
        assertTrue(igniteUnavailable);
    }
}
Also used : IntStream(java.util.stream.IntStream) CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) Arrays(java.util.Arrays) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) U(org.apache.ignite.internal.util.typedef.internal.U) Random(java.util.Random) EVT_CACHE_OBJECT_READ(org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_READ) ClientServerError(org.apache.ignite.internal.client.thin.ClientServerError) Future(java.util.concurrent.Future) ServiceContext(org.apache.ignite.services.ServiceContext) Map(java.util.Map) Cache(javax.cache.Cache) EVTS_CACHE(org.apache.ignite.events.EventType.EVTS_CACHE) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) ServiceConfiguration(org.apache.ignite.services.ServiceConfiguration) EVT_CACHE_OBJECT_REMOVED(org.apache.ignite.events.EventType.EVT_CACHE_OBJECT_REMOVED) ClientOperation(org.apache.ignite.internal.client.thin.ClientOperation) CyclicBarrier(java.util.concurrent.CyclicBarrier) Query(org.apache.ignite.cache.query.Query) Iterator(java.util.Iterator) IgniteException(org.apache.ignite.IgniteException) FailureHandler(org.apache.ignite.failure.FailureHandler) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) AbstractThinClientTest(org.apache.ignite.internal.client.thin.AbstractThinClientTest) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Stream(java.util.stream.Stream) Ignition(org.apache.ignite.Ignition) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Service(org.apache.ignite.services.Service) ScanQuery(org.apache.ignite.cache.query.ScanQuery) CacheMode(org.apache.ignite.cache.CacheMode) Random(java.util.Random) Cache(javax.cache.Cache) Test(org.junit.Test) AbstractThinClientTest(org.apache.ignite.internal.client.thin.AbstractThinClientTest)

Example 4 with Query

use of org.apache.ignite.cache.query.Query in project ignite by apache.

the class GridReduceQueryExecutor method createReduceQueryRun.

/**
 * Query run factory method.
 *
 * @param conn H2 connection.
 * @param mapQueries Map queries.
 * @param nodes Target nodes.
 * @param pageSize Page size.
 * @param nodeToSegmentsCnt Segments per-index.
 * @param skipMergeTbl Skip merge table flag.
 * @param explain Explain query flag.
 * @param dataPageScanEnabled DataPage scan enabled flag.
 * @return Reduce query run.
 */
@NotNull
private ReduceQueryRun createReduceQueryRun(H2PooledConnection conn, List<GridCacheSqlQuery> mapQueries, Collection<ClusterNode> nodes, int pageSize, Map<ClusterNode, Integer> nodeToSegmentsCnt, boolean skipMergeTbl, boolean explain, Boolean dataPageScanEnabled) {
    final ReduceQueryRun r = new ReduceQueryRun(mapQueries.size(), pageSize, dataPageScanEnabled);
    int tblIdx = 0;
    int replicatedQrysCnt = 0;
    for (GridCacheSqlQuery mapQry : mapQueries) {
        Reducer reducer;
        if (skipMergeTbl)
            reducer = UnsortedOneWayReducer.createDummy(ctx);
        else {
            ReduceTable tbl;
            try {
                tbl = createMergeTable(conn, mapQry, explain);
            } catch (IgniteCheckedException e) {
                throw new IgniteException(e);
            }
            reducer = tbl.getReducer();
            fakeTable(conn, tblIdx++).innerTable(tbl);
        }
        // If the query has only replicated tables, we have to run it on a single node only.
        if (!mapQry.isPartitioned()) {
            ClusterNode node = F.rand(nodes);
            mapQry.node(node.id());
            replicatedQrysCnt++;
            // Replicated tables can have only 1 segment.
            reducer.setSources(singletonMap(node, 1));
        } else
            reducer.setSources(nodeToSegmentsCnt);
        reducer.setPageSize(r.pageSize());
        r.reducers().add(reducer);
    }
    int cnt = nodeToSegmentsCnt.values().stream().mapToInt(i -> i).sum();
    r.init((r.reducers().size() - replicatedQrysCnt) * cnt + replicatedQrysCnt);
    return r;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) SQL_DML_QRY_RESP(org.apache.ignite.internal.processors.tracing.SpanType.SQL_DML_QRY_RESP) GridQueryCancel(org.apache.ignite.internal.processors.query.GridQueryCancel) QueryUtils(org.apache.ignite.internal.processors.query.QueryUtils) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) IGNITE_SQL_RETRY_TIMEOUT(org.apache.ignite.IgniteSystemProperties.IGNITE_SQL_RETRY_TIMEOUT) SQL_FAIL_RESP(org.apache.ignite.internal.processors.tracing.SpanType.SQL_FAIL_RESP) H2Utils(org.apache.ignite.internal.processors.query.h2.H2Utils) Index(org.h2.index.Index) IgniteSystemProperties(org.apache.ignite.IgniteSystemProperties) ReduceH2QueryInfo(org.apache.ignite.internal.processors.query.h2.ReduceH2QueryInfo) Collections.singletonList(java.util.Collections.singletonList) ResultSet(java.sql.ResultSet) Map(java.util.Map) GridIoPolicy(org.apache.ignite.internal.managers.communication.GridIoPolicy) GridQueryFailResponse(org.apache.ignite.internal.processors.query.h2.twostep.messages.GridQueryFailResponse) GridH2DmlResponse(org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2DmlResponse) GridCacheTwoStepQuery(org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery) H2PooledConnection(org.apache.ignite.internal.processors.query.h2.H2PooledConnection) IgniteFuture(org.apache.ignite.lang.IgniteFuture) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) CIX2(org.apache.ignite.internal.util.typedef.CIX2) Collection(java.util.Collection) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) UpdateResult(org.apache.ignite.internal.processors.query.h2.UpdateResult) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TransactionAlreadyCompletedException(org.apache.ignite.transactions.TransactionAlreadyCompletedException) UUID(java.util.UUID) IgniteTxAlreadyCompletedCheckedException(org.apache.ignite.internal.transactions.IgniteTxAlreadyCompletedCheckedException) EMPTY_PARAMS(org.apache.ignite.internal.processors.cache.query.GridCacheSqlQuery.EMPTY_PARAMS) PreparedStatement(java.sql.PreparedStatement) GridCacheSqlQuery(org.apache.ignite.internal.processors.cache.query.GridCacheSqlQuery) Nullable(org.jetbrains.annotations.Nullable) ERROR(org.apache.ignite.internal.processors.tracing.SpanTags.ERROR) List(java.util.List) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) MTC(org.apache.ignite.internal.processors.tracing.MTC) InlineIndexImpl(org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexImpl) CreateTableData(org.h2.command.ddl.CreateTableData) Message(org.apache.ignite.plugin.extensions.communication.Message) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) NotNull(org.jetbrains.annotations.NotNull) GridQueryNextPageRequest(org.apache.ignite.internal.processors.query.h2.twostep.messages.GridQueryNextPageRequest) GridSqlSortColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSortColumn) DiscoveryEvent(org.apache.ignite.events.DiscoveryEvent) GridH2QueryRequest(org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2QueryRequest) MvccUtils.tx(org.apache.ignite.internal.processors.cache.mvcc.MvccUtils.tx) GridQueryCancelRequest(org.apache.ignite.internal.processors.query.h2.twostep.messages.GridQueryCancelRequest) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) IgniteLogger(org.apache.ignite.IgniteLogger) QueryRetryException(org.apache.ignite.cache.query.QueryRetryException) IgniteBiClosure(org.apache.ignite.lang.IgniteBiClosure) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) GridKernalContext(org.apache.ignite.internal.GridKernalContext) SQL_PAGE_RESP(org.apache.ignite.internal.processors.tracing.SpanType.SQL_PAGE_RESP) LinkedHashMap(java.util.LinkedHashMap) Column(org.h2.table.Column) SQLException(java.sql.SQLException) Session(org.h2.engine.Session) ClusterNode(org.apache.ignite.cluster.ClusterNode) IntArray(org.h2.util.IntArray) H2FieldsIterator(org.apache.ignite.internal.processors.query.h2.H2FieldsIterator) IgniteH2Indexing(org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) CacheException(javax.cache.CacheException) TransactionException(org.apache.ignite.transactions.TransactionException) Collections.singletonMap(java.util.Collections.singletonMap) C2(org.apache.ignite.internal.util.typedef.C2) Value(org.h2.value.Value) GridSqlQuerySplitter.mergeTableIdentifier(org.apache.ignite.internal.processors.query.h2.sql.GridSqlQuerySplitter.mergeTableIdentifier) F(org.apache.ignite.internal.util.typedef.F) Query(org.apache.ignite.cache.query.Query) Iterator(java.util.Iterator) ReentrantLock(java.util.concurrent.locks.ReentrantLock) DmlDistributedUpdateRun(org.apache.ignite.internal.processors.query.h2.dml.DmlDistributedUpdateRun) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridH2DmlRequest(org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2DmlRequest) GridSqlType(org.apache.ignite.internal.processors.query.h2.sql.GridSqlType) TimeUnit(java.util.concurrent.TimeUnit) GridQueryNextPageResponse(org.apache.ignite.internal.processors.query.h2.twostep.messages.GridQueryNextPageResponse) AtomicLong(java.util.concurrent.atomic.AtomicLong) Lock(java.util.concurrent.locks.Lock) GridTopic(org.apache.ignite.internal.GridTopic) BitSet(java.util.BitSet) MvccUtils.checkActive(org.apache.ignite.internal.processors.cache.mvcc.MvccUtils.checkActive) Collections(java.util.Collections) MvccQueryTracker(org.apache.ignite.internal.processors.cache.mvcc.MvccQueryTracker) QueryContext(org.apache.ignite.internal.processors.query.h2.opt.QueryContext) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) GridCacheSqlQuery(org.apache.ignite.internal.processors.cache.query.GridCacheSqlQuery) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with Query

use of org.apache.ignite.cache.query.Query in project ignite by apache.

the class PerformanceStatisticsQueryTest method runQueryAndCheck.

/**
 * Runs query and checks statistics.
 */
private void runQueryAndCheck(GridCacheQueryType expType, Query<?> qry, String expText, boolean hasLogicalReads, boolean hasPhysicalReads) throws Exception {
    long startTime = U.currentTimeMillis();
    cleanPerformanceStatisticsDir();
    startCollectStatistics();
    Collection<UUID> expNodeIds = new ArrayList<>();
    if (clientType == SERVER) {
        srv.cache(DEFAULT_CACHE_NAME).query(qry).getAll();
        expNodeIds.add(srv.localNode().id());
    } else if (clientType == CLIENT) {
        client.cache(DEFAULT_CACHE_NAME).query(qry).getAll();
        expNodeIds.add(client.localNode().id());
    } else if (clientType == THIN_CLIENT) {
        thinClient.cache(DEFAULT_CACHE_NAME).query(qry).getAll();
        expNodeIds.addAll(F.nodeIds(client.cluster().forServers().nodes()));
    }
    Set<UUID> readsNodes = new HashSet<>();
    if (hasLogicalReads)
        srv.cluster().forServers().nodes().forEach(node -> readsNodes.add(node.id()));
    AtomicInteger queryCnt = new AtomicInteger();
    AtomicInteger readsCnt = new AtomicInteger();
    HashSet<Long> qryIds = new HashSet<>();
    stopCollectStatisticsAndRead(new TestHandler() {

        @Override
        public void query(UUID nodeId, GridCacheQueryType type, String text, long id, long queryStartTime, long duration, boolean success) {
            queryCnt.incrementAndGet();
            qryIds.add(id);
            assertTrue(expNodeIds.contains(nodeId));
            assertEquals(expType, type);
            assertEquals(expText, text);
            assertTrue(queryStartTime >= startTime);
            assertTrue(duration >= 0);
            assertTrue(success);
        }

        @Override
        public void queryReads(UUID nodeId, GridCacheQueryType type, UUID queryNodeId, long id, long logicalReads, long physicalReads) {
            readsCnt.incrementAndGet();
            qryIds.add(id);
            readsNodes.remove(nodeId);
            assertTrue(expNodeIds.contains(queryNodeId));
            assertEquals(expType, type);
            assertTrue(logicalReads > 0);
            assertTrue(hasPhysicalReads ? physicalReads > 0 : physicalReads == 0);
        }
    });
    assertEquals(1, queryCnt.get());
    assertTrue("Query reads expected on nodes: " + readsNodes, readsNodes.isEmpty());
    assertEquals(1, qryIds.size());
}
Also used : CLIENT(org.apache.ignite.internal.processors.performancestatistics.AbstractPerformanceStatisticsTest.ClientType.CLIENT) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Assume.assumeFalse(org.junit.Assume.assumeFalse) RunWith(org.junit.runner.RunWith) U(org.apache.ignite.internal.util.typedef.internal.U) SERVER(org.apache.ignite.internal.processors.performancestatistics.AbstractPerformanceStatisticsTest.ClientType.SERVER) IgniteEx(org.apache.ignite.internal.IgniteEx) THIN_CLIENT(org.apache.ignite.internal.processors.performancestatistics.AbstractPerformanceStatisticsTest.ClientType.THIN_CLIENT) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Config(org.apache.ignite.client.Config) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteClient(org.apache.ignite.client.IgniteClient) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) QueryEntity(org.apache.ignite.cache.QueryEntity) LinkedList(java.util.LinkedList) Parameterized(org.junit.runners.Parameterized) ACTIVE(org.apache.ignite.cluster.ClusterState.ACTIVE) F(org.apache.ignite.internal.util.typedef.F) Query(org.apache.ignite.cache.query.Query) INACTIVE(org.apache.ignite.cluster.ClusterState.INACTIVE) Collection(java.util.Collection) Set(java.util.Set) Test(org.junit.Test) UUID(java.util.UUID) SCAN(org.apache.ignite.internal.processors.cache.query.GridCacheQueryType.SCAN) IgniteCache(org.apache.ignite.IgniteCache) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Ignition(org.apache.ignite.Ignition) GridCacheQueryType(org.apache.ignite.internal.processors.cache.query.GridCacheQueryType) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) DFLT_SCHEMA(org.apache.ignite.internal.processors.query.QueryUtils.DFLT_SCHEMA) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) SQL_FIELDS(org.apache.ignite.internal.processors.cache.query.GridCacheQueryType.SQL_FIELDS) Collections(java.util.Collections) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) ScanQuery(org.apache.ignite.cache.query.ScanQuery) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) ArrayList(java.util.ArrayList) GridCacheQueryType(org.apache.ignite.internal.processors.cache.query.GridCacheQueryType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UUID(java.util.UUID) HashSet(java.util.HashSet)

Aggregations

Query (org.apache.ignite.cache.query.Query)11 List (java.util.List)7 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)7 ScanQuery (org.apache.ignite.cache.query.ScanQuery)6 SqlQuery (org.apache.ignite.cache.query.SqlQuery)6 QueryCursor (org.apache.ignite.cache.query.QueryCursor)5 Test (org.junit.Test)5 Map (java.util.Map)4 Ignition (org.apache.ignite.Ignition)4 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3 TimeUnit (java.util.concurrent.TimeUnit)3 Cache (javax.cache.Cache)3 Ignite (org.apache.ignite.Ignite)3 ClientConfiguration (org.apache.ignite.configuration.ClientConfiguration)3 U (org.apache.ignite.internal.util.typedef.internal.U)3 SerializedLambda (java.lang.invoke.SerializedLambda)2 Collections (java.util.Collections)2 Iterator (java.util.Iterator)2