-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
storage: decouple the region storage from LevelDB backend (tikv#7859)
ref tikv#3453 In the previous implementation, LevelDB and `RegionStorage` were strongly coupled. This PR decouples region storage from it to further optimize code abstraction, so we can get a more reusable `LevelDBBackend` and a more specialized `RegionStorage` implementation. Signed-off-by: JmPotato <[email protected]>
- Loading branch information
Showing
10 changed files
with
423 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2024 TiKV Project 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 storage | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLevelDBBackend(t *testing.T) { | ||
re := require.New(t) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
backend, err := newLevelDBBackend(ctx, t.TempDir(), nil) | ||
re.NoError(err) | ||
re.NotNil(backend) | ||
key, value := "k1", "v1" | ||
// Save without flush. | ||
err = backend.SaveIntoBatch(key, []byte(value)) | ||
re.NoError(err) | ||
val, err := backend.Load(key) | ||
re.NoError(err) | ||
re.Empty(val) | ||
// Flush and load. | ||
err = backend.Flush() | ||
re.NoError(err) | ||
val, err = backend.Load(key) | ||
re.NoError(err) | ||
re.Equal(value, val) | ||
// Delete and load. | ||
err = backend.Remove(key) | ||
re.NoError(err) | ||
val, err = backend.Load(key) | ||
re.NoError(err) | ||
re.Empty(val) | ||
// Save twice without flush. | ||
err = backend.SaveIntoBatch(key, []byte(value)) | ||
re.NoError(err) | ||
val, err = backend.Load(key) | ||
re.NoError(err) | ||
re.Empty(val) | ||
value = "v2" | ||
err = backend.SaveIntoBatch(key, []byte(value)) | ||
re.NoError(err) | ||
val, err = backend.Load(key) | ||
re.NoError(err) | ||
re.Empty(val) | ||
// Delete before flush. | ||
err = backend.Remove(key) | ||
re.NoError(err) | ||
val, err = backend.Load(key) | ||
re.NoError(err) | ||
re.Empty(val) | ||
// Flush and load. | ||
err = backend.Flush() | ||
re.NoError(err) | ||
val, err = backend.Load(key) | ||
re.NoError(err) | ||
re.Equal(value, val) | ||
// Delete and load. | ||
err = backend.Remove(key) | ||
re.NoError(err) | ||
val, err = backend.Load(key) | ||
re.NoError(err) | ||
re.Empty(val) | ||
// Test the background flush. | ||
backend.flushRate = defaultDirtyFlushTick | ||
err = backend.SaveIntoBatch(key, []byte(value)) | ||
re.NoError(err) | ||
val, err = backend.Load(key) | ||
re.NoError(err) | ||
re.Empty(val) | ||
time.Sleep(defaultDirtyFlushTick * 2) | ||
val, err = backend.Load(key) | ||
re.NoError(err) | ||
re.Equal(value, val) | ||
err = backend.Remove(key) | ||
re.NoError(err) | ||
val, err = backend.Load(key) | ||
re.NoError(err) | ||
re.Empty(val) | ||
backend.flushRate = defaultFlushRate | ||
// Test the flush when the cache is full. | ||
backend.flushRate = time.Minute | ||
for i := 0; i < backend.batchSize; i++ { | ||
key, value = fmt.Sprintf("k%d", i), fmt.Sprintf("v%d", i) | ||
err = backend.SaveIntoBatch(key, []byte(value)) | ||
re.NoError(err) | ||
if i < backend.batchSize-1 { | ||
// The cache is not full yet. | ||
val, err = backend.Load(key) | ||
re.NoError(err) | ||
re.Empty(val) | ||
} else { | ||
// The cache is full, and the flush is triggered. | ||
val, err = backend.Load(key) | ||
re.NoError(err) | ||
re.Equal(value, val) | ||
} | ||
} | ||
backend.flushRate = defaultFlushRate | ||
// Close the backend. | ||
err = backend.Close() | ||
re.NoError(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2024 TiKV Project 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 storage | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/gogo/protobuf/proto" | ||
"github.com/pingcap/kvproto/pkg/metapb" | ||
"github.com/tikv/pd/pkg/core" | ||
"github.com/tikv/pd/pkg/encryption" | ||
"github.com/tikv/pd/pkg/errs" | ||
"github.com/tikv/pd/pkg/storage/endpoint" | ||
"github.com/tikv/pd/pkg/storage/kv" | ||
) | ||
|
||
// RegionStorage is a storage for the PD region meta information based on LevelDB, | ||
// which will override the default implementation of the `endpoint.RegionStorage`. | ||
type RegionStorage struct { | ||
kv.Base | ||
backend *levelDBBackend | ||
} | ||
|
||
var _ endpoint.RegionStorage = (*RegionStorage)(nil) | ||
|
||
func newRegionStorage(backend *levelDBBackend) *RegionStorage { | ||
return &RegionStorage{Base: backend.Base, backend: backend} | ||
} | ||
|
||
// LoadRegion implements the `endpoint.RegionStorage` interface. | ||
func (s *RegionStorage) LoadRegion(regionID uint64, region *metapb.Region) (bool, error) { | ||
return s.backend.LoadRegion(regionID, region) | ||
} | ||
|
||
// LoadRegions implements the `endpoint.RegionStorage` interface. | ||
func (s *RegionStorage) LoadRegions(ctx context.Context, f func(region *core.RegionInfo) []*core.RegionInfo) error { | ||
return s.backend.LoadRegions(ctx, f) | ||
} | ||
|
||
// SaveRegion implements the `endpoint.RegionStorage` interface. | ||
// Instead of saving the region directly, it will encrypt the region and then save it in batch. | ||
func (s *RegionStorage) SaveRegion(region *metapb.Region) error { | ||
encryptedRegion, err := encryption.EncryptRegion(region, s.backend.ekm) | ||
if err != nil { | ||
return err | ||
} | ||
value, err := proto.Marshal(encryptedRegion) | ||
if err != nil { | ||
return errs.ErrProtoMarshal.Wrap(err).GenWithStackByCause() | ||
} | ||
return s.backend.SaveIntoBatch(endpoint.RegionPath(region.GetId()), value) | ||
} | ||
|
||
// DeleteRegion implements the `endpoint.RegionStorage` interface. | ||
func (s *RegionStorage) DeleteRegion(region *metapb.Region) error { | ||
return s.backend.Remove((endpoint.RegionPath(region.GetId()))) | ||
} | ||
|
||
// Flush implements the `endpoint.RegionStorage` interface. | ||
func (s *RegionStorage) Flush() error { | ||
return s.backend.Flush() | ||
} | ||
|
||
// Close implements the `endpoint.RegionStorage` interface. | ||
func (s *RegionStorage) Close() error { | ||
return s.backend.Close() | ||
} |
Oops, something went wrong.