Source file src/runtime/testdata/testgoroutineleakprofile/goker/cockroach3710.go

     1  // Copyright 2025 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a MIT
     3  // license that can be found in the LICENSE file.
     4  
     5  /*
     6   * Project: cockroach
     7   * Issue or PR  : https://github.com/cockroachdb/cockroach/pull/3710
     8   * Buggy version: 4afdd4860fd7c3bd9e92489f84a95e5cc7d11a0d
     9   * fix commit-id: cb65190f9caaf464723e7d072b1f1b69a044ef7b
    10   * Flaky: 2/100
    11   */
    12  
    13  package main
    14  
    15  import (
    16  	"os"
    17  	"runtime"
    18  	"runtime/pprof"
    19  	"sync"
    20  	"time"
    21  	"unsafe"
    22  )
    23  
    24  func init() {
    25  	register("Cockroach3710", Cockroach3710)
    26  }
    27  
    28  type Store_cockroach3710 struct {
    29  	raftLogQueue *baseQueue
    30  	replicas     map[int]*Replica_cockroach3710
    31  
    32  	mu struct {
    33  		sync.RWMutex
    34  	}
    35  }
    36  
    37  func (s *Store_cockroach3710) ForceRaftLogScanAndProcess() {
    38  	s.mu.RLock()
    39  	runtime.Gosched()
    40  	for _, r := range s.replicas {
    41  		s.raftLogQueue.MaybeAdd(r)
    42  	}
    43  	s.mu.RUnlock()
    44  }
    45  
    46  func (s *Store_cockroach3710) RaftStatus() {
    47  	s.mu.RLock()
    48  	defer s.mu.RUnlock()
    49  }
    50  
    51  func (s *Store_cockroach3710) processRaft() {
    52  	go func() {
    53  		for {
    54  			var replicas []*Replica_cockroach3710
    55  			s.mu.Lock()
    56  			for _, r := range s.replicas {
    57  				replicas = append(replicas, r)
    58  			}
    59  			s.mu.Unlock()
    60  			break
    61  		}
    62  	}()
    63  }
    64  
    65  type Replica_cockroach3710 struct {
    66  	store *Store_cockroach3710
    67  }
    68  
    69  type baseQueue struct {
    70  	sync.Mutex
    71  	impl *raftLogQueue
    72  }
    73  
    74  func (bq *baseQueue) MaybeAdd(repl *Replica_cockroach3710) {
    75  	bq.Lock()
    76  	defer bq.Unlock()
    77  	bq.impl.shouldQueue(repl)
    78  }
    79  
    80  type raftLogQueue struct{}
    81  
    82  func (*raftLogQueue) shouldQueue(r *Replica_cockroach3710) {
    83  	getTruncatableIndexes(r)
    84  }
    85  
    86  func getTruncatableIndexes(r *Replica_cockroach3710) {
    87  	r.store.RaftStatus()
    88  }
    89  
    90  func NewStore_cockroach3710() *Store_cockroach3710 {
    91  	rlq := &raftLogQueue{}
    92  	bq := &baseQueue{impl: rlq}
    93  	store := &Store_cockroach3710{
    94  		raftLogQueue: bq,
    95  		replicas:     make(map[int]*Replica_cockroach3710),
    96  	}
    97  	r1 := &Replica_cockroach3710{store}
    98  	r2 := &Replica_cockroach3710{store}
    99  
   100  	makeKey := func(r *Replica_cockroach3710) int {
   101  		return int((uintptr(unsafe.Pointer(r)) >> 1) % 7)
   102  	}
   103  	store.replicas[makeKey(r1)] = r1
   104  	store.replicas[makeKey(r2)] = r2
   105  
   106  	return store
   107  }
   108  
   109  func Cockroach3710() {
   110  	prof := pprof.Lookup("goroutineleak")
   111  	defer func() {
   112  		time.Sleep(100 * time.Millisecond)
   113  		prof.WriteTo(os.Stdout, 2)
   114  	}()
   115  	for i := 0; i < 10000; i++ {
   116  		go func() {
   117  			store := NewStore_cockroach3710()
   118  			go store.ForceRaftLogScanAndProcess() // G1
   119  			go store.processRaft()                // G2
   120  		}()
   121  	}
   122  }
   123  

View as plain text