Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/ Mutex bug fix #271

Merged
merged 1 commit into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 28 additions & 29 deletions x/register/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,43 @@ import (
regtypes "github.com/stratosnet/stratos-chain/x/register/types"
)

var (
metaNodeBitMapIndexCacheStatus = types.CACHE_DIRTY
cacheMutex sync.RWMutex
)

// Keeper of the register store
type Keeper struct {
storeKey sdk.StoreKey
cdc codec.Codec
paramSpace paramtypes.Subspace
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
distrKeeper types.DistrKeeper
hooks types.RegisterHooks
resourceNodeCache map[string]cachedResourceNode
resourceNodeCacheList *list.List
metaNodeCache map[string]cachedMetaNode
metaNodeCacheList *list.List
metaNodeBitMapIndexCache map[string]int
storeKey sdk.StoreKey
cdc codec.Codec
paramSpace paramtypes.Subspace
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
distrKeeper types.DistrKeeper
hooks types.RegisterHooks
resourceNodeCache map[string]cachedResourceNode
resourceNodeCacheList *list.List
metaNodeCache map[string]cachedMetaNode
metaNodeCacheList *list.List
metaNodeBitMapIndexCache map[string]int
metaNodeBitMapIndexCacheStatus types.CacheStatus
cacheMutex sync.RWMutex
}

// NewKeeper creates a register keeper
func NewKeeper(cdc codec.Codec, key sdk.StoreKey, paramSpace paramtypes.Subspace,
accountKeeper types.AccountKeeper, bankKeeper types.BankKeeper, distrKeeper types.DistrKeeper) Keeper {

keeper := Keeper{
storeKey: key,
cdc: cdc,
paramSpace: paramSpace.WithKeyTable(types.ParamKeyTable()),
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
distrKeeper: distrKeeper,
hooks: nil,
resourceNodeCache: make(map[string]cachedResourceNode, resourceNodeCacheSize),
resourceNodeCacheList: list.New(),
metaNodeCache: make(map[string]cachedMetaNode, metaNodeCacheSize),
metaNodeCacheList: list.New(),
metaNodeBitMapIndexCache: make(map[string]int),
storeKey: key,
cdc: cdc,
paramSpace: paramSpace.WithKeyTable(types.ParamKeyTable()),
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
distrKeeper: distrKeeper,
hooks: nil,
resourceNodeCache: make(map[string]cachedResourceNode, resourceNodeCacheSize),
resourceNodeCacheList: list.New(),
metaNodeCache: make(map[string]cachedMetaNode, metaNodeCacheSize),
metaNodeCacheList: list.New(),
metaNodeBitMapIndexCache: make(map[string]int),
metaNodeBitMapIndexCacheStatus: types.CACHE_DIRTY,
cacheMutex: sync.RWMutex{},
}
return keeper
}
Expand Down
34 changes: 18 additions & 16 deletions x/register/keeper/meta_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,8 @@ func (k Keeper) OwnMetaNode(ctx sdk.Context, ownerAddr sdk.AccAddress, p2pAddr s
func (k Keeper) GetMetaNodeBitMapIndex(ctx sdk.Context, networkAddr stratos.SdsAddress) (index int, err error) {
k.UpdateMetaNodeBitMapIdxCache(ctx)

cacheMutex.RLock()
defer cacheMutex.RUnlock()
k.cacheMutex.RLock()
defer k.cacheMutex.RUnlock()

index, ok := k.metaNodeBitMapIndexCache[networkAddr.String()]
if !ok {
Expand All @@ -571,29 +571,30 @@ func (k Keeper) GetMetaNodeBitMapIndex(ctx sdk.Context, networkAddr stratos.SdsA
}

func (k Keeper) AddMetaNodeToBitMapIdxCache(networkAddr stratos.SdsAddress) {
cacheMutex.Lock()
defer cacheMutex.Unlock()
k.cacheMutex.Lock()
defer k.cacheMutex.Unlock()

k.metaNodeBitMapIndexCache[networkAddr.String()] = -1
metaNodeBitMapIndexCacheStatus = types.CACHE_DIRTY
k.metaNodeBitMapIndexCacheStatus = types.CACHE_DIRTY
}

func (k Keeper) RemoveMetaNodeFromBitMapIdxCache(networkAddr stratos.SdsAddress) {
cacheMutex.Lock()
defer cacheMutex.Unlock()
k.cacheMutex.Lock()
defer k.cacheMutex.Unlock()

delete(k.metaNodeBitMapIndexCache, networkAddr.String())
metaNodeBitMapIndexCacheStatus = types.CACHE_DIRTY
k.metaNodeBitMapIndexCacheStatus = types.CACHE_DIRTY
}

func (k Keeper) UpdateMetaNodeBitMapIdxCache(ctx sdk.Context) {
cacheMutex.Lock()
defer cacheMutex.Unlock()
k.cacheMutex.Lock()

if metaNodeBitMapIndexCacheStatus == types.CACHE_NOT_DIRTY {
if k.metaNodeBitMapIndexCacheStatus == types.CACHE_NOT_DIRTY {
k.cacheMutex.Unlock()
return
}
if len(k.metaNodeBitMapIndexCache) == 0 {
k.cacheMutex.Unlock()
k.ReloadMetaNodeBitMapIdxCache(ctx)
return
}
Expand All @@ -608,14 +609,15 @@ func (k Keeper) UpdateMetaNodeBitMapIdxCache(ctx sdk.Context) {
for index, key := range keys {
k.metaNodeBitMapIndexCache[key] = index
}
metaNodeBitMapIndexCacheStatus = types.CACHE_NOT_DIRTY
k.metaNodeBitMapIndexCacheStatus = types.CACHE_NOT_DIRTY
k.cacheMutex.Unlock()
}

func (k Keeper) ReloadMetaNodeBitMapIdxCache(ctx sdk.Context) {
cacheMutex.Lock()
defer cacheMutex.Unlock()
k.cacheMutex.Lock()
defer k.cacheMutex.Unlock()

if metaNodeBitMapIndexCacheStatus == types.CACHE_NOT_DIRTY {
if k.metaNodeBitMapIndexCacheStatus == types.CACHE_NOT_DIRTY {
return
}
keys := make([]string, 0)
Expand All @@ -637,5 +639,5 @@ func (k Keeper) ReloadMetaNodeBitMapIdxCache(ctx sdk.Context) {
for index, key := range keys {
k.metaNodeBitMapIndexCache[key] = index
}
metaNodeBitMapIndexCacheStatus = types.CACHE_NOT_DIRTY
k.metaNodeBitMapIndexCacheStatus = types.CACHE_NOT_DIRTY
}