Commit e5fbcdbe authored by liuyuhang's avatar liuyuhang

add prune kvmvcc

parent 9a927074
...@@ -117,9 +117,9 @@ type RoundState struct { ...@@ -117,9 +117,9 @@ type RoundState struct {
// RoundStateMessage ... // RoundStateMessage ...
func (rs *RoundState) RoundStateMessage() *tmtypes.NewRoundStepMsg { func (rs *RoundState) RoundStateMessage() *tmtypes.NewRoundStepMsg {
return &tmtypes.NewRoundStepMsg{ return &tmtypes.NewRoundStepMsg{
Height: rs.Height, Height: rs.Height,
Round: int32(rs.Round), Round: int32(rs.Round),
Step: int32(rs.Step), Step: int32(rs.Step),
SecondsSinceStartTime: int32(time.Since(rs.StartTime).Seconds()), SecondsSinceStartTime: int32(time.Since(rs.StartTime).Seconds()),
LastCommitRound: int32(rs.LastCommit.Round()), LastCommitRound: int32(rs.LastCommit.Round()),
} }
......
...@@ -73,9 +73,9 @@ func ParseX509CertificateToSm2(x509Cert *x509.Certificate) *sm2.Certificate { ...@@ -73,9 +73,9 @@ func ParseX509CertificateToSm2(x509Cert *x509.Certificate) *sm2.Certificate {
UnknownExtKeyUsage: x509Cert.UnknownExtKeyUsage, UnknownExtKeyUsage: x509Cert.UnknownExtKeyUsage,
BasicConstraintsValid: x509Cert.BasicConstraintsValid, BasicConstraintsValid: x509Cert.BasicConstraintsValid,
IsCA: x509Cert.IsCA, IsCA: x509Cert.IsCA,
MaxPathLen: x509Cert.MaxPathLen, MaxPathLen: x509Cert.MaxPathLen,
MaxPathLenZero: x509Cert.MaxPathLenZero, MaxPathLenZero: x509Cert.MaxPathLenZero,
SubjectKeyId: x509Cert.SubjectKeyId, SubjectKeyId: x509Cert.SubjectKeyId,
AuthorityKeyId: x509Cert.AuthorityKeyId, AuthorityKeyId: x509Cert.AuthorityKeyId,
...@@ -136,9 +136,9 @@ func ParseSm2CertificateToX509(sm2Cert *sm2.Certificate) *x509.Certificate { ...@@ -136,9 +136,9 @@ func ParseSm2CertificateToX509(sm2Cert *sm2.Certificate) *x509.Certificate {
UnknownExtKeyUsage: sm2Cert.UnknownExtKeyUsage, UnknownExtKeyUsage: sm2Cert.UnknownExtKeyUsage,
BasicConstraintsValid: sm2Cert.BasicConstraintsValid, BasicConstraintsValid: sm2Cert.BasicConstraintsValid,
IsCA: sm2Cert.IsCA, IsCA: sm2Cert.IsCA,
MaxPathLen: sm2Cert.MaxPathLen, MaxPathLen: sm2Cert.MaxPathLen,
MaxPathLenZero: sm2Cert.MaxPathLenZero, MaxPathLenZero: sm2Cert.MaxPathLenZero,
SubjectKeyId: sm2Cert.SubjectKeyId, SubjectKeyId: sm2Cert.SubjectKeyId,
AuthorityKeyId: sm2Cert.AuthorityKeyId, AuthorityKeyId: sm2Cert.AuthorityKeyId,
......
...@@ -13,14 +13,15 @@ import ( ...@@ -13,14 +13,15 @@ import (
"fmt" "fmt"
"bytes"
"github.com/33cn/chain33/account" "github.com/33cn/chain33/account"
"github.com/33cn/chain33/common" "github.com/33cn/chain33/common"
dbm "github.com/33cn/chain33/common/db"
drivers "github.com/33cn/chain33/system/store" drivers "github.com/33cn/chain33/system/store"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
dbm "github.com/33cn/chain33/common/db"
"bytes"
) )
const MaxKeylenth int = 64 const MaxKeylenth int = 64
...@@ -541,7 +542,6 @@ func TestDelMavlData(t *testing.T) { ...@@ -541,7 +542,6 @@ func TestDelMavlData(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
} }
func TestPruning(t *testing.T) { func TestPruning(t *testing.T) {
dir, err := ioutil.TempDir("", "example") dir, err := ioutil.TempDir("", "example")
assert.Nil(t, err) assert.Nil(t, err)
...@@ -551,7 +551,6 @@ func TestPruning(t *testing.T) { ...@@ -551,7 +551,6 @@ func TestPruning(t *testing.T) {
store := New(storeCfg, nil).(*KVmMavlStore) store := New(storeCfg, nil).(*KVmMavlStore)
assert.NotNil(t, store) assert.NotNil(t, store)
kvmvccStore := NewKVMVCC(&subKVMVCCConfig{}, store.GetDB()) kvmvccStore := NewKVMVCC(&subKVMVCCConfig{}, store.GetDB())
SetPruneHeight(10) SetPruneHeight(10)
...@@ -596,10 +595,10 @@ func TestPruning(t *testing.T) { ...@@ -596,10 +595,10 @@ func TestPruning(t *testing.T) {
//check //check
getDatas := &types.StoreGet{ getDatas := &types.StoreGet{
StateHash: drivers.EmptyRoot[:], StateHash: drivers.EmptyRoot[:],
Keys: keys, Keys: keys,
} }
for i := 0; i < len(hashes); i++ { for i := 0; i < len(hashes); i++ {
getDatas.StateHash = hashes[i] getDatas.StateHash = hashes[i]
values := store.Get(getDatas) values := store.Get(getDatas)
value = fmt.Sprintf("vv%d", i) value = fmt.Sprintf("vv%d", i)
......
...@@ -5,23 +5,24 @@ ...@@ -5,23 +5,24 @@
package kvmvccmavl package kvmvccmavl
import ( import (
"bytes"
"fmt"
"strconv"
"sync/atomic"
"github.com/33cn/chain33/common" "github.com/33cn/chain33/common"
dbm "github.com/33cn/chain33/common/db" dbm "github.com/33cn/chain33/common/db"
"github.com/33cn/chain33/queue" "github.com/33cn/chain33/queue"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"sync/atomic"
"fmt"
"strconv"
"bytes"
) )
const ( const (
pruningStateStart = 1 pruningStateStart = 1
pruningStateEnd = 0 pruningStateEnd = 0
onceScanCount = 10000 // 单次扫描数目 onceScanCount = 10000 // 单次扫描数目
onceCount = 1000 // 容器长度 onceCount = 1000 // 容器长度
LevelPruningHeight = 100 * 10000 levelPruningHeight = 100 * 10000
) )
var ( var (
...@@ -29,28 +30,28 @@ var ( ...@@ -29,28 +30,28 @@ var (
// 是否开启裁剪 // 是否开启裁剪
enablePrune bool enablePrune bool
// 每个10000裁剪一次 // 每个10000裁剪一次
pruneHeight = 10000 pruneHeight = 10000
pruningState int32 pruningState int32
) )
var ( var (
//同common/db中的mvcc相关的定义保持一致 //同common/db中的mvcc相关的定义保持一致
mvccPrefix = []byte(".-mvcc-.") mvccPrefix = []byte(".-mvcc-.")
mvccMeta = append(mvccPrefix, []byte("m.")...) //mvccMeta = append(mvccPrefix, []byte("m.")...)
mvccData = append(mvccPrefix, []byte("d.")...) mvccData = append(mvccPrefix, []byte("d.")...)
mvccLast = append(mvccPrefix, []byte("l.")...) //mvccLast = append(mvccPrefix, []byte("l.")...)
mvccMetaVersion = append(mvccMeta, []byte("version.")...) //mvccMetaVersion = append(mvccMeta, []byte("version.")...)
mvccMetaVersionKeyList = append(mvccMeta, []byte("versionkl.")...) //mvccMetaVersionKeyList = append(mvccMeta, []byte("versionkl.")...)
) )
// KVMVCCStore provide kvmvcc store interface implementation // KVMVCCStore provide kvmvcc store interface implementation
type KVMVCCStore struct { type KVMVCCStore struct {
db dbm.DB db dbm.DB
mvcc dbm.MVCC mvcc dbm.MVCC
kvsetmap map[string][]*types.KeyValue kvsetmap map[string][]*types.KeyValue
enableMVCCIter bool enableMVCCIter bool
enableMavlPrune bool enableMavlPrune bool
pruneHeight int32 pruneHeight int32
} }
// NewKVMVCC construct KVMVCCStore module // NewKVMVCC construct KVMVCCStore module
...@@ -62,10 +63,10 @@ func NewKVMVCC(sub *subKVMVCCConfig, db dbm.DB) *KVMVCCStore { ...@@ -62,10 +63,10 @@ func NewKVMVCC(sub *subKVMVCCConfig, db dbm.DB) *KVMVCCStore {
} }
if enable { if enable {
kvs = &KVMVCCStore{db, dbm.NewMVCCIter(db), make(map[string][]*types.KeyValue), kvs = &KVMVCCStore{db, dbm.NewMVCCIter(db), make(map[string][]*types.KeyValue),
true, sub.EnableMavlPrune, sub.PruneHeight} true, sub.EnableMavlPrune, sub.PruneHeight}
} else { } else {
kvs = &KVMVCCStore{db, dbm.NewMVCC(db), make(map[string][]*types.KeyValue), kvs = &KVMVCCStore{db, dbm.NewMVCC(db), make(map[string][]*types.KeyValue),
false, sub.EnableMavlPrune, sub.PruneHeight} false, sub.EnableMavlPrune, sub.PruneHeight}
} }
EnablePrune(sub.EnableMavlPrune) EnablePrune(sub.EnableMavlPrune)
SetPruneHeight(int(sub.PruneHeight)) SetPruneHeight(int(sub.PruneHeight))
...@@ -280,8 +281,8 @@ func calcHash(datas proto.Message) []byte { ...@@ -280,8 +281,8 @@ func calcHash(datas proto.Message) []byte {
return common.Sha256(b) return common.Sha256(b)
} }
/*裁剪-------------------------------------------*/
//裁剪-------------------------------------------
// EnablePrune 使能裁剪 // EnablePrune 使能裁剪
func EnablePrune(enable bool) { func EnablePrune(enable bool) {
enablePrune = enable enablePrune = enable
...@@ -324,10 +325,10 @@ func pruningFirst(db dbm.DB, curHeight int64) { ...@@ -324,10 +325,10 @@ func pruningFirst(db dbm.DB, curHeight int64) {
continue continue
} }
if curHeight < int64(height) + LevelPruningHeight && if curHeight < int64(height)+levelPruningHeight &&
curHeight >= int64(height) + int64(pruneHeight) { curHeight >= int64(height)+int64(pruneHeight) {
mp[string(key)] = append(mp[string(key)], height) mp[string(key)] = append(mp[string(key)], height)
count++ count++
} }
if len(mp) >= onceCount-1 || count > onceScanCount { if len(mp) >= onceCount-1 || count > onceScanCount {
deleteOldKV(mp, curHeight, batch) deleteOldKV(mp, curHeight, batch)
...@@ -349,8 +350,8 @@ func deleteOldKV(mp map[string][]int64, curHeight int64, batch dbm.Batch) { ...@@ -349,8 +350,8 @@ func deleteOldKV(mp map[string][]int64, curHeight int64, batch dbm.Batch) {
batch.Reset() batch.Reset()
for key, vals := range mp { for key, vals := range mp {
if len(vals) > 1 && vals[1] != vals[0] { //防止相同高度时候出现的误删除 if len(vals) > 1 && vals[1] != vals[0] { //防止相同高度时候出现的误删除
for _, val := range vals[1:] { //从第二个开始判断 for _, val := range vals[1:] { //从第二个开始判断
if curHeight >= val + int64(pruneHeight) { if curHeight >= val+int64(pruneHeight) {
batch.Delete(genKeyVersion([]byte(key), val)) // 删除老版本key batch.Delete(genKeyVersion([]byte(key), val)) // 删除老版本key
if batch.ValueSize() > batchDataSize { if batch.ValueSize() > batchDataSize {
batch.Write() batch.Write()
...@@ -376,7 +377,7 @@ func getKeyVersion(vsnKey []byte) ([]byte, int64, error) { ...@@ -376,7 +377,7 @@ func getKeyVersion(vsnKey []byte) ([]byte, int64, error) {
if !bytes.Contains(vsnKey, mvccData) { if !bytes.Contains(vsnKey, mvccData) {
return nil, 0, types.ErrSize return nil, 0, types.ErrSize
} }
if len(vsnKey) < len(mvccData) + 1 + 20 { if len(vsnKey) < len(mvccData)+1+20 {
return nil, 0, types.ErrSize return nil, 0, types.ErrSize
} }
sLen := vsnKey[len(vsnKey)-20:] sLen := vsnKey[len(vsnKey)-20:]
...@@ -400,4 +401,4 @@ func isPruning() bool { ...@@ -400,4 +401,4 @@ func isPruning() bool {
func setPruning(state int32) { func setPruning(state int32) {
atomic.StoreInt32(&pruningState, state) atomic.StoreInt32(&pruningState, state)
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment