forked from hazelcast/hazelcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SqlServiceImpl.java
328 lines (276 loc) · 10.7 KB
/
SqlServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
* Copyright 2023 Hazelcast Inc.
*
* Licensed under the Hazelcast Community License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://hazelcast.com/hazelcast-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.sql.impl;
import com.hazelcast.internal.util.Preconditions;
import com.hazelcast.internal.util.counters.Counter;
import com.hazelcast.internal.util.counters.MwCounter;
import com.hazelcast.jet.impl.util.Util;
import com.hazelcast.jet.sql.impl.CalciteSqlOptimizer;
import com.hazelcast.logging.ILogger;
import com.hazelcast.spi.impl.NodeEngineImpl;
import com.hazelcast.sql.SqlExpectedResultType;
import com.hazelcast.sql.SqlResult;
import com.hazelcast.sql.SqlStatement;
import com.hazelcast.sql.impl.optimizer.OptimizationTask;
import com.hazelcast.sql.impl.optimizer.PlanKey;
import com.hazelcast.sql.impl.optimizer.SqlPlan;
import com.hazelcast.sql.impl.plan.cache.PlanCache;
import com.hazelcast.sql.impl.plan.cache.PlanCacheChecker;
import com.hazelcast.sql.impl.schema.SqlCatalog;
import com.hazelcast.sql.impl.security.NoOpSqlSecurityContext;
import com.hazelcast.sql.impl.security.SqlSecurityContext;
import com.hazelcast.sql.impl.state.QueryClientStateRegistry;
import com.hazelcast.sql.impl.state.QueryResultRegistry;
import javax.annotation.Nonnull;
import java.security.AccessControlException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static com.hazelcast.sql.SqlExpectedResultType.ANY;
import static com.hazelcast.sql.SqlExpectedResultType.ROWS;
import static com.hazelcast.sql.SqlExpectedResultType.UPDATE_COUNT;
import static com.hazelcast.sql.impl.QueryUtils.CATALOG;
import static java.util.Arrays.asList;
/**
* Base SQL service implementation that bridges optimizer implementation, public and private APIs.
*/
public class SqlServiceImpl implements InternalSqlService {
/**
* Default state check frequency.
*/
private static final long STATE_CHECK_FREQUENCY = 1_000L;
/**
* Default plan cache size.
*/
private static final int PLAN_CACHE_SIZE = 10_000;
private final ILogger logger;
private final NodeEngineImpl nodeEngine;
private final NodeServiceProviderImpl nodeServiceProvider;
private final PlanCache planCache = new PlanCache(PLAN_CACHE_SIZE);
private final long queryTimeout;
private CalciteSqlOptimizer optimizer;
private SqlInternalService internalService;
private final Counter sqlQueriesSubmitted = MwCounter.newMwCounter();
private final Counter sqlStreamingQueriesExecuted = MwCounter.newMwCounter();
public SqlServiceImpl(NodeEngineImpl nodeEngine) {
this.logger = nodeEngine.getLogger(getClass());
this.nodeEngine = nodeEngine;
this.nodeServiceProvider = new NodeServiceProviderImpl(nodeEngine);
long queryTimeout = nodeEngine.getConfig().getSqlConfig().getStatementTimeoutMillis();
assert queryTimeout >= 0L;
this.queryTimeout = queryTimeout;
}
public void start() {
if (!Util.isJetEnabled(nodeEngine)) {
return;
}
QueryResultRegistry resultRegistry = new QueryResultRegistry();
optimizer = new CalciteSqlOptimizer(nodeEngine, resultRegistry);
String instanceName = nodeEngine.getHazelcastInstance().getName();
PlanCacheChecker planCacheChecker = new PlanCacheChecker(
nodeEngine,
planCache,
optimizer.tableResolvers()
);
DataConnectionConsistencyChecker dataConnectionConsistencyChecker = new DataConnectionConsistencyChecker(
nodeEngine.getHazelcastInstance(),
nodeEngine
);
internalService = new SqlInternalService(
resultRegistry,
instanceName,
nodeServiceProvider,
STATE_CHECK_FREQUENCY,
planCacheChecker,
dataConnectionConsistencyChecker
);
internalService.start();
}
public void reset() {
if (!Util.isJetEnabled(nodeEngine)) {
return;
}
planCache.clear();
}
public void shutdown() {
if (!Util.isJetEnabled(nodeEngine)) {
return;
}
planCache.clear();
if (internalService != null) {
internalService.shutdown();
}
}
public SqlInternalService getInternalService() {
return internalService;
}
public long getSqlQueriesSubmittedCount() {
return sqlQueriesSubmitted.get();
}
public long getSqlStreamingQueriesExecutedCount() {
return sqlStreamingQueriesExecuted.get();
}
/**
* For testing only.
*/
public CalciteSqlOptimizer getOptimizer() {
return optimizer;
}
/**
* For testing only.
*/
public PlanCache getPlanCache() {
return planCache;
}
@Nonnull
@Override
public SqlResult execute(@Nonnull SqlStatement statement) {
return execute(statement, NoOpSqlSecurityContext.INSTANCE);
}
public SqlResult execute(@Nonnull SqlStatement statement, SqlSecurityContext securityContext) {
return execute(statement, securityContext, null);
}
public SqlResult execute(@Nonnull SqlStatement statement, SqlSecurityContext securityContext, QueryId queryId) {
return execute(statement, securityContext, queryId, false);
}
public SqlResult execute(
@Nonnull SqlStatement statement,
SqlSecurityContext securityContext,
QueryId queryId,
boolean skipStats
) {
Preconditions.checkNotNull(statement, "Query cannot be null");
if (!skipStats) {
sqlQueriesSubmitted.inc();
}
try {
if (nodeEngine.getLocalMember().isLiteMember()) {
throw QueryException.error("SQL queries cannot be executed on lite members");
}
Util.checkJetIsEnabled(nodeEngine);
long timeout = statement.getTimeoutMillis();
if (timeout == SqlStatement.TIMEOUT_NOT_SET) {
timeout = queryTimeout;
}
if (queryId == null) {
queryId = QueryId.create(nodeServiceProvider.getLocalMemberId());
}
SqlResult sqlResult = query0(
queryId,
statement.getSchema(),
statement.getSql(),
statement.getParameters(),
timeout,
statement.getCursorBufferSize(),
statement.getExpectedResultType(),
securityContext
);
if (!skipStats) {
updateSqlStreamingQueriesExecuted(sqlResult);
}
return sqlResult;
} catch (AccessControlException e) {
throw e;
} catch (Exception e) {
throw CoreQueryUtils.toPublicException(e, nodeServiceProvider.getLocalMemberId());
}
}
private void updateSqlStreamingQueriesExecuted(SqlResult sqlResult) {
if (sqlResult instanceof AbstractSqlResult) {
if (((AbstractSqlResult) sqlResult).isInfiniteRows()) {
sqlStreamingQueriesExecuted.inc();
}
}
}
private SqlResult query0(
QueryId queryId,
String schema,
String sql,
List<Object> args,
long timeout,
int pageSize,
SqlExpectedResultType expectedResultType,
SqlSecurityContext securityContext
) {
// Validate and normalize
if (sql == null || sql.isEmpty()) {
throw QueryException.error("SQL statement cannot be empty.");
}
List<Object> args0 = new ArrayList<>(args);
if (timeout < 0) {
throw QueryException.error("Timeout cannot be negative: " + timeout);
}
if (pageSize <= 0) {
throw QueryException.error("Page size must be positive: " + pageSize);
}
// Prepare and execute
SqlPlan plan = prepare(schema, sql, args0, expectedResultType);
if (securityContext.isSecurityEnabled()) {
plan.checkPermissions(securityContext);
}
// TODO: pageSize ?
return plan.execute(queryId, args0, timeout, securityContext);
}
private SqlPlan prepare(String schema, String sql, List<Object> arguments, SqlExpectedResultType expectedResultType) {
List<List<String>> searchPaths = prepareSearchPaths(schema);
PlanKey planKey = new PlanKey(searchPaths, sql);
SqlPlan plan = planCache.get(planKey);
if (plan == null) {
SqlCatalog catalog = new SqlCatalog(optimizer.tableResolvers());
plan = optimizer.prepare(new OptimizationTask(sql, arguments, searchPaths, catalog));
if (plan.isCacheable()) {
planCache.put(planKey, plan);
}
}
checkReturnType(plan, expectedResultType);
return plan;
}
private void checkReturnType(SqlPlan plan, SqlExpectedResultType expectedResultType) {
if (expectedResultType == ANY) {
return;
}
boolean producesRows = plan.producesRows();
if (producesRows && expectedResultType == UPDATE_COUNT) {
throw QueryException.error("The statement doesn't produce update count");
}
if (!producesRows && expectedResultType == ROWS) {
throw QueryException.error("The statement doesn't produce rows");
}
}
private List<List<String>> prepareSearchPaths(String schema) {
List<List<String>> currentSearchPaths;
if (schema == null || schema.isEmpty()) {
currentSearchPaths = Collections.emptyList();
} else {
currentSearchPaths = Collections.singletonList(asList(CATALOG, schema));
}
return QueryUtils.prepareSearchPaths(currentSearchPaths, optimizer.tableResolvers());
}
public String mappingDdl(String name) {
return optimizer.mappingDdl(name);
}
@Override
public void closeOnError(QueryId queryId) {
if (!Util.isJetEnabled(nodeEngine)) {
return;
}
getInternalService().getClientStateRegistry().closeOnError(queryId);
}
@Override
public QueryClientStateRegistry getClientStateRegistry() {
return getInternalService().getClientStateRegistry();
}
}