Skip to content

Commit

Permalink
Adjust the buffer size to avoid OOM
Browse files Browse the repository at this point in the history
Adjust the buffer size to avoid OOM

Fixes vmware-tanzu/velero#6847

Signed-off-by: Wenkai Yin(尹文开) <[email protected]>
  • Loading branch information
ywk253100 committed Oct 10, 2023
1 parent d65ba53 commit 800a4dd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
4 changes: 2 additions & 2 deletions backupstoragelocation.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ spec:
# See https://docs.microsoft.com/en-us/rest/api/storageservices/understanding-block-blobs--append-blobs--and-page-blobs#about-block-blobs
# for more information on block blobs.
#
# Optional (defaults to 104857600, i.e. 100MB).
blockSizeInBytes: "104857600"
# Optional (defaults to 1048576, i.e. 1MB, maximum 104857600, i.e. 100MB).
blockSizeInBytes: "1048576"
```
12 changes: 9 additions & 3 deletions velero-plugin-for-microsoft-azure/object_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ const (
blockSizeConfigKey = "blockSizeInBytes"
// blocks must be less than/equal to 100MB in size
// ref. https://docs.microsoft.com/en-us/rest/api/storageservices/put-block#uri-parameters
defaultBlockSize = 100 * 1024 * 1024
maxBlockSize = 100 * 1024 * 1024
defaultBlockSize = 1 * 1024 * 1024
)

type containerGetter interface {
Expand Down Expand Up @@ -259,11 +260,16 @@ func getBlockSize(log logrus.FieldLogger, config map[string]string) int {
return defaultBlockSize
}

if blockSize <= 0 || blockSize > defaultBlockSize {
log.WithError(err).Warnf("Value provided for config.blockSizeInBytes (%d) is outside the allowed range of 1 to %d, using default block size of %d", blockSize, defaultBlockSize, defaultBlockSize)
if blockSize <= 0 {
log.WithError(err).Warnf("Value provided for config.blockSizeInBytes (%d) is < 1, using default block size of %d", blockSize, defaultBlockSize)
return defaultBlockSize
}

if blockSize > maxBlockSize {
log.WithError(err).Warnf("Value provided for config.blockSizeInBytes (%d) is > the max size %d, using max block size of %d", blockSize, maxBlockSize, maxBlockSize)
return maxBlockSize
}

return blockSize
}

Expand Down
29 changes: 29 additions & 0 deletions velero-plugin-for-microsoft-azure/object_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blockblob"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -138,3 +139,31 @@ func (m *mockBlob) GetSASURI(ttl time.Duration, sharedKeyCredential *azblob.Shar
args := m.Called(ttl, sharedKeyCredential)
return args.String(0), args.Error(1)
}

func TestGetBlockSize(t *testing.T) {
logger := logrus.New()
config := map[string]string{}
// not specified
size := getBlockSize(logger, config)
assert.Equal(t, defaultBlockSize, size)

// invalid value specified
config[blockSizeConfigKey] = "invalid"
size = getBlockSize(logger, config)
assert.Equal(t, defaultBlockSize, size)

// value < 0 specified
config[blockSizeConfigKey] = "0"
size = getBlockSize(logger, config)
assert.Equal(t, defaultBlockSize, size)

// value > max size specified
config[blockSizeConfigKey] = "1048576000"
size = getBlockSize(logger, config)
assert.Equal(t, maxBlockSize, size)

// valid value specified
config[blockSizeConfigKey] = "1048570"
size = getBlockSize(logger, config)
assert.Equal(t, 1048570, size)
}

0 comments on commit 800a4dd

Please sign in to comment.