forked from m3db/m3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
383 lines (313 loc) · 7.98 KB
/
context.go
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package context
import (
stdctx "context"
"sync"
xopentracing "github.com/m3db/m3/src/x/opentracing"
"github.com/m3db/m3/src/x/resource"
lightstep "github.com/lightstep/lightstep-tracer-go"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/uber/jaeger-client-go"
)
var (
noopTracer opentracing.NoopTracer
)
// NB(r): using golang.org/x/net/context is too GC expensive.
// Instead, we just embed one.
type ctx struct {
sync.RWMutex
goCtx stdctx.Context
pool contextPool
done bool
wg sync.WaitGroup
finalizeables *finalizeableList
parent Context
checkedAndNotSampled bool
}
type finalizeable struct {
finalizer resource.Finalizer
closer resource.Closer
}
// NewContext creates a new context.
func NewContext() Context {
return newContext()
}
// NewPooledContext returns a new context that is returned to a pool when closed.
func newPooledContext(pool contextPool) Context {
return &ctx{pool: pool}
}
// newContext returns an empty ctx
func newContext() *ctx {
return &ctx{}
}
func (c *ctx) GoContext() (stdctx.Context, bool) {
if c.goCtx == nil {
return nil, false
}
return c.goCtx, true
}
func (c *ctx) SetGoContext(v stdctx.Context) {
c.goCtx = v
}
func (c *ctx) IsClosed() bool {
parent := c.parentCtx()
if parent != nil {
return parent.IsClosed()
}
c.RLock()
done := c.done
c.RUnlock()
return done
}
func (c *ctx) RegisterFinalizer(f resource.Finalizer) {
parent := c.parentCtx()
if parent != nil {
parent.RegisterFinalizer(f)
return
}
c.registerFinalizeable(finalizeable{finalizer: f})
}
func (c *ctx) RegisterCloser(f resource.Closer) {
parent := c.parentCtx()
if parent != nil {
parent.RegisterCloser(f)
return
}
c.registerFinalizeable(finalizeable{closer: f})
}
func (c *ctx) registerFinalizeable(f finalizeable) {
if c.Lock(); c.done {
c.Unlock()
return
}
if c.finalizeables == nil {
if c.pool != nil {
c.finalizeables = c.pool.getFinalizeablesList()
} else {
c.finalizeables = newFinalizeableList(nil)
}
}
c.finalizeables.PushBack(f)
c.Unlock()
}
func (c *ctx) numFinalizeables() int {
if c.finalizeables == nil {
return 0
}
return c.finalizeables.Len()
}
func (c *ctx) DependsOn(blocker Context) {
parent := c.parentCtx()
if parent != nil {
parent.DependsOn(blocker)
return
}
c.Lock()
if !c.done {
c.wg.Add(1)
blocker.RegisterFinalizer(c)
}
c.Unlock()
}
// Finalize handles a call from another context that was depended upon closing.
func (c *ctx) Finalize() {
c.wg.Done()
}
type closeMode int
const (
closeAsync closeMode = iota
closeBlock
)
type returnToPoolMode int
const (
returnToPool returnToPoolMode = iota
reuse
)
func (c *ctx) Close() {
returnMode := returnToPool
parent := c.parentCtx()
if parent != nil {
if !parent.IsClosed() {
parent.Close()
}
c.tryReturnToPool(returnMode)
return
}
c.close(closeAsync, returnMode)
}
func (c *ctx) BlockingClose() {
returnMode := returnToPool
parent := c.parentCtx()
if parent != nil {
if !parent.IsClosed() {
parent.BlockingClose()
}
c.tryReturnToPool(returnMode)
return
}
c.close(closeBlock, returnMode)
}
func (c *ctx) BlockingCloseReset() {
returnMode := reuse
parent := c.parentCtx()
if parent != nil {
if !parent.IsClosed() {
parent.BlockingCloseReset()
}
c.tryReturnToPool(returnMode)
return
}
c.close(closeBlock, returnMode)
c.Reset()
}
func (c *ctx) close(mode closeMode, returnMode returnToPoolMode) {
if c.Lock(); c.done {
c.Unlock()
return
}
c.done = true
// Capture finalizeables to avoid concurrent r/w if Reset
// is used after a caller waits for the finalizers to finish
f := c.finalizeables
c.finalizeables = nil
c.Unlock()
if f == nil {
c.tryReturnToPool(returnMode)
return
}
switch mode {
case closeAsync:
go c.finalize(f, returnMode)
case closeBlock:
c.finalize(f, returnMode)
}
}
func (c *ctx) finalize(f *finalizeableList, returnMode returnToPoolMode) {
// Wait for dependencies.
c.wg.Wait()
// Now call finalizers.
for elem := f.Front(); elem != nil; elem = elem.Next() {
if elem.Value.finalizer != nil {
elem.Value.finalizer.Finalize()
}
if elem.Value.closer != nil {
elem.Value.closer.Close()
}
}
if c.pool != nil {
// NB(r): Always return finalizeables, only the
// context itself might want to be reused immediately.
c.pool.putFinalizeablesList(f)
}
c.tryReturnToPool(returnMode)
}
func (c *ctx) Reset() {
parent := c.parentCtx()
if parent != nil {
parent.Reset()
return
}
c.Lock()
c.done, c.finalizeables, c.goCtx, c.checkedAndNotSampled = false, nil, nil, false
c.Unlock()
}
func (c *ctx) tryReturnToPool(returnMode returnToPoolMode) {
if c.pool == nil || returnMode != returnToPool {
return
}
c.Reset()
c.pool.Put(c)
}
func (c *ctx) newChildContext() Context {
var childCtx *ctx
if c.pool != nil {
pooled, ok := c.pool.Get().(*ctx)
if ok {
childCtx = pooled
}
}
if childCtx == nil {
childCtx = newContext()
}
childCtx.setParentCtx(c)
return childCtx
}
func (c *ctx) setParentCtx(parentCtx Context) {
c.Lock()
c.parent = parentCtx
c.Unlock()
}
func (c *ctx) parentCtx() Context {
c.RLock()
parent := c.parent
c.RUnlock()
return parent
}
func (c *ctx) StartSampledTraceSpan(name string) (Context, opentracing.Span, bool) {
goCtx, exists := c.GoContext()
if !exists || c.checkedAndNotSampled {
return c, noopTracer.StartSpan(name), false
}
childGoCtx, span, sampled := StartSampledTraceSpan(goCtx, name)
if !sampled {
c.checkedAndNotSampled = true
return c, noopTracer.StartSpan(name), false
}
child := c.newChildContext()
child.SetGoContext(childGoCtx)
return child, span, true
}
func (c *ctx) StartTraceSpan(name string) (Context, opentracing.Span) {
ctx, sp, _ := c.StartSampledTraceSpan(name)
return ctx, sp
}
// StartSampledTraceSpan starts a span that may or may not be sampled and will
// return whether it was sampled or not.
func StartSampledTraceSpan(ctx stdctx.Context, name string, opts ...opentracing.StartSpanOption) (stdctx.Context, opentracing.Span, bool) {
sp, spCtx := xopentracing.StartSpanFromContext(ctx, name, opts...)
sampled := spanIsSampled(sp)
if !sampled {
return ctx, noopTracer.StartSpan(name), false
}
return spCtx, sp, true
}
func spanIsSampled(sp opentracing.Span) bool {
if sp == nil {
return false
}
// Until OpenTracing supports the `IsSampled()` method, we need to cast to a Jaeger/Lightstep/etc. spans.
// See https://github.com/opentracing/specification/issues/92 for more information.
spanCtx := sp.Context()
jaegerSpCtx, ok := spanCtx.(jaeger.SpanContext)
if ok && jaegerSpCtx.IsSampled() {
return true
}
lightstepSpCtx, ok := spanCtx.(lightstep.SpanContext)
if ok && lightstepSpCtx.TraceID != 0 {
return true
}
mockSpCtx, ok := spanCtx.(mocktracer.MockSpanContext)
if ok && mockSpCtx.Sampled {
return true
}
return false
}