/
memstore_tracing_utils.go
222 lines (189 loc) · 5.75 KB
/
memstore_tracing_utils.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
// Copyright 2015 The LUCI Authors.
//
// Licensed 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 memory
import (
"encoding/hex"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"sync/atomic"
)
var logMemCollectionFolder = flag.String(
"luci.gae.store_trace_folder", "",
"Set to a folder path to enable debugging traces to be dumped there. Set to '-' to dump to stdout.")
var logMemCollectionFolderTmp string
var logMemCollectionOnce sync.Once
var logMemCounter uint32
var logMemNameKey = "holds a string indicating the StoreDebuggingTraceName"
var stdoutLock sync.Mutex
func wrapTracingMemStore(store memStore) memStore {
var writer traceWriter
logNum := atomic.AddUint32(&logMemCounter, 1) - 1
collName := fmt.Sprintf("coll%d", logNum)
if *logMemCollectionFolder == "-" {
writer = func(format string, a ...interface{}) {
stdoutLock.Lock()
defer stdoutLock.Unlock()
fmt.Printf(format+"\n", a...)
}
} else {
logMemCollectionOnce.Do(func() {
var err error
logMemCollectionFolderTmp, err = ioutil.TempDir(*logMemCollectionFolder, "luci-gae-store_trace")
if err != nil {
panic(err)
}
logMemCollectionFolderTmp, err = filepath.Abs(logMemCollectionFolderTmp)
if err != nil {
panic(err)
}
fmt.Fprintf(os.Stderr, "Saving store trace files to %q\n", logMemCollectionFolderTmp)
})
if logMemCollectionFolderTmp == "" {
panic("unable to create folder for tracefiles")
}
lck := sync.Mutex{}
fname := fmt.Sprintf(filepath.Join(logMemCollectionFolderTmp, fmt.Sprintf("%d.trace", logNum)))
fil, err := os.Create(fname)
if err != nil {
panic(err)
}
writer = func(format string, a ...interface{}) {
lck.Lock()
defer lck.Unlock()
fmt.Fprintf(fil, format+"\n", a...)
}
runtime.SetFinalizer(&writer, func(_ *traceWriter) { fil.Close() })
}
writer("%s := newMemStore()", collName)
return &tracingMemStoreImpl{store, writer, collName, 0, false}
}
type traceWriter func(format string, a ...interface{})
type tracingMemStoreImpl struct {
i memStore
w traceWriter
collName string
// for the mutable store, this is a counter that increments for every
// Snapshot, and for snapshots, this is the number of the snapshot.
snapNum uint
isSnap bool
}
var _ memStore = (*tracingMemStoreImpl)(nil)
func (t *tracingMemStoreImpl) ImATestingSnapshot() {}
func (t *tracingMemStoreImpl) colWriter(action, name string) traceWriter {
ident := t.ident()
hexname := hex.EncodeToString([]byte(name))
writer := func(format string, a ...interface{}) {
if strings.HasPrefix(format, "//") { // comment
t.w(format, a...)
} else {
t.w(fmt.Sprintf("%s_%s%s", ident, hexname, format), a...)
}
}
writer(" := %s.%s(%q)", ident, action, name)
return writer
}
func (t *tracingMemStoreImpl) ident() string {
if t.isSnap {
return fmt.Sprintf("%s_snap%d", t.collName, t.snapNum)
}
return t.collName
}
func (t *tracingMemStoreImpl) GetCollection(name string) memCollection {
coll := t.i.GetCollection(name)
if coll == nil {
t.w("// %s.GetCollection(%q) -> nil", t.ident(), name)
return nil
}
writer := t.colWriter("GetCollection", name)
return &tracingMemCollectionImpl{coll, writer, 0, 0}
}
func (t *tracingMemStoreImpl) GetCollectionNames() []string {
t.w("%s.GetCollectionNames()", t.ident())
return t.i.GetCollectionNames()
}
func (t *tracingMemStoreImpl) GetOrCreateCollection(name string) memCollection {
writer := t.colWriter("GetOrCreateCollection", name)
return &tracingMemCollectionImpl{t.i.GetOrCreateCollection(name), writer, 0, 0}
}
func (t *tracingMemStoreImpl) Snapshot() memStore {
snap := t.i.Snapshot()
if snap == t.i {
t.w("// %s.Snapshot() -> self", t.ident())
return t
}
ret := &tracingMemStoreImpl{snap, t.w, t.collName, t.snapNum, true}
t.w("%s := %s.Snapshot()", ret.ident(), t.ident())
t.snapNum++
return ret
}
func (t *tracingMemStoreImpl) IsReadOnly() bool {
return t.i.IsReadOnly()
}
type tracingMemCollectionImpl struct {
i memCollection
w traceWriter
iterNumber uint
forEachNumber uint
}
var _ memCollection = (*tracingMemCollectionImpl)(nil)
func (t *tracingMemCollectionImpl) Name() string {
return t.i.Name()
}
func (t *tracingMemCollectionImpl) Delete(k []byte) {
t.w(".Delete(%#v)", k)
t.i.Delete(k)
}
func (t *tracingMemCollectionImpl) Get(k []byte) []byte {
t.w(".Get(%#v)", k)
return t.i.Get(k)
}
func (t *tracingMemCollectionImpl) MinItem() *storeEntry {
t.w(".MinItem()")
return t.i.MinItem()
}
func (t *tracingMemCollectionImpl) Set(k, v []byte) {
t.w(".Set(%#v, %#v)", k, v)
t.i.Set(k, v)
}
func (t *tracingMemCollectionImpl) Iterator(pivot []byte) memIterator {
inum := t.iterNumber
t.iterNumber++
t.w(".Iterator(%#v) // Created Iterator #%d", pivot, inum)
return &tracingMemIteratorImpl{t.i.Iterator(pivot), t.w, inum}
}
func (t *tracingMemCollectionImpl) ForEachItem(fn memVisitor) {
vnum := t.forEachNumber
t.forEachNumber++
t.w(".ForEachItem(func(k, v []byte) bool{ return true }) // BEGIN ForEachItem(%d)", vnum)
defer t.w("// END ForEachItem(%d)", vnum)
t.i.ForEachItem(fn)
}
func (t *tracingMemCollectionImpl) IsReadOnly() bool {
return t.i.IsReadOnly()
}
type tracingMemIteratorImpl struct {
i memIterator
w traceWriter
num uint
}
func (t *tracingMemIteratorImpl) Next() *storeEntry {
t.w(".Next() // Iterator #%d", t.num)
return t.i.Next()
}