-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ExecutionEngine.scala
261 lines (220 loc) · 11.5 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
252
253
254
255
256
257
258
259
260
261
/*
* Copyright (c) 2002-2015 "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
import java.lang.Boolean.FALSE
import java.util.{Map => JavaMap}
import org.neo4j.cypher.internal.compiler.v3_0.prettifier.Prettifier
import org.neo4j.cypher.internal.compiler.v3_0.{LRUCache => LRUCachev3_0, _}
import org.neo4j.cypher.internal.frontend.v3_0.helpers.JavaCompatibility
import JavaCompatibility._
import org.neo4j.cypher.internal.tracing.{TimingCompilationTracer, CompilationTracer}
import org.neo4j.cypher.internal.{CypherCompiler, _}
import org.neo4j.graphdb.GraphDatabaseService
import org.neo4j.graphdb.config.Setting
import org.neo4j.graphdb.factory.GraphDatabaseSettings
import org.neo4j.kernel.impl.core.ThreadToStatementContextBridge
import org.neo4j.kernel.impl.factory.GraphDatabaseFacade
import org.neo4j.kernel.impl.query.{QueryEngineProvider, QueryExecutionMonitor, QuerySession}
import org.neo4j.kernel.impl.transaction.log.TransactionIdStore
import org.neo4j.kernel.{GraphDatabaseAPI, api, monitoring}
import org.neo4j.logging.{LogProvider, NullLogProvider}
import scala.collection.JavaConverters._
trait StringCacheMonitor extends CypherCacheMonitor[String, api.Statement]
class ExecutionEngine(graph: GraphDatabaseService, logProvider: LogProvider = NullLogProvider.getInstance()) {
require(graph != null, "Can't work with a null graph database")
// true means we run inside REST server
protected val isServer = false
protected val graphAPI = graph.asInstanceOf[GraphDatabaseAPI]
protected val kernel = graphAPI.getDependencyResolver.resolveDependency(classOf[org.neo4j.kernel.api.KernelAPI])
private val lastTxId: () => Long =
graphAPI.getDependencyResolver.resolveDependency( classOf[TransactionIdStore]).getLastCommittedTransactionId
protected val kernelMonitors: monitoring.Monitors = graphAPI.getDependencyResolver.resolveDependency(classOf[org.neo4j.kernel.monitoring.Monitors])
private val compilationTracer: CompilationTracer = {
if(optGraphSetting(graph, 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(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 LRUCachev3_0[String, PreParsedQuery](getPlanCacheSize)
private val parsedQueries = new LRUCachev3_0[String, ParsedQuery](getPlanCacheSize)
@throws(classOf[SyntaxException])
def profile(query: String): ExtendedExecutionResult = profile(query, Map[String, Any](), QueryEngineProvider.embeddedSession)
@throws(classOf[SyntaxException])
def profile(query: String, params: JavaMap[String, Any]): ExtendedExecutionResult = profile(query, params.asScala.toMap, QueryEngineProvider.embeddedSession)
@throws(classOf[SyntaxException])
def profile(query: String, params: Map[String, Any]): ExtendedExecutionResult = profile(query, params, QueryEngineProvider.embeddedSession)
@throws(classOf[SyntaxException])
def profile(query: String, params: Map[String, Any],session: QuerySession): ExtendedExecutionResult = {
executionMonitor.startQueryExecution(session, query, asJavaMap(params))
val (preparedPlanExecution, txInfo) = planQuery(query)
preparedPlanExecution.profile(graphAPI, txInfo, params, session)
}
@throws(classOf[SyntaxException])
def profile(query: String, params: JavaMap[String, Any], session: QuerySession): ExtendedExecutionResult =
profile(query, params.asScala.toMap, session)
@throws(classOf[SyntaxException])
def execute(query: String): ExtendedExecutionResult = execute(query, Map[String, Any]())
@throws(classOf[SyntaxException])
def execute(query: String, params: JavaMap[String, Any]): ExtendedExecutionResult = execute(query, params.asScala.toMap, QueryEngineProvider.embeddedSession)
@throws(classOf[SyntaxException])
def execute(query: String, params: Map[String, Any]): ExtendedExecutionResult =
execute(query, params, QueryEngineProvider.embeddedSession)
@throws(classOf[SyntaxException])
def execute(query: String, params: JavaMap[String, Any], session: QuerySession): ExtendedExecutionResult =
execute(query, params.asScala.toMap, session)
@throws(classOf[SyntaxException])
def execute(query: String, params: Map[String, Any], session: QuerySession): ExtendedExecutionResult = {
executionMonitor.startQueryExecution(session, query,
asJavaMap(params))
val (preparedPlanExecution, txInfo) = planQuery(query)
preparedPlanExecution.execute(graphAPI, txInfo, params, session)
}
@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(queryText: String): (PreparedPlanExecution, TransactionInfo) = {
val phaseTracer = compilationTracer.compileQuery(queryText)
try {
val preParsedQuery = preParseQuery(queryText)
val executionMode = preParsedQuery.executionMode
val cacheKey = preParsedQuery.statementWithVersionAndPlanner
var n = 0
while (n < ExecutionEngine.PLAN_BUILDING_TRIES) {
// create transaction and query context
var touched = false
val isTopLevelTx = !txBridge.hasTransaction
val tx = graph.beginTx()
val kernelStatement = txBridge.get()
val (plan: ExecutionPlan, extractedParameters) = try {
// fetch plan cache
val cache: LRUCachev3_0[String, (ExecutionPlan, Map[String, Any])] = getOrCreateFromSchemaState(kernelStatement, {
cacheMonitor.cacheFlushDetected(kernelStatement)
new LRUCachev3_0[String, (ExecutionPlan, Map[String, Any])](getPlanCacheSize)
})
Iterator.continually {
cacheAccessor.getOrElseUpdate(cache)(cacheKey, {
touched = true
val parsedQuery = parsePreParsedQuery(preParsedQuery, phaseTracer)
parsedQuery.plan(kernelStatement, phaseTracer)
})
}.flatMap { case (candidatePlan, params) =>
if (!touched && candidatePlan.isStale(lastTxId, kernelStatement)) {
cacheAccessor.remove(cache)(cacheKey)
None
} else {
Some((candidatePlan, params))
}
}.next()
} catch {
case (t: Throwable) =>
kernelStatement.close()
tx.failure()
tx.close()
throw t
}
if (touched) {
kernelStatement.close()
tx.success()
tx.close()
} else {
// close the old statement reference after the statement has been "upgraded"
// to either a schema data or a schema statement, so that the locks are "handed over".
kernelStatement.close()
val preparedPlanExecution = PreparedPlanExecution(plan, executionMode, extractedParameters)
val txInfo = TransactionInfo(tx, isTopLevelTx, txBridge.get())
return (preparedPlanExecution, txInfo)
}
n += 1
}
} finally phaseTracer.close()
throw new IllegalStateException("Could not execute query due to insanely frequent schema changes")
}
private val txBridge = graph.asInstanceOf[GraphDatabaseAPI]
.getDependencyResolver
.resolveDependency(classOf[ThreadToStatementContextBridge])
private def getOrCreateFromSchemaState[V](statement: api.Statement, creator: => V) = {
val javaCreator = new org.neo4j.function.Function[ExecutionEngine, V]() {
def apply(key: ExecutionEngine) = creator
}
statement.readOperations().schemaStateGetOrCreate(this, javaCreator)
}
def prettify(query: String): String = Prettifier(query)
private def createCompiler: CypherCompiler = {
val version = CypherVersion(optGraphSetting[String](
graph, GraphDatabaseSettings.cypher_parser_version, CypherVersion.default.name))
val planner = CypherPlanner(optGraphSetting[String](
graph, GraphDatabaseSettings.cypher_planner, CypherPlanner.default.name))
val runtime = CypherRuntime(optGraphSetting[String](
graph, GraphDatabaseSettings.cypher_runtime, CypherRuntime.default.name))
val useErrorsOverWarnings: java.lang.Boolean = optGraphSetting[java.lang.Boolean](
graph, GraphDatabaseSettings.cypher_hints_error,
GraphDatabaseSettings.cypher_hints_error.getDefaultValue.toBoolean
)
if (((version != CypherVersion.v2_3) || (version != CypherVersion.v3_0)) && (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(graph, kernel, kernelMonitors, version, planner, runtime, useErrorsOverWarnings, logProvider)
}
private def getPlanCacheSize: Int =
optGraphSetting[java.lang.Integer](
graph, GraphDatabaseSettings.query_cache_size,
GraphDatabaseSettings.query_cache_size.getDefaultValue.toInt
)
private def optGraphSetting[V](graph: GraphDatabaseService, setting: Setting[V], defaultValue: V): V = {
def optGraphAs[T <: GraphDatabaseService : Manifest]: PartialFunction[GraphDatabaseService, T] = {
case (db: T) => db
}
optGraphAs[GraphDatabaseFacade]
.andThen(g => {
Option(g.platformModule.config.get(setting))
})
.andThen(_.getOrElse(defaultValue))
.applyOrElse(graph, (_: GraphDatabaseService) => defaultValue)
}
}
object ExecutionEngine {
val PLAN_BUILDING_TRIES: Int = 20
}