-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ExecutionEngine.scala
251 lines (214 loc) · 11.8 KB
/
ExecutionEngine.scala
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
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.cypher.internal
import java.util.{Map => JavaMap}
import org.neo4j.cypher._
import org.neo4j.cypher.internal.compiler.v3_2._
import org.neo4j.cypher.internal.compiler.v3_2.helpers.{RuntimeJavaValueConverter, RuntimeScalaValueConverter}
import org.neo4j.cypher.internal.compiler.v3_2.prettifier.Prettifier
import org.neo4j.cypher.internal.spi.TransactionalContextWrapperv3_2
import org.neo4j.cypher.internal.tracing.{CompilationTracer, TimingCompilationTracer}
import org.neo4j.graphdb.config.Setting
import org.neo4j.graphdb.factory.GraphDatabaseSettings
import org.neo4j.kernel.api.ReadOperations
import org.neo4j.kernel.api.security.AccessMode
import org.neo4j.kernel.configuration.Config
import org.neo4j.kernel.impl.query.{QueryExecutionMonitor, TransactionalContext}
import org.neo4j.kernel.{GraphDatabaseQueryService, api, monitoring}
import org.neo4j.logging.{LogProvider, NullLogProvider}
trait StringCacheMonitor extends CypherCacheMonitor[String, api.Statement]
/**
* This class construct and initialize both the cypher compiler and the cypher runtime, which is a very expensive
* operation so please make sure this will be constructed only once and properly reused.
*/
class ExecutionEngine(val queryService: GraphDatabaseQueryService, logProvider: LogProvider = NullLogProvider.getInstance()) {
require(queryService != null, "Can't work with a null graph database")
// true means we run inside REST server
protected val isServer = false
protected val kernel = queryService.getDependencyResolver.resolveDependency(classOf[org.neo4j.kernel.api.KernelAPI])
private val lastCommittedTxId = LastCommittedTxIdProvider(queryService)
protected val kernelMonitors: monitoring.Monitors = queryService.getDependencyResolver.resolveDependency(classOf[org.neo4j.kernel.monitoring.Monitors])
private val compilationTracer: CompilationTracer = {
if( true) //optGraphSetting(queryService, GraphDatabaseSettings.cypher_compiler_tracing, FALSE))
new TimingCompilationTracer(kernelMonitors.newMonitor(classOf[TimingCompilationTracer.EventListener]))
else
CompilationTracer.NO_COMPILATION_TRACING
}
protected val compiler = createCompiler
private val log = logProvider.getLog( getClass )
private val cacheMonitor = kernelMonitors.newMonitor(classOf[StringCacheMonitor])
kernelMonitors.addMonitorListener( new StringCacheMonitor {
override def cacheDiscard(ignored: String, query: String) {
log.info(s"Discarded stale query from the query cache: $query")
}
})
private val executionMonitor = kernelMonitors.newMonitor(classOf[QueryExecutionMonitor])
private val cacheAccessor = new MonitoringCacheAccessor[String, (ExecutionPlan, Map[String, Any])](cacheMonitor)
private val preParsedQueries = new LFUCache[String, PreParsedQuery](getPlanCacheSize)
private val parsedQueries = new LFUCache[String, ParsedQuery](getPlanCacheSize)
private val javaValues = new RuntimeJavaValueConverter(isGraphKernelResultValue, identity)
private val scalaValues = new RuntimeScalaValueConverter(isGraphKernelResultValue, identity)
@throws(classOf[SyntaxException])
def profile(query: String, scalaParams: Map[String, Any], context: TransactionalContext): ExecutionResult = {
// we got deep scala parameters => convert to deep java parameters
val javaParams = javaValues.asDeepJavaMap(scalaParams).asInstanceOf[JavaMap[String, AnyRef]]
profile(query, javaParams, context)
}
@throws(classOf[SyntaxException])
def profile(query: String, javaParams: JavaMap[String, AnyRef], context: TransactionalContext): ExecutionResult = {
// we got deep java parameters => convert to shallow scala parameters for passing into the engine
val scalaParams = scalaValues.asShallowScalaMap(javaParams)
val (preparedPlanExecution, wrappedContext) = planQuery(context)
preparedPlanExecution.profile(wrappedContext, scalaParams)
}
@throws(classOf[SyntaxException])
def execute(query: String, scalaParams: Map[String, Any], context: TransactionalContext): ExecutionResult = {
// we got deep scala parameters => convert to deep java parameters
val javaParams = javaValues.asDeepJavaMap(scalaParams).asInstanceOf[JavaMap[String, AnyRef]]
execute(query, javaParams, context)
}
@throws(classOf[SyntaxException])
def execute(query: String, javaParams: JavaMap[String, AnyRef], context: TransactionalContext): ExecutionResult = {
// we got deep java parameters => convert to shallow scala parameters for passing into the engine
val scalaParams = scalaValues.asShallowScalaMap(javaParams)
val (preparedPlanExecution, wrappedContext) = planQuery(context)
preparedPlanExecution.execute(wrappedContext, scalaParams)
}
@throws(classOf[SyntaxException])
protected def parseQuery(queryText: String): ParsedQuery =
parsePreParsedQuery(preParseQuery(queryText), CompilationPhaseTracer.NO_TRACING)
@throws(classOf[SyntaxException])
private def parsePreParsedQuery(preParsedQuery: PreParsedQuery, tracer: CompilationPhaseTracer): ParsedQuery = {
parsedQueries.get(preParsedQuery.statementWithVersionAndPlanner).getOrElse {
val parsedQuery = compiler.parseQuery(preParsedQuery, tracer)
//don't cache failed queries
if (!parsedQuery.hasErrors) parsedQueries.put(preParsedQuery.statementWithVersionAndPlanner, parsedQuery)
parsedQuery
}
}
@throws(classOf[SyntaxException])
private def preParseQuery(queryText: String): PreParsedQuery =
preParsedQueries.getOrElseUpdate(queryText, compiler.preParseQuery(queryText))
@throws(classOf[SyntaxException])
protected def planQuery(transactionalContext: TransactionalContext): (PreparedPlanExecution, TransactionalContextWrapperv3_2) = {
val executingQuery = transactionalContext.executingQuery()
val queryText = executingQuery.queryText()
executionMonitor.startQueryExecution(executingQuery)
val phaseTracer = compilationTracer.compileQuery(queryText)
try {
val externalTransactionalContext = new TransactionalContextWrapperv3_2(transactionalContext)
val preParsedQuery = try {
preParseQuery(queryText)
} catch {
case e: SyntaxException =>
externalTransactionalContext.close(success = false)
throw e
}
val executionMode = preParsedQuery.executionMode
val cacheKey = preParsedQuery.statementWithVersionAndPlanner
var n = 0
while (n < ExecutionEngine.PLAN_BUILDING_TRIES) {
// create transaction and query context
val tc = externalTransactionalContext.getOrBeginNewIfClosed()
// Temporarily change access mode during query planning
// NOTE: This will force read access mode if the current transaction did not have it
val revertable = tc.restrictCurrentTransaction(tc.securityContext.withMode(AccessMode.Static.READ))
val ((plan: ExecutionPlan, extractedParameters), touched) = try {
// fetch plan cache
val cache = getOrCreateFromSchemaState(tc.readOperations, {
cacheMonitor.cacheFlushDetected(tc.statement)
val lruCache = new LFUCache[String, (ExecutionPlan, Map[String, Any])](getPlanCacheSize)
new QueryCache(cacheAccessor, lruCache)
})
def isStale(plan: ExecutionPlan, ignored: Map[String, Any]) = plan.isStale(lastCommittedTxId, tc)
def producePlan() = {
val parsedQuery = parsePreParsedQuery(preParsedQuery, phaseTracer)
parsedQuery.plan(tc, phaseTracer)
}
cache.getOrElseUpdate(cacheKey, queryText, (isStale _).tupled, producePlan())
}
catch {
case (t: Throwable) =>
tc.close(success = false)
throw t
} finally {
revertable.close()
}
if (touched) {
tc.close(success = true)
} else {
tc.cleanForReuse()
return (PreparedPlanExecution(plan, executionMode, extractedParameters), tc)
}
n += 1
}
} finally phaseTracer.close()
throw new IllegalStateException("Could not execute query due to insanely frequent schema changes")
}
private def getOrCreateFromSchemaState[V](operations: ReadOperations, creator: => V) = {
val javaCreator = new java.util.function.Function[ExecutionEngine, V]() {
def apply(key: ExecutionEngine) = creator
}
operations.schemaStateGetOrCreate(this, javaCreator)
}
def prettify(query: String): String = Prettifier(query)
def isPeriodicCommit(query: String) = parseQuery(query).isPeriodicCommit
private def createCompiler: CypherCompiler = {
val version = CypherVersion(optGraphSetting[String](
queryService, GraphDatabaseSettings.cypher_parser_version, CypherVersion.default.name))
val planner = CypherPlanner(optGraphSetting[String](
queryService, GraphDatabaseSettings.cypher_planner, CypherPlanner.default.name))
val runtime = CypherRuntime(optGraphSetting[String](
queryService, GraphDatabaseSettings.cypher_runtime, CypherRuntime.default.name))
val useErrorsOverWarnings = optGraphSetting[java.lang.Boolean](
queryService, GraphDatabaseSettings.cypher_hints_error,
GraphDatabaseSettings.cypher_hints_error.getDefaultValue.toBoolean)
val idpMaxTableSize: Int = optGraphSetting[java.lang.Integer](
queryService, GraphDatabaseSettings.cypher_idp_solver_table_threshold,
GraphDatabaseSettings.cypher_idp_solver_table_threshold.getDefaultValue.toInt)
val idpIterationDuration: Long = optGraphSetting[java.lang.Long](
queryService, GraphDatabaseSettings.cypher_idp_solver_duration_threshold,
GraphDatabaseSettings.cypher_idp_solver_duration_threshold.getDefaultValue.toLong)
val errorIfShortestPathFallbackUsedAtRuntime = optGraphSetting[java.lang.Boolean](
queryService, GraphDatabaseSettings.forbid_exhaustive_shortestpath,
GraphDatabaseSettings.forbid_exhaustive_shortestpath.getDefaultValue.toBoolean
)
if (((version != CypherVersion.v2_3) || (version != CypherVersion.v3_1) || (version != CypherVersion.v3_2)) &&
(planner == CypherPlanner.greedy || planner == CypherPlanner.idp || planner == CypherPlanner.dp)) {
val message = s"Cannot combine configurations: ${GraphDatabaseSettings.cypher_parser_version.name}=${version.name} " +
s"with ${GraphDatabaseSettings.cypher_planner.name} = ${planner.name}"
log.error(message)
throw new IllegalStateException(message)
}
new CypherCompiler(queryService, kernel, kernelMonitors, version, planner, runtime, useErrorsOverWarnings, idpMaxTableSize, idpIterationDuration, errorIfShortestPathFallbackUsedAtRuntime, logProvider)
}
private def getPlanCacheSize: Int =
optGraphSetting[java.lang.Integer](
queryService, GraphDatabaseSettings.query_cache_size,
GraphDatabaseSettings.query_cache_size.getDefaultValue.toInt
)
private def optGraphSetting[V](graph: GraphDatabaseQueryService, setting: Setting[V], defaultValue: V): V = {
val config = graph.getDependencyResolver.resolveDependency(classOf[Config])
Option(config.get(setting)).getOrElse(defaultValue)
}
}
object ExecutionEngine {
val PLAN_BUILDING_TRIES: Int = 20
}