remove put function and use RLock in Iter function

Signed-off-by: codwu <wuhan9087@163.com>
This commit is contained in:
codwu 2018-06-25 21:52:11 +08:00
parent 84a45cb79a
commit cd145c90d5
5 changed files with 11 additions and 18 deletions

View file

@ -440,7 +440,7 @@ Outer:
if intervalOverlap(mint, maxt, chk.MinTime, chk.MaxTime) { if intervalOverlap(mint, maxt, chk.MinTime, chk.MaxTime) {
// Delete only until the current values and not beyond. // Delete only until the current values and not beyond.
tmin, tmax := clampInterval(mint, maxt, chks[0].MinTime, chks[len(chks)-1].MaxTime) tmin, tmax := clampInterval(mint, maxt, chks[0].MinTime, chks[len(chks)-1].MaxTime)
stones.put(p.At(), Intervals{{tmin, tmax}}) stones.addInterval(p.At(), Interval{tmin, tmax})
continue Outer continue Outer
} }
} }

View file

@ -808,7 +808,7 @@ func TestTombstoneCleanFail(t *testing.T) {
// Add some some fake tombstones to trigger the compaction. // Add some some fake tombstones to trigger the compaction.
tomb := NewMemTombstones() tomb := NewMemTombstones()
tomb.put(0,Intervals{{0, 1}}) tomb.addInterval(0, Interval{0, 1})
block.tombstones = tomb block.tombstones = tomb
db.blocks = append(db.blocks, block) db.blocks = append(db.blocks, block)

View file

@ -557,7 +557,7 @@ func TestBlockQuerierDelete(t *testing.T) {
}, },
}, },
}, },
tombstones: NewMemTombstones().put(1, Intervals{{1, 3}}).put(2, Intervals{{1, 3}, {6, 10}}).put(3, Intervals{{6, 10}}), tombstones: NewMemTombstones().addInterval(1, Interval{1, 3}).addInterval(2, Interval{1, 3}, Interval{6, 10}).addInterval(3, Interval{6, 10}),
queries: []query{ queries: []query{
{ {
mint: 2, mint: 2,

View file

@ -16,14 +16,12 @@ package tsdb
import ( import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"github.com/pkg/errors"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
"github.com/pkg/errors"
) )
const tombstoneFilename = "tombstones" const tombstoneFilename = "tombstones"
@ -174,8 +172,8 @@ func (t *memTombstones) Get(ref uint64) (Intervals, error) {
} }
func (t *memTombstones) Iter(f func(uint64, Intervals) error) error { func (t *memTombstones) Iter(f func(uint64, Intervals) error) error {
t.mtx.Lock() t.mtx.RLock()
defer t.mtx.Unlock() defer t.mtx.RUnlock()
for ref, ivs := range t.mts { for ref, ivs := range t.mts {
if err := f(ref, ivs); err != nil { if err := f(ref, ivs); err != nil {
return err return err
@ -185,16 +183,12 @@ func (t *memTombstones) Iter(f func(uint64, Intervals) error) error {
} }
// addInterval to an existing memTombstones // addInterval to an existing memTombstones
func (t *memTombstones) addInterval(ref uint64, itv Interval) { func (t *memTombstones) addInterval(ref uint64, itvs ...Interval) *memTombstones {
t.mtx.Lock()
t.mts[ref] = t.mts[ref].add(itv)
t.mtx.Unlock()
}
func (t *memTombstones) put(ref uint64, itvs Intervals) *memTombstones {
t.mtx.Lock() t.mtx.Lock()
defer t.mtx.Unlock() defer t.mtx.Unlock()
t.mts[ref] = itvs for _, itv := range itvs {
t.mts[ref] = t.mts[ref].add(itv)
}
return t return t
} }

View file

@ -41,7 +41,7 @@ func TestWriteAndReadbackTombStones(t *testing.T) {
dranges = dranges.add(Interval{mint, mint + rand.Int63n(1000)}) dranges = dranges.add(Interval{mint, mint + rand.Int63n(1000)})
mint += rand.Int63n(1000) + 1 mint += rand.Int63n(1000) + 1
} }
stones.put(ref, dranges) stones.addInterval(ref, dranges...)
} }
testutil.Ok(t, writeTombstoneFile(tmpdir, stones)) testutil.Ok(t, writeTombstoneFile(tmpdir, stones))
@ -132,7 +132,6 @@ func TestMemTombstonesConcurrency(t *testing.T) {
go func() { go func() {
for x := 0; x < totalRuns; x++ { for x := 0; x < totalRuns; x++ {
tomb.put(uint64(x), Intervals{{int64(x), int64(x)}})
tomb.addInterval(uint64(x), Interval{int64(x), int64(x)}) tomb.addInterval(uint64(x), Interval{int64(x), int64(x)})
} }
wg.Done() wg.Done()