This is an automated email from the ASF dual-hosted git repository.
hulk pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks-controller.git
The following commit(s) were added to refs/heads/unstable by this push:
new 4b1f5e5 Add the random string when generating the session id (#172)
4b1f5e5 is described below
commit 4b1f5e565862f36b8326e5ff837c1eef0c567974
Author: hulk <[email protected]>
AuthorDate: Sat May 4 11:18:18 2024 +0800
Add the random string when generating the session id (#172)
---
config/config.go | 23 ++++++++------------
config/config_test.go | 3 ---
server/helper/helper.go | 25 ++++++++++++++++++++--
.../config_test.go => server/helper/helper_test.go | 24 +++++++++------------
server/middleware/middleware.go | 3 +++
server/server.go | 10 ++++++---
6 files changed, 52 insertions(+), 36 deletions(-)
diff --git a/config/config.go b/config/config.go
index 559f092..8916e48 100644
--- a/config/config.go
+++ b/config/config.go
@@ -24,10 +24,13 @@ import (
"fmt"
"net"
"os"
+ "strings"
+ "github.com/go-playground/validator/v10"
+
+ "github.com/apache/kvrocks-controller/logger"
"github.com/apache/kvrocks-controller/store/engine/etcd"
"github.com/apache/kvrocks-controller/store/engine/zookeeper"
- "github.com/go-playground/validator/v10"
)
type AdminConfig struct {
@@ -35,11 +38,8 @@ type AdminConfig struct {
}
type FailOverConfig struct {
- GCIntervalSeconds int `yaml:"gc_interval_seconds"`
- PingIntervalSeconds int `yaml:"ping_interval_seconds"`
- MaxPingCount int64 `yaml:"max_ping_count"`
- MinAliveSize int `yaml:"min_alive_size"`
- MaxFailureRatio float64 `yaml:"max_failure_ratio"`
+ PingIntervalSeconds int `yaml:"ping_interval_seconds"`
+ MaxPingCount int64 `yaml:"max_ping_count"`
}
type ControllerConfig struct {
@@ -59,11 +59,8 @@ type Config struct {
func DefaultFailOverConfig() *FailOverConfig {
return &FailOverConfig{
- GCIntervalSeconds: 3600,
PingIntervalSeconds: 3,
MaxPingCount: 5,
- MinAliveSize: 10,
- MaxFailureRatio: 0.6,
}
}
@@ -84,14 +81,12 @@ func (c *Config) Validate() error {
if c.Controller.FailOver.MaxPingCount < 3 {
return errors.New("max ping count required >= 3")
}
- if c.Controller.FailOver.GCIntervalSeconds < 60 {
- return errors.New("gc interval required >= 1min")
- }
if c.Controller.FailOver.PingIntervalSeconds < 1 {
return errors.New("ping interval required >= 1s")
}
- if c.Controller.FailOver.MinAliveSize < 2 {
- return errors.New("min alive size required >= 2")
+ hostPort := strings.Split(c.Addr, ":")
+ if hostPort[0] == "0.0.0.0" || hostPort[0] == "127.0.0.1" {
+ logger.Get().Warn("Leader forward may not work if the host is "
+ hostPort[0])
}
return nil
}
diff --git a/config/config_test.go b/config/config_test.go
index cd91823..a2002fe 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -29,11 +29,8 @@ func TestDefaultControllerConfigSet(t *testing.T) {
cfg := Default()
expectedControllerConfig := &ControllerConfig{
FailOver: &FailOverConfig{
- GCIntervalSeconds: 3600,
PingIntervalSeconds: 3,
MaxPingCount: 5,
- MinAliveSize: 10,
- MaxFailureRatio: 0.6,
},
}
diff --git a/server/helper/helper.go b/server/helper/helper.go
index 3f1eb4b..896a2a4 100644
--- a/server/helper/helper.go
+++ b/server/helper/helper.go
@@ -17,15 +17,19 @@
* under the License.
*
*/
+
package helper
import (
"errors"
+ "fmt"
"net/http"
-
- "github.com/apache/kvrocks-controller/consts"
+ "strings"
"github.com/gin-gonic/gin"
+
+ "github.com/apache/kvrocks-controller/consts"
+ "github.com/apache/kvrocks-controller/util"
)
type Error struct {
@@ -79,3 +83,20 @@ func ResponseError(c *gin.Context, err error) {
})
c.Abort()
}
+
+// generateSessionID encodes the addr to a session ID,
+// which is used to identify the session. And then can be used to
+// parse the leader listening address back.
+func GenerateSessionID(addr string) string {
+ return fmt.Sprintf("%s/%s", util.RandString(8), addr)
+}
+
+// extractAddrFromSessionID decodes the session ID to the addr.
+func ExtractAddrFromSessionID(sessionID string) string {
+ parts := strings.Split(sessionID, "/")
+ if len(parts) != 2 {
+ // for the old session ID format, we use the addr as the
session ID
+ return sessionID
+ }
+ return parts[1]
+}
diff --git a/config/config_test.go b/server/helper/helper_test.go
similarity index 67%
copy from config/config_test.go
copy to server/helper/helper_test.go
index cd91823..f9496ff 100644
--- a/config/config_test.go
+++ b/server/helper/helper_test.go
@@ -17,25 +17,21 @@
* under the License.
*
*/
-package config
+
+package helper
import (
"testing"
- "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
)
-func TestDefaultControllerConfigSet(t *testing.T) {
- cfg := Default()
- expectedControllerConfig := &ControllerConfig{
- FailOver: &FailOverConfig{
- GCIntervalSeconds: 3600,
- PingIntervalSeconds: 3,
- MaxPingCount: 5,
- MinAliveSize: 10,
- MaxFailureRatio: 0.6,
- },
- }
+func TestGenerateSessionID(t *testing.T) {
+ testAddr := "127.0.0.1:1234"
+ sessionID := GenerateSessionID(testAddr)
+ decodedAddr := ExtractAddrFromSessionID(sessionID)
+ require.Equal(t, testAddr, decodedAddr)
- assert.Equal(t, expectedControllerConfig, cfg.Controller)
+ // old format
+ require.Equal(t, testAddr, ExtractAddrFromSessionID(testAddr))
}
diff --git a/server/middleware/middleware.go b/server/middleware/middleware.go
index 9ccfbe3..9e87939 100644
--- a/server/middleware/middleware.go
+++ b/server/middleware/middleware.go
@@ -17,6 +17,7 @@
* under the License.
*
*/
+
package middleware
import (
@@ -68,6 +69,8 @@ func RedirectIfNotLeader(c *gin.Context) {
if !storage.IsLeader() {
if !c.GetBool(consts.HeaderIsRedirect) {
c.Set(consts.HeaderIsRedirect, true)
+ peerAddr :=
helper.ExtractAddrFromSessionID(storage.Leader())
+ c.Redirect(http.StatusTemporaryRedirect,
"http://"+peerAddr+c.Request.RequestURI)
c.Redirect(http.StatusTemporaryRedirect,
"http://"+storage.Leader()+c.Request.RequestURI)
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": "no leader
now, please retry later"})
diff --git a/server/server.go b/server/server.go
index 65f5ae9..b4b8a08 100644
--- a/server/server.go
+++ b/server/server.go
@@ -17,6 +17,7 @@
* under the License.
*
*/
+
package server
import (
@@ -32,6 +33,7 @@ import (
"github.com/apache/kvrocks-controller/config"
"github.com/apache/kvrocks-controller/controller"
"github.com/apache/kvrocks-controller/logger"
+ "github.com/apache/kvrocks-controller/server/helper"
"github.com/apache/kvrocks-controller/store"
"github.com/apache/kvrocks-controller/store/engine"
"github.com/apache/kvrocks-controller/store/engine/etcd"
@@ -49,16 +51,18 @@ type Server struct {
func NewServer(cfg *config.Config) (*Server, error) {
var persist engine.Engine
var err error
+
+ sessionID := helper.GenerateSessionID(cfg.Addr)
switch {
case strings.EqualFold(cfg.StorageType, "etcd"):
logger.Get().Info("Use Etcd as store")
- persist, err = etcd.New(cfg.Addr, cfg.Etcd)
+ persist, err = etcd.New(sessionID, cfg.Etcd)
case strings.EqualFold(cfg.StorageType, "zookeeper"):
logger.Get().Info("Use Zookeeper as store")
- persist, err = zookeeper.New(cfg.Addr, cfg.Zookeeper)
+ persist, err = zookeeper.New(sessionID, cfg.Zookeeper)
default:
logger.Get().Info("Use Etcd as default store")
- persist, err = etcd.New(cfg.Addr, cfg.Etcd)
+ persist, err = etcd.New(sessionID, cfg.Etcd)
}
if err != nil {