mirror of
				https://github.com/prometheus/prometheus.git
				synced 2025-10-31 00:11:23 +01:00 
			
		
		
		
	* tsdb/agent: Fix deadlock from simultaneous GC and write This commit fixes a potential deadlock where storing in-memory series references could deadlock with a WAL GC cycle. Signed-off-by: Robert Fratto <robertfratto@gmail.com> * add missing license header Signed-off-by: Robert Fratto <robertfratto@gmail.com> * order local imports Signed-off-by: Robert Fratto <robertfratto@gmail.com> * align deadlock testing with discovery/manager_test.go method Also prevents GCs from running concurrently, which could also cause a deadlock (even though it's currently impossible for two GCs to run concurrently). Signed-off-by: Robert Fratto <robertfratto@gmail.com>
		
			
				
	
	
		
			77 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			77 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2022 The Prometheus 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 agent
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"math"
 | |
| 	"sync"
 | |
| 	"testing"
 | |
| 	"time"
 | |
| 
 | |
| 	"github.com/stretchr/testify/require"
 | |
| 
 | |
| 	"github.com/prometheus/prometheus/model/labels"
 | |
| 	"github.com/prometheus/prometheus/tsdb/chunks"
 | |
| )
 | |
| 
 | |
| func TestNoDeadlock(t *testing.T) {
 | |
| 	const numWorkers = 1000
 | |
| 
 | |
| 	var (
 | |
| 		wg           sync.WaitGroup
 | |
| 		started      = make(chan struct{})
 | |
| 		stripeSeries = newStripeSeries(3)
 | |
| 	)
 | |
| 
 | |
| 	wg.Add(numWorkers)
 | |
| 	for i := 0; i < numWorkers; i++ {
 | |
| 		go func() {
 | |
| 			defer wg.Done()
 | |
| 			<-started
 | |
| 			_ = stripeSeries.GC(math.MaxInt64)
 | |
| 		}()
 | |
| 	}
 | |
| 
 | |
| 	wg.Add(numWorkers)
 | |
| 	for i := 0; i < numWorkers; i++ {
 | |
| 		go func(i int) {
 | |
| 			defer wg.Done()
 | |
| 			<-started
 | |
| 
 | |
| 			series := &memSeries{
 | |
| 				ref: chunks.HeadSeriesRef(i),
 | |
| 				lset: labels.FromMap(map[string]string{
 | |
| 					"id": fmt.Sprintf("%d", i),
 | |
| 				}),
 | |
| 			}
 | |
| 			stripeSeries.Set(series.lset.Hash(), series)
 | |
| 		}(i)
 | |
| 	}
 | |
| 
 | |
| 	finished := make(chan struct{})
 | |
| 	go func() {
 | |
| 		wg.Wait()
 | |
| 		close(finished)
 | |
| 	}()
 | |
| 
 | |
| 	close(started)
 | |
| 	select {
 | |
| 	case <-finished:
 | |
| 		return
 | |
| 	case <-time.After(15 * time.Second):
 | |
| 		require.FailNow(t, "deadlock detected")
 | |
| 	}
 | |
| }
 |