Skip to content

Commit

Permalink
fix commit namespace config panic without prepare (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
teckick authored Sep 5, 2020
1 parent 9d70803 commit bf044de
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
3 changes: 3 additions & 0 deletions core/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,7 @@ var (
ErrInternalServer = errors.New("internal server error")
// ErrUserIsReadOnly user is readonly
ErrUserIsReadOnly = errors.New("user is readonly")

// ErrNamespaceNotPrepared commit namespace config without prepare
ErrNamespaceNotPrepared = errors.New("namespace is not prepared")
)
20 changes: 15 additions & 5 deletions proxy/server/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ import (
"sync"
"time"

"github.com/XiaoMi/Gaea/core/errors"
"github.com/XiaoMi/Gaea/log"
"github.com/XiaoMi/Gaea/models"
"github.com/XiaoMi/Gaea/mysql"
"github.com/XiaoMi/Gaea/parser"
"github.com/XiaoMi/Gaea/stats"
"github.com/XiaoMi/Gaea/stats/prometheus"
"github.com/XiaoMi/Gaea/util"
"github.com/XiaoMi/Gaea/util/sync2"
)

// LoadAndCreateManager load namespace config, and create manager
Expand Down Expand Up @@ -126,10 +128,11 @@ func loadAllNamespace(cfg *models.Proxy) (map[string]*models.Namespace, error) {

// Manager contains namespace manager and user manager
type Manager struct {
switchIndex util.BoolIndex
namespaces [2]*NamespaceManager
users [2]*UserManager
statistics *StatisticManager
reloadPrepared sync2.AtomicBool
switchIndex util.BoolIndex
namespaces [2]*NamespaceManager
users [2]*UserManager
statistics *StatisticManager
}

// NewManager return empty Manager
Expand Down Expand Up @@ -196,12 +199,19 @@ func (m *Manager) ReloadNamespacePrepare(namespaceConfig *models.Namespace) erro
newUserManager := CloneUserManager(currentUserManager)
newUserManager.RebuildNamespaceUsers(namespaceConfig)
m.users[other] = newUserManager
m.reloadPrepared.Set(true)

return nil
}

// ReloadNamespaceCommit commit config
func (m *Manager) ReloadNamespaceCommit(name string) error {
if !m.reloadPrepared.CompareAndSwap(true, false) {
err := errors.ErrNamespaceNotPrepared
log.Warn("commit namespace error, namespace: %s, err: %v", err)
return err
}

current, _, index := m.switchIndex.Get()

currentNamespace := m.namespaces[current].GetNamespace(name)
Expand Down Expand Up @@ -836,6 +846,6 @@ func (s *StatisticManager) recordConnectPoolInuseCount(namespace string, slice s

//record wait queue length
func (s *StatisticManager) recordConnectPoolWaitCount(namespace string, slice string, addr string, count int64) {
statsKey := []string{s.clusterName, namespace, slice, addr}
statsKey := []string{s.clusterName, namespace, slice, addr}
s.backendConnectPoolWaitCounts.Set(statsKey, count)
}

0 comments on commit bf044de

Please sign in to comment.