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

Merging to release-5.3: [TT-12417] Do not delete keys on synchronization (#6642) #6667

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
2 changes: 2 additions & 0 deletions gateway/rpc_storage_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,8 @@ func (r *RPCStorageHandler) CheckForReload(orgId string) bool {
r.CheckForReload(orgId)
}
} else if !strings.Contains(err.Error(), "Cannot obtain response during") {
forcer := rpc.NewSyncForcer(r.Gw.StorageConnectionHandler, r.buildNodeInfo)
forcer.SetFirstConnection(true)
log.Warning("[RPC STORE] RPC Reload Checker encountered unexpected error: ", err)
}

Expand Down
41 changes: 33 additions & 8 deletions rpc/synchronization_forcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,55 @@ package rpc

import (
"errors"
"sync"

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/storage"
)

type SyncronizerForcer struct {
store *storage.RedisCluster
getNodeDataFunc func() []byte
store *storage.RedisCluster
getNodeDataFunc func() []byte
isFirstConnection bool
}

var (
syncForcerInstance *SyncronizerForcer
syncForcerOnce sync.Once
)

// NewSyncForcer returns a new syncforcer with a connected redis with a key prefix synchronizer-group- for group synchronization control.
func NewSyncForcer(controller *storage.ConnectionHandler, getNodeDataFunc func() []byte) *SyncronizerForcer {
sf := &SyncronizerForcer{}
sf.getNodeDataFunc = getNodeDataFunc
sf.store = &storage.RedisCluster{KeyPrefix: "synchronizer-group-", ConnectionHandler: controller}
sf.store.Connect()
syncForcerOnce.Do(func() {
sf := &SyncronizerForcer{}
sf.store = &storage.RedisCluster{KeyPrefix: "synchronizer-group-", ConnectionHandler: controller}
sf.store.Connect()
sf.getNodeDataFunc = getNodeDataFunc
sf.isFirstConnection = true

syncForcerInstance = sf
})

if syncForcerInstance != nil {
syncForcerInstance.getNodeDataFunc = getNodeDataFunc
}

return syncForcerInstance
}

func (sf *SyncronizerForcer) SetFirstConnection(isFirstConnection bool) {
sf.isFirstConnection = isFirstConnection
}

return sf
func (sf *SyncronizerForcer) GetIsFirstConnection() bool {
return sf.isFirstConnection
}

// GroupLoginCallback checks if the groupID key exists in the storage to turn on/off ForceSync param.
// If the the key doesn't exists in the storage, it creates it and set ForceSync to true
func (sf *SyncronizerForcer) GroupLoginCallback(userKey string, groupID string) interface{} {
shouldForce := false
shouldForce := sf.isFirstConnection
sf.SetFirstConnection(false)

_, err := sf.store.GetKey(groupID)
if err != nil && errors.Is(err, storage.ErrKeyNotFound) {
Expand Down
Loading