diff --git a/blobstore/common/crc32block/sized_coder.go b/blobstore/common/crc32block/sized_coder.go index 0defa41a4..57920e54f 100644 --- a/blobstore/common/crc32block/sized_coder.go +++ b/blobstore/common/crc32block/sized_coder.go @@ -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 } diff --git a/blobstore/common/crc32block/sized_coder_test.go b/blobstore/common/crc32block/sized_coder_test.go index 796332435..3c6caffa3 100644 --- a/blobstore/common/crc32block/sized_coder_test.go +++ b/blobstore/common/crc32block/sized_coder_test.go @@ -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} diff --git a/blobstore/common/kvstorev2/kvstore.go b/blobstore/common/kvstorev2/kvstore.go index 44b9d556e..6c9ffd0f0 100644 --- a/blobstore/common/kvstorev2/kvstore.go +++ b/blobstore/common/kvstorev2/kvstore.go @@ -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. diff --git a/blobstore/common/kvstorev2/kvstore_test.go b/blobstore/common/kvstorev2/kvstore_test.go index bd893b121..bfe3e5e1e 100644 --- a/blobstore/common/kvstorev2/kvstore_test.go +++ b/blobstore/common/kvstorev2/kvstore_test.go @@ -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. diff --git a/blobstore/common/kvstorev2/rocksdb.go b/blobstore/common/kvstorev2/rocksdb.go index db48f222d..60363d6a9 100644 --- a/blobstore/common/kvstorev2/rocksdb.go +++ b/blobstore/common/kvstorev2/rocksdb.go @@ -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. diff --git a/blobstore/common/kvstorev2/rocksdb_test.go b/blobstore/common/kvstorev2/rocksdb_test.go index 4c4909ee5..70ab4c2b1 100644 --- a/blobstore/common/kvstorev2/rocksdb_test.go +++ b/blobstore/common/kvstorev2/rocksdb_test.go @@ -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. diff --git a/blobstore/common/tcmalloc/tcmalloc.go b/blobstore/common/tcmalloc/tcmalloc.go index 8df7238cb..d269940f5 100644 --- a/blobstore/common/tcmalloc/tcmalloc.go +++ b/blobstore/common/tcmalloc/tcmalloc.go @@ -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. diff --git a/blobstore/common/tcmalloc/tcmalloc_manage.cc b/blobstore/common/tcmalloc/tcmalloc_manage.cc index dc93eb09c..a787853c9 100644 --- a/blobstore/common/tcmalloc/tcmalloc_manage.cc +++ b/blobstore/common/tcmalloc/tcmalloc_manage.cc @@ -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(); -} \ No newline at end of file +} diff --git a/blobstore/common/tcmalloc/tcmalloc_manage.h b/blobstore/common/tcmalloc/tcmalloc_manage.h index 78a4142ec..313ca3864 100644 --- a/blobstore/common/tcmalloc/tcmalloc_manage.h +++ b/blobstore/common/tcmalloc/tcmalloc_manage.h @@ -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(); \ No newline at end of file +double tcmalloc_memory_release_rate(); diff --git a/blobstore/util/alignment.go b/blobstore/util/alignment.go index 35c585076..08cfa4bd4 100644 --- a/blobstore/util/alignment.go +++ b/blobstore/util/alignment.go @@ -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 +} diff --git a/blobstore/util/alignment_test.go b/blobstore/util/alignment_test.go index 3b176fe63..962765553 100644 --- a/blobstore/util/alignment_test.go +++ b/blobstore/util/alignment_test.go @@ -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) + } + } +} diff --git a/blobstore/util/defaulter/defaulter.go b/blobstore/util/defaulter/defaulter.go index 0552226e4..e9507c132 100644 --- a/blobstore/util/defaulter/defaulter.go +++ b/blobstore/util/defaulter/defaulter.go @@ -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) { diff --git a/blobstore/util/statistic/statistic.go b/blobstore/util/statistic/statistic.go index 54b4ed54a..e5158100d 100644 --- a/blobstore/util/statistic/statistic.go +++ b/blobstore/util/statistic/statistic.go @@ -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. diff --git a/blobstore/util/statistic/statistic_test.go b/blobstore/util/statistic/statistic_test.go index 153a6b23c..6333ea575 100644 --- a/blobstore/util/statistic/statistic_test.go +++ b/blobstore/util/statistic/statistic_test.go @@ -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. diff --git a/blobstore/util/types.go b/blobstore/util/types.go new file mode 100644 index 000000000..73066d804 --- /dev/null +++ b/blobstore/util/types.go @@ -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 +} diff --git a/blobstore/util/util.go b/blobstore/util/util.go index a36e8bae6..3fba5dc71 100644 --- a/blobstore/util/util.go +++ b/blobstore/util/util.go @@ -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. diff --git a/blobstore/util/util_test.go b/blobstore/util/util_test.go index d1e76050b..dc1ef0939 100644 --- a/blobstore/util/util_test.go +++ b/blobstore/util/util_test.go @@ -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.