-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbox.go
269 lines (226 loc) · 5.91 KB
/
box.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package tnt
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"sync"
"time"
)
const (
dirSnap = "snap"
dirWAL = "wal"
)
// Box is a tarantool instance with specified config and BoxOptions.
type Box struct {
Root string
Port uint
cmd *exec.Cmd
stopOnce sync.Once
stopped chan bool
}
// BoxOptions is the options for the Box instance.
type BoxOptions struct {
Listen uint
PortMin uint
PortMax uint
InitLua string
}
// NewBox instance.
func NewBox(config string, options ...BoxOptions) (*Box, error) {
var opts BoxOptions
if len(options) > 0 {
opts = options[0]
} else {
opts = BoxOptions{}
}
if opts.PortMin == 0 {
opts.PortMin = 8000
}
if opts.PortMax == 0 {
opts.PortMax = 9000
}
if opts.Listen != 0 {
opts.PortMin = opts.Listen
opts.PortMax = opts.Listen
}
var box *Box
START_LOOP:
for port := opts.PortMin; port <= opts.PortMax; port += 3 {
tmpDir, err := ioutil.TempDir("", "")
if err != nil {
return nil, err
}
tarantoolConf := `
slab_alloc_arena = 1
slab_alloc_factor = 1.04
pid_file = {root}/box.pid
work_dir = {root}
snap_dir = {snap}
wal_dir = {wal}
snap_io_rate_limit = 10
rows_per_wal = 1000000
too_long_threshold = 0.025
primary_port = {port1}
memcached_expire = false
memcached_port = {port2}
admin_port = {port3}
replication_port = {port4}
`
tarantoolConf = strings.Replace(tarantoolConf, "{port1}", fmt.Sprintf("%d", port), -1)
tarantoolConf = strings.Replace(tarantoolConf, "{port2}", fmt.Sprintf("%d", port+1), -1)
tarantoolConf = strings.Replace(tarantoolConf, "{port3}", fmt.Sprintf("%d", port+2), -1)
tarantoolConf = strings.Replace(tarantoolConf, "{port4}", fmt.Sprintf("%d", port+3), -1)
tarantoolConf = strings.Replace(tarantoolConf, "{root}", tmpDir, -1)
tarantoolConf = strings.Replace(tarantoolConf, "{snap}", dirSnap, -1)
tarantoolConf = strings.Replace(tarantoolConf, "{wal}", dirWAL, -1)
tarantoolConf = fmt.Sprintf("%s\n%s", tarantoolConf, config)
tarantoolConfFile := path.Join(tmpDir, "tarantool.conf")
if err = ioutil.WriteFile(tarantoolConfFile, []byte(tarantoolConf), 0644); err != nil {
return nil, err
}
initLuaFile := path.Join(tmpDir, "init.lua")
if err = ioutil.WriteFile(initLuaFile, []byte(opts.InitLua), 0644); err != nil {
return nil, err
}
for _, subDir := range []string{"snap", "wal"} {
if err = os.Mkdir(path.Join(tmpDir, subDir), 0755); err != nil {
return nil, err
}
}
cmd0 := exec.Command("tarantool_box", "-c", tarantoolConfFile, "--init-storage")
if err = cmd0.Run(); err != nil {
return nil, err
}
cmd := exec.Command("tarantool_box", "-c", tarantoolConfFile)
boxStderr, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
if err = cmd.Start(); err != nil {
return nil, err
}
var boxStderrBuffer bytes.Buffer
p := make([]byte, 1024)
box = &Box{
Root: tmpDir,
Port: port,
cmd: cmd,
stopped: make(chan bool),
}
WAIT_LOOP:
for {
if strings.Contains(boxStderrBuffer.String(), "is already in use, will retry binding after") {
cmd.Process.Kill()
cmd.Process.Wait()
break WAIT_LOOP
}
if strings.Contains(boxStderrBuffer.String(), "entering event loop") {
break START_LOOP
}
n, err := boxStderr.Read(p)
if n > 0 {
boxStderrBuffer.Write(p[:n])
}
if err != nil {
fmt.Println(boxStderrBuffer.String())
return nil, err
}
}
os.RemoveAll(box.Root)
box = nil
}
if box == nil {
return nil, fmt.Errorf("couldn't bind any port from %d to %d", opts.PortMin, opts.PortMax)
}
return box, nil
}
// Listen is the primary addr of the box.
func (box *Box) Listen() string {
return fmt.Sprintf("127.0.0.1:%v", box.Port)
}
// ListenMemcache is the memcache addr of the box.
func (box *Box) ListenMemcache() string {
return fmt.Sprintf("127.0.0.1:%v", box.Port+1)
}
// ListenAdmin is the admin addr of the box.
func (box *Box) ListenAdmin() string {
return fmt.Sprintf("127.0.0.1:%v", box.Port+2)
}
// ListenReplica is the replication addr of the box.
func (box *Box) ListenReplica() string {
return fmt.Sprintf("127.0.0.1:%v", box.Port+3)
}
// SaveSnapshot and return it's filename (with full path).
func (box *Box) SaveSnapshot() (string, error) {
const (
cmdSnapshot = "save snapshot\n"
cmdOK = "ok"
cmdFileExists = "fail: can't save snapshot, errno 17 (File exists)"
cmdSeparators = "\n-. "
)
conn, err := net.Dial("tcp", box.ListenAdmin())
if err != nil {
return "", err
}
defer conn.Close()
if err = conn.SetDeadline(time.Now().Add(5 * time.Second)); err != nil {
return "", err
}
if _, err = conn.Write([]byte(cmdSnapshot)); err != nil {
return "", err
}
respBytes := make([]byte, 256)
n, err := conn.Read(respBytes)
if err != nil {
return "", err
}
response := strings.TrimRight(strings.TrimLeft(string(respBytes[:n]), cmdSeparators), cmdSeparators)
switch response {
case cmdOK, cmdFileExists:
return box.Snapshot()
default:
return "", errors.New(response)
}
}
// SnapDir of the box.
func (box *Box) SnapDir() string {
return filepath.Join(box.Root, dirSnap)
}
// WALDir of the box.
func (box *Box) WALDir() string {
return filepath.Join(box.Root, dirWAL)
}
var ErrSnapshotNotFound = errors.New("snapshot file hasn't been found")
// Snapshot returns the latest snapshot filename with full path.
func (box *Box) Snapshot() (string, error) {
const snapExt = ".snap"
files, err := ioutil.ReadDir(box.SnapDir())
if err != nil {
return "", err
}
// files are sorted alphabetically
for i := len(files) - 1; i >= 0; i-- {
if filepath.Ext(files[i].Name()) != snapExt {
continue
}
return filepath.Join(box.SnapDir(), files[i].Name()), nil
}
return "", ErrSnapshotNotFound
}
// Close Box instance.
func (box *Box) Close() {
box.stopOnce.Do(func() {
box.cmd.Process.Kill()
box.cmd.Process.Wait()
os.RemoveAll(box.Root)
close(box.stopped)
})
<-box.stopped
}