-
Notifications
You must be signed in to change notification settings - Fork 13
/
process-config-admin.go
114 lines (98 loc) · 2.6 KB
/
process-config-admin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package process_master
import (
"encoding/json"
"github.com/eolinker/eosc"
"github.com/eolinker/eosc/common/dispatcher"
"github.com/eolinker/eosc/log"
"github.com/eolinker/eosc/process"
"strings"
"sync"
)
// AdminController admin控制器,管理admin进程的启动、重启
type AdminController struct {
adminProcess *process.ProcessController
locker sync.RWMutex
isExtenderSuccess bool
data *dispatcher.Data
isLeader bool
registerChannel chan<- int
lastExtenderConfig map[string]string
}
func (ac *AdminController) doEvent(event dispatcher.IEvent) error {
ac.data.DoEvent(event)
if event.Event() == eosc.EventReset {
ac.restart()
} else if event.Namespace() == eosc.NamespaceExtender {
// 变更插件配置时
ac.checkExtender()
}
return nil
}
func (ac *AdminController) checkExtender() {
ac.locker.Lock()
defer ac.locker.Unlock()
if !ac.isLeader {
return
}
extendersData, _ := ac.data.GetNamespace(eosc.NamespaceExtender)
newExtenders := ac.toExtends(extendersData)
for id, v := range newExtenders {
if ov, has := ac.lastExtenderConfig[id]; has {
if !strings.EqualFold(v, ov) {
// 存在相同插件切版本不一致,reload admin
ac.restart()
return
}
}
}
return
}
func (ac *AdminController) toExtends(org map[string][]byte) map[string]string {
tmp := make(map[string]string)
if org != nil {
for k, v := range org {
tmp[k] = string(v)
}
}
return tmp
}
func (ac *AdminController) LeaderChange(isLeader bool) {
log.Debug("Leader change:", isLeader)
ac.locker.Lock()
defer ac.locker.Unlock()
if ac.isLeader != isLeader {
ac.isLeader = isLeader
if isLeader {
configs := ac.data.GET()
if configs == nil {
configs = map[string]map[string][]byte{}
}
data, _ := json.Marshal(configs)
ac.lastExtenderConfig = ac.toExtends(configs[eosc.NamespaceExtender])
ac.adminProcess.Start(data, nil)
} else {
ac.adminProcess.Shutdown()
}
}
}
func (ac *AdminController) restart() {
configs := ac.data.GET()
if configs == nil {
configs = map[string]map[string][]byte{}
}
data, _ := json.Marshal(configs)
ac.lastExtenderConfig = ac.toExtends(configs[eosc.NamespaceExtender])
ac.adminProcess.TryRestart(data, nil)
}
func (ac *AdminController) Stop() {
ac.adminProcess.Stop()
close(ac.registerChannel)
}
func NewAdminConfig(raftData dispatcher.IDispatchCenter, adminProcess *process.ProcessController) *AdminController {
wc := &AdminController{
adminProcess: adminProcess,
data: dispatcher.NewMyData(map[string]map[string][]byte{}),
}
wc.registerChannel = raftData.Register(wc.doEvent)
return wc
}