Skip to content

Commit 345bf85

Browse files
committed
fix: run sync instance (#152)
1 parent de0b0a2 commit 345bf85

File tree

6 files changed

+133
-52
lines changed

6 files changed

+133
-52
lines changed

pkg/retrieval/engine/postgres/logical/dump.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func (d *DumpJob) Run(ctx context.Context) (err error) {
220220
return errors.Wrap(err, "failed to scan pulling image response")
221221
}
222222

223-
hostConfig, err := d.buildHostConfig()
223+
hostConfig, err := d.buildHostConfig(ctx)
224224
if err != nil {
225225
return errors.Wrap(err, "failed to build container host config")
226226
}
@@ -361,13 +361,13 @@ func (d *DumpJob) buildContainerConfig(password string) *container.Config {
361361
}
362362
}
363363

364-
func (d *DumpJob) buildHostConfig() (*container.HostConfig, error) {
364+
func (d *DumpJob) buildHostConfig(ctx context.Context) (*container.HostConfig, error) {
365365
hostConfig := &container.HostConfig{
366366
Mounts: d.getMountVolumes(),
367367
NetworkMode: d.getContainerNetworkMode(),
368368
}
369369

370-
if err := tools.AddVolumesToHostConfig(hostConfig, d.globalCfg.DataDir); err != nil {
370+
if err := tools.AddVolumesToHostConfig(ctx, d.dockerClient, hostConfig, d.globalCfg.DataDir); err != nil {
371371
return nil, err
372372
}
373373

pkg/retrieval/engine/postgres/logical/restore.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (r *RestoreJob) Run(ctx context.Context) (err error) {
121121
return errors.Wrap(err, "failed to scan image pulling response")
122122
}
123123

124-
hostConfig, err := r.buildHostConfig()
124+
hostConfig, err := r.buildHostConfig(ctx)
125125
if err != nil {
126126
return errors.Wrap(err, "failed to build container host config")
127127
}
@@ -213,7 +213,7 @@ func (r *RestoreJob) buildContainerConfig(password string) *container.Config {
213213
}
214214
}
215215

216-
func (r *RestoreJob) buildHostConfig() (*container.HostConfig, error) {
216+
func (r *RestoreJob) buildHostConfig(ctx context.Context) (*container.HostConfig, error) {
217217
hostConfig := &container.HostConfig{
218218
Mounts: []mount.Mount{
219219
{
@@ -224,7 +224,7 @@ func (r *RestoreJob) buildHostConfig() (*container.HostConfig, error) {
224224
},
225225
}
226226

227-
if err := tools.AddVolumesToHostConfig(hostConfig, r.globalCfg.DataDir); err != nil {
227+
if err := tools.AddVolumesToHostConfig(ctx, r.dockerClient, hostConfig, r.globalCfg.DataDir); err != nil {
228228
return nil, err
229229
}
230230

pkg/retrieval/engine/postgres/physical/physical.go

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func (r *RestoreJob) Run(ctx context.Context) (err error) {
130130
log.Msg(fmt.Sprintf("Run job: %s. Options: %v", r.Name(), r.CopyOptions))
131131

132132
defer func() {
133-
if err != nil && r.CopyOptions.SyncInstance {
133+
if err == nil && r.CopyOptions.SyncInstance {
134134
if syncErr := r.runSyncInstance(ctx); syncErr != nil {
135135
log.Err("Failed to run sync instance", syncErr)
136136
}
@@ -148,10 +148,6 @@ func (r *RestoreJob) Run(ctx context.Context) (err error) {
148148
return nil
149149
}
150150

151-
if err := tools.PullImage(ctx, r.dockerClient, r.CopyOptions.DockerImage); err != nil {
152-
return errors.Wrap(err, "failed to scan image pulling response")
153-
}
154-
155151
contID, err := r.startContainer(ctx, r.restoreContainerName())
156152
if err != nil {
157153
return errors.Wrapf(err, "failed to create container: %s", r.restoreContainerName())
@@ -216,7 +212,7 @@ func (r *RestoreJob) Run(ctx context.Context) (err error) {
216212
}
217213

218214
// Start PostgreSQL instance.
219-
startCommand, err := r.dockerClient.ContainerExecCreate(ctx, contID, startingPostgresConfig(r.globalCfg.DataDir))
215+
startCommand, err := r.dockerClient.ContainerExecCreate(ctx, contID, startingPostgresConfig(r.globalCfg.DataDir, pgVersion))
220216

221217
if err != nil {
222218
return errors.Wrap(err, "failed to create an exec command")
@@ -241,7 +237,7 @@ func (r *RestoreJob) Run(ctx context.Context) (err error) {
241237
}
242238

243239
func (r *RestoreJob) startContainer(ctx context.Context, containerName string) (string, error) {
244-
hostConfig, err := r.buildHostConfig()
240+
hostConfig, err := r.buildHostConfig(ctx)
245241
if err != nil {
246242
return "", errors.Wrap(err, "failed to build container host config")
247243
}
@@ -251,6 +247,10 @@ func (r *RestoreJob) startContainer(ctx context.Context, containerName string) (
251247
return "", errors.Wrap(err, "failed to generate PostgreSQL password")
252248
}
253249

250+
if err := tools.PullImage(ctx, r.dockerClient, r.CopyOptions.DockerImage); err != nil {
251+
return "", errors.Wrap(err, "failed to scan image pulling response")
252+
}
253+
254254
syncInstance, err := r.dockerClient.ContainerCreate(ctx,
255255
r.buildContainerConfig(pwd),
256256
hostConfig,
@@ -269,12 +269,15 @@ func (r *RestoreJob) startContainer(ctx context.Context, containerName string) (
269269
return syncInstance.ID, nil
270270
}
271271

272-
func startingPostgresConfig(pgDataDir string) types.ExecConfig {
272+
func startingPostgresConfig(pgDataDir, pgVersion string) types.ExecConfig {
273+
command := fmt.Sprintf("/usr/lib/postgresql/%s/bin/postgres", pgVersion)
274+
273275
return types.ExecConfig{
274276
AttachStdout: true,
275277
AttachStderr: true,
276-
Cmd: []string{"postgres", "-D", pgDataDir},
278+
Cmd: []string{command, "-D", pgDataDir},
277279
User: defaults.Username,
280+
Env: os.Environ(),
278281
}
279282
}
280283

@@ -284,11 +287,17 @@ func isDatabaseReady(input io.Reader) error {
284287
timer := time.NewTimer(time.Minute)
285288
defer timer.Stop()
286289

287-
for scanner.Scan() {
290+
LOOP:
291+
for {
288292
select {
289293
case <-timer.C:
290294
return errors.New("timeout exceeded")
291295
default:
296+
if !scanner.Scan() {
297+
break LOOP
298+
}
299+
300+
timer.Reset(time.Minute)
292301
}
293302

294303
text := scanner.Text()
@@ -335,16 +344,29 @@ func (r *RestoreJob) runSyncInstance(ctx context.Context) error {
335344
return err
336345
}
337346

338-
startSyncCommand, err := r.dockerClient.ContainerExecCreate(ctx, syncInstanceID, startingPostgresConfig(r.globalCfg.DataDir))
347+
// Set permissions.
348+
if err := tools.ExecCommand(ctx, r.dockerClient, syncInstanceID, types.ExecConfig{
349+
Cmd: []string{"chown", "-R", "postgres", r.globalCfg.DataDir},
350+
}); err != nil {
351+
return errors.Wrap(err, "failed to set permissions")
352+
}
353+
354+
pgVersion, err := tools.DetectPGVersion(r.globalCfg.DataDir)
355+
if err != nil {
356+
return err
357+
}
358+
359+
startSyncCommand, err := r.dockerClient.ContainerExecCreate(ctx, syncInstanceID, startingPostgresConfig(r.globalCfg.DataDir, pgVersion))
339360
if err != nil {
340361
return errors.Wrap(err, "failed to create exec command")
341362
}
342363

343-
if err = r.dockerClient.ContainerExecStart(ctx, startSyncCommand.ID, types.ExecStartCheck{Tty: true}); err != nil {
364+
if err = r.dockerClient.ContainerExecStart(ctx, startSyncCommand.ID, types.ExecStartCheck{
365+
Detach: true, Tty: true}); err != nil {
344366
return errors.Wrap(err, "failed to attach to exec command")
345367
}
346368

347-
if err := tools.InspectCommandResponse(ctx, r.dockerClient, startSyncCommand.ID, startSyncCommand.ID); err != nil {
369+
if err := tools.InspectCommandResponse(ctx, r.dockerClient, syncInstanceID, startSyncCommand.ID); err != nil {
348370
return errors.Wrap(err, "failed to perform exec command")
349371
}
350372

@@ -373,16 +395,16 @@ func (r *RestoreJob) buildContainerConfig(password string) *container.Config {
373395
return &container.Config{
374396
Labels: map[string]string{"label": tools.DBLabControlLabel},
375397
Env: r.getEnvironmentVariables(password),
376-
Image: r.DockerImage,
398+
Image: r.CopyOptions.DockerImage,
377399
}
378400
}
379401

380-
func (r *RestoreJob) buildHostConfig() (*container.HostConfig, error) {
402+
func (r *RestoreJob) buildHostConfig(ctx context.Context) (*container.HostConfig, error) {
381403
hostConfig := &container.HostConfig{
382404
Mounts: r.restorer.GetMounts(),
383405
}
384406

385-
if err := tools.AddVolumesToHostConfig(hostConfig, r.globalCfg.DataDir); err != nil {
407+
if err := tools.AddVolumesToHostConfig(ctx, r.dockerClient, hostConfig, r.globalCfg.DataDir); err != nil {
386408
return nil, err
387409
}
388410

pkg/retrieval/engine/postgres/snapshot/physical.go

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ const (
4747
promoteContainerPrefix = "dblab_promote_"
4848

4949
// Defines container health check options.
50-
hcPromoteInterval = 2 * time.Second
51-
hcPromoteRetries = 100
50+
hcPromoteInterval = 5 * time.Second
51+
hcPromoteRetries = 200
5252

5353
syncContainerStopTimeout = 2 * time.Minute
5454
)
@@ -142,7 +142,10 @@ func (p *PhysicalInitial) Name() string {
142142

143143
// Run starts the job.
144144
func (p *PhysicalInitial) Run(ctx context.Context) (err error) {
145-
p.scheduleOnce.Do(p.startScheduler(ctx))
145+
// Start scheduling after initial snapshot.
146+
defer func() {
147+
p.scheduleOnce.Do(p.startScheduler(ctx))
148+
}()
146149

147150
select {
148151
case <-ctx.Done():
@@ -181,6 +184,10 @@ func (p *PhysicalInitial) Run(ctx context.Context) (err error) {
181184
if err := p.dockerClient.ContainerStart(ctx, syncContainer.ID, types.ContainerStartOptions{}); err != nil {
182185
log.Err(fmt.Sprintf("failed to start %q: %v", p.syncInstanceName(), err))
183186
}
187+
188+
if err := tools.RunPostgres(ctx, p.dockerClient, syncContainer.ID, p.globalCfg.DataDir); err != nil {
189+
log.Err(fmt.Sprintf("failed to start PostgreSQL instance inside %q: %v", p.syncInstanceName(), err))
190+
}
184191
}()
185192
}
186193

@@ -307,7 +314,7 @@ func (p *PhysicalInitial) promoteInstance(ctx context.Context, clonePath string)
307314
return err
308315
}
309316

310-
pgVersion, err := tools.DetectPGVersion(p.globalCfg.DataDir)
317+
pgVersion, err := tools.DetectPGVersion(clonePath)
311318
if err != nil {
312319
return errors.Wrap(err, "failed to detect the Postgres version")
313320
}
@@ -317,7 +324,7 @@ func (p *PhysicalInitial) promoteInstance(ctx context.Context, clonePath string)
317324
return errors.Wrap(err, "failed to adjust recovery configuration")
318325
}
319326

320-
hostConfig, err := p.buildHostConfig(clonePath)
327+
hostConfig, err := p.buildHostConfig(ctx, clonePath)
321328
if err != nil {
322329
return errors.Wrap(err, "failed to build container host config")
323330
}
@@ -359,27 +366,9 @@ func (p *PhysicalInitial) promoteInstance(ctx context.Context, clonePath string)
359366

360367
log.Msg(fmt.Sprintf("Running container: %s. ID: %v", p.promoteContainerName(), cont.ID))
361368

362-
// Set permissions.
363-
if err := tools.ExecCommand(ctx, p.dockerClient, cont.ID, types.ExecConfig{
364-
Cmd: []string{"chown", "-R", "postgres", clonePath},
365-
}); err != nil {
366-
return errors.Wrap(err, "failed to set permissions")
367-
}
368-
369369
// Start PostgreSQL instance.
370-
startCommand, err := p.dockerClient.ContainerExecCreate(ctx, cont.ID, types.ExecConfig{
371-
Cmd: []string{"postgres", "-D", clonePath},
372-
User: defaults.Username,
373-
})
374-
375-
if err != nil {
376-
return errors.Wrap(err, "failed to create an exec command")
377-
}
378-
379-
log.Msg("Running PostgreSQL instance")
380-
381-
if _, err := p.dockerClient.ContainerExecAttach(ctx, startCommand.ID, types.ExecStartCheck{Tty: true}); err != nil {
382-
return errors.Wrap(err, "failed to attach to the exec command")
370+
if err := tools.RunPostgres(ctx, p.dockerClient, cont.ID, clonePath); err != nil {
371+
return errors.Wrap(err, "failed to start PostgreSQL instance")
383372
}
384373

385374
if err := tools.CheckContainerReadiness(ctx, p.dockerClient, cont.ID); err != nil {
@@ -484,10 +473,10 @@ func (p *PhysicalInitial) buildContainerConfig(clonePath, promoteImage, password
484473
}
485474
}
486475

487-
func (p *PhysicalInitial) buildHostConfig(clonePath string) (*container.HostConfig, error) {
476+
func (p *PhysicalInitial) buildHostConfig(ctx context.Context, clonePath string) (*container.HostConfig, error) {
488477
hostConfig := &container.HostConfig{}
489478

490-
if err := tools.AddVolumesToHostConfig(hostConfig, clonePath); err != nil {
479+
if err := tools.AddVolumesToHostConfig(ctx, p.dockerClient, hostConfig, clonePath); err != nil {
491480
return nil, err
492481
}
493482

pkg/retrieval/engine/postgres/snapshot/snapshot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func applyUsersConfigs(usersConfig map[string]string, filename string) error {
5353
sb.WriteString("\n")
5454

5555
for configKey, configValue := range usersConfig {
56-
sb.WriteString(fmt.Sprintf("%s = %s\n", configKey, configValue))
56+
sb.WriteString(fmt.Sprintf("%s = '%s'\n", configKey, configValue))
5757
}
5858

5959
_, err = configFile.WriteString(sb.String())

0 commit comments

Comments
 (0)