Skip to content

Commit

Permalink
Adding a specific case for cluster initialization.
Browse files Browse the repository at this point in the history
When no services are available, we want to die and let k8s
re-initiialize the pod, *unless* this is the initial pod, in which case
it is valid to allow initialization to coninue without cloning from a
service or bucket.
  • Loading branch information
dougfales authored and AMecea committed Jan 9, 2020
1 parent 56bf9cc commit 317cb48
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
10 changes: 8 additions & 2 deletions pkg/sidecar/appclone.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ import (
// master; bucket URL exists | The assumption is that this is the bootstrap case: the
// | very first mysql pod is being initialized.
// ------------------------------------------------------------------------------------
// No healthy replcias; no | If this is the first pod in the cluster, then allow it
// master; no bucket URL | to initialize as an empty instance, otherwise, return an
// | error to allow k8s to kill and restart the pod.
// ------------------------------------------------------------------------------------
func RunCloneCommand(cfg *Config) error {
log.Info("cloning command", "host", cfg.Hostname)

Expand Down Expand Up @@ -79,9 +83,11 @@ func RunCloneCommand(cfg *Config) error {
if err := cloneFromBucket(cfg.InitBucketURL); err != nil {
return fmt.Errorf("failed to clone from bucket, err: %s", err)
}
} else {
log.Info("nothing to clone from: no existing data found, no replicas and no master available, and no clone bucket url found")
} else if cfg.IsFirstPodInSet() {
log.Info("nothing to clone from: empty cluster initializing")
return nil
} else {
return fmt.Errorf("nothing to clone from: no existing data found, no replicas and no master available, and no clone bucket url found")
}

// prepare backup
Expand Down
13 changes: 12 additions & 1 deletion pkg/sidecar/appclone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ var _ = Describe("Test RunCloneCommand cloning logic", func() {
stopFakeMasterService()
})

It("should return nil", func() {
It("should return nil for first pod", func() {
cfg.Hostname = "mysql-mysql-0"
Expect(fakeBackupFile).ShouldNot(BeAnExistingFile())

err := RunCloneCommand(cfg)
Expand All @@ -264,6 +265,16 @@ var _ = Describe("Test RunCloneCommand cloning logic", func() {
Expect(fakeBackupFile).ShouldNot(BeAnExistingFile())
})

It("should return an error for subsequent pods", func() {
cfg.Hostname = "mysql-mysql-1"
Expect(fakeBackupFile).ShouldNot(BeAnExistingFile())

err := RunCloneCommand(cfg)
Expect(err).To(HaveOccurred())

Expect(fakeBackupFile).ShouldNot(BeAnExistingFile())
})

})

})
5 changes: 5 additions & 0 deletions pkg/sidecar/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ func (cfg *Config) MysqlDSN() string {
)
}

func (cfg *Config) IsFirstPodInSet() bool {
ordinal := getOrdinalFromHostname(cfg.Hostname)
return ordinal == 0
}

// ShouldCloneFromBucket returns true if it's time to initialize from a bucket URL provided
func (cfg *Config) ShouldCloneFromBucket() bool {
return !cfg.ExistsMySQLData && cfg.ServerID() == cfg.MyServerIDOffset && len(cfg.InitBucketURL) != 0
Expand Down

0 comments on commit 317cb48

Please sign in to comment.