-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgraphviz.go
More file actions
360 lines (322 loc) · 9.6 KB
/
Copy pathgraphviz.go
File metadata and controls
360 lines (322 loc) · 9.6 KB
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
//
// Copyright 2022 Google LLC
//
// 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 main
import (
"fmt"
"io"
"sort"
"sync"
"time"
"github.com/cloudspannerecosystem/spanner-change-streams-tail/changestreams"
)
const (
rootPartitionToken = "root"
)
type Partition struct {
Token string
StartTimestamp time.Time
EndTimestamp time.Time
RecordSequence string
Parents []*Partition
}
type MoveEvent struct {
Source string
Dest string
Timestamp time.Time
}
type PartitionVisualizer struct {
partitions map[string]*Partition
timestamps map[time.Time]bool
moves []MoveEvent
mu sync.Mutex
out io.Writer
}
func NewPartitionVisualizer(out io.Writer) *PartitionVisualizer {
partitions := make(map[string]*Partition)
// Root partition.
partitions[rootPartitionToken] = &Partition{Token: rootPartitionToken}
return &PartitionVisualizer{
partitions: partitions,
timestamps: make(map[time.Time]bool),
out: out,
}
}
func (v *PartitionVisualizer) Read(result *changestreams.ReadResult) error {
v.mu.Lock()
defer v.mu.Unlock()
hasImmutableKeyRange := false
for _, r := range result.ChangeRecords {
if len(r.ChildPartitionsRecords) > 0 {
hasImmutableKeyRange = true
break
}
}
if hasImmutableKeyRange {
return v.readImmutableKeyRange(result)
}
return v.readMutableKeyRange(result)
}
func (v *PartitionVisualizer) readImmutableKeyRange(result *changestreams.ReadResult) error {
for _, changeRecord := range result.ChangeRecords {
for _, partitionRecord := range changeRecord.ChildPartitionsRecords {
for _, childPartition := range partitionRecord.ChildPartitions {
token := childPartition.Token
p := v.getOrCreatePartition(token)
if !p.StartTimestamp.IsZero() {
continue
}
p.StartTimestamp = partitionRecord.StartTimestamp
p.RecordSequence = partitionRecord.RecordSequence
var parents []*Partition
for _, parentToken := range childPartition.ParentPartitionTokens {
parent := v.getOrCreatePartition(parentToken)
parents = append(parents, parent)
}
if len(parents) == 0 {
parents = append(parents, v.partitions[rootPartitionToken])
}
p.Parents = parents
}
}
}
return nil
}
func (v *PartitionVisualizer) readMutableKeyRange(result *changestreams.ReadResult) error {
currentPartitionToken := result.PartitionToken
for _, changeRecord := range result.ChangeRecords {
// Handle Partition Start
for _, startRecord := range changeRecord.PartitionStartRecords {
t := startRecord.StartTimestamp
v.touchPartition(currentPartitionToken, t)
for _, token := range startRecord.PartitionTokens {
p := v.touchPartition(token, t)
p.StartTimestamp = t
p.RecordSequence = startRecord.RecordSequence
if currentPartitionToken != "" {
parent := v.getOrCreatePartition(currentPartitionToken)
v.addParentLink(p, parent)
}
}
}
// Handle Partition End
for _, endRecord := range changeRecord.PartitionEndRecords {
t := endRecord.EndTimestamp
token := endRecord.PartitionToken
p := v.touchPartition(token, t)
p.EndTimestamp = t
p.RecordSequence = endRecord.RecordSequence
}
// Handle Partition Events
for _, eventRecord := range changeRecord.PartitionEventRecords {
t := eventRecord.CommitTimestamp
token := eventRecord.PartitionToken
v.touchPartition(token, t)
for _, moveIn := range eventRecord.MoveInEvents {
v.touchPartition(moveIn.SourcePartitionToken, t)
v.addMove(moveIn.SourcePartitionToken, token, t)
}
for _, moveOut := range eventRecord.MoveOutEvents {
v.touchPartition(moveOut.DestinationPartitionToken, t)
v.addMove(token, moveOut.DestinationPartitionToken, t)
}
}
}
return nil
}
func (v *PartitionVisualizer) getOrCreatePartition(token string) *Partition {
p, ok := v.partitions[token]
if !ok {
p = &Partition{Token: token}
v.partitions[token] = p
}
return p
}
func (v *PartitionVisualizer) touchPartition(token string, t time.Time) *Partition {
if token == "" {
return nil
}
v.timestamps[t] = true
p := v.getOrCreatePartition(token)
if p.StartTimestamp.IsZero() || t.Before(p.StartTimestamp) {
p.StartTimestamp = t
}
return p
}
func (v *PartitionVisualizer) addParentLink(child, parent *Partition) {
for _, p := range child.Parents {
if p.Token == parent.Token {
return
}
}
child.Parents = append(child.Parents, parent)
}
func (v *PartitionVisualizer) addMove(source, dest string, t time.Time) {
for _, m := range v.moves {
if m.Source == source && m.Dest == dest && m.Timestamp.Equal(t) {
return
}
}
v.moves = append(v.moves, MoveEvent{Source: source, Dest: dest, Timestamp: t})
}
func (v *PartitionVisualizer) Draw() {
fmt.Fprintf(v.out, "digraph {\n")
partitions := sortPartitions(v.partitions)
if len(v.timestamps) == 0 {
v.drawImmutableKeyRange(partitions)
} else {
v.drawMutableKeyRange(partitions)
}
fmt.Fprintf(v.out, "}\n")
}
func (v *PartitionVisualizer) drawImmutableKeyRange(partitions []*Partition) {
fmt.Fprintf(v.out, " node [shape=record];\n")
for _, partition := range partitions {
var timestamp string
if !partition.StartTimestamp.IsZero() {
timestamp = partition.StartTimestamp.Format(time.RFC3339)
}
fmt.Fprintf(v.out, ` "%s" [label="{token|start_timestamp|record_sequence}|{{%s}|{%s}|{%s}}"];`, partition.Token, partition.Token, timestamp, partition.RecordSequence)
fmt.Fprintln(v.out, "")
}
for _, partition := range partitions {
for _, parent := range partition.Parents {
fmt.Fprintf(v.out, ` "%s" -> "%s"`, parent.Token, partition.Token)
fmt.Fprintln(v.out, "")
}
}
}
func (v *PartitionVisualizer) drawMutableKeyRange(partitions []*Partition) {
var timelinePoints []time.Time
for t := range v.timestamps {
timelinePoints = append(timelinePoints, t)
}
sort.Slice(timelinePoints, func(i, j int) bool {
return timelinePoints[i].Before(timelinePoints[j])
})
// Assign aliases
tokenToAlias := make(map[string]string)
aliasCount := 1
for _, partition := range partitions {
if partition.Token == rootPartitionToken {
continue
}
alias := fmt.Sprintf("P%d", aliasCount)
tokenToAlias[partition.Token] = alias
aliasCount++
}
// Draw timeline spine
for i, t := range timelinePoints {
fmt.Fprintf(v.out, ` t%d [label="%s", shape=plaintext];`, i, t.Format(time.RFC3339))
fmt.Fprintln(v.out, "")
}
for i := 0; i < len(timelinePoints)-1; i++ {
fmt.Fprintf(v.out, ` t%d -> t%d [style=invis];`, i, i+1)
fmt.Fprintln(v.out, "")
}
// Draw partition bars
rankGroups := make(map[int][]string)
for _, partition := range partitions {
if partition.Token == rootPartitionToken {
continue
}
alias := tokenToAlias[partition.Token]
startBar := partition.StartTimestamp
endBar := partition.EndTimestamp
if endBar.IsZero() {
endBar = timelinePoints[len(timelinePoints)-1]
}
var activeNodes []string
for idx, t := range timelinePoints {
if (t.After(startBar) || t.Equal(startBar)) && (t.Before(endBar) || t.Equal(endBar)) {
nodeID := fmt.Sprintf("%s_t%d", alias, idx)
activeNodes = append(activeNodes, nodeID)
rankGroups[idx] = append(rankGroups[idx], nodeID)
// Style
label := ""
shape := "point"
if t.Equal(startBar) {
label = alias
shape = "box"
} else if t.Equal(partition.EndTimestamp) {
label = fmt.Sprintf("%s (ended)", alias)
shape = "box"
}
if shape == "point" {
fmt.Fprintf(v.out, ` "%s" [shape=point];`, nodeID)
} else {
fmt.Fprintf(v.out, ` "%s" [label="%s", shape=%s];`, nodeID, label, shape)
}
fmt.Fprintln(v.out, "")
}
}
// Link active nodes vertically
for i := 0; i < len(activeNodes)-1; i++ {
fmt.Fprintf(v.out, ` "%s" -> "%s" [penwidth=3, arrowhead=none];`, activeNodes[i], activeNodes[i+1])
fmt.Fprintln(v.out, "")
}
}
// Align with spine
var rankIndices []int
for idx := range rankGroups {
rankIndices = append(rankIndices, idx)
}
sort.Ints(rankIndices)
for _, idx := range rankIndices {
nodes := rankGroups[idx]
fmt.Fprintf(v.out, " { rank=same; t%d; ", idx)
for _, node := range nodes {
fmt.Fprintf(v.out, `"%s"; `, node)
}
fmt.Fprintf(v.out, "}\n")
}
// Draw move edges
for _, m := range v.moves {
idx := -1
for i, t := range timelinePoints {
if t.Equal(m.Timestamp) {
idx = i
break
}
}
if idx != -1 {
sourceAlias := tokenToAlias[m.Source]
destAlias := tokenToAlias[m.Dest]
fmt.Fprintf(v.out, ` "%s_t%d" -> "%s_t%d" [style=dashed, constraint=false, label="move"];`, sourceAlias, idx, destAlias, idx)
fmt.Fprintln(v.out, "")
}
}
// Draw legend
legend := "Partition Mapping:\\n"
for _, partition := range partitions {
if partition.Token == rootPartitionToken {
continue
}
alias := tokenToAlias[partition.Token]
legend += fmt.Sprintf("%s: %s\\n", alias, partition.Token)
}
fmt.Fprintf(v.out, " label=\"%s\";\n", legend)
fmt.Fprintf(v.out, " labelloc=\"b\";\n")
}
func sortPartitions(partitionsMap map[string]*Partition) []*Partition {
var partitions []*Partition
for _, p := range partitionsMap {
partitions = append(partitions, p)
}
sort.Slice(partitions, func(i, j int) bool {
return partitions[i].Token < partitions[j].Token
})
return partitions
}