-
Notifications
You must be signed in to change notification settings - Fork 2
/
twemproxy.go
172 lines (134 loc) · 3.18 KB
/
twemproxy.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package ralph
import (
"log"
"os"
"os/exec"
"reflect"
"sync"
"syscall"
"github.com/bobrik/marathoner"
"gopkg.in/yaml.v2"
)
// TwemproxyConfigurator implements ConfiguratorImplementation for twemproxy
type TwemproxyConfigurator struct {
state twemproxyConfig
mutex sync.Mutex
conf string
bind string
args []string
pid int
}
// NewTwemproxyConfigurator creates configurator with specified config path,
// bind address and extra args for twemproxy
func NewTwemproxyConfigurator(conf string, bind string, args []string) *TwemproxyConfigurator {
return &TwemproxyConfigurator{
state: nil,
mutex: sync.Mutex{},
conf: conf,
bind: bind,
args: args,
}
}
// Update runs update and logs an error if it happens
func (t *TwemproxyConfigurator) Update(s marathoner.State, r *bool) error {
err := t.update(s, r)
if err != nil {
log.Println("error updating configuration:", err)
}
return err
}
// update runs all steps to perform configuration update and reload
func (t *TwemproxyConfigurator) update(s marathoner.State, r *bool) error {
t.mutex.Lock()
defer t.mutex.Unlock()
log.Println("received update request")
state := stateToPools(s, t.bind)
if reflect.DeepEqual(state, t.state) {
log.Println("state is the same, not doing any updates")
*r = false
return nil
}
t.state = state
err := t.updateConfig()
if err != nil {
log.Fatal(err)
return err
}
log.Println("config updated")
err = t.checkConfig()
if err != nil {
return err
}
log.Println("config validity checked")
err = t.reload()
if err != nil {
return err
}
log.Println("twemproxy reloaded")
*r = true
return nil
}
// updateConfig updates yaml config or twemproxy
func (t *TwemproxyConfigurator) updateConfig() error {
c, err := yaml.Marshal(t.state)
if err != nil {
return err
}
f, err := os.Create(t.conf)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(c)
return err
}
// checkConfig checks if config looks good for twemproxy
func (t *TwemproxyConfigurator) checkConfig() error {
return exec.Command("nutcracker", "-t", "-c", t.conf).Run()
}
// reload sends SIGUSR1 to twemproxy to reload config
func (t *TwemproxyConfigurator) reload() error {
if t.pid == 0 {
log.Println("twemproxy is not started yet, starting")
return t.runTwemproxy()
}
err := syscall.Kill(t.pid, syscall.SIGUSR1)
if err != nil {
if err != syscall.ESRCH {
return err
}
return t.runTwemproxy()
}
return err
}
// runTwemproxy starts twemproxy process
func (t *TwemproxyConfigurator) runTwemproxy() error {
args := append([]string{"-c", "/etc/nutcracker.yml"}, t.args...)
cmd := exec.Command("nutcracker", args...)
err := cmd.Start()
if err != nil {
return err
}
t.pid = cmd.Process.Pid
go cmd.Wait()
return nil
}
// stateToPools converts marathoner state to twemproxy config
func stateToPools(s marathoner.State, bind string) twemproxyConfig {
c := map[string]twemproxyPool{}
for n, a := range s {
var name string
if v, ok := a.Labels["twemproxy_pool"]; !ok {
continue
} else {
name = v
}
pool, err := newPoolFromApp(a, bind)
if err != nil {
log.Printf("twemproxy creation error for %s: %s", n, err)
continue
}
c[name] = pool
}
return c
}