-
Notifications
You must be signed in to change notification settings - Fork 726
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server/grpc_service: make update gc_safepoint concurrently safe (#5070)
close #5018 Signed-off-by: shirly <[email protected]> Co-authored-by: buffer <[email protected]> Co-authored-by: Ti Chi Robot <[email protected]>
- Loading branch information
1 parent
36db3c7
commit 12a9513
Showing
4 changed files
with
151 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2022 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 gc | ||
|
||
import ( | ||
"github.com/tikv/pd/pkg/syncutil" | ||
"github.com/tikv/pd/server/storage/endpoint" | ||
) | ||
|
||
// SafePointManager is the manager for safePoint of GC and services | ||
type SafePointManager struct { | ||
*gcSafePointManager | ||
// TODO add ServiceSafepointManager | ||
} | ||
|
||
// NewSafepointManager creates a SafePointManager of GC and services | ||
func NewSafepointManager(store endpoint.GCSafePointStorage) *SafePointManager { | ||
return &SafePointManager{ | ||
newGCSafePointManager(store), | ||
} | ||
} | ||
|
||
type gcSafePointManager struct { | ||
syncutil.Mutex | ||
store endpoint.GCSafePointStorage | ||
} | ||
|
||
func newGCSafePointManager(store endpoint.GCSafePointStorage) *gcSafePointManager { | ||
return &gcSafePointManager{store: store} | ||
} | ||
|
||
// LoadGCSafePoint loads current GC safe point from storage. | ||
func (manager *gcSafePointManager) LoadGCSafePoint() (uint64, error) { | ||
return manager.store.LoadGCSafePoint() | ||
} | ||
|
||
// UpdateGCSafePoint updates the safepoint if it is greater than the previous one | ||
// it returns the old safepoint in the storage. | ||
func (manager *gcSafePointManager) UpdateGCSafePoint(newSafePoint uint64) (oldSafePoint uint64, err error) { | ||
manager.Lock() | ||
defer manager.Unlock() | ||
// TODO: cache the safepoint in the storage. | ||
oldSafePoint, err = manager.store.LoadGCSafePoint() | ||
if err != nil { | ||
return | ||
} | ||
if oldSafePoint >= newSafePoint { | ||
return | ||
} | ||
err = manager.store.SaveGCSafePoint(newSafePoint) | ||
return | ||
} |
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,80 @@ | ||
// Copyright 2022 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 gc | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/tikv/pd/server/storage/endpoint" | ||
"github.com/tikv/pd/server/storage/kv" | ||
) | ||
|
||
func newGCStorage() endpoint.GCSafePointStorage { | ||
return endpoint.NewStorageEndpoint(kv.NewMemoryKV(), nil) | ||
} | ||
|
||
func TestGCSafePointUpdateSequentially(t *testing.T) { | ||
gcSafePointManager := newGCSafePointManager(newGCStorage()) | ||
re := require.New(t) | ||
curSafePoint := uint64(0) | ||
// update gc safePoint with asc value. | ||
for id := 10; id < 20; id++ { | ||
safePoint, err := gcSafePointManager.LoadGCSafePoint() | ||
re.NoError(err) | ||
re.Equal(curSafePoint, safePoint) | ||
previousSafePoint := curSafePoint | ||
curSafePoint = uint64(id) | ||
oldSafePoint, err := gcSafePointManager.UpdateGCSafePoint(curSafePoint) | ||
re.NoError(err) | ||
re.Equal(previousSafePoint, oldSafePoint) | ||
} | ||
|
||
safePoint, err := gcSafePointManager.LoadGCSafePoint() | ||
re.NoError(err) | ||
re.Equal(curSafePoint, safePoint) | ||
// update with smaller value should be failed. | ||
oldSafePoint, err := gcSafePointManager.UpdateGCSafePoint(safePoint - 5) | ||
re.NoError(err) | ||
re.Equal(safePoint, oldSafePoint) | ||
curSafePoint, err = gcSafePointManager.LoadGCSafePoint() | ||
re.NoError(err) | ||
// current safePoint should not change since the update value was smaller | ||
re.Equal(safePoint, curSafePoint) | ||
} | ||
|
||
func TestGCSafePointUpdateCurrently(t *testing.T) { | ||
gcSafePointManager := newGCSafePointManager(newGCStorage()) | ||
maxSafePoint := uint64(1000) | ||
wg := sync.WaitGroup{} | ||
re := require.New(t) | ||
|
||
// update gc safePoint concurrently | ||
for id := 0; id < 20; id++ { | ||
wg.Add(1) | ||
go func(step uint64) { | ||
for safePoint := step; safePoint <= maxSafePoint; safePoint += step { | ||
_, err := gcSafePointManager.UpdateGCSafePoint(safePoint) | ||
re.NoError(err) | ||
} | ||
wg.Done() | ||
}(uint64(id + 1)) | ||
} | ||
wg.Wait() | ||
safePoint, err := gcSafePointManager.LoadGCSafePoint() | ||
re.NoError(err) | ||
re.Equal(maxSafePoint, safePoint) | ||
} |
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