-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostgres.go
230 lines (180 loc) · 4.79 KB
/
postgres.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
package pgtest
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/theckman/go-flock"
"io/fs"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"sync"
"sync/atomic"
"syscall"
"time"
)
type Process struct {
port int
data string
lock *flock.Flock
cmd *exec.Cmd
children sync.WaitGroup
}
type Config struct {
Binary string
Snapshot string
Workdir string
}
func Start(config Config) (*Process, error) {
tempdir := os.TempDir()
pgdata, err := prepareSnapshot(config)
if err != nil {
return nil, errors.WithMessage(err, "prepare snapshot")
}
port, lock, err := lockInstancePort(tempdir)
if err != nil {
return nil, errors.WithMessage(err, "get instance port")
}
instance := &Process{
data: pgdata,
port: port,
lock: lock,
cmd: exec.Command(config.Binary,
"-F",
"-D", pgdata+"/pgdata",
"-p", strconv.Itoa(port),
"-c", "listen_addresses=",
"-c", "autovacuum=off",
"-c", "unix_socket_directories="+pgdata),
}
fmt.Println(instance.cmd.Args)
instance.cmd.Stderr = logWriter("postgres")
modifyProcessOnSystem(instance.cmd)
debugf("Starting new postgres instance on port %d", port)
if err := instance.cmd.Start(); err != nil {
_ = instance.Close()
return nil, errors.WithMessage(err, "starting postgres process")
}
return instance, nil
}
func prepareSnapshot(config Config) (string, error) {
lockfile := filepath.Join(config.Workdir, "snapshots.lock")
lock := flock.New(lockfile)
if err := lock.Lock(); err != nil {
return "", errors.WithMessage(err, "getting lockfile")
}
defer lock.Unlock()
// check for files
f := os.DirFS(config.Workdir)
entries, err := fs.ReadDir(f, ".")
if err != nil {
return "", errors.WithMessage(err, "read directory")
}
var maxIndex int
pName := regexp.MustCompile("pgtest-([0-9]+)")
for _, entry := range entries {
if !entry.IsDir() {
continue
}
match := pName.FindStringSubmatch(entry.Name())
if match == nil {
continue
}
idx, _ := strconv.Atoi(match[1])
maxIndex = max(maxIndex, idx)
info, err := entry.Info()
if err != nil {
continue
}
if time.Since(info.ModTime()) > 10*time.Minute {
// found an old snapshot to cleanup
_ = os.RemoveAll(filepath.Join(config.Workdir, entry.Name()))
}
}
pgdata := filepath.Join(config.Workdir, fmt.Sprintf("pgtest-%d", maxIndex+1))
debugf("Setup pgdata at %s", pgdata)
if err := exec.Command("cp", "-r", config.Snapshot, pgdata).Run(); err != nil {
return "", errors.WithMessagef(err, "copy snapshot to %q", pgdata)
}
return pgdata, nil
}
func (proc *Process) Close() error {
debugf("Waiting for all children of postgres to close (port %d)", proc.port)
proc.children.Wait()
debugf("Stopping postgres instance on port %d", proc.port)
if proc.cmd.Process != nil {
pgid, err := syscall.Getpgid(proc.cmd.Process.Pid)
if err == nil {
_ = syscall.Kill(-pgid, syscall.SIGKILL)
}
_ = proc.cmd.Wait()
}
// the process should now be stopped. we can free the lock
// and let another instance run on this port.
if err := proc.lock.Unlock(); err != nil {
log("could not release postgres instance lock: ", err)
}
err := os.RemoveAll(proc.data)
return errors.WithMessage(err, "cleanup of pgdata")
}
func (proc *Process) dns(dbname string) string {
return fmt.Sprintf(
"user=postgres host='%s' port=%d dbname='%s' sslmode=disable",
proc.data, proc.port, dbname,
)
}
func lockInstancePort(tempdir string) (int, *flock.Flock, error) {
for port := 20000; port < 21000; port++ {
lock := flock.New(filepath.Join(tempdir, fmt.Sprintf("pgtest-%d.lock", port)))
locked, err := lock.TryLock()
if err != nil {
return 0, nil, errors.WithMessage(err, "getting postgres lock")
}
if locked {
return port, lock, nil
}
}
return 0, nil, errors.New("no free port found for postgres")
}
var instance atomic.Int32
type Instance struct {
URL string
dbname string
proc *Process
}
func (proc *Process) Child(ctx context.Context) (*Instance, error) {
pool, err := connect(ctx, proc.dns("postgres"))
if err != nil {
return nil, errors.WithMessage(err, "connect to master instance")
}
defer pool.Close()
dbname := fmt.Sprintf("db%d", instance.Add(1))
if _, err := pool.ExecContext(ctx, "CREATE DATABASE "+dbname); err != nil {
return nil, errors.WithMessage(err, "create child database")
}
inst := Instance{
URL: proc.dns(dbname),
proc: proc,
dbname: dbname,
}
// register us as a new child
proc.children.Add(1)
return &inst, nil
}
func (inst *Instance) Close() error {
cleanup := func() {
db, err := connect(context.Background(), inst.URL)
if err != nil {
return
}
defer db.Close()
// cleanup in background to save some space
_, _ = db.Exec("DROP DATABASE " + inst.dbname)
}
go cleanup()
// tell the parent that we're done here
inst.proc.children.Done()
return nil
}