Skip to content

Commit bf044de

Browse files
authored
fix commit namespace config panic without prepare (#104)
1 parent 9d70803 commit bf044de

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

core/errors/errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,7 @@ var (
9393
ErrInternalServer = errors.New("internal server error")
9494
// ErrUserIsReadOnly user is readonly
9595
ErrUserIsReadOnly = errors.New("user is readonly")
96+
97+
// ErrNamespaceNotPrepared commit namespace config without prepare
98+
ErrNamespaceNotPrepared = errors.New("namespace is not prepared")
9699
)

proxy/server/manager.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ import (
2525
"sync"
2626
"time"
2727

28+
"github.com/XiaoMi/Gaea/core/errors"
2829
"github.com/XiaoMi/Gaea/log"
2930
"github.com/XiaoMi/Gaea/models"
3031
"github.com/XiaoMi/Gaea/mysql"
3132
"github.com/XiaoMi/Gaea/parser"
3233
"github.com/XiaoMi/Gaea/stats"
3334
"github.com/XiaoMi/Gaea/stats/prometheus"
3435
"github.com/XiaoMi/Gaea/util"
36+
"github.com/XiaoMi/Gaea/util/sync2"
3537
)
3638

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

127129
// Manager contains namespace manager and user manager
128130
type Manager struct {
129-
switchIndex util.BoolIndex
130-
namespaces [2]*NamespaceManager
131-
users [2]*UserManager
132-
statistics *StatisticManager
131+
reloadPrepared sync2.AtomicBool
132+
switchIndex util.BoolIndex
133+
namespaces [2]*NamespaceManager
134+
users [2]*UserManager
135+
statistics *StatisticManager
133136
}
134137

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

200204
return nil
201205
}
202206

203207
// ReloadNamespaceCommit commit config
204208
func (m *Manager) ReloadNamespaceCommit(name string) error {
209+
if !m.reloadPrepared.CompareAndSwap(true, false) {
210+
err := errors.ErrNamespaceNotPrepared
211+
log.Warn("commit namespace error, namespace: %s, err: %v", err)
212+
return err
213+
}
214+
205215
current, _, index := m.switchIndex.Get()
206216

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

837847
//record wait queue length
838848
func (s *StatisticManager) recordConnectPoolWaitCount(namespace string, slice string, addr string, count int64) {
839-
statsKey := []string{s.clusterName, namespace, slice, addr}
849+
statsKey := []string{s.clusterName, namespace, slice, addr}
840850
s.backendConnectPoolWaitCounts.Set(statsKey, count)
841851
}

0 commit comments

Comments
 (0)