/** {@inheritDoc} */ @Override public SqlFieldsQueryEx setTimeout(int timeout, TimeUnit timeUnit) { super.setTimeout(timeout, timeUnit); return this; }
/** {@inheritDoc} */ @Override public Integer call() { try (Transaction ignored = node.transactions().txStart(TransactionConcurrency.PESSIMISTIC, TransactionIsolation.REPEATABLE_READ)) { List<List<?>> res = cache .query( new SqlFieldsQuery("select * from " + tableName(cache) + " where id = " + key + " for update").setTimeout(1, TimeUnit.SECONDS) ) .getAll(); return (Integer)res.get(0).get(0); } } }));
@Override public void run() { int id = idx.getAndIncrement(); IgniteEx node = grid(id); try { try (Transaction tx = node.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) { if (timeoutMode == TimeoutMode.TX) tx.timeout(TIMEOUT); String sqlText = "INSERT INTO MvccTestSqlIndexValue (_key, idxVal1) " + "SELECT DISTINCT _key, _val FROM \"int\".Integer ORDER BY _key"; String sqlAsc = sqlText + " ASC"; String sqlDesc = sqlText + " DESC"; SqlFieldsQuery qry = new SqlFieldsQuery((id % 2) == 0 ? sqlAsc : sqlDesc); if (timeoutMode == TimeoutMode.STMT) qry.setTimeout(TIMEOUT, TimeUnit.MILLISECONDS); IgniteCache<Object, Object> cache0 = node.cache(DEFAULT_CACHE_NAME); cache0.query(qry).getAll(); barrier.await(); qry = new SqlFieldsQuery((id % 2) == 0 ? sqlDesc : sqlAsc); if (timeoutMode == TimeoutMode.STMT) qry.setTimeout(TIMEOUT, TimeUnit.MILLISECONDS); cache0.query(qry).getAll(); tx.commit(); } } catch (Exception e) { onException(ex, e); } } }, 2);
qry.setTimeout(TX_TIMEOUT, TimeUnit.MILLISECONDS); qry.setTimeout(TX_TIMEOUT, TimeUnit.MILLISECONDS);
/** * @throws Exception If failed. */ @Test public void testQueryInsertStaticCacheImplicit() throws Exception { ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT) .setIndexedTypes(Integer.class, Integer.class); startGridsMultiThreaded(4); Random rnd = ThreadLocalRandom.current(); Ignite checkNode = grid(rnd.nextInt(4)); Ignite updateNode = grid(rnd.nextInt(4)); IgniteCache cache = checkNode.cache(DEFAULT_CACHE_NAME); SqlFieldsQuery qry = new SqlFieldsQuery("INSERT INTO Integer (_key, _val) values (1,1),(2,2),(3,3)") .setTimeout(TX_TIMEOUT, TimeUnit.MILLISECONDS); IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME); try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) { assertEquals(3L, cur.iterator().next().get(0)); } assertEquals(1, cache.get(1)); assertEquals(2, cache.get(2)); assertEquals(3, cache.get(3)); }
/** */ private void mergeInRandomOrder(IgniteEx ignite, IgniteCache<?, ?> cache, List<Integer> keys) { List<Integer> keys0 = new ArrayList<>(keys); for (int i = 0; i < 100; i++) { Collections.shuffle(keys0); try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) { SqlFieldsQuery qry = new SqlFieldsQuery("merge into Integer(_key, _val) values(?, ?)") .setTimeout(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); int op = 0; for (Integer key : keys0) cache.query(qry.setArgs(key, op++)); tx.commit(); } catch (Exception e) { assertTrue(msgContains(e, "Failed to acquire lock within provided timeout for transaction") || msgContains(e, "Cannot serialize transaction due to write conflict")); } finally { ignite.context().cache().context().tm().resetContext(); } } } }
qry.setTimeout(timeoutUnits, timeUnit);
/** * @throws Exception If failed. */ @Test public void testQueryInsertSubqueryImplicit() throws Exception { ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT) .setIndexedTypes(Integer.class, Integer.class, Integer.class, MvccTestSqlIndexValue.class); startGridsMultiThreaded(4); awaitPartitionMapExchange(); Random rnd = ThreadLocalRandom.current(); Ignite checkNode = grid(rnd.nextInt(4)); Ignite updateNode = grid(rnd.nextInt(4)); IgniteCache cache = checkNode.cache(DEFAULT_CACHE_NAME); cache.putAll(F.asMap( 1, new MvccTestSqlIndexValue(1), 2, new MvccTestSqlIndexValue(2), 3, new MvccTestSqlIndexValue(3))); SqlFieldsQuery qry = new SqlFieldsQuery("INSERT INTO Integer (_key, _val)" + " SELECT _key * 10, idxVal1 FROM MvccTestSqlIndexValue") .setTimeout(TX_TIMEOUT, TimeUnit.MILLISECONDS); IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME); try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) { assertEquals(3L, cur.iterator().next().get(0)); } assertEquals(1, cache0.get(10)); assertEquals(2, cache0.get(20)); assertEquals(3, cache0.get(30)); }
/** * @throws Exception If failed. */ @Test public void testQueryUpdateSubqueryImplicit() throws Exception { ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT) .setIndexedTypes(Integer.class, Integer.class, Integer.class, MvccTestSqlIndexValue.class); startGridsMultiThreaded(4); awaitPartitionMapExchange(); Random rnd = ThreadLocalRandom.current(); Ignite checkNode = grid(rnd.nextInt(4)); Ignite updateNode = grid(rnd.nextInt(4)); IgniteCache<Object, Object> cache = checkNode.cache(DEFAULT_CACHE_NAME); cache.putAll(F.asMap( 1, new MvccTestSqlIndexValue(1), 2, new MvccTestSqlIndexValue(2), 3, new MvccTestSqlIndexValue(3))); SqlFieldsQuery qry = new SqlFieldsQuery("UPDATE MvccTestSqlIndexValue AS t " + "SET (idxVal1) = (SELECT idxVal1*10 FROM MvccTestSqlIndexValue WHERE t._key = _key)") .setTimeout(TX_TIMEOUT, TimeUnit.MILLISECONDS); IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME); try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) { assertEquals(3L, cur.iterator().next().get(0)); } assertEquals(10, ((MvccTestSqlIndexValue)cache.get(1)).idxVal1); assertEquals(20, ((MvccTestSqlIndexValue)cache.get(2)).idxVal1); assertEquals(30, ((MvccTestSqlIndexValue)cache.get(3)).idxVal1); }
/** * @throws Exception If failed. */ @Test public void testQueryDeleteStaticCacheImplicit() throws Exception { ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT) .setIndexedTypes(Integer.class, Integer.class); startGridsMultiThreaded(4); Random rnd = ThreadLocalRandom.current(); Ignite checkNode = grid(rnd.nextInt(4)); Ignite updateNode = grid(rnd.nextInt(4)); IgniteCache cache = checkNode.cache(DEFAULT_CACHE_NAME); cache.putAll(F.asMap(1, 1, 2, 2, 3, 3)); assertEquals(1, cache.get(1)); assertEquals(2, cache.get(2)); assertEquals(3, cache.get(3)); SqlFieldsQuery qry = new SqlFieldsQuery("DELETE FROM Integer WHERE 1 = 1") .setTimeout(TX_TIMEOUT, TimeUnit.MILLISECONDS); IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME); try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) { assertEquals(3L, cur.iterator().next().get(0)); } assertNull(cache.get(1)); assertNull(cache.get(2)); assertNull(cache.get(3)); }
/** * @throws Exception If failed. */ @Test public void testQueryUpdateStaticCacheImplicit() throws Exception { ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT) .setIndexedTypes(Integer.class, Integer.class); startGridsMultiThreaded(4); Random rnd = ThreadLocalRandom.current(); Ignite checkNode = grid(rnd.nextInt(4)); Ignite updateNode = grid(rnd.nextInt(4)); IgniteCache cache = checkNode.cache(DEFAULT_CACHE_NAME); cache.putAll(F.asMap(1, 1, 2, 2, 3, 3)); assertEquals(1, cache.get(1)); assertEquals(2, cache.get(2)); assertEquals(3, cache.get(3)); SqlFieldsQuery qry = new SqlFieldsQuery("UPDATE Integer SET _val = (_key * 10)") .setTimeout(TX_TIMEOUT, TimeUnit.MILLISECONDS); IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME); try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) { assertEquals(3L, cur.iterator().next().get(0)); } assertEquals(10, cache.get(1)); assertEquals(20, cache.get(2)); assertEquals(30, cache.get(3)); }
.setTimeout(TX_TIMEOUT, TimeUnit.MILLISECONDS);
.setTimeout(TX_TIMEOUT, TimeUnit.MILLISECONDS);
.setTimeout(TX_TIMEOUT, TimeUnit.MILLISECONDS);
/** * @throws Exception If failed. */ @Test public void testQueryInsertClientImplicit() throws Exception { ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, DFLT_PARTITION_COUNT) .setIndexedTypes(Integer.class, Integer.class); startGrid(0); client = true; startGrid(1); awaitPartitionMapExchange(); Ignite checkNode = grid(0); Ignite updateNode = grid(1); IgniteCache cache = checkNode.cache(DEFAULT_CACHE_NAME); SqlFieldsQuery qry = new SqlFieldsQuery("INSERT INTO Integer (_key, _val) values (1,1),(2,2),(3,3)") .setTimeout(TX_TIMEOUT, TimeUnit.MILLISECONDS); IgniteCache<Object, Object> cache0 = updateNode.cache(DEFAULT_CACHE_NAME); try (FieldsQueryCursor<List<?>> cur = cache0.query(qry)) { assertEquals(3L, cur.iterator().next().get(0)); } assertEquals(1, cache.get(1)); assertEquals(2, cache.get(2)); assertEquals(3, cache.get(3)); }
/** * Reads fields query. * * @param reader Binary reader. * @return Query. */ private Query readFieldsQuery(BinaryRawReaderEx reader) { boolean loc = reader.readBoolean(); String sql = reader.readString(); final int pageSize = reader.readInt(); Object[] args = readQueryArgs(reader); boolean distrJoins = reader.readBoolean(); boolean enforceJoinOrder = reader.readBoolean(); boolean lazy = reader.readBoolean(); int timeout = reader.readInt(); boolean replicated = reader.readBoolean(); boolean collocated = reader.readBoolean(); String schema = reader.readString(); return new SqlFieldsQuery(sql) .setPageSize(pageSize) .setArgs(args) .setLocal(loc) .setDistributedJoins(distrJoins) .setEnforceJoinOrder(enforceJoinOrder) .setLazy(lazy) .setTimeout(timeout, TimeUnit.MILLISECONDS) .setReplicatedOnly(replicated) .setCollocated(collocated) .setSchema(schema); }
/** {@inheritDoc} */ @Override public UpdateSourceIterator<?> prepareDistributedUpdate(GridCacheContext<?, ?> cctx, int[] ids, int[] parts, String schema, String qry, Object[] params, int flags, int pageSize, int timeout, AffinityTopologyVersion topVer, MvccSnapshot mvccSnapshot, GridQueryCancel cancel) throws IgniteCheckedException { SqlFieldsQuery fldsQry = new SqlFieldsQuery(qry); if (params != null) fldsQry.setArgs(params); fldsQry.setEnforceJoinOrder(isFlagSet(flags, GridH2QueryRequest.FLAG_ENFORCE_JOIN_ORDER)); fldsQry.setTimeout(timeout, TimeUnit.MILLISECONDS); fldsQry.setPageSize(pageSize); fldsQry.setLocal(true); fldsQry.setDataPageScanEnabled(isDataPageScanEnabled(flags)); boolean loc = true; final boolean replicated = isFlagSet(flags, GridH2QueryRequest.FLAG_REPLICATED); GridCacheContext<?, ?> cctx0; if (!replicated && !F.isEmpty(ids) && (cctx0 = CU.firstPartitioned(cctx.shared(), ids)) != null && cctx0.config().getQueryParallelism() > 1) { fldsQry.setDistributedJoins(true); loc = false; } Connection conn = connMgr.connectionForThread().connection(schema); H2Utils.setupConnection(conn, false, fldsQry.isEnforceJoinOrder()); PreparedStatement stmt = preparedStatementWithParams(conn, fldsQry.getSql(), F.asList(fldsQry.getArgs()), true); return dmlProc.prepareDistributedUpdate(schema, conn, stmt, fldsQry, backupFilter(topVer, parts), cancel, loc, topVer, mvccSnapshot); }
/** {@inheritDoc} */ @SuppressWarnings("deprecation") @Override public SqlFieldsQuery generateFieldsQuery(String cacheName, SqlQuery qry) { String schemaName = schema(cacheName); String type = qry.getType(); H2TableDescriptor tblDesc = schemaMgr.tableForType(schemaName, cacheName, type); if (tblDesc == null) throw new IgniteSQLException("Failed to find SQL table for type: " + type, IgniteQueryErrorCode.TABLE_NOT_FOUND); String sql; try { sql = H2Utils.generateFieldsQueryString(qry.getSql(), qry.getAlias(), tblDesc); } catch (IgniteCheckedException e) { throw new IgniteException(e); } SqlFieldsQuery res = new SqlFieldsQuery(sql); res.setArgs(qry.getArgs()); res.setDistributedJoins(qry.isDistributedJoins()); res.setLocal(qry.isLocal()); res.setPageSize(qry.getPageSize()); res.setPartitions(qry.getPartitions()); res.setReplicatedOnly(qry.isReplicatedOnly()); res.setSchema(schemaName); res.setSql(sql); res.setDataPageScanEnabled(qry.isDataPageScanEnabled()); if (qry.getTimeout() > 0) res.setTimeout(qry.getTimeout(), TimeUnit.MILLISECONDS); return res; }
.setCollocated(collocated) .setLazy(lazy) .setTimeout(timeout, TimeUnit.MILLISECONDS);
qry.setTimeout(TX_TIMEOUT, TimeUnit.MILLISECONDS);