Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate locking from postings lists #889

Merged
merged 1 commit into from
Sep 7, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 5 additions & 47 deletions src/m3ninx/postings/roaring/roaring.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package roaring

import (
"errors"
"sync"

"github.com/m3db/m3/src/m3ninx/postings"
"github.com/m3db/m3/src/m3ninx/x"
Expand All @@ -37,9 +36,8 @@ var (
errIteratorClosed = errors.New("iterator has been closed")
)

// postingsList wraps a Roaring Bitmap with a mutex for thread safety.
// postingsList wraps a Roaring Bitmap with the m3ninx pl API.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth calling out that it's not goroutine-safe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will do in a follow up change.

type postingsList struct {
sync.RWMutex
bitmap *roaring.Bitmap
}

Expand All @@ -51,9 +49,7 @@ func NewPostingsList() postings.MutableList {
}

func (d *postingsList) Insert(i postings.ID) {
d.Lock()
d.bitmap.Add(uint32(i))
d.Unlock()
}

func (d *postingsList) Intersect(other postings.List) error {
Expand All @@ -62,11 +58,7 @@ func (d *postingsList) Intersect(other postings.List) error {
return errIntersectRoaringOnly
}

o.RLock()
d.Lock()
d.bitmap.And(o.bitmap)
d.Unlock()
o.RUnlock()
return nil
}

Expand All @@ -76,11 +68,7 @@ func (d *postingsList) Difference(other postings.List) error {
return errDifferenceRoaringOnly
}

o.RLock()
d.Lock()
d.bitmap.AndNot(o.bitmap)
d.Unlock()
o.RUnlock()
return nil
}

Expand All @@ -90,29 +78,21 @@ func (d *postingsList) Union(other postings.List) error {
return errUnionRoaringOnly
}

o.RLock()
d.Lock()
d.bitmap.Or(o.bitmap)
d.Unlock()
o.RUnlock()
return nil
}

func (d *postingsList) AddRange(min, max postings.ID) {
d.Lock()
d.bitmap.AddRange(uint64(min), uint64(max))
d.Unlock()
}

func (d *postingsList) AddIterator(iter postings.Iterator) error {
safeIter := x.NewSafeCloser(iter)
defer safeIter.Close()

d.Lock()
for iter.Next() {
d.bitmap.Add(uint32(iter.Current()))
}
d.Unlock()

if err := iter.Err(); err != nil {
return err
Expand All @@ -122,58 +102,39 @@ func (d *postingsList) AddIterator(iter postings.Iterator) error {
}

func (d *postingsList) RemoveRange(min, max postings.ID) {
d.Lock()
d.bitmap.RemoveRange(uint64(min), uint64(max))
d.Unlock()
}

func (d *postingsList) Reset() {
d.Lock()
d.bitmap.Clear()
d.Unlock()
}

func (d *postingsList) Contains(i postings.ID) bool {
d.RLock()
contains := d.bitmap.Contains(uint32(i))
d.RUnlock()
return contains
return d.bitmap.Contains(uint32(i))
}

func (d *postingsList) IsEmpty() bool {
d.RLock()
empty := d.bitmap.IsEmpty()
d.RUnlock()
return empty
return d.bitmap.IsEmpty()
}

func (d *postingsList) Max() (postings.ID, error) {
d.RLock()
if d.bitmap.IsEmpty() {
d.RUnlock()
return 0, postings.ErrEmptyList
}
max := d.bitmap.Maximum()
d.RUnlock()
return postings.ID(max), nil
}

func (d *postingsList) Min() (postings.ID, error) {
d.RLock()
if d.bitmap.IsEmpty() {
d.RUnlock()
return 0, postings.ErrEmptyList
}
min := d.bitmap.Minimum()
d.RUnlock()
return postings.ID(min), nil
}

func (d *postingsList) Len() int {
d.RLock()
l := d.bitmap.GetCardinality()
d.RUnlock()
return int(l)
return int(d.bitmap.GetCardinality())
}

func (d *postingsList) Iterator() postings.Iterator {
Expand All @@ -183,15 +144,12 @@ func (d *postingsList) Iterator() postings.Iterator {
}

func (d *postingsList) Clone() postings.MutableList {
d.RLock()
// TODO: It's cheaper to Clone than to cache roaring bitmaps, see
// `postings_list_bench_test.go`. Their internals don't allow for
// pooling at the moment. We should address this when get a chance
// (move to another implementation / address deficiencies).
clone := d.bitmap.Clone()
d.RUnlock()
return &postingsList{
bitmap: clone,
bitmap: d.bitmap.Clone(),
}
}

Expand Down