-
Notifications
You must be signed in to change notification settings - Fork 447
/
pitr_test.go
229 lines (198 loc) · 7.31 KB
/
pitr_test.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
// Copyright 2016 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package integration
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
"github.com/sorintlab/stolon/internal/cluster"
"github.com/sorintlab/stolon/internal/common"
"github.com/sorintlab/stolon/internal/store"
"github.com/gofrs/uuid"
)
func TestPITR(t *testing.T) {
t.Parallel()
testPITR(t, false)
}
func TestPITRRecoveryTarget(t *testing.T) {
t.Parallel()
testPITR(t, true)
}
func testPITR(t *testing.T, recoveryTarget bool) {
dir, err := ioutil.TempDir("", "stolon")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)
baseBackupDir, err := ioutil.TempDir(dir, "basebackup")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
archiveBackupDir, err := ioutil.TempDir(dir, "archivebackup")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
tstore := setupStore(t, dir)
defer tstore.Stop()
storeEndpoints := fmt.Sprintf("%s:%s", tstore.listenAddress, tstore.port)
clusterName := uuid.Must(uuid.NewV4()).String()
storePath := filepath.Join(common.StorePrefix, clusterName)
sm := store.NewKVBackedStore(tstore.store, storePath)
initialClusterSpec := &cluster.ClusterSpec{
InitMode: cluster.ClusterInitModeP(cluster.ClusterInitModeNew),
SleepInterval: &cluster.Duration{Duration: 2 * time.Second},
FailInterval: &cluster.Duration{Duration: 5 * time.Second},
ConvergenceTimeout: &cluster.Duration{Duration: 30 * time.Second},
PGParameters: pgParametersWithDefaults(cluster.PGParameters{
"archive_mode": "on",
"archive_command": fmt.Sprintf("cp %%p %s/%%f", archiveBackupDir),
}),
}
initialClusterSpecFile, err := writeClusterSpec(dir, initialClusterSpec)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
tk, err := NewTestKeeper(t, dir, clusterName, pgSUUsername, pgSUPassword, pgReplUsername, pgReplPassword, tstore.storeBackend, storeEndpoints)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if err := tk.Start(); err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer tk.Stop()
ts, err := NewTestSentinel(t, dir, clusterName, tstore.storeBackend, storeEndpoints, fmt.Sprintf("--initial-cluster-spec=%s", initialClusterSpecFile))
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if err := ts.Start(); err != nil {
t.Fatalf("unexpected err: %v", err)
}
// Wait for clusterView containing a master
_, err = WaitClusterDataWithMaster(sm, 30*time.Second)
if err != nil {
t.Fatal("expected a master in cluster view")
}
if err := tk.WaitDBUp(60 * time.Second); err != nil {
t.Fatalf("unexpected err: %v", err)
}
if err := tk.WaitDBRole(common.RoleMaster, nil, 30*time.Second); err != nil {
t.Fatalf("unexpected err: %v", err)
}
if err := populate(t, tk); err != nil {
t.Fatalf("unexpected err: %v", err)
}
if err := write(t, tk, 2, 2); err != nil {
t.Fatalf("unexpected err: %v", err)
}
// save current time used to define recovery_target_time
now := time.Now()
// ioutil.Tempfile already creates files with 0600 permissions
pgpass, err := ioutil.TempFile("", "pgpass")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if _, err := pgpass.WriteString(fmt.Sprintf("%s:%s:*:%s:%s\n", tk.pgListenAddress, tk.pgPort, tk.pgReplUsername, tk.pgReplPassword)); err != nil {
t.Fatalf("unexpected err: %v", err)
}
// Don't save the wal during the basebackup (-x). This to test that archive_command and restore command correctly work.
cmd := exec.Command("pg_basebackup", "-F", "tar", "-D", baseBackupDir, "-h", tk.pgListenAddress, "-p", tk.pgPort, "-U", tk.pgReplUsername)
cmd.Env = append(cmd.Env, fmt.Sprintf("PGPASSFILE=%s", pgpass.Name()))
t.Logf("execing cmd: %v", cmd)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("error: %v, output: %s", err, string(out))
}
// Switch wal so they will be archived
if err := tk.SwitchWals(1); err != nil {
t.Fatalf("unexpected err: %v", err)
}
ts.Stop()
// Delete the current cluster data
if err := tstore.store.Delete(context.TODO(), filepath.Join(storePath, "clusterdata")); err != nil {
t.Fatalf("unexpected err: %v", err)
}
// Delete sentinel leader key to just speedup new election
if err := tstore.store.Delete(context.TODO(), filepath.Join(storePath, common.SentinelLeaderKey)); err != nil && err != store.ErrKeyNotFound {
t.Fatalf("unexpected err: %v", err)
}
// Now initialize a new cluster with the existing keeper
initialClusterSpec = &cluster.ClusterSpec{
InitMode: cluster.ClusterInitModeP(cluster.ClusterInitModePITR),
SleepInterval: &cluster.Duration{Duration: 2 * time.Second},
FailInterval: &cluster.Duration{Duration: 5 * time.Second},
ConvergenceTimeout: &cluster.Duration{Duration: 30 * time.Second},
PITRConfig: &cluster.PITRConfig{
DataRestoreCommand: fmt.Sprintf("tar xvf %s/base.tar -C %%d", baseBackupDir),
ArchiveRecoverySettings: &cluster.ArchiveRecoverySettings{
RestoreCommand: fmt.Sprintf("cp %s/%%f %%p", archiveBackupDir),
},
},
PGParameters: pgParametersWithDefaults(cluster.PGParameters{
"max_prepared_transactions": "100",
}),
}
if recoveryTarget {
initialClusterSpec.PITRConfig.RecoveryTargetSettings = &cluster.RecoveryTargetSettings{
// Looks like Postgres12 cannot parse locations in UTC TZ like "2019-11-19T12:10:54Z" that previous versions were able to parse
// workaround this by not writing the timezone
RecoveryTargetTime: now.Format("2006-01-02T15:04:05"),
}
}
initialClusterSpecFile, err = writeClusterSpec(dir, initialClusterSpec)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
ts, err = NewTestSentinel(t, dir, clusterName, tstore.storeBackend, storeEndpoints, fmt.Sprintf("--initial-cluster-spec=%s", initialClusterSpecFile))
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if err := ts.Start(); err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer ts.Stop()
if err := WaitClusterPhase(sm, cluster.ClusterPhaseNormal, 60*time.Second); err != nil {
t.Fatalf("unexpected err: %v", err)
}
_, err = WaitClusterDataWithMaster(sm, 30*time.Second)
if err != nil {
t.Fatal("expected a master in cluster view")
}
if err := tk.WaitDBUp(60 * time.Second); err != nil {
t.Fatalf("unexpected err: %v", err)
}
if err := tk.WaitDBRole(common.RoleMaster, nil, 30*time.Second); err != nil {
t.Fatalf("unexpected err: %v", err)
}
if err := tk.WaitDBUp(60 * time.Second); err != nil {
t.Fatalf("unexpected err: %v", err)
}
c, err := getLines(t, tk)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if c != 1 {
t.Fatalf("wrong number of lines, want: %d, got: %d", 2, c)
}
pgParameters, err := tk.GetPGParameters()
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if v := pgParameters["max_prepared_transactions"]; v != "100" {
t.Fatalf("expected max_prepared_transactions == 100 got %q", v)
}
}