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 3 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 TestChunkIsZero(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
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,40 @@ func TestCompressionMetricsCollection(t *testing.T) {
}
}

func TestSnapshotReadWriteAllZeroedChunk(t *testing.T) {
jkuradobery marked this conversation as resolved.
Show resolved Hide resolved
for _, testCase := range testCases() {
t.Run(
testCase.name, func(t *testing.T) {
f := createFixture(t)
defer f.teardown()

chunk := common.Chunk{
Index: 0,
Data: make([]byte, 1024*1024*4),
}
chunkID, err := f.storage.WriteChunk(
f.ctx,
"",
"test",
chunk,
testCase.useS3,
)
require.NoError(t, err)

readChunk := common.Chunk{
ID: chunkID,
Data: make([]byte, len(chunk.Data)),
StoredInS3: testCase.useS3,
}
err = f.storage.ReadChunk(f.ctx, &readChunk)
require.NoError(t, err)
require.Equal(t, chunk.Data, readChunk.Data)
require.True(t, readChunk.Zero)
},
)
}
}

func expectHistogramCalledOnce(registry *mocks.RegistryMock, name string, compressionTags map[string]string) *mock.Call {
return registry.GetHistogram(
name,
Expand Down
Loading