Skip to content

Commit 49e8fa5

Browse files
committed
Return http 100 before sync is complete
1 parent bd08c5c commit 49e8fa5

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

healthcheck/healthcheck.go

+30-11
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
)
66

77
const (
8-
SYNCED_STATE = "4"
8+
JOINING_STATE = "1"
99
DONOR_DESYNCED_STATE = "2"
10+
JOINED_STATE = "3"
11+
SYNCED_STATE = "4"
1012
)
1113

1214
type Healthchecker struct {
@@ -19,35 +21,52 @@ type HealthcheckerConfig struct {
1921
AvailableWhenReadOnly bool
2022
}
2123

24+
type HealthResult struct {
25+
Healthy bool
26+
}
27+
2228
func New(db *sql.DB, config HealthcheckerConfig) *Healthchecker {
2329
return &Healthchecker{
2430
db: db,
2531
config: config,
2632
}
2733
}
2834

29-
func (h *Healthchecker) Check() (bool, string) {
35+
var was_joined = false
36+
var old_state = "0"
37+
38+
func (h *Healthchecker) Check() (*HealthResult, string) {
3039
var variable_name string
31-
var value string
32-
err := h.db.QueryRow("SHOW STATUS LIKE 'wsrep_local_state'").Scan(&variable_name, &value)
40+
var state string
41+
err := h.db.QueryRow("SHOW STATUS LIKE 'wsrep_local_state'").Scan(&variable_name, &state)
3342

43+
var res, msg = &HealthResult{Healthy: false}, "not synced"
3444
switch {
3545
case err != nil:
36-
return false, err.Error()
37-
case value == SYNCED_STATE || (value == DONOR_DESYNCED_STATE && h.config.AvailableWhenDonor):
46+
res, msg = &HealthResult{Healthy: false}, err.Error()
47+
case state != SYNCED_STATE && !was_joined:
48+
if old_state == JOINED_STATE && state != JOINED_STATE {
49+
res, msg = &HealthResult{Healthy: false}, "no synced"
50+
was_joined = true
51+
} else {
52+
res, msg = nil, "syncing"
53+
}
54+
case state == SYNCED_STATE || (state == DONOR_DESYNCED_STATE && h.config.AvailableWhenDonor):
55+
was_joined = true
56+
res, msg = &HealthResult{Healthy: true}, "synced"
3857
if !h.config.AvailableWhenReadOnly {
3958
var ro_variable_name string
4059
var ro_value string
4160
ro_err := h.db.QueryRow("SHOW GLOBAL VARIABLES LIKE 'read_only'").Scan(&ro_variable_name, &ro_value)
4261
switch {
4362
case ro_err != nil:
44-
return false, ro_err.Error()
63+
res, msg = &HealthResult{Healthy: false}, ro_err.Error()
4564
case ro_value == "ON":
46-
return false, "read-only"
65+
res, msg = &HealthResult{Healthy: false}, "read-only"
4766
}
4867
}
49-
return true, "synced"
50-
default:
51-
return false, "not synced"
5268
}
69+
70+
old_state = state
71+
return res, msg
5372
}

server.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"os"
1010
"strconv"
1111

12-
"github.com/cloudfoundry-incubator/galera-healthcheck/healthcheck"
13-
. "github.com/cloudfoundry-incubator/galera-healthcheck/logger"
12+
"github.com/sttts/galera-healthcheck/healthcheck"
13+
. "github.com/sttts/galera-healthcheck/logger"
1414
_ "github.com/go-sql-driver/mysql"
1515
)
1616

@@ -60,10 +60,12 @@ var healthchecker *healthcheck.Healthchecker
6060

6161
func handler(w http.ResponseWriter, r *http.Request) {
6262
result, msg := healthchecker.Check()
63-
if result {
63+
if result != nil && result.Healthy {
6464
w.WriteHeader(http.StatusOK)
65-
} else {
65+
} else if result != nil && !result.Healthy {
6666
w.WriteHeader(http.StatusServiceUnavailable)
67+
} else {
68+
w.WriteHeader(http.StatusContinue)
6769
}
6870

6971
fmt.Fprintf(w, "Galera Cluster Node status: %s", msg)

0 commit comments

Comments
 (0)