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

fix: 解决健康检查规则打印时候出现的协程panic问题 #1024

Merged
merged 2 commits into from
Mar 20, 2023
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
9 changes: 7 additions & 2 deletions apiserver/httpserver/i18n/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,26 @@ package i18n

import (
"fmt"
"sync"

"github.com/BurntSushi/toml"
ii18n "github.com/nicksnyder/go-i18n/v2/i18n"
"go.uber.org/zap"
"golang.org/x/text/language"

"github.com/polarismesh/polaris/common/log"
"github.com/polarismesh/polaris/common/utils"
)

var (
bundle *ii18n.Bundle
i18nMsgCache map[uint32]*ii18n.Message
once sync.Once
)

func init() {
bundle = ii18n.NewBundle(language.English)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
LoadI18nMessageFile("conf/i18n/zh.toml")
LoadI18nMessageFile("conf/i18n/en.toml")
}

// LoadI18nMessageFile 加载i18n配置文件
Expand All @@ -51,6 +52,10 @@ func LoadI18nMessageFile(path string) {

// Translate 国际化code所对应的msg信息
func Translate(code uint32, langs ...string) (string, error) {
once.Do(func() {
LoadI18nMessageFile(utils.ConfDir + "i18n/zh.toml")
LoadI18nMessageFile(utils.ConfDir + "i18n/en.toml")
})
msg, ok := i18nMsgCache[code]
if !ok {
msg = &ii18n.Message{ID: fmt.Sprintf("%d", code)}
Expand Down
9 changes: 9 additions & 0 deletions bootstrap/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ var (
func Start(configFilePath string) {
// 加载配置
ConfigFilePath = configFilePath
utils.ConfDir = parseConfDir(configFilePath)
cfg, err := boot_config.Load(configFilePath)
if err != nil {
fmt.Printf("[ERROR] load config fail\n")
Expand Down Expand Up @@ -282,6 +283,14 @@ func StartDiscoverComponents(ctx context.Context, cfg *boot_config.Config, s sto
return nil
}

func parseConfDir(path string) string {
slashIndex := strings.LastIndex(path, "/")
if slashIndex == -1 {
return "./"
}
return path[0 : slashIndex+1]
}

// StartConfigCenterComponents 启动配置中心模块
func StartConfigCenterComponents(ctx context.Context, cfg *boot_config.Config, s store.Store,
cacheMgn *cache.CacheManager, authMgn auth.AuthServer) error {
Expand Down
1 change: 0 additions & 1 deletion cache/faultdetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ func (f *faultDetectCache) checkServiceSpecificCache(
log.Infof(
"checkServiceSpecificCache name %s, namespace %s, values %v", name, namespace, f.svcSpecificRules)
svcRules, ok := f.svcSpecificRules[namespace]
log.Infof("svcSpecificRules for ns %s, values %v", name, svcRules)
if ok {
return svcRules[name]
}
Expand Down
2 changes: 2 additions & 0 deletions common/utils/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const (
var (
// LocalHost local host
LocalHost = "127.0.0.1"
// ConfDir default config dir
ConfDir = "conf/"
)

const (
Expand Down
17 changes: 17 additions & 0 deletions service/faultdetect_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
package service_test

import (
"context"
"fmt"
"sync"
"testing"
"time"

"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/wrappers"
Expand Down Expand Up @@ -178,4 +180,19 @@ func TestCreateFaultDetectRule(t *testing.T) {
}
wg.Wait()
})

t.Run("创建探测规则,并通过客户端接口查询,返回正确规则", func(t *testing.T) {
fdRules, resp := createFaultDetectRules(discoverSuit, 1)
defer cleanFaultDetectRules(discoverSuit, resp)
checkFaultDetectRuleResponse(t, fdRules, resp)
time.Sleep(5 * time.Second)
discoverResp := discoverSuit.DiscoverServer().GetFaultDetectWithCache(context.Background(), &apiservice.Service{
Name: &wrappers.StringValue{Value: "testDestService"},
Namespace: &wrappers.StringValue{Value: "test"},
})
assert.Equal(t, int(apimodel.Code_ExecuteSuccess), int(discoverResp.GetCode().GetValue()))
faultDetector := discoverResp.GetFaultDetector()
assert.NotNil(t, faultDetector)
assert.Equal(t, 1, len(faultDetector.GetRules()))
})
}
4 changes: 4 additions & 0 deletions service/healthcheck/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sync"
"time"

"github.com/golang/protobuf/proto"
apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"

Expand Down Expand Up @@ -214,6 +215,9 @@ func (s *Server) RecordHistory(entry *model.RecordEntry) {
// publishInstanceEvent 发布服务事件
func (s *Server) publishInstanceEvent(serviceID string, event model.InstanceEvent) {
event.SvcId = serviceID
if nil != event.Instance {
event.Instance = proto.Clone(event.Instance).(*apiservice.Instance)
}
eventhub.Publish(eventhub.InstanceEventTopic, event)
}

Expand Down