/
Tracing.java
368 lines (309 loc) · 11.5 KB
/
Tracing.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.apache.cassandra.tracing;
import java.io.IOException;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.cassandra.concurrent.ExecutorLocals;
import org.apache.cassandra.config.DatabaseDescriptor;
import org.apache.cassandra.io.IVersionedSerializer;
import org.apache.cassandra.io.util.DataInputPlus;
import org.apache.cassandra.io.util.DataOutputPlus;
import org.apache.cassandra.locator.InetAddressAndPort;
import org.apache.cassandra.net.Message;
import org.apache.cassandra.net.ParamType;
import org.apache.cassandra.utils.FBUtilities;
import org.apache.cassandra.utils.JVMStabilityInspector;
import org.apache.cassandra.utils.TimeUUID;
import static org.apache.cassandra.config.CassandraRelevantProperties.CUSTOM_TRACING_CLASS;
import static org.apache.cassandra.utils.TimeUUID.Generator.nextTimeUUID;
/**
* A trace session context. Able to track and store trace sessions. A session is usually a user initiated query, and may
* have multiple local and remote events before it is completed.
*/
public abstract class Tracing extends ExecutorLocals.Impl
{
public static final IVersionedSerializer<TraceType> traceTypeSerializer = new IVersionedSerializer<TraceType>()
{
public void serialize(TraceType traceType, DataOutputPlus out, int version) throws IOException
{
out.write((byte)traceType.ordinal());
}
public TraceType deserialize(DataInputPlus in, int version) throws IOException
{
return TraceType.deserialize(in.readByte());
}
public long serializedSize(TraceType traceType, int version)
{
return 1;
}
};
/* this enum is used in serialization; preserve order for compatibility */
public enum TraceType
{
NONE,
QUERY,
REPAIR;
private static final TraceType[] ALL_VALUES = values();
public static TraceType deserialize(byte b)
{
if (b < 0 || ALL_VALUES.length <= b)
return NONE;
return ALL_VALUES[b];
}
public static byte serialize(TraceType value)
{
return (byte) value.ordinal();
}
private static final int[] TTLS = { DatabaseDescriptor.getTracetypeQueryTTL(),
DatabaseDescriptor.getTracetypeQueryTTL(),
DatabaseDescriptor.getTracetypeRepairTTL() };
public int getTTL()
{
return TTLS[ordinal()];
}
}
protected static final Logger logger = LoggerFactory.getLogger(Tracing.class);
private final InetAddressAndPort localAddress = FBUtilities.getLocalAddressAndPort();
protected final ConcurrentMap<TimeUUID, TraceState> sessions = new ConcurrentHashMap<>();
public static final Tracing instance;
static
{
Tracing tracing = null;
String customTracingClass = CUSTOM_TRACING_CLASS.getString();
if (null != customTracingClass)
{
try
{
tracing = FBUtilities.construct(customTracingClass, "Tracing");
logger.info("Using the {} class to trace queries (as requested by the {} system property)",
customTracingClass, CUSTOM_TRACING_CLASS.getKey());
}
catch (Exception e)
{
JVMStabilityInspector.inspectThrowable(e);
logger.error(String.format("Cannot use class %s for tracing, ignoring by defaulting to normal tracing", customTracingClass), e);
}
}
instance = null != tracing ? tracing : new TracingImpl();
}
public TimeUUID getSessionId()
{
assert isTracing();
return get().sessionId;
}
public TraceType getTraceType()
{
assert isTracing();
return get().traceType;
}
public int getTTL()
{
assert isTracing();
return get().ttl;
}
/**
* Indicates if the current thread's execution is being traced.
*/
public static boolean isTracing()
{
return instance.get() != null;
}
public TimeUUID newSession(Map<String,ByteBuffer> customPayload)
{
return newSession(
nextTimeUUID(),
TraceType.QUERY,
customPayload);
}
public TimeUUID newSession(TraceType traceType)
{
return newSession(
nextTimeUUID(),
traceType,
Collections.EMPTY_MAP);
}
public TimeUUID newSession(TimeUUID sessionId, Map<String,ByteBuffer> customPayload)
{
return newSession(sessionId, TraceType.QUERY, customPayload);
}
/** This method is intended to be overridden in tracing implementations that need access to the customPayload */
protected TimeUUID newSession(TimeUUID sessionId, TraceType traceType, Map<String,ByteBuffer> customPayload)
{
assert get() == null;
TraceState ts = newTraceState(localAddress, sessionId, traceType);
set(ts);
sessions.put(sessionId, ts);
return sessionId;
}
public void doneWithNonLocalSession(TraceState state)
{
if (state.releaseReference() == 0)
sessions.remove(state.sessionId);
}
/**
* Stop the session and record its complete. Called by coodinator when request is complete.
*/
public void stopSession()
{
TraceState state = get();
if (state == null) // inline isTracing to avoid implicit two calls to state.get()
{
logger.trace("request complete");
}
else
{
stopSessionImpl();
state.stop();
sessions.remove(state.sessionId);
set(null);
}
}
protected abstract void stopSessionImpl();
public TraceState get()
{
return ExecutorLocals.current().traceState;
}
public TraceState get(TimeUUID sessionId)
{
return sessions.get(sessionId);
}
public void set(TraceState tls)
{
ExecutorLocals current = ExecutorLocals.current();
ExecutorLocals.Impl.set(tls, current.clientWarnState);
}
public TraceState begin(final String request, final Map<String, String> parameters)
{
return begin(request, null, parameters);
}
public abstract TraceState begin(String request, InetAddress client, Map<String, String> parameters);
/**
* Determines the tracing context from a message. Does NOT set the threadlocal state.
*
* @param header The internode message header
*/
public TraceState initializeFromMessage(final Message.Header header)
{
final TimeUUID sessionId = header.traceSession();
if (sessionId == null)
return null;
TraceState ts = get(sessionId);
if (ts != null && ts.acquireReference())
return ts;
TraceType traceType = header.traceType();
if (header.verb.isResponse())
{
// received a message for a session we've already closed out. see CASSANDRA-5668
return new ExpiredTraceState(newTraceState(header.from, sessionId, traceType));
}
else
{
ts = newTraceState(header.from, sessionId, traceType);
sessions.put(sessionId, ts);
return ts;
}
}
/**
* Record any tracing data, if enabled on this message.
*/
public void traceOutgoingMessage(Message<?> message, int serializedSize, InetAddressAndPort sendTo)
{
try
{
final TimeUUID sessionId = message.traceSession();
if (sessionId == null)
return;
String logMessage = String.format("Sending %s message to %s message size %d bytes", message.verb(), sendTo,
serializedSize);
TraceState state = get(sessionId);
if (state == null) // session may have already finished; see CASSANDRA-5668
{
TraceType traceType = message.traceType();
trace(sessionId.toBytes(), logMessage, traceType.getTTL());
}
else
{
state.trace(logMessage);
if (message.verb().isResponse())
doneWithNonLocalSession(state);
}
}
catch (Exception e)
{
logger.warn("failed to capture the tracing info for an outbound message to {}, ignoring", sendTo, e);
}
}
public Map<ParamType, Object> addTraceHeaders(Map<ParamType, Object> addToMutable)
{
assert isTracing();
addToMutable.put(ParamType.TRACE_SESSION, Tracing.instance.getSessionId());
addToMutable.put(ParamType.TRACE_TYPE, Tracing.instance.getTraceType());
return addToMutable;
}
protected abstract TraceState newTraceState(InetAddressAndPort coordinator, TimeUUID sessionId, Tracing.TraceType traceType);
// repair just gets a varargs method since it's so heavyweight anyway
public static void traceRepair(String format, Object... args)
{
final TraceState state = instance.get();
if (state == null) // inline isTracing to avoid implicit two calls to state.get()
return;
state.trace(format, args);
}
// normal traces get zero-, one-, and two-argument overloads so common case doesn't need to create varargs array
public static void trace(String message)
{
final TraceState state = instance.get();
if (state == null) // inline isTracing to avoid implicit two calls to state.get()
return;
state.trace(message);
}
public static void trace(String format, Object arg)
{
final TraceState state = instance.get();
if (state == null) // inline isTracing to avoid implicit two calls to state.get()
return;
state.trace(format, arg);
}
public static void trace(String format, Object arg1, Object arg2)
{
final TraceState state = instance.get();
if (state == null) // inline isTracing to avoid implicit two calls to state.get()
return;
state.trace(format, arg1, arg2);
}
public static void trace(String format, Object... args)
{
final TraceState state = instance.get();
if (state == null) // inline isTracing to avoid implicit two calls to state.get()
return;
state.trace(format, args);
}
/**
* Called for non-local traces (traces that are not initiated by local node == coordinator).
*/
public abstract void trace(ByteBuffer sessionId, String message, int ttl);
}