Skip to content

Commit 26b1257

Browse files
committed
add CBT bitmap implementation
Signed-off-by: Lyndon-Li <lyonghui@vmware.com>
1 parent a1fd85c commit 26b1257

6 files changed

Lines changed: 77 additions & 64 deletions

File tree

pkg/uploader/cbt/bitmap.go

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,49 +20,9 @@ import (
2020
"math/bits"
2121

2222
"github.com/RoaringBitmap/roaring"
23-
)
24-
25-
// Bitmap defines the methods to store and iterate the CBT bitmap
26-
type Bitmap interface {
27-
// Set sets bits within the provided range
28-
Set(uint64, uint64)
29-
30-
// SetFull sets all bits to the bitmap
31-
SetFull()
32-
33-
// Snapshot returns snapshot of the bitmap
34-
Snapshot() string
35-
36-
// ChangeID returns the changeID of the bitmap
37-
ChangeID() string
38-
39-
// VolumeID return ID of the volume from which the snapshot is taken
40-
VolumeID() string
41-
42-
// Iterator returns the iterator for the CBT Bitmap
43-
Iterator() Iterator
44-
}
4523

46-
// Iterator defines the methods to iterate the CBT bitmap and query the associated information
47-
type Iterator interface {
48-
// ChangeID returns the changeID of the bitmap
49-
ChangeID() string
50-
51-
// Snapshot returns snapshot of the bitmap
52-
Snapshot() string
53-
54-
// VolumeID return ID of the volume from which the snapshot is taken
55-
VolumeID() string
56-
57-
// BlockSize returns the granularity of the bitmap
58-
BlockSize() uint
59-
60-
// Count returns the toal number of count in the bitmap
61-
Count() uint64
62-
63-
// Next returns the offset of the next set block and whether it comes to the end of the iteration
64-
Next() (uint64, bool)
65-
}
24+
"github.com/vmware-tanzu/velero/pkg/uploader/cbt/types"
25+
)
6626

6727
const (
6828
InvalidOffset64 = ^uint64(0)
@@ -83,7 +43,7 @@ type bitmapIterator struct {
8343
iterator roaring.IntPeekable
8444
}
8545

86-
func NewBitmap(blockSize uint, length uint64, snapshot string, changeID string, volumeID string) Bitmap {
46+
func NewBitmap(blockSize uint, length uint64, snapshot string, changeID string, volumeID string) types.Bitmap {
8747
return &bitmapImpl{
8848
bitmap: roaring.New(),
8949
blockSize: blockSize,
@@ -105,14 +65,14 @@ func (c *bitmapImpl) Set(offset, length uint64) {
10565
}
10666

10767
start := offset >> c.blockSizeLog
108-
end := uint64((offset + length + uint64(c.blockSize) - 1) >> c.blockSizeLog)
68+
end := (offset + length + uint64(c.blockSize) - 1) >> c.blockSizeLog
10969

11070
c.bitmap.AddRange(start, end)
11171
}
11272

11373
func (c *bitmapImpl) SetFull() {
11474
start := uint64(0)
115-
end := uint64((c.length + uint64(c.blockSize) - 1) >> c.blockSizeLog)
75+
end := (c.length + uint64(c.blockSize) - 1) >> c.blockSizeLog
11676

11777
c.bitmap.AddRange(start, end)
11878
}
@@ -129,7 +89,7 @@ func (c *bitmapImpl) VolumeID() string {
12989
return c.volumeID
13090
}
13191

132-
func (c *bitmapImpl) Iterator() Iterator {
92+
func (c *bitmapImpl) Iterator() types.Iterator {
13393
if c.bitmap == nil {
13494
return nil
13595
}

pkg/uploader/cbt/set.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ import (
2222
"github.com/pkg/errors"
2323

2424
"github.com/vmware-tanzu/velero/pkg/cbtservice"
25+
"github.com/vmware-tanzu/velero/pkg/uploader/cbt/types"
2526
)
2627

2728
// SetBitmapOrFull translates the allocated/changed blocks from CBT service to the given bitmap or set the bitmap to full when error happens
28-
func SetBitmapOrFull(ctx context.Context, service cbtservice.Service, bitmap Bitmap) (err error) {
29+
func SetBitmapOrFull(ctx context.Context, service cbtservice.Service, bitmap types.Bitmap) (err error) {
2930
defer func() {
3031
if err != nil {
3132
bitmap.SetFull()

pkg/uploader/cbt/set_test.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package cbt_test
17+
package cbt
1818

1919
import (
2020
"context"
@@ -27,8 +27,7 @@ import (
2727

2828
"github.com/vmware-tanzu/velero/pkg/cbtservice"
2929
cbtservicemocks "github.com/vmware-tanzu/velero/pkg/cbtservice/mocks"
30-
"github.com/vmware-tanzu/velero/pkg/uploader/cbt"
31-
cbtmocks "github.com/vmware-tanzu/velero/pkg/uploader/cbt/mocks"
30+
cbtmocks "github.com/vmware-tanzu/velero/pkg/uploader/cbt/types/mocks"
3231
)
3332

3433
func TestSetBitmapOrFull(t *testing.T) {
@@ -126,13 +125,7 @@ func TestSetBitmapOrFull(t *testing.T) {
126125
svc = svcMock
127126
}
128127

129-
// Use type assertion to bypass gopls false positive on mock type
130-
var bmp cbt.Bitmap
131-
if bmpMock != nil {
132-
bmp = interface{}(bmpMock).(cbt.Bitmap)
133-
}
134-
135-
err := cbt.SetBitmapOrFull(context.Background(), svc, bmp)
128+
err := SetBitmapOrFull(context.Background(), svc, bmpMock)
136129

137130
if tt.expectedErrStr != "" {
138131
require.Error(t, err)
Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/uploader/cbt/types/types.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright The Velero Contributors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package types
18+
19+
// Bitmap defines the methods to store and iterate the CBT bitmap
20+
type Bitmap interface {
21+
// Set sets bits within the provided range
22+
Set(uint64, uint64)
23+
24+
// SetFull sets all bits to the bitmap
25+
SetFull()
26+
27+
// Snapshot returns snapshot of the bitmap
28+
Snapshot() string
29+
30+
// ChangeID returns the changeID of the bitmap
31+
ChangeID() string
32+
33+
// VolumeID return ID of the volume from which the snapshot is taken
34+
VolumeID() string
35+
36+
// Iterator returns the iterator for the CBT Bitmap
37+
Iterator() Iterator
38+
}
39+
40+
// Iterator defines the methods to iterate the CBT bitmap and query the associated information
41+
type Iterator interface {
42+
// ChangeID returns the changeID of the bitmap
43+
ChangeID() string
44+
45+
// Snapshot returns snapshot of the bitmap
46+
Snapshot() string
47+
48+
// VolumeID return ID of the volume from which the snapshot is taken
49+
VolumeID() string
50+
51+
// BlockSize returns the granularity of the bitmap
52+
BlockSize() uint
53+
54+
// Count returns the toal number of count in the bitmap
55+
Count() uint64
56+
57+
// Next returns the offset of the next set block and whether it comes to the end of the iteration
58+
Next() (uint64, bool)
59+
}

0 commit comments

Comments
 (0)