/
core.go
137 lines (128 loc) · 2.89 KB
/
core.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Copyright (C) 2018 Simon Schmidt
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// Core
package core
import avl "github.com/emirpasic/gods/trees/avltree"
import "github.com/emirpasic/gods/utils"
import "time"
//import "github.com/valyala/gorpc"
import "github.com/byte-mug/walcast/nodes"
import "github.com/byte-mug/walcast/rpc"
//import "github.com/byte-mug/walcast/rpcdp"
import "sync"
import "fmt"
type nodeEvent struct{
enter bool
name string
}
type State uint
const (
ALONE State = iota
MASTER
SLAVE
)
func (s State) String() string {
switch s {
case ALONE: return "ALONE"
case MASTER: return "MASTER"
case SLAVE: return "SLAVE"
}
return fmt.Sprintf("%d",uint(s))
}
type IConfig interface{
SetNodeNameList(s []interface{})
Export() interface{}
Import(i interface{})
}
type NodeEngine struct {
Self nodes.NodeRpc
Pool rpc.ConnectionPool
Config IConfig
dirty bool
master *nodes.NodeRpc
slaves *avl.Tree
allNodes *avl.Tree
nodeEvs chan nodeEvent
stateLk sync.RWMutex
}
func(ne *NodeEngine) Init() {
ne.Self.Startup = time.Now().UTC()
ne.Pool.Init()
ne.slaves = avl.NewWith(utils.StringComparator)
ne.allNodes = avl.NewWith(utils.StringComparator)
ne.allNodes.Put(ne.Self.Name,nil)
ne.nodeEvs = make(chan nodeEvent,16)
go ne.perform()
}
func(ne *NodeEngine) State() State {
if ne.master!=nil {
return SLAVE
} else if ne.slaves.Root!=nil {
return MASTER
}
return ALONE
}
func(ne *NodeEngine) perform() {
ticker := time.Tick(time.Second)
refresh := time.Tick(time.Second*5)
for {
select {
case ev := <- ne.nodeEvs:
if ev.enter {
ne.enter(ev.name)
} else {
ne.leave(ev.name)
}
case <- ticker:
ne.pushSlaves()
case <- refresh:
switch ne.State() {
case ALONE:
if nd,ok := ne.Pool.Obs.Map.GetAny(ne.Self.Name); ok && nd!=nil {
go ne.hookUp(nd.Name)
}
case SLAVE:
ne.recheck()
}
}
}
}
func(ne *NodeEngine) enter(n string) {
node,ok := ne.Pool.Obs.Map.Get(n)
if !ok { return }
if node.Name==ne.Self.Name { return }
switch ne.State() {
case ALONE:
if node.Startup.Before(ne.Self.Startup) {
go ne.hookUp(node.Name)
}
case MASTER:
if node.Startup.Before(ne.Self.Startup) {
go func() {
ne.hookUp(node.Name)
time.Sleep(time.Second)
ne.redirectSlaves(node.Rpc())
}()
}
case SLAVE: // ignore
}
}
func(ne *NodeEngine) leave(n string) {
node,ok := ne.Pool.Obs.Map.Get(n)
if !ok { return }
if node.Name==ne.Self.Name { return }
ne.stateLk.Lock(); defer ne.stateLk.Unlock()
ne.slaves.Remove(node.Name)
if ne.master==nil { return }
if ne.master.Name == node.Name { ne.master=nil }
}
func(ne *NodeEngine) OnEnter(n string) {
ne.nodeEvs <- nodeEvent{true,n}
}
func(ne *NodeEngine) OnLeave(n string) {
ne.nodeEvs <- nodeEvent{false,n}
}