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

[Disk Manager]: Snapshot: Check chunk contents for zeroes #32

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions cloud/disk_manager/internal/pkg/dataplane/common/chunk.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package common

import (
"bytes"
"hash/crc32"
)

////////////////////////////////////////////////////////////////////////////////

var zeroes = make([]byte, 1024*1024)

type Chunk struct {
ID string
Index uint32
Expand All @@ -18,3 +21,20 @@ type Chunk struct {
func (chunk Chunk) Checksum() uint32 {
return crc32.ChecksumIEEE(chunk.Data)
}

func (chunk Chunk) CheckDataIsAllZeroes() bool {
for i := 0; i < len(chunk.Data); i += len(zeroes) {
endOffset := i + len(zeroes)

if endOffset > len(chunk.Data) {
endOffset = len(chunk.Data)
}
jkuradobery marked this conversation as resolved.
Show resolved Hide resolved

dataToCheck := chunk.Data[i:endOffset]

if !bytes.Equal(dataToCheck, zeroes[:len(dataToCheck)]) {
return false
}
}
return true
}
22 changes: 22 additions & 0 deletions cloud/disk_manager/internal/pkg/dataplane/common/chunk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package common

import (
"testing"

"github.com/stretchr/testify/require"
)

jkuradobery marked this conversation as resolved.
Show resolved Hide resolved
////////////////////////////////////////////////////////////////////////////////

func TestCheckDataIsAllZeroes(t *testing.T) {
chunk := Chunk{
Data: []byte{1, 2, 3, 4, 5},
}
require.False(t, chunk.CheckDataIsAllZeroes())
jkuradobery marked this conversation as resolved.
Show resolved Hide resolved

chunk = Chunk{Data: []byte{0, 0, 0}}
require.True(t, chunk.CheckDataIsAllZeroes())

chunk = Chunk{Data: make([]byte, 1024*1024*5)}
require.True(t, chunk.CheckDataIsAllZeroes())
}
4 changes: 4 additions & 0 deletions cloud/disk_manager/internal/pkg/dataplane/common/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ func (t Transferer) goRead(
return
}

if !chunk.Zero {
chunk.Zero = chunk.CheckDataIsAllZeroes()
}

select {
case chunks <- chunk:
case <-ctx.Done():
Expand Down
1 change: 1 addition & 0 deletions cloud/disk_manager/internal/pkg/dataplane/common/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SRCS(

GO_TEST_SRCS(
transfer_test.go
chunk_test.go
)

END()
Expand Down
Loading