-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathpostgres.go
195 lines (148 loc) · 5.02 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
/*
2019-2020 © Postgres.ai
*/
// Package postgres provides an interface to work Postgres application.
package postgres
import (
"database/sql"
"fmt"
"strconv"
"strings"
"time"
_ "github.com/lib/pq" // Register Postgres database driver.
"github.com/pkg/errors"
"gitlab.com/postgres-ai/database-lab/v3/internal/provision/databases/postgres/pgconfig"
"gitlab.com/postgres-ai/database-lab/v3/internal/provision/docker"
"gitlab.com/postgres-ai/database-lab/v3/internal/provision/resources"
"gitlab.com/postgres-ai/database-lab/v3/internal/provision/runners"
"gitlab.com/postgres-ai/database-lab/v3/pkg/log"
)
const (
// waitPostgresConnectionTimeout defines timeout to wait for Postgres initial connection.
waitPostgresConnectionTimeout = 120
// waitPostgresStartTimeout defines timeout to wait for Postgres start.
waitPostgresStartTimeout = 360
// checkPostgresStatusPeriod defines period to check Postgres status.
checkPostgresStatusPeriod = 500
// logsMinuteWindow defines number of minutes to get logs from container.
logsMinuteWindow = 1
)
// Start starts Postgres instance.
func Start(r runners.Runner, c *resources.AppConfig) error {
log.Dbg("Starting Postgres container...")
if extraConf := c.ExtraConf(); len(extraConf) > 0 {
configManager, err := pgconfig.NewCorrector(c.DataDir())
if err != nil {
return errors.Wrap(err, "failed to create a config manager")
}
if err := configManager.ApplyUserConfig(extraConf); err != nil {
return errors.Wrap(err, "cannot apply user configs")
}
}
if err := docker.RunContainer(r, c); err != nil {
return errors.Wrap(err, "failed to run container")
}
// Waiting for server to become ready and promote if needed.
first := true
cnt := 0
waitPostgresTimeout := waitPostgresConnectionTimeout
for {
logs, err := docker.GetLogs(r, c, logsMinuteWindow)
if err != nil {
return errors.Wrap(err, "failed to read container logs")
}
fatalCount := strings.Count(logs, "FATAL")
startingUpCount := strings.Count(logs, "FATAL: the database system is starting up")
if fatalCount > 0 && startingUpCount != fatalCount {
return errors.Wrap(fmt.Errorf("postgres fatal error"), "cannot start Postgres")
}
out, err := runSimpleSQL("select pg_is_in_recovery()", getPgConnStr(c.Host, c.DB.DBName, c.DB.Username, c.Port))
if err == nil {
// Server does not need promotion if it is not in recovery.
if out == "f" || out == "false" {
break
}
// Run promotion if needed only first time.
if out == "t" && first {
log.Dbg("Postgres instance needs promotion.")
// Increase Postgres start timeout for promotion.
waitPostgresTimeout = waitPostgresStartTimeout
first = false
_, err = pgctlPromote(r, c)
if err != nil {
if runnerError := Stop(r, c.Pool, c.CloneName); runnerError != nil {
log.Err(runnerError)
}
return err
}
}
} else {
log.Err("Currently cannot connect to Postgres: ", out, err)
}
cnt++
if cnt > waitPostgresTimeout {
if runnerErr := Stop(r, c.Pool, c.CloneName); runnerErr != nil {
log.Err(runnerErr)
}
return errors.Wrap(err, "postgres start timeout")
}
time.Sleep(checkPostgresStatusPeriod * time.Millisecond)
}
return nil
}
// Stop stops Postgres instance.
func Stop(r runners.Runner, p *resources.Pool, name string) error {
log.Dbg("Stopping Postgres container...")
if _, err := docker.RemoveContainer(r, name); err != nil {
const errorPrefix = "Error: No such container:"
if e, ok := err.(runners.RunnerError); ok && !strings.HasPrefix(e.Stderr, errorPrefix) {
return errors.Wrap(err, "failed to remove container")
}
log.Msg("docker container was not found, ignore", err)
}
if _, err := r.Run("rm -rf " + p.SocketCloneDir(name) + "/*"); err != nil {
return errors.Wrap(err, "failed to clean unix socket directory")
}
return nil
}
// List gets running Postgres instances filtered by label.
func List(r runners.Runner, label string) ([]string, error) {
return docker.ListContainers(r, label)
}
func pgctlPromote(r runners.Runner, c *resources.AppConfig) (string, error) {
promoteCmd := `pg_ctl --pgdata ` + c.DataDir() + ` ` +
`-W ` + // No wait.
`promote`
return docker.Exec(r, c, promoteCmd)
}
// Generate postgres connection string.
func getPgConnStr(host, dbname, username string, port uint) string {
var sb strings.Builder
if host != "" {
sb.WriteString("host=" + host + " ")
}
sb.WriteString("port=" + strconv.Itoa(int(port)) + " ")
sb.WriteString("dbname=" + dbname + " ")
sb.WriteString("user=" + username + " ")
return sb.String()
}
// runSimpleSQL executes simple SQL commands which returns one string value.
func runSimpleSQL(command, connStr string) (string, error) {
db, err := sql.Open("postgres", connStr)
if err != nil {
return "", errors.Wrap(err, "cannot connect to database")
}
result := ""
row := db.QueryRow(command)
err = row.Scan(&result)
defer func() {
err := db.Close()
if err != nil {
log.Err("Cannot close database connection.")
}
}()
if err != nil && err == sql.ErrNoRows {
return "", nil
}
return result, err
}