Skip to content

Commit

Permalink
fix: #1214 - go race detect (#1220)
Browse files Browse the repository at this point in the history
* fix: #1214 - go race detect

* add license header

* add comment to track issue

* remove dup return

* add a benchmark test for JoinIfNotEqual

* revise benchmark test
  • Loading branch information
beiwei30 committed May 27, 2021
1 parent 5a9a038 commit 1a83480
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 38 deletions.
14 changes: 10 additions & 4 deletions cluster/router/utils/bitmap_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ import (
var EmptyAddr = roaring.NewBitmap()

func JoinIfNotEqual(left *roaring.Bitmap, right *roaring.Bitmap) *roaring.Bitmap {
if !left.Equals(right) {
left = left.Clone()
left.And(right)
}
// FIXME: cannot use left.Equals(right) as a shortcut because roaring bitmap has a race issue in its equals impl.
// see issue: https://github.com/RoaringBitmap/roaring/issues/305
/*
if !left.Equals(right) {
left = left.Clone()
left.And(right)
}
*/
left = left.Clone()
left.And(right)
return left
}

Expand Down
78 changes: 78 additions & 0 deletions cluster/router/utils/bitmap_util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 utils

import (
"sync"
"testing"
)

import (
"github.com/RoaringBitmap/roaring"
"github.com/stretchr/testify/assert"
)

func TestJoinIfNotEqual(t *testing.T) {
l := roaring.NewBitmap()
l.Add(uint32(1))
l.Add(uint32(2))
l.Add(uint32(3))
r := roaring.NewBitmap()
r.AddRange(1, 4)

// this is for race condition test
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
JoinIfNotEqual(l, r)
}()

go func() {
defer wg.Done()
JoinIfNotEqual(l, r)
}()
wg.Wait()

assert.True(t, l.Equals(JoinIfNotEqual(l, r)))
}

func BenchmarkJoinIfNotEqual10(b *testing.B) {
benchmarkJoinIfNotEqual(b, 10)
}

func BenchmarkJoinIfNotEqual1000(b *testing.B) {
benchmarkJoinIfNotEqual(b, 1000)
}

func BenchmarkJoinIfNotEqual100000(b *testing.B) {
benchmarkJoinIfNotEqual(b, 100000)
}

func benchmarkJoinIfNotEqual(b *testing.B, size int) {
l := roaring.NewBitmap()
for i := 0; i < size; i++ {
l.Add(uint32(i))
}
r := roaring.NewBitmap()
r.AddRange(0, uint64(size+1))

for i := 0; i < b.N; i++ {
JoinIfNotEqual(l, r)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/apache/dubbo-go
go 1.15

require (
github.com/RoaringBitmap/roaring v0.5.5
github.com/RoaringBitmap/roaring v0.6.1
github.com/Workiva/go-datastructures v1.0.52
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5
github.com/alibaba/sentinel-golang v1.0.2
Expand Down
Loading

0 comments on commit 1a83480

Please sign in to comment.