Skip to content

Commit

Permalink
Merge pull request #932 from cityiron/feature/fix-config_center-file
Browse files Browse the repository at this point in the history
[Fix] file service discovery path can run in windows
  • Loading branch information
AlexStocks committed Jan 10, 2021
2 parents 374a01a + cc7fb2e commit 794ab77
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 33 deletions.
110 changes: 80 additions & 30 deletions config_center/file/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"os"
"os/exec"
"os/user"
"path"
"path/filepath"
"runtime"
"strings"
Expand All @@ -41,11 +40,19 @@ import (
"github.com/apache/dubbo-go/config_center/parser"
)

var (
osType = runtime.GOOS
)

const (
windowsOS = "windows"
)

const (
PARAM_NAME_PREFIX = "dubbo.config-center."
CONFIG_CENTER_DIR_PARAM_NAME = PARAM_NAME_PREFIX + "dir"
CONFIG_CENTER_ENCODING_PARAM_NAME = PARAM_NAME_PREFIX + "encoding"
DEFAULT_CONFIG_CENTER_ENCODING = "UTF-8"
ParamNamePrefix = "dubbo.config-center."
ConfigCenterDirParamName = ParamNamePrefix + "dir"
ConfigCenterEncodingParamName = ParamNamePrefix + "encoding"
defaultConfigCenterEncoding = "UTF-8"
)

// FileSystemDynamicConfiguration
Expand All @@ -59,24 +66,14 @@ type FileSystemDynamicConfiguration struct {
}

func newFileSystemDynamicConfiguration(url *common.URL) (*FileSystemDynamicConfiguration, error) {
encode := url.GetParam(CONFIG_CENTER_ENCODING_PARAM_NAME, DEFAULT_CONFIG_CENTER_ENCODING)
encode := url.GetParam(ConfigCenterEncodingParamName, defaultConfigCenterEncoding)

root := url.GetParam(CONFIG_CENTER_DIR_PARAM_NAME, "")
root := url.GetParam(ConfigCenterDirParamName, "")
var c *FileSystemDynamicConfiguration
if _, err := os.Stat(root); err != nil {
// not exist, use default, /XXX/xx/.dubbo/config-center
if rp, err := Home(); err != nil {
return nil, perrors.WithStack(err)
} else {
root = path.Join(rp, ".dubbo", "config-center")
}
}

if _, err := os.Stat(root); err != nil {
// it must be dir, if not exist, will create
if err = createDir(root); err != nil {
return nil, perrors.WithStack(err)
}
root, err := mkdirIfNecessary(root)
if err != nil {
return nil, err
}

c = &FileSystemDynamicConfiguration{
Expand Down Expand Up @@ -195,18 +192,18 @@ func (fsdc *FileSystemDynamicConfiguration) Close() error {
// GetPath get path
func (fsdc *FileSystemDynamicConfiguration) GetPath(key string, group string) string {
if len(key) == 0 {
return path.Join(fsdc.rootPath, group)
return filepath.Join(fsdc.rootPath, group)
}

if len(group) == 0 {
group = config_center.DEFAULT_GROUP
}

return path.Join(fsdc.rootPath, group, key)
return filepath.Join(fsdc.rootPath, group, adapterKey(key))
}

func (fsdc *FileSystemDynamicConfiguration) deleteDelay(path string) (bool, error) {
if path == "" {
if len(path) == 0 {
return false, nil
}

Expand All @@ -226,9 +223,7 @@ func (fsdc *FileSystemDynamicConfiguration) write2File(fp string, value string)
}

func forceMkdirParent(fp string) error {
pd := getParentDirectory(fp)

return createDir(pd)
return createDir(getParentDirectory(fp))
}

func createDir(path string) error {
Expand All @@ -250,6 +245,7 @@ func substr(s string, pos, length int) string {
if l > len(runes) {
l = len(runes)
}

return string(runes[pos:l])
}

Expand All @@ -264,7 +260,7 @@ func Home() (string, error) {
}

// cross compile support
if "windows" == runtime.GOOS {
if windowsOS == osType {
return homeWindows()
}

Expand All @@ -287,7 +283,7 @@ func homeUnix() (string, error) {
}

result := strings.TrimSpace(stdout.String())
if result == "" {
if len(result) == 0 {
return "", errors.New("blank output when reading home directory")
}

Expand All @@ -298,12 +294,66 @@ func homeWindows() (string, error) {
drive := os.Getenv("HOMEDRIVE")
homePath := os.Getenv("HOMEPATH")
home := drive + homePath
if drive == "" || homePath == "" {
if len(drive) == 0 || len(homePath) == 0 {
home = os.Getenv("USERPROFILE")
}
if home == "" {
if len(home) == 0 {
return "", errors.New("HOMEDRIVE, HOMEPATH, and USERPROFILE are blank")
}

return home, nil
}

func mkdirIfNecessary(urlRoot string) (string, error) {
if !legalPath(urlRoot) {
// not exist, use default, mac is: /XXX/xx/.dubbo/config-center
rp, err := Home()
if err != nil {
return "", perrors.WithStack(err)
}

urlRoot = adapterUrl(rp)
}

if _, err := os.Stat(urlRoot); err != nil {
// it must be dir, if not exist, will create
if err = createDir(urlRoot); err != nil {
return "", perrors.WithStack(err)
}
}

return urlRoot, nil
}

func legalPath(path string) bool {
if len(path) == 0 {
return false
}
if _, err := os.Stat(path); err != nil {
return false
}

return true
}

func adapterUrl(rp string) string {
if osType == windowsOS {
return filepath.Join(rp, "_dubbo", "config-center")
}

return filepath.Join(rp, ".dubbo", "config-center")
}

// used for GetPath. param key default is instance's id.
// e.g: (ip:port) 127.0.0.1:20081, in windows env, will change to 127_0_0_1_20081
func adapterKey(key string) string {
if len(key) == 0 {
return ""
}

if osType == windowsOS {
return strings.ReplaceAll(strings.ReplaceAll(key, ".", "_"), ":", "_")
}

return key
}
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ github.com/RoaringBitmap/roaring v0.5.5 h1:naNqvO1mNnghk2UvcsqnzHDBn9DRbCIRy94Gm
github.com/RoaringBitmap/roaring v0.5.5/go.mod h1:puNo5VdzwbaIQxSiDIwfXl4Hnc+fbovcX4IW/dSTtUk=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 h1:fLjPD/aNc3UIOA6tDi6QXUemppXK3P9BI7mr2hd6gx8=
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
Expand Down Expand Up @@ -234,7 +233,6 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
github.com/go-ole/go-ole v1.2.1 h1:2lOsA72HgjxAuMlKpFiCbHTvu44PIVkZ5hqm3RSdI/E=
github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8=
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
Expand Down
2 changes: 1 addition & 1 deletion registry/file/service_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func newFileSystemServiceDiscovery(name string) (registry.ServiceDiscovery, erro
fdcf := extension.GetConfigCenterFactory(constant.FILE_KEY)
p := path.Join(rp, ".dubbo", constant.REGISTRY_KEY)
url, _ := common.NewURL("")
url.AddParamAvoidNil(file.CONFIG_CENTER_DIR_PARAM_NAME, p)
url.AddParamAvoidNil(file.ConfigCenterDirParamName, p)
c, err := fdcf.GetDynamicConfiguration(url)
if err != nil {
return nil, perrors.WithStack(err)
Expand Down

0 comments on commit 794ab77

Please sign in to comment.