Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add health check endpoints #32

Merged
merged 1 commit into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,32 @@ The result of the above command is:
}
```

## Health check

You can check the health status of the node.

```bash
$ ./bin/cete healthcheck | jq .
```

Also provides the following REST APIs

### Liveness prove

This endpoint always returns 200 and should be used to check Cete health.

```bash
$ curl -X GET http://localhost:8000/v1/liveness_check | jq .
```

### Readiness probe

This endpoint returns 200 when Cete is ready to serve traffic (i.e. respond to queries).

```bash
$ curl -X GET http://localhost:8000/v1/readiness_check | jq .
```

## Putting a key-value

To put a key-value, execute the following command:
Expand Down
16 changes: 16 additions & 0 deletions client/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ func (c *GRPCClient) Target() string {
return c.conn.Target()
}

func (c *GRPCClient) LivenessCheck(opts ...grpc.CallOption) (*protobuf.LivenessCheckResponse, error) {
if resp, err := c.client.LivenessCheck(c.ctx, &empty.Empty{}, opts...); err != nil {
return nil, err
} else {
return resp, nil
}
}

func (c *GRPCClient) ReadinessCheck(opts ...grpc.CallOption) (*protobuf.ReadinessCheckResponse, error) {
if resp, err := c.client.ReadinessCheck(c.ctx, &empty.Empty{}, opts...); err != nil {
return nil, err
} else {
return resp, nil
}
}

func (c *GRPCClient) Join(req *protobuf.JoinRequest, opts ...grpc.CallOption) error {
if _, err := c.client.Join(c.ctx, req, opts...); err != nil {
return err
Expand Down
102 changes: 102 additions & 0 deletions cmd/healthcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package cmd

import (
"context"
"encoding/json"
"fmt"
"os"

"github.com/mitchellh/go-homedir"
"github.com/mosuka/cete/client"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
healthCheckCmd = &cobra.Command{
Use: "healthcheck",
Short: "Health check a node",
Long: "Health check a node",
RunE: func(cmd *cobra.Command, args []string) error {
grpcAddress = viper.GetString("grpc_address")

certificateFile = viper.GetString("certificate_file")
commonName = viper.GetString("common_name")

c, err := client.NewGRPCClientWithContextTLS(grpcAddress, context.Background(), certificateFile, commonName)
if err != nil {
return err
}
defer func() {
_ = c.Close()
}()

lResp, err := c.LivenessCheck()
if err != nil {
return err
}

rResp, err := c.ReadinessCheck()
if err != nil {
return err
}

resp := map[string]bool{
"liveness": lResp.Alive,
"readiness:": rResp.Ready,
}

respBytes, err := json.Marshal(resp)
if err != nil {
return err
}

fmt.Println(string(respBytes))

return nil
},
}
)

func init() {
rootCmd.AddCommand(healthCheckCmd)

cobra.OnInitialize(func() {
if configFile != "" {
viper.SetConfigFile(configFile)
} else {
home, err := homedir.Dir()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

viper.AddConfigPath("/etc")
viper.AddConfigPath(home)
viper.SetConfigName("cete")

}

viper.SetEnvPrefix("CETE")
viper.AutomaticEnv()

if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
// cete.yaml does not found in config search path
default:
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
})

healthCheckCmd.PersistentFlags().StringVar(&configFile, "config-file", "", "config file. if omitted, cete.yaml in /etc and home directory will be searched")
healthCheckCmd.PersistentFlags().StringVar(&grpcAddress, "grpc-address", ":9000", "gRPC server listen address")
healthCheckCmd.PersistentFlags().StringVar(&certificateFile, "certificate-file", "", "path to the client server TLS certificate file")
healthCheckCmd.PersistentFlags().StringVar(&commonName, "common-name", "", "certificate common name")

_ = viper.BindPFlag("grpc_address", healthCheckCmd.PersistentFlags().Lookup("grpc-address"))
_ = viper.BindPFlag("certificate_file", healthCheckCmd.PersistentFlags().Lookup("certificate-file"))
_ = viper.BindPFlag("common_name", healthCheckCmd.PersistentFlags().Lookup("common-name"))
}
15 changes: 1 addition & 14 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
// Copyright (c) 2020 Minoru Osuka
//
// 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 errors

import "errors"

var (
ErrNotFoundLeader = errors.New("does not found leader")
ErrNodeAlreadyExists = errors.New("node already exists")
ErrNodeNotReady = errors.New("node not ready")
ErrNotFound = errors.New("not found")
ErrTimeout = errors.New("timeout")
)
14 changes: 0 additions & 14 deletions marshaler/marshaler.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright (c) 2020 Minoru Osuka
//
// 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 marshaler

import (
Expand Down
29 changes: 12 additions & 17 deletions marshaler/util.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright (c) 2020 Minoru Osuka
//
// 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 marshaler

import (
Expand All @@ -24,16 +10,25 @@ import (
)

func init() {
registry.RegisterType("protobuf.LivenessCheckResponse", reflect.TypeOf(protobuf.LivenessCheckResponse{}))
registry.RegisterType("protobuf.ReadinessCheckResponse", reflect.TypeOf(protobuf.ReadinessCheckResponse{}))
registry.RegisterType("protobuf.Metadata", reflect.TypeOf(protobuf.Metadata{}))
registry.RegisterType("protobuf.Node", reflect.TypeOf(protobuf.Node{}))
registry.RegisterType("protobuf.Cluster", reflect.TypeOf(protobuf.Cluster{}))
registry.RegisterType("protobuf.JoinRequest", reflect.TypeOf(protobuf.JoinRequest{}))
registry.RegisterType("protobuf.LeaveRequest", reflect.TypeOf(protobuf.LeaveRequest{}))
registry.RegisterType("protobuf.NodeResponse", reflect.TypeOf(protobuf.NodeResponse{}))
registry.RegisterType("protobuf.ClusterResponse", reflect.TypeOf(protobuf.ClusterResponse{}))
registry.RegisterType("protobuf.GetRequest", reflect.TypeOf(protobuf.GetRequest{}))
registry.RegisterType("protobuf.GetResponse", reflect.TypeOf(protobuf.GetResponse{}))
registry.RegisterType("protobuf.SetRequest", reflect.TypeOf(protobuf.SetRequest{}))
registry.RegisterType("protobuf.DeleteRequest", reflect.TypeOf(protobuf.DeleteRequest{}))
registry.RegisterType("protobuf.KeyValuePair", reflect.TypeOf(protobuf.KeyValuePair{}))
registry.RegisterType("protobuf.SetMetadataRequest", reflect.TypeOf(protobuf.SetMetadataRequest{}))
registry.RegisterType("protobuf.DeleteMetadataRequest", reflect.TypeOf(protobuf.DeleteMetadataRequest{}))
registry.RegisterType("protobuf.Metadata", reflect.TypeOf(protobuf.Metadata{}))
registry.RegisterType("protobuf.Node", reflect.TypeOf(protobuf.Node{}))
registry.RegisterType("protobuf.Event", reflect.TypeOf(protobuf.Event{}))
registry.RegisterType("protobuf.WatchResponse", reflect.TypeOf(protobuf.WatchResponse{}))
registry.RegisterType("protobuf.MetricsResponse", reflect.TypeOf(protobuf.MetricsResponse{}))
registry.RegisterType("protobuf.KeyValuePair", reflect.TypeOf(protobuf.KeyValuePair{}))
registry.RegisterType("map[string]interface {}", reflect.TypeOf((map[string]interface{})(nil)))
}

Expand Down
Loading