-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ExecutingQuery.java
274 lines (244 loc) · 9.44 KB
/
ExecutingQuery.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
/*
* Copyright (c) 2002-2017 "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.kernel.api.query;
import org.apache.commons.lang3.builder.ToStringBuilder;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.function.LongSupplier;
import org.neo4j.io.pagecache.tracing.cursor.PageCursorCounters;
import org.neo4j.kernel.impl.locking.ActiveLock;
import org.neo4j.kernel.impl.locking.LockTracer;
import org.neo4j.kernel.impl.locking.LockWaitEvent;
import org.neo4j.kernel.impl.query.clientconnection.ClientConnectionInfo;
import org.neo4j.resources.HeapAllocation;
import org.neo4j.storageengine.api.lock.ResourceType;
import org.neo4j.resources.CpuClock;
import org.neo4j.time.SystemNanoClock;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.atomic.AtomicLongFieldUpdater.newUpdater;
/**
* Represents a currently running query.
*/
public class ExecutingQuery
{
private static final AtomicLongFieldUpdater<ExecutingQuery> WAIT_TIME =
newUpdater( ExecutingQuery.class, "waitTimeNanos" );
private final long queryId;
private final LockTracer lockTracer = this::waitForLock;
private final PageCursorCounters pageCursorCounters;
private final String username;
private final ClientConnectionInfo clientConnection;
private final String queryText;
private final Map<String,Object> queryParameters;
private final long startTimeNanos, startTimestampMillis;
/** Uses write barrier of {@link #status}. */
private long planningDoneNanos;
private final Thread threadExecutingTheQuery;
private final LongSupplier activeLockCount;
private final SystemNanoClock clock;
private final CpuClock cpuClock;
private final HeapAllocation heapAllocation;
private final long cpuTimeNanosWhenQueryStarted;
private final long heapAllocatedBytesWhenQueryStarted;
private final Map<String,Object> transactionAnnotationData;
/** Uses write barrier of {@link #status}. */
private PlannerInfo plannerInfo;
private volatile ExecutingQueryStatus status = SimpleState.planning();
/** Updated through {@link #WAIT_TIME} */
@SuppressWarnings( "unused" )
private volatile long waitTimeNanos;
public ExecutingQuery(
long queryId,
ClientConnectionInfo clientConnection,
String username,
String queryText,
Map<String,Object> queryParameters,
Map<String,Object> transactionAnnotationData,
LongSupplier activeLockCount,
PageCursorCounters pageCursorCounters,
Thread threadExecutingTheQuery,
SystemNanoClock clock,
CpuClock cpuClock,
HeapAllocation heapAllocation )
{
// Capture timestamps first
this.cpuTimeNanosWhenQueryStarted = cpuClock.cpuTimeNanos( threadExecutingTheQuery );
this.startTimeNanos = clock.nanos();
this.startTimestampMillis = clock.millis();
// then continue with assigning fields
this.queryId = queryId;
this.clientConnection = clientConnection;
this.pageCursorCounters = pageCursorCounters;
this.username = username;
this.queryText = queryText;
this.queryParameters = queryParameters;
this.transactionAnnotationData = transactionAnnotationData;
this.activeLockCount = activeLockCount;
this.threadExecutingTheQuery = threadExecutingTheQuery;
this.cpuClock = cpuClock;
this.heapAllocation = heapAllocation;
this.clock = clock;
this.heapAllocatedBytesWhenQueryStarted = heapAllocation.allocatedBytes( threadExecutingTheQuery );
}
// update state
public void planningCompleted( PlannerInfo plannerInfo )
{
this.plannerInfo = plannerInfo;
this.planningDoneNanos = clock.nanos();
this.status = SimpleState.running(); // write barrier - must be last
}
public LockTracer lockTracer()
{
return lockTracer;
}
public void waitsForQuery( ExecutingQuery child )
{
if ( child == null )
{
WAIT_TIME.addAndGet( this, status.waitTimeNanos( clock.nanos() ) );
this.status = SimpleState.running();
}
else
{
this.status = new WaitingOnQuery( child, clock.nanos() );
}
}
// snapshot state
public QuerySnapshot snapshot()
{
// capture a consistent snapshot of the "live" state
ExecutingQueryStatus status;
long waitTimeNanos, currentTimeNanos, cpuTimeNanos;
do
{
status = this.status; // read barrier, must be first
waitTimeNanos = this.waitTimeNanos; // the reason for the retry loop: don't count the wait time twice
cpuTimeNanos = cpuClock.cpuTimeNanos( threadExecutingTheQuery );
currentTimeNanos = clock.nanos(); // capture the time as close to the snapshot as possible
}
while ( this.status != status );
// guarded by barrier - unused if status is planning, stable otherwise
long planningDoneNanos = this.planningDoneNanos;
// guarded by barrier - like planningDoneNanos
PlannerInfo planner = status.isPlanning() ? null : this.plannerInfo;
// just needs to be captured at some point...
long activeLockCount = this.activeLockCount.getAsLong();
long heapAllocatedBytes = heapAllocation.allocatedBytes( threadExecutingTheQuery );
PageCounterValues pageCounters = new PageCounterValues( pageCursorCounters );
// - at this point we are done capturing the "live" state, and can start computing the snapshot -
long planningTimeNanos = (status.isPlanning() ? currentTimeNanos : planningDoneNanos) - startTimeNanos;
long elapsedTimeNanos = currentTimeNanos - startTimeNanos;
cpuTimeNanos -= cpuTimeNanosWhenQueryStarted;
waitTimeNanos += status.waitTimeNanos( currentTimeNanos );
// TODO: when we start allocating native memory as well during query execution,
// we should have a tracer that keeps track of how much memory we have allocated for the query,
// and get the value from that here.
heapAllocatedBytes = heapAllocatedBytesWhenQueryStarted < 0 ? -1 : // mark that we were unable to measure
heapAllocatedBytes - heapAllocatedBytesWhenQueryStarted;
return new QuerySnapshot(
this,
planner,
pageCounters,
NANOSECONDS.toMillis( planningTimeNanos ),
NANOSECONDS.toMillis( elapsedTimeNanos ),
cpuTimeNanos == 0 && cpuTimeNanosWhenQueryStarted == -1 ? -1 : NANOSECONDS.toMillis( cpuTimeNanos ),
NANOSECONDS.toMillis( waitTimeNanos ),
status.name(),
status.toMap( currentTimeNanos ),
activeLockCount,
heapAllocatedBytes
);
}
// basic methods
@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
ExecutingQuery that = (ExecutingQuery) o;
return queryId == that.queryId;
}
@Override
public int hashCode()
{
return (int) (queryId ^ (queryId >>> 32));
}
@Override
public String toString()
{
return ToStringBuilder.reflectionToString( this );
}
// access stable state
public long internalQueryId()
{
return queryId;
}
public String username()
{
return username;
}
public String queryText()
{
return queryText;
}
public Map<String,Object> queryParameters()
{
return queryParameters;
}
public long startTimestampMillis()
{
return startTimestampMillis;
}
public Map<String,Object> transactionAnnotationData()
{
return transactionAnnotationData;
}
ClientConnectionInfo clientConnection()
{
return clientConnection;
}
private LockWaitEvent waitForLock( boolean exclusive, ResourceType resourceType, long[] resourceIds )
{
WaitingOnLockEvent event = new WaitingOnLockEvent(
exclusive ? ActiveLock.EXCLUSIVE_MODE : ActiveLock.SHARED_MODE,
resourceType,
resourceIds,
this,
clock.nanos(),
status );
status = event;
return event;
}
void doneWaitingOnLock( WaitingOnLockEvent waiting )
{
if ( status != waiting )
{
return; // already closed
}
WAIT_TIME.addAndGet( this, waiting.waitTimeNanos( clock.nanos() ) );
status = waiting.previousStatus();
}
}