Skip to content

Commit

Permalink
[WIP] perform couchdb instance health checks to indicate maintenance
Browse files Browse the repository at this point in the history
Needs every public address for all nodes, which might be different from the internally known cluster node addresses.

Relates to #40
  • Loading branch information
gesellix committed Aug 5, 2019
1 parent 57e91a1 commit dd6e354
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/couchdb-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,39 @@ type MembershipResponse struct {
ClusterNodes []string `json:"cluster_nodes"`
}

func (c *CouchdbClient) getHealthByNodeName(urisByNodeName map[string]string) (map[string]HealthResponse, error) {
healthByNodeName := make(map[string]HealthResponse)
for name, uri := range urisByNodeName {
var health HealthResponse

// Instead of http://localhost:15984/_node/[email protected]/_up
// we have to request http://localhost:15984/_up,
// but how do we find the correct base uris, say: ip and port, for each node?
data, err := c.Request("GET", fmt.Sprintf("%s/_up", uri), nil)
if err != nil {
err = fmt.Errorf("error reading couchdb health: %v", err)
//if !strings.Contains(err.Error(), "\"error\":\"nodedown\"") {
// return nil, err
//}
klog.Error(fmt.Errorf("continuing despite error: %v", err))
continue
}

err = json.Unmarshal(data, &health)
if err != nil {
return nil, fmt.Errorf("error unmarshalling health: %v", err)
}

healthByNodeName[name] = health
}

if len(urisByNodeName) == 0 {
return nil, fmt.Errorf("all nodes down")
}

return healthByNodeName, nil
}

func (c *CouchdbClient) getNodeInfo(uri string) (NodeInfo, error) {
data, err := c.Request("GET", fmt.Sprintf("%s/", uri), nil)
if err != nil {
Expand Down Expand Up @@ -179,6 +212,10 @@ func (c *CouchdbClient) getStats(config CollectorConfig) (Stats, error) {
if err != nil {
return Stats{}, err
}
//nodeHealth, err := c.getHealthByNodeName(urisByNode)
//if err != nil {
// return Stats{}, err
//}
nodeStats, err := c.getStatsByNodeName(urisByNode)
if err != nil {
return Stats{}, err
Expand Down Expand Up @@ -211,6 +248,7 @@ func (c *CouchdbClient) getStats(config CollectorConfig) (Stats, error) {
}

return Stats{
//HealthByNodeName: nodeHealth,
StatsByNodeName: nodeStats,
DatabasesTotal: len(databasesList),
DatabaseStatsByDbName: databaseStats,
Expand Down
7 changes: 7 additions & 0 deletions lib/couchdb-stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ type CouchReplicator struct {
Connection map[string]Counter `json:"connection"`
}

type HealthResponse struct {
Status string `json:"status"`
// see https://github.com/apache/couchdb/pull/1658
//Seeds map[string]SeedStat `json:"seeds"`
}

type StatsResponse struct {
Couchdb CouchdbStats `json:"couchdb"`
Up float64 `json:"-"`
Expand Down Expand Up @@ -229,6 +235,7 @@ type SystemResponse struct {
}

type Stats struct {
//HealthByNodeName map[string]HealthResponse
StatsByNodeName map[string]StatsResponse
DatabasesTotal int
DatabaseStatsByDbName DatabaseStatsByDbName
Expand Down

0 comments on commit dd6e354

Please sign in to comment.