1
2
3
4
5
15 package main
16
17 import (
18 "os"
19 "runtime/pprof"
20 "sync"
21 "time"
22 "unsafe"
23 )
24
25 func init() {
26 register("Cockroach10214", Cockroach10214)
27 }
28
29 type Store_cockroach10214 struct {
30 coalescedMu struct {
31 sync.Mutex
32 heartbeatResponses []int
33 }
34 mu struct {
35 replicas map[int]*Replica_cockroach10214
36 }
37 }
38
39 func (s *Store_cockroach10214) sendQueuedHeartbeats() {
40 s.coalescedMu.Lock()
41 defer s.coalescedMu.Unlock()
42 for i := 0; i < len(s.coalescedMu.heartbeatResponses); i++ {
43 s.sendQueuedHeartbeatsToNode()
44 }
45 }
46
47 func (s *Store_cockroach10214) sendQueuedHeartbeatsToNode() {
48 for i := 0; i < len(s.mu.replicas); i++ {
49 r := s.mu.replicas[i]
50 r.reportUnreachable()
51 }
52 }
53
54 type Replica_cockroach10214 struct {
55 raftMu sync.Mutex
56 mu sync.Mutex
57 store *Store_cockroach10214
58 }
59
60 func (r *Replica_cockroach10214) reportUnreachable() {
61 r.raftMu.Lock()
62 time.Sleep(time.Millisecond)
63 defer r.raftMu.Unlock()
64 }
65
66 func (r *Replica_cockroach10214) tick() {
67 r.raftMu.Lock()
68 defer r.raftMu.Unlock()
69 r.tickRaftMuLocked()
70 }
71
72 func (r *Replica_cockroach10214) tickRaftMuLocked() {
73 r.mu.Lock()
74 defer r.mu.Unlock()
75 if r.maybeQuiesceLocked() {
76 return
77 }
78 }
79
80 func (r *Replica_cockroach10214) maybeQuiesceLocked() bool {
81 for i := 0; i < 2; i++ {
82 if !r.maybeCoalesceHeartbeat() {
83 return true
84 }
85 }
86 return false
87 }
88
89 func (r *Replica_cockroach10214) maybeCoalesceHeartbeat() bool {
90 msgtype := uintptr(unsafe.Pointer(r)) % 3
91 switch msgtype {
92 case 0, 1, 2:
93 r.store.coalescedMu.Lock()
94 default:
95 return false
96 }
97 r.store.coalescedMu.Unlock()
98 return true
99 }
100
101 func Cockroach10214() {
102 prof := pprof.Lookup("goroutineleak")
103 defer func() {
104 time.Sleep(100 * time.Millisecond)
105 prof.WriteTo(os.Stdout, 2)
106 }()
107 for i := 0; i < 1000; i++ {
108 go func() {
109 store := &Store_cockroach10214{}
110 responses := &store.coalescedMu.heartbeatResponses
111 *responses = append(*responses, 1, 2)
112 store.mu.replicas = make(map[int]*Replica_cockroach10214)
113
114 rp1 := &Replica_cockroach10214{
115 store: store,
116 }
117 rp2 := &Replica_cockroach10214{
118 store: store,
119 }
120 store.mu.replicas[0] = rp1
121 store.mu.replicas[1] = rp2
122
123 go store.sendQueuedHeartbeats()
124 go rp1.tick()
125 }()
126 }
127 }
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
View as plain text