Skip to content

Commit c492aa4

Browse files
ygerzhedovichantonovsergey93
authored andcommitted
GG-23230 Several open iterators by lazy SQL query can lead to assertion error.
1 parent 4eebffa commit c492aa4

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,14 @@ private ParsingResult parseAndSplit(String schemaName, SqlFieldsQuery qry, int f
16721672
return new ParsingResult(prepared, newQry, remainingSql, twoStepQry, cachedQryKey, meta);
16731673
}
16741674

1675+
1676+
// The part executed in user thread. In case user open few iterators ThreadLocal context will be invalid.
1677+
// To prevent it we keep old context and restore after.
1678+
GridH2QueryContext oldCtx = GridH2QueryContext.get();
1679+
1680+
if(oldCtx != null)
1681+
GridH2QueryContext.clearThreadLocal();
1682+
16751683
try {
16761684
GridH2QueryContext.set(new GridH2QueryContext(locNodeId, locNodeId, 0, PREPARE)
16771685
.distributedJoinMode(distributedJoinMode(qry.isLocal(), qry.isDistributedJoins())));
@@ -1693,6 +1701,10 @@ private ParsingResult parseAndSplit(String schemaName, SqlFieldsQuery qry, int f
16931701
}
16941702
finally {
16951703
GridH2QueryContext.clearThreadLocal();
1704+
1705+
if(oldCtx != null)
1706+
GridH2QueryContext.set(oldCtx);
1707+
16961708
}
16971709
}
16981710

modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/LocalQueryLazyTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,36 @@ public void testDuplicates() throws Exception {
112112
assertEquals(KEY_CNT, r0 + r1);
113113
}
114114

115+
/** */
116+
public void testParallelIterator() throws Exception {
117+
IgniteEx g1 = startGrid(0);
118+
119+
awaitPartitionMapExchange(true, true, null);
120+
121+
Iterator<List<?>> cursorIter0 = sql(g1, "SELECT * FROM test").iterator();
122+
Iterator<List<?>> cursorIter1 = distributedSql(g1, "SELECT * FROM test").iterator();
123+
124+
int r0 = 0;
125+
int r1 = 0;
126+
127+
for (int i =0; i<KEY_CNT; i++) {
128+
if (cursorIter0.hasNext()) {
129+
r0++;
130+
131+
cursorIter0.next();
132+
}
133+
134+
if (cursorIter1.hasNext()) {
135+
r1++;
136+
137+
cursorIter1.next();
138+
}
139+
}
140+
141+
assertTrue(r0 < KEY_CNT);
142+
assertEquals(KEY_CNT, r1);
143+
}
144+
115145
/**
116146
* @param sql SQL query.
117147
* @param args Query parameters.
@@ -134,4 +164,17 @@ private FieldsQueryCursor<List<?>> sql(IgniteEx ign, String sql, Object ... args
134164
.setSchema("TEST")
135165
.setArgs(args), false);
136166
}
167+
168+
/**
169+
* @param ign Node.
170+
* @param sql SQL query.
171+
* @param args Query parameters.
172+
* @return Results cursor.
173+
*/
174+
private FieldsQueryCursor<List<?>> distributedSql(IgniteEx ign, String sql, Object ... args) {
175+
return ign.context().query().querySqlFields(new SqlFieldsQuery(sql)
176+
.setLazy(true)
177+
.setSchema("TEST")
178+
.setArgs(args), false);
179+
}
137180
}

0 commit comments

Comments
 (0)