Skip to content

Commit 32dd330

Browse files
committed
fixed sync man lock problem
1 parent 3e9a286 commit 32dd330

File tree

5 files changed

+43
-31
lines changed

5 files changed

+43
-31
lines changed

config/load.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ func loadEnvironmentVariable(c *Config) {
1919
p.Secret = tempEnvVar
2020
}
2121
}
22-
for _, value := range p.Modules.Crud {
23-
if strings.HasPrefix(value.Conn, "$") {
24-
tempStringC := strings.TrimPrefix(value.Conn, "$")
25-
tempEnvVarC, presentC := os.LookupEnv(tempStringC)
26-
27-
if presentC {
28-
value.Conn = tempEnvVarC
22+
if p.Modules != nil {
23+
for _, value := range p.Modules.Crud {
24+
if strings.HasPrefix(value.Conn, "$") {
25+
tempStringC := strings.TrimPrefix(value.Conn, "$")
26+
tempEnvVarC, presentC := os.LookupEnv(tempStringC)
27+
28+
if presentC {
29+
value.Conn = tempEnvVarC
30+
}
2931
}
3032
}
3133
}

go.sum

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 h1:nn5Wsu0esKSJiIVhscUt
167167
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
168168
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
169169
github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
170+
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
170171
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
171172
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
172173
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
@@ -225,7 +226,6 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv
225226
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
226227
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
227228
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
228-
golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed h1:uPxWBzB3+mlnjy9W58qY1j/cjyFjutgw/Vhan2zLy/A=
229229
golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
230230
golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f h1:25KHgbfyiSm6vwQLbM3zZIe1v9p/3ea4Rz+nnM5K/i4=
231231
golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

utils/syncman/operations.go

+18-15
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ import (
1515

1616
// SetGlobalConfig sets the global config. This must be called before the Start command.
1717
func (s *SyncManager) SetGlobalConfig(c *config.Config) {
18-
s.lock.Lock()
18+
s.internalLock.Lock()
1919
s.projectConfig = c
20-
s.lock.Unlock()
20+
s.internalLock.Unlock()
2121
}
2222

2323
// GetGlobalConfig gets the global config
2424
func (s *SyncManager) GetGlobalConfig() *config.Config {
25-
s.lock.RLock()
26-
defer s.lock.RUnlock()
25+
s.internalLock.Lock()
26+
defer s.internalLock.Unlock()
2727
return s.projectConfig
2828
}
2929

@@ -59,8 +59,8 @@ func makeRequest(method, token, url string, data *bytes.Buffer) error {
5959
// SetOperationModeConfig applies the operation config to the raft log
6060
func (s *SyncManager) SetOperationModeConfig(token string, op *config.OperationConfig) error {
6161
// Acquire a lock to make sure only a single operation occurs at any given point of time
62-
s.lock.Lock()
63-
defer s.lock.Unlock()
62+
s.internalLock.Lock()
63+
defer s.internalLock.Unlock()
6464

6565
if s.raft.State() != raft.Leader {
6666
// Marshal json into byte array
@@ -84,8 +84,8 @@ func (s *SyncManager) SetOperationModeConfig(token string, op *config.OperationC
8484
// SetProjectConfig applies the config to the raft log
8585
func (s *SyncManager) SetProjectConfig(token string, project *config.Project) error {
8686
// Acquire a lock to make sure only a single operation occurs at any given point of time
87-
s.lock.Lock()
88-
defer s.lock.Unlock()
87+
s.internalLock.Lock()
88+
defer s.internalLock.Unlock()
8989

9090
if s.raft.State() != raft.Leader {
9191
// Marshal json into byte array
@@ -105,7 +105,10 @@ func (s *SyncManager) SetProjectConfig(token string, project *config.Project) er
105105

106106
// Create a raft command
107107
c := &model.RaftCommand{Kind: utils.RaftCommandSet, Project: project, ID: project.ID}
108-
data, _ := json.Marshal(c)
108+
data, err := json.Marshal(c)
109+
if err != nil {
110+
return err
111+
}
109112

110113
// Apply the command to the raft log
111114
return s.raft.Apply(data, 0).Error()
@@ -114,8 +117,8 @@ func (s *SyncManager) SetProjectConfig(token string, project *config.Project) er
114117
// SetDeployConfig applies the config to the raft log
115118
func (s *SyncManager) SetDeployConfig(token string, deploy *config.Deploy) error {
116119
// Acquire a lock to make sure only a single operation occurs at any given point of time
117-
s.lock.Lock()
118-
defer s.lock.Unlock()
120+
s.internalLock.Lock()
121+
defer s.internalLock.Unlock()
119122

120123
if s.raft.State() != raft.Leader {
121124
// Marshal json into byte array
@@ -139,8 +142,8 @@ func (s *SyncManager) SetDeployConfig(token string, deploy *config.Deploy) error
139142
// DeleteConfig applies the config to the raft log
140143
func (s *SyncManager) DeleteConfig(token, projectID string) error {
141144
// Acquire a lock to make sure only a single operation occurs at any given point of time
142-
s.lock.Lock()
143-
defer s.lock.Unlock()
145+
s.internalLock.Lock()
146+
defer s.internalLock.Unlock()
144147

145148
if s.raft.State() != raft.Leader {
146149

@@ -161,8 +164,8 @@ func (s *SyncManager) DeleteConfig(token, projectID string) error {
161164

162165
// GetConfig returns the config present in the state
163166
func (s *SyncManager) GetConfig(projectID string) (*config.Project, error) {
164-
s.lock.RLock()
165-
defer s.lock.RUnlock()
167+
s.internalLock.Lock()
168+
defer s.internalLock.Unlock()
166169

167170
// Iterate over all projects stored
168171
for _, p := range s.projectConfig.Projects {

utils/syncman/raft.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ func (s *SyncManager) initRaft(seeds []*node) error {
4747
config := raft.DefaultConfig()
4848
config.LocalID = raft.ServerID(s.projectConfig.NodeID)
4949
config.LogOutput = ioutil.Discard
50+
config.SnapshotInterval = time.Hour * 3
51+
config.SnapshotThreshold = 3
52+
53+
// Check if config is valid
54+
if err := raft.ValidateConfig(config); err != nil {
55+
return err
56+
}
5057

5158
// Instantiate the Raft systems.
5259
r, err := raft.NewRaft(config, s, logStore, stableStore, snapshots, transport)
@@ -73,15 +80,14 @@ func (s *SyncManager) initRaft(seeds []*node) error {
7380

7481
// Apply applies a Raft log entry to the key-value store
7582
func (s *SyncManager) Apply(l *raft.Log) interface{} {
76-
s.lock.Lock()
77-
defer s.lock.Unlock()
83+
// s.internalLock.Lock()
84+
// defer s.internalLock.Unlock()
7885

7986
var c model.RaftCommand
8087
json.Unmarshal(l.Data, &c)
8188

8289
switch c.Kind {
8390
case utils.RaftCommandSet:
84-
8591
found := false
8692
for i, p := range s.projectConfig.Projects {
8793
if p.ID == c.Project.ID {
@@ -126,6 +132,7 @@ func (s *SyncManager) Apply(l *raft.Log) interface{} {
126132
// Write the config to file
127133
config.StoreConfigToFile(s.projectConfig, s.configFile)
128134
}
135+
129136
return nil
130137
}
131138

@@ -144,8 +151,8 @@ func (s *SyncManager) Restore(rc io.ReadCloser) error {
144151

145152
// Snapshot returns a snapshot of the key-value store.
146153
func (s *SyncManager) Snapshot() (raft.FSMSnapshot, error) {
147-
s.lock.Lock()
148-
defer s.lock.Unlock()
154+
s.internalLock.Lock()
155+
defer s.internalLock.Unlock()
149156

150157
return &fsmSnapshot{store: s.projectConfig}, nil
151158
}

utils/syncman/syncman.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
// SyncManager syncs the project config between folders
1717
type SyncManager struct {
18-
lock sync.RWMutex
18+
internalLock sync.Mutex
1919
raft *raft.Raft
2020
projectConfig *config.Config
2121
configFile string
@@ -45,7 +45,7 @@ func New(projects *projects.Projects, d *deploy.Module, adminMan *admin.Manager)
4545
// Start begins the sync manager operations
4646
func (s *SyncManager) Start(nodeID, configFilePath, gossipPort, raftPort string, seeds []string) error {
4747
// Save the ports
48-
s.lock.Lock()
48+
s.internalLock.Lock()
4949
s.gossipPort = gossipPort
5050
s.raftPort = raftPort
5151

@@ -64,7 +64,7 @@ func (s *SyncManager) Start(nodeID, configFilePath, gossipPort, raftPort string,
6464
}
6565
}
6666

67-
s.lock.Unlock()
67+
s.internalLock.Unlock()
6868

6969
// Start the membership protocol
7070
if err := s.initMembership(seeds); err != nil {

0 commit comments

Comments
 (0)