-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
QueryLoggerTest.java
316 lines (277 loc) · 12.3 KB
/
QueryLoggerTest.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
/*
* 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 Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.query;
import java.time.Clock;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.neo4j.kernel.api.ExecutingQuery;
import org.neo4j.kernel.impl.query.QueryLoggerKernelExtension.QueryLogger;
import org.neo4j.kernel.impl.query.clientsession.ClientSessionInfo;
import org.neo4j.kernel.impl.query.clientsession.ShellSessionInfo;
import org.neo4j.logging.AssertableLogProvider;
import org.neo4j.logging.LogProvider;
import org.neo4j.time.Clocks;
import org.neo4j.time.CpuClock;
import org.neo4j.time.FakeClock;
import static java.lang.String.format;
import static java.util.Collections.emptyMap;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.sameInstance;
import static org.hamcrest.core.Is.is;
import static org.neo4j.helpers.collection.MapUtil.map;
import static org.neo4j.logging.AssertableLogProvider.inLog;
public class QueryLoggerTest
{
private static final ClientSessionInfo SESSION_1 = new ShellSessionInfo( "{session one}" );
private static final ClientSessionInfo SESSION_2 = new ShellSessionInfo( "{session two}" );
private static final ClientSessionInfo SESSION_3 = new ShellSessionInfo( "{session three}" );
private static final String QUERY_1 = "MATCH (n) RETURN n";
private static final String QUERY_2 = "MATCH (a)--(b) RETURN b.name";
private static final String QUERY_3 = "MATCH (c)-[:FOO]->(d) RETURN d.size";
private static final String QUERY_4 = "MATCH (n) WHERE n.age IN {ages} RETURN n";
@Test
public void shouldLogQuerySlowerThanThreshold() throws Exception
{
// given
final AssertableLogProvider logProvider = new AssertableLogProvider();
ExecutingQuery query = query( 0, SESSION_1, "TestUser", QUERY_1 );
FakeClock clock = Clocks.fakeClock();
QueryLogger queryLogger = queryLoggerWithoutParams( logProvider, clock );
// when
queryLogger.startQueryExecution( query );
clock.forward( 11, TimeUnit.MILLISECONDS );
queryLogger.endSuccess( query );
// then
String expectedSessionString = sessionConnectionDetails( SESSION_1, "TestUser" );
logProvider.assertExactly(
inLog( getClass() ).info( format( "%d ms: %s - %s - {}", 11L, expectedSessionString, QUERY_1 ) )
);
}
@Test
public void shouldNotLogQueryFasterThanThreshold() throws Exception
{
// given
final AssertableLogProvider logProvider = new AssertableLogProvider();
ExecutingQuery query = query( 0, SESSION_1, "TestUser", QUERY_1 );
FakeClock clock = Clocks.fakeClock();
QueryLogger queryLogger = queryLoggerWithoutParams( logProvider, clock );
// when
queryLogger.startQueryExecution( query );
clock.forward( 9, TimeUnit.MILLISECONDS );
queryLogger.endSuccess( query );
// then
logProvider.assertNoLoggingOccurred();
}
@Test
public void shouldKeepTrackOfDifferentSessions() throws Exception
{
// given
final AssertableLogProvider logProvider = new AssertableLogProvider();
ExecutingQuery query1 = query( 0, SESSION_1, "TestUser1", QUERY_1 );
ExecutingQuery query2 = query( 1, SESSION_2, "TestUser2", QUERY_2 );
ExecutingQuery query3 = query( 2, SESSION_3, "TestUser3", QUERY_3 );
FakeClock clock = Clocks.fakeClock();
QueryLogger queryLogger = queryLoggerWithoutParams( logProvider, clock );
// when
queryLogger.startQueryExecution( query1 );
clock.forward( 1, TimeUnit.MILLISECONDS );
queryLogger.startQueryExecution( query2 );
clock.forward( 1, TimeUnit.MILLISECONDS );
queryLogger.startQueryExecution( query3 );
clock.forward( 7, TimeUnit.MILLISECONDS );
queryLogger.endSuccess( query3 );
clock.forward( 7, TimeUnit.MILLISECONDS );
queryLogger.endSuccess( query2 );
clock.forward( 7, TimeUnit.MILLISECONDS );
queryLogger.endSuccess( query1 );
// then
String expectedSession1String = sessionConnectionDetails( SESSION_1, "TestUser1" );
String expectedSession2String = sessionConnectionDetails( SESSION_2, "TestUser2" );
logProvider.assertExactly(
inLog( getClass() ).info( format( "%d ms: %s - %s - {}", 15L, expectedSession2String, QUERY_2 ) ),
inLog( getClass() ).info( format( "%d ms: %s - %s - {}", 23L, expectedSession1String, QUERY_1 ) )
);
}
@Test
public void shouldLogQueryOnFailureEvenIfFasterThanThreshold() throws Exception
{
// given
final AssertableLogProvider logProvider = new AssertableLogProvider();
ExecutingQuery query = query( 0, SESSION_1, "TestUser", QUERY_1 );
FakeClock clock = Clocks.fakeClock();
QueryLogger queryLogger = queryLoggerWithoutParams( logProvider, clock );
RuntimeException failure = new RuntimeException();
// when
queryLogger.startQueryExecution( query );
clock.forward( 1, TimeUnit.MILLISECONDS );
queryLogger.endFailure( query, failure );
// then
logProvider.assertExactly(
inLog( getClass() )
.error( is( "1 ms: " + sessionConnectionDetails( SESSION_1, "TestUser" )
+ " - MATCH (n) RETURN n - {}" ), sameInstance( failure ) )
);
}
@Test
public void shouldLogQueryParameters() throws Exception
{
// given
final AssertableLogProvider logProvider = new AssertableLogProvider();
Map<String,Object> params = new HashMap<>();
params.put( "ages", Arrays.asList( 41, 42, 43 ) );
ExecutingQuery query = query( 0,
SESSION_1, "TestUser", QUERY_4, params, emptyMap()
);
FakeClock clock = Clocks.fakeClock();
QueryLogger queryLogger = queryLoggerWithParams( logProvider, clock );
// when
queryLogger.startQueryExecution( query );
clock.forward( 11, TimeUnit.MILLISECONDS );
queryLogger.endSuccess( query );
// then
String expectedSessionString = sessionConnectionDetails( SESSION_1, "TestUser" );
logProvider.assertExactly(
inLog( getClass() ).info( format( "%d ms: %s - %s - %s - {}", 11L, expectedSessionString, QUERY_4,
"{ages: " +
"[41, 42, 43]}" ) )
);
}
@Test
public void shouldLogQueryParametersOnFailure() throws Exception
{
// given
final AssertableLogProvider logProvider = new AssertableLogProvider();
Map<String,Object> params = new HashMap<>();
params.put( "ages", Arrays.asList( 41, 42, 43 ) );
ExecutingQuery query = query( 0,
SESSION_1, "TestUser", QUERY_4, params, emptyMap()
);
FakeClock clock = Clocks.fakeClock();
QueryLogger queryLogger = queryLoggerWithParams( logProvider, clock );
RuntimeException failure = new RuntimeException();
// when
queryLogger.startQueryExecution( query );
clock.forward( 1, TimeUnit.MILLISECONDS );
queryLogger.endFailure( query, failure );
// then
logProvider.assertExactly(
inLog( getClass() ).error(
is( "1 ms: " + sessionConnectionDetails( SESSION_1, "TestUser" )
+ " - MATCH (n) WHERE n.age IN {ages} RETURN n - {ages: [41, 42, 43]} - {}" ),
sameInstance( failure ) )
);
}
@Test
public void shouldLogUserName() throws Exception
{
// given
final AssertableLogProvider logProvider = new AssertableLogProvider();
FakeClock clock = Clocks.fakeClock();
QueryLogger queryLogger = queryLoggerWithoutParams( logProvider, clock );
// when
ExecutingQuery query = query( 0, SESSION_1, "TestUser", QUERY_1 );
queryLogger.startQueryExecution( query );
clock.forward( 10, TimeUnit.MILLISECONDS );
queryLogger.endSuccess( query );
ExecutingQuery anotherQuery = query( 10, SESSION_1, "AnotherUser", QUERY_1 );
queryLogger.startQueryExecution( anotherQuery );
clock.forward( 10, TimeUnit.MILLISECONDS );
queryLogger.endSuccess( anotherQuery );
// then
logProvider.assertExactly(
inLog( getClass() ).info( format( "%d ms: %s - %s - {}", 10L,
sessionConnectionDetails( SESSION_1, "TestUser" ), QUERY_1 ) ),
inLog( getClass() ).info( format( "%d ms: %s - %s - {}", 10L,
sessionConnectionDetails( SESSION_1, "AnotherUser" ), QUERY_1 ) )
);
}
@Test
public void shouldLogMetaData() throws Exception
{
// given
final AssertableLogProvider logProvider = new AssertableLogProvider();
FakeClock clock = Clocks.fakeClock();
QueryLogger queryLogger = queryLoggerWithoutParams( logProvider, clock );
// when
ExecutingQuery query = query( 0,
SESSION_1,
"TestUser", QUERY_1, emptyMap(), map( "User", "UltiMate" )
);
queryLogger.startQueryExecution( query );
clock.forward( 10, TimeUnit.MILLISECONDS );
queryLogger.endSuccess( query );
ExecutingQuery anotherQuery =
query( 10, SESSION_1, "AnotherUser", QUERY_1, emptyMap(), map( "Place", "Town" ) );
queryLogger.startQueryExecution( anotherQuery );
clock.forward( 10, TimeUnit.MILLISECONDS );
Throwable error = new Throwable();
queryLogger.endFailure( anotherQuery, error );
// then
logProvider.assertExactly(
inLog( getClass() ).info( format( "%d ms: %s - %s - {User: 'UltiMate'}", 10L,
sessionConnectionDetails( SESSION_1, "TestUser" ), QUERY_1
) ),
inLog( getClass() ).error(
equalTo( format( "%d ms: %s - %s - {Place: 'Town'}", 10L,
sessionConnectionDetails( SESSION_1, "AnotherUser" ), QUERY_1 ) ),
sameInstance( error ) )
);
}
private QueryLogger queryLoggerWithoutParams( LogProvider logProvider, Clock clock )
{
return new QueryLogger( clock, logProvider.getLog( getClass() ), 10/*ms*/, false );
}
private QueryLogger queryLoggerWithParams( LogProvider logProvider, Clock clock )
{
return new QueryLogger( clock, logProvider.getLog( getClass() ), 10/*ms*/, true );
}
private ExecutingQuery query(
long startTime,
ClientSessionInfo sessionInfo,
String username,
String queryText )
{
return query( startTime, sessionInfo, username, queryText, emptyMap(), emptyMap() );
}
private String sessionConnectionDetails( ClientSessionInfo sessionInfo, String username )
{
return sessionInfo.withUsername( username ).asConnectionDetails();
}
private int queryId;
private ExecutingQuery query(
long startTime, ClientSessionInfo sessionInfo, String username, String queryText, Map<String,Object> params,
Map<String,Object> metaData
)
{
FakeClock clock = Clocks.fakeClock( startTime, TimeUnit.MILLISECONDS );
return new ExecutingQuery( queryId++,
sessionInfo.withUsername( username ),
username,
queryText,
params,
metaData,
Thread.currentThread(),
clock,
CpuClock.CPU_CLOCK );
}
}