From fc65716cb14d829c5ad9f79aa909ec0564eea2bd Mon Sep 17 00:00:00 2001
From: chenshijun <csj_sky@126.com>
Date: 星期六, 12 十月 2019 11:36:03 +0800
Subject: [PATCH] 判断SnapshotPath合法性
---
config.go | 90 +++++++++++++++++++++++++++++++++++++++++----
1 files changed, 82 insertions(+), 8 deletions(-)
diff --git a/config.go b/config.go
index 8a25908..4b34256 100644
--- a/config.go
+++ b/config.go
@@ -14,13 +14,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package serf
+package syncdb
import (
"fmt"
+ "net"
+ "os"
"strconv"
+ "strings"
- "github.com/apache/servicecomb-service-center/syncer/pkg/utils"
+ //"github.com/apache/servicecomb-service-center/syncer/pkg/utils"
"github.com/hashicorp/memberlist"
"github.com/hashicorp/serf/cmd/serf/command/agent"
"github.com/hashicorp/serf/serf"
@@ -34,14 +37,29 @@
ModeCluster = "cluster"
retryMaxAttempts = 3
groupExpect = 3
- tagKeyClusterName = "syncer-cluster-name"
+ DefaultEncryptKey = "bjbasic@aiotlink"
+ tagKeyClusterID = "syncer-cluster-name"
TagKeyClusterPort = "syncer-cluster-port"
TagKeyRPCPort = "syncer-rpc-port"
+ BroadcastIP = "255.255.255.255"
+ BroadcastPort = 30193
+ BroadcastInterval = 5
+ MaxQueryRespSize = 50 * 1024 * 1024
+ MaxQuerySize = 50 * 1024 * 1024
+ MaxUserEventSize = 9 * 1024
+ ReplayOnJoinDefault = false
+ SnapshotPathDefault = "./serfSnapShot"
+ MaxEventBufferCount = 2048
+
+ TcpTransportPort = 30194 //tcp浼犺緭澶ф暟鎹噺鎺ュ彛
)
// DefaultConfig default config
func DefaultConfig() *Config {
agentConf := agent.DefaultConfig()
+ agentConf.QueryResponseSizeLimit = MaxQueryRespSize
+ agentConf.QuerySizeLimit = MaxQuerySize
+ agentConf.UserEventSizeLimit = MaxUserEventSize
agentConf.BindAddr = fmt.Sprintf("0.0.0.0:%d", DefaultBindPort)
agentConf.RPCAddr = fmt.Sprintf("0.0.0.0:%d", DefaultRPCPort)
return &Config{
@@ -58,7 +76,7 @@
Mode string `json:"mode"`
// name to group members into cluster
- ClusterName string `json:"cluster_name"`
+ ClusterID string `json:"cluster_name"`
// port to communicate between cluster members
ClusterPort int `yaml:"cluster_port"`
@@ -73,11 +91,33 @@
return nil
}
+func isFileRightful(filePath string) bool {
+ if filePath != "" {
+ _, err := os.Stat(filePath)
+ if err != nil && os.IsNotExist(err) {
+ pos := strings.LastIndex(filePath, "/")
+ if pos != -1 {
+ filePath = filePath[0:pos]
+ }
+
+ _, err = os.Stat(filePath)
+ if err == nil || !os.IsNotExist(err) {
+ return true
+ } else {
+ return false
+ }
+ } else {
+ return false
+ }
+ }
+ return false
+}
+
// convertToSerf convert Config to serf.Config
-func (c *Config) convertToSerf() (*serf.Config, error) {
+func (c *Config) convertToSerf(snapshotPath string) (*serf.Config, error) {
serfConf := serf.DefaultConfig()
- bindIP, bindPort, err := utils.SplitHostPort(c.BindAddr, DefaultBindPort)
+ bindIP, bindPort, err := SplitHostPort(c.BindAddr, DefaultBindPort)
if err != nil {
return nil, fmt.Errorf("invalid bind address: %s", err)
}
@@ -96,15 +136,49 @@
serfConf.MemberlistConfig.BindAddr = bindIP
serfConf.MemberlistConfig.BindPort = bindPort
serfConf.NodeName = c.NodeName
+
serfConf.Tags = map[string]string{TagKeyRPCPort: strconv.Itoa(c.RPCPort)}
- if c.ClusterName != "" {
- serfConf.Tags[tagKeyClusterName] = c.ClusterName
+ if c.ClusterID != "" {
+ serfConf.Tags[tagKeyClusterID] = c.ClusterID
serfConf.Tags[TagKeyClusterPort] = strconv.Itoa(c.ClusterPort)
}
if c.Mode == ModeCluster && c.RetryMaxAttempts <= 0 {
c.RetryMaxAttempts = retryMaxAttempts
}
+
+ c.SnapshotPath = SnapshotPathDefault
+ if isFileRightful(snapshotPath) {
+ c.SnapshotPath = snapshotPath
+ }
+
+ c.ReplayOnJoin = ReplayOnJoinDefault
+
+ serfConf.QueryResponseSizeLimit = c.QueryResponseSizeLimit
+ serfConf.QuerySizeLimit = c.QuerySizeLimit
+ serfConf.UserEventSizeLimit = c.UserEventSizeLimit
+ serfConf.SnapshotPath = c.SnapshotPath
+ serfConf.EventBuffer = MaxEventBufferCount
+
return serfConf, nil
}
+
+// SplitHostPort returns the parts of the address and port. If the port does not exist, use defaultPort.
+func SplitHostPort(address string, defaultPort int) (string, int, error) {
+ _, _, err := net.SplitHostPort(address)
+ if ae, ok := err.(*net.AddrError); ok && ae.Err == "missing port in address" {
+ address = fmt.Sprintf("%s:%d", address, defaultPort)
+ _, _, err = net.SplitHostPort(address)
+ }
+ if err != nil {
+ return "", 0, err
+ }
+
+ addr, err := net.ResolveTCPAddr("tcp", address)
+ if err != nil {
+ return "", 0, err
+ }
+
+ return addr.IP.String(), addr.Port, nil
+}
--
Gitblit v1.8.0