Skip to content

Commit

Permalink
add shutdown-timeout, raft-snapshot-interval
Browse files Browse the repository at this point in the history
  • Loading branch information
tdx committed Aug 29, 2020
1 parent 346505f commit ab138be
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
4 changes: 4 additions & 0 deletions api/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package api

import (
"time"

dbApi "github.com/tdx/rkv/db/api"

"github.com/hashicorp/raft"
Expand All @@ -23,4 +25,6 @@ type Config struct {
LogLevel string
// Backend
Backend dbApi.Backend
// Used in k8s. Delay to allow balancer remove traffice from endpoint
ShutdownDelay time.Duration
}
24 changes: 21 additions & 3 deletions cmd/rkvd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path"
"path/filepath"
"syscall"
"time"

"github.com/tdx/rkv"
rkvApi "github.com/tdx/rkv/api"
Expand Down Expand Up @@ -66,6 +67,7 @@ func setupFlags(cmd *cobra.Command) error {
cmd.Flags().Int("rpc-port", 8402, "Port for RPC connections.")
cmd.Flags().String("http-addr", ":8403", "Address to bind HTTP on.")
cmd.Flags().String("log-level", "info", "Log level.")
cmd.Flags().String("shutdown-delay", "", "Used for k8s balancer to remove traffic from pod")

return viper.BindPFlags(cmd.Flags())
}
Expand Down Expand Up @@ -117,6 +119,8 @@ func (c *cli) setupConfig(cmd *cobra.Command, args []string) error {
c.Config.RPCPort = viper.GetInt("rpc-port")
c.Config.HTTPAddr = viper.GetString("http-addr")

c.Config.ShutdownDelay = viper.GetDuration("shutdown-delay")

return nil
}

Expand All @@ -128,11 +132,25 @@ func (c *cli) run(cmd *cobra.Command, args []string) error {
c.logger = client.Logger("main")
c.logger.SetPrefix("rkvd ")

c.logger.Println("config:", "shutdown-delay=", c.Config.ShutdownDelay)

done := make(chan struct{})
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
s := <-sigc

c.logger.Println("stopped by signal:", s.String())
signal.Notify(sigc, syscall.SIGTERM)
go func() {
s := <-sigc
c.logger.Println("got signal signal:", s.String(),
", delay shutdown:", c.Config.ShutdownDelay)

// allow k8s load balancer remove traffic from pod
if c.Config.ShutdownDelay > 0 {
time.Sleep(c.Config.ShutdownDelay)
}
done <- struct{}{}
}()

<-done

return client.Shutdown()
}
2 changes: 2 additions & 0 deletions deploy/rkvd/templates/statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ spec:
bootstrap: $([ $ID = 0 ] && echo true || echo false )
http-addr: ":{{.Values.httpPort}}"
log-level: {{ .Values.log }}
raft-snapshot-interval: {{ .Values.raftSnapshotInterval }}
shutdown-delay: {{ .Values.shutdownDelay }}
EOD
volumeMounts:
- name: datadir
Expand Down
3 changes: 2 additions & 1 deletion deploy/rkvd/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ rpcPort: 8402
httpPort: 8403
replicas: 3
storage: 1Gi
raftSnapshotInterval: 120s
db: bolt
log: debug
log: trace
1 change: 1 addition & 0 deletions example/cluster/put
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ curl -X POST \
-s -w " %{http_code}" \
-H "Content-Type: application/json" \
-d '{"tab": "'$tab'","key":"'$key'","val":"'$val'"}' \
--connect-timeout 10 --max-time 10 \
http://$hostname:$port/db/put

2 changes: 1 addition & 1 deletion internal/cluster/raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (d *Backend) setupRaft(dataDir string) error {
d.logger.Named("snapshot"),
)

maxPool := 5
maxPool := 1
timeout := 10 * time.Second
transport := raft.NewNetworkTransport(
d.config.StreamLayer,
Expand Down

0 comments on commit ab138be

Please sign in to comment.