feat(util): alignment size's head and padding tail

. #22984144

Signed-off-by: slasher <shenjie1@oppo.com>
This commit is contained in:
slasher 2025-01-15 15:40:26 +08:00
parent d00703a458
commit 7f8a4916d8
17 changed files with 88 additions and 35 deletions

View File

@ -17,13 +17,14 @@ package crc32block
import (
"encoding"
"encoding/binary"
"errors"
"fmt"
"hash"
"hash/crc32"
"io"
"sync"
// "github.com/cubefs/cubefs/blobstore/common/rpc2/transport"
"github.com/cubefs/cubefs/blobstore/common/rpc2/transport"
"github.com/cubefs/cubefs/blobstore/util"
)
// Notice: All closer in this file must close once at most.
@ -49,13 +50,11 @@ const (
ModeBlockEncode uint8 = 11 // encode mode, crc at head of block
ModeBlockDecode uint8 = 12 // decode, return buffer with crc size and head
// TODO: transport
// _alignment = transport.Alignment
_alignment = 512
_alignment = transport.Alignment
_alignmentMask = _alignment - 1
)
var ErrFrameContinue = errors.New("crc32block: add back later")
var ErrFrameContinue = transport.ErrFrameContinue
var (
be = binary.BigEndian
@ -128,8 +127,8 @@ func newSizedRangeDecoder(rc io.ReadCloser,
) (int64, int64, io.ReadCloser) {
payload := BlockPayload(blockSize)
head := from % payload
tail := (payload - to%payload) % payload
head := util.AlignedHead(from, payload)
tail := util.AlignedTail(to, payload)
if more := to + tail; more > actualSize {
tail -= more - actualSize
}
@ -765,8 +764,8 @@ func NewSizedFixer(rc io.ReadCloser, actualSize, from, to, blockSize int64) (int
}
payload := BlockPayload(blockSize)
head := from % payload
tail := (payload - to%payload) % payload
head := util.AlignedHead(from, payload)
tail := util.AlignedTail(to, payload)
if more := to + tail; more > actualSize {
tail -= more - actualSize
}

View File

@ -180,7 +180,7 @@ func (aw *appendWriter) Write(p []byte) (int, error) {
func (*appendWriter) Read(p []byte) (int, error) { return 0, io.EOF }
func (*appendWriter) Close() error { return nil }
// TODO: remove
// TODO: replace with io.NopCloser in higher golang version.
func nopCloser(r io.Reader) io.ReadCloser {
if _, ok := r.(io.WriterTo); ok {
return nopCloserWriterTo{r}

View File

@ -1,4 +1,4 @@
// Copyright 2023 The Cuber Authors.
// Copyright 2025 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
// Copyright 2023 The Cuber Authors.
// Copyright 2025 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
// Copyright 2023 The Cuber Authors.
// Copyright 2025 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
// Copyright 2023 The Cuber Authors.
// Copyright 2025 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
// Copyright 2023 The Cuber Authors.
// Copyright 2025 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
// Copyright 2023 The Cuber Authors.
// Copyright 2025 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -30,4 +30,4 @@ void tcmalloc_free() {
double tcmalloc_memory_release_rate() {
return MallocExtension::instance()->GetMemoryReleaseRate();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2023 The Cuber Authors.
* Copyright 2025 The CubeFS Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,4 +16,4 @@
char* tcmalloc_stats();
void tcmalloc_free();
double tcmalloc_memory_release_rate();
double tcmalloc_memory_release_rate();

View File

@ -1,4 +1,4 @@
// Copyright 2024 The Cuber Authors.
// Copyright 2024 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -31,3 +31,13 @@ func AlignedBuffer(head, capacity, alignment int) []byte {
capa := offset + head + capacity
return buff[offset:capa:capa]
}
// AlignedHead head with alignment.
func AlignedHead[I Integer](size, alignment I) I {
return size % alignment
}
// AlignedTail tail with alignment.
func AlignedTail[I Integer](size, alignment I) I {
return (alignment - size%alignment) % alignment
}

View File

@ -1,4 +1,4 @@
// Copyright 2024 The Cuber Authors.
// Copyright 2024 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -18,9 +18,11 @@ import (
"math/rand"
"testing"
"unsafe"
"github.com/stretchr/testify/require"
)
func TestUtilAlignment(t *testing.T) {
func TestUtilAlignmentBuffer(t *testing.T) {
for _, head := range []int{0, 4, 8, 127} {
for _, alignment := range []int{128, 512} {
for range [100]struct{}{} {
@ -34,3 +36,24 @@ func TestUtilAlignment(t *testing.T) {
}
}
}
func TestUtilAlignmentHeadTail(t *testing.T) {
require.Equal(t, 0, AlignedHead(0, 128))
require.Equal(t, int64(1), AlignedHead[int64](513, 512))
require.Equal(t, uint64(0), AlignedTail[uint64](512, 512))
require.Equal(t, uint(1023), AlignedTail[uint](1025, 1024))
for range [100]struct{}{} {
size := rand.Int63() + 1
alignment := rand.Int63n(1<<20) + 1
if rand.Int63()%7 == 0 {
size -= size % alignment
}
head := AlignedHead(size, alignment)
tail := AlignedTail(size, alignment)
if head == 0 {
require.Equal(t, int64(0), head+tail)
} else {
require.Equal(t, alignment, head+tail)
}
}
}

View File

@ -21,19 +21,15 @@ import (
"fmt"
"math"
"reflect"
"github.com/cubefs/cubefs/blobstore/util"
)
const zeroFloat = 1e-9
type (
Float interface {
~float32 | ~float64
}
Integer interface {
~uintptr |
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}
Float util.Float
Integer util.Integer
)
func FloatEqual[F Float](val *F, def F) {

View File

@ -1,4 +1,4 @@
// Copyright 2025 The Cuber Authors.
// Copyright 2025 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
// Copyright 2025 The Cuber Authors.
// Copyright 2025 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.

25
blobstore/util/types.go Normal file
View File

@ -0,0 +1,25 @@
// Copyright 2024 The CubeFS Authors.
//
// Licensed 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 util
type Float interface {
~float32 | ~float64
}
type Integer interface {
~uintptr |
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

View File

@ -1,4 +1,4 @@
// Copyright 2023 The Cuber Authors.
// Copyright 2024 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
// Copyright 2023 The Cuber Authors.
// Copyright 2024 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.