Merge remote-tracking branch 'origin/master' into common/metadata_range

pull/77/head
leslie 6 years ago
commit 73325d2ce2
  1. 27
      .gitignore
  2. 12
      .travis.yml
  3. 61
      doc/wiki-cn/kratos-tool.md
  4. 12
      doc/wiki-cn/protoc.md
  5. 4
      go.mod
  6. 3
      pkg/conf/env/env.go
  7. 15
      pkg/conf/paladin/file_test.go
  8. 6
      pkg/database/tidb/discovery.go
  9. 24
      pkg/ecode/status_test.go
  10. 2
      pkg/log/log.go
  11. 438
      pkg/naming/discovery/discovery.go
  12. 93
      pkg/naming/naming.go
  13. 4
      pkg/net/rpc/warden/client.go
  14. 2
      pkg/net/rpc/warden/internal/examples/grpcDebug/client.go
  15. 14
      pkg/net/rpc/warden/internal/status/status_test.go
  16. 13
      pkg/net/rpc/warden/resolver/direct/direct.go
  17. 10
      pkg/net/rpc/warden/resolver/resolver.go
  18. 6
      pkg/net/rpc/warden/resolver/test/mockdiscovery.go
  19. 9
      pkg/net/rpc/warden/resolver/test/resovler_test.go
  20. 6
      pkg/net/rpc/warden/server_test.go
  21. 4
      tool/kratos/project.go

27
.gitignore vendored

@ -1,4 +1,29 @@
# idea ignore
.idea/
*.ipr
*.iml
*.iws
.vscode/
# temp ignore
*.log
*.cache
*.diff
*.exe
*.exe~
*.patch
*.swp
*.tmp
go.sum
BUILD
# system ignore
.DS_Store
Thumbs.db
# project
*.cert
*.key
tool/kratos/kratos
tool/kratos/kratos-protoc/kratos-protoc
tool/kratos/protobuf/protoc-gen-bm/protoc-gen-bm
tool/kratos/protobuf/protoc-gen-bswagger/protoc-gen-bswagger

@ -9,7 +9,12 @@ git:
# Force-enable Go modules. This will be unnecessary when Go 1.12 lands.
env:
global:
- GO111MODULE=on
- REGION=sh
- ZONE=sh001
- DEPLOY_ENV=dev
- DISCOVERY_NODES=127.0.0.1:7171
# Skip the install step. Don't `go get` dependencies. Only build with the code
# in vendor/
@ -19,11 +24,14 @@ install: true
# build and immediately stop. It's sorta like having set -e enabled in bash.
# Make sure golangci-lint is vendored.
before_script:
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $GOPATH/bin
- curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s -- -b $GOPATH/bin
- curl -sfL https://raw.githubusercontent.com/bilibili/discovery/master/install.sh | sh -s -- -b $GOPATH/bin
- curl -sfL https://raw.githubusercontent.com/bilibili/discovery/master/cmd/discovery/discovery-example.toml -o $GOPATH/bin/discovery.toml
- nohup bash -c "$GOPATH/bin/discovery -conf $GOPATH/bin/discovery.toml &"
script:
- go test ./...
- go build ./...
- go test ./...
after_success:
- golangci-lint run # run a bunch of code checkers/linters in parallel

@ -61,19 +61,20 @@ kratos new kratos-demo -o YourName -d YourPath
kratos new kratos-demo -o YourName -d YourPath --proto
```
特别注意,如果不是macos系统,生成的示例项目`api`目录下的`proto`文件并不会自动生成对应的`.pb.go`和`.bm.go`文件,需要参考以下说明进行生成。
[protoc说明](protoc.md)
> 特别注意,如果不是MacOS系统,需要自己进行手动安装protoc,用于生成的示例项目`api`目录下的`proto`文件并不会自动生成对应的`.pb.go`和`.bm.go`文件。
> 也可以参考以下说明进行生成:[protoc说明](protoc.md)
# kratos build & run
`kratos build`和`kratos run`是`go build`和`go run`的封装,并无特别用途。
`kratos build`和`kratos run`是`go build`和`go run`的封装,可以在当前项目任意目录进行快速运行进行调试,并无特别用途。
# kratos tool
`kratos tool`是基于proto生成http&grpc代码,生成缓存回源代码,生成memcache执行代码,生成swagger文档等工具集,先看下`kratos tool`的执行效果:
`kratos tool`是基于proto生成http&grpc代码,生成缓存回源代码,生成memcache执行代码,生成swagger文档等工具集,先看下的执行效果:
```shell
kratos tool
```
swagger(已安装): swagger api文档 Author(goswagger.io) [2019/05/05]
protoc(已安装): 快速方便生成pb.go和bm.go的protoc封装,windows、Linux请先安装protoc工具 Author(kratos) [2019/05/04]
kratos(已安装): Kratos工具集本体 Author(kratos) [2019/04/02]
@ -84,21 +85,30 @@ kratos(已安装): Kratos工具集本体 Author(kratos) [2019/04/02]
详细文档: https://github.com/bilibili/kratos/blob/master/doc/wiki-cn/kratos-tool.md
```
***小小说明:如未安装工具,第一次运行也可自动安装,不需要特别执行install***
> 小小说明:如未安装工具,第一次运行也可自动安装,不需要特别执行install
目前已经集成的工具有:
* `protoc`用于快速生成`*.pb.go`和`*.bm.go`文件,但目前不支持windows,Linux也需要先自己安装`protoc`工具。
* `swagger`用于显示自动生成的BM API接口文档,通过 `kratos tool swagger serve api/api.swagger.json` 可以访问到文档
* TODOs...
* kratos 为本体工具,只用于安装更新使用;
* protoc 用于快速生成gRPC、HTTP、Swagger文件,该命令Windows,Linux用户需要手动安装 protobuf 工具
* swagger 用于显示自动生成的HTTP API接口文档,通过 `kratos tool swagger serve api/api.swagger.json` 可以查看文档。
### kratos tool protoc
该命令运行没其他参数,直接`kratos tool protoc`运行即可。但使用前需特别说明:
```
// generate all
kratos tool protoc api.proto
// generate gRPC
kratos tool protoc --grpc api.proto
// generate BM HTTP
kratos tool protoc --bm api.proto
// generate swagger
kratos tool protoc --swagger api.proto
```
执行对应生成 `api.pb.go/api.bm.go/api.swagger.json` 源文档。
* 该工具在Windows/Linux下运行,需提前安装好`protoc`工具
> 该工具在Windows/Linux下运行,需提前安装好 protobuf 工具
该工具实际是一段`shell`脚本,其中自动将`protoc`命令进行了拼接,识别了需要`*.proto`的目录和当前目录下的`proto`文件,最终会拼接为如下命令进行执行:
该工具实际是一段`shell`脚本,其中自动将`protoc`命令进行了拼接,识别了需要的`*.proto`文件和当前目录下的`proto`文件,最终会拼接为如下命令进行执行:
```shell
export $KRATOS_HOME = kratos路径
@ -106,31 +116,22 @@ export $KRATOS_DEMO = 项目路径
// 生成:api.pb.go
protoc -I$GOPATH/src:$KRATOS_HOME/tool/protobuf/pkg/extensions:$KRATOS_DEMO/api --gogofast_out=plugins=grpc:$KRATOS_DEMO/api $KRATOS_DEMO/api/api.proto
// 生成:api.bm.go
protoc -I$GOPATH/src:$KRATOS_HOME/tool/protobuf/pkg/extensions:$KRATOS_DEMO/api --bm_out=$KRATOS_DEMO/api $KRATOS_DEMO/api/api.proto
// 生成:api.swagger.json
protoc -I$GOPATH/src:$KRATOS_HOME/tool/protobuf/pkg/extensions:$KRATOS_DEMO/api --bswagger_out=$KRATOS_DEMO/api $KRATOS_DEMO/api/api.proto
```
大家也可以参考该命令进行`proto`生成`*.pb.go`和`*.bm.go`文件,也可以参考[protoc说明](protoc.md)
大家也可以参考该命令进行`proto`生成,也可以参考[protobuf](https://github.com/google/protobuf)官方参数
### Tool examples
kratos tool swagger
```shell
// new a project
cd $GOPATH/src
kratos new kratos-demo -o Tinker --proto
// build & run
cd kratos-demo
kratos run
// swagger docs
kratos tool swagger serve kratos-demo/api/api.swagger.json
// generate proto
cd kratos-demo/api
kratos tool protoc api.proto
kratos tool swagger serve api/api.swagger.json
```
执行命令后,浏览器会自动打开swagger文档地址。
同时也可以查看更多的 [go-swagger](https://github.com/go-swagger/go-swagger) 官方参数进行使用。
-------------

@ -9,7 +9,17 @@
安装好对应工具后,我们可以进入`api`目录,执行如下命令:
```shell
protoc -I/Users/felix/work/go/src:/usr/local/include --gogofast_out=plugins=grpc:/Users/felix/work/go/src /Users/felix/work/go/src/kratos-demo/api/api.proto
export $KRATOS_HOME = kratos路径
export $KRATOS_DEMO = 项目路径
// 生成:api.pb.go
protoc -I$GOPATH/src:$KRATOS_HOME/tool/protobuf/pkg/extensions:$KRATOS_DEMO/api --gogofast_out=plugins=grpc:$KRATOS_DEMO/api $KRATOS_DEMO/api/api.proto
// 生成:api.bm.go
protoc -I$GOPATH/src:$KRATOS_HOME/tool/protobuf/pkg/extensions:$KRATOS_DEMO/api --bm_out=$KRATOS_DEMO/api $KRATOS_DEMO/api/api.proto
// 生成:api.swagger.json
protoc -I$GOPATH/src:$KRATOS_HOME/tool/protobuf/pkg/extensions:$KRATOS_DEMO/api --bswagger_out=$KRATOS_DEMO/api $KRATOS_DEMO/api/api.proto
```
请注意替换`/Users/felix/work/go/src`目录为你本地开发环境对应GOPATH目录,其中`--gogofast_out`意味着告诉`protoc`工具需要使用`gogo protobuf`的工具生成代码。

@ -17,6 +17,8 @@ require (
github.com/gogo/protobuf v1.2.0
github.com/golang/protobuf v1.2.0
github.com/leodido/go-urn v1.1.0 // indirect
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/montanaflynn/stats v0.5.0
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v0.9.2
@ -24,7 +26,6 @@ require (
github.com/samuel/go-zookeeper v0.0.0-20180130194729-c4fab1ac1bec // indirect
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726
github.com/sirupsen/logrus v1.4.1 // indirect
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a
github.com/stretchr/testify v1.3.0
github.com/tsuna/gohbase v0.0.0-20190201102810-d3184c1526df
github.com/urfave/cli v1.20.0
@ -32,7 +33,6 @@ require (
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8
google.golang.org/grpc v1.20.1
gopkg.in/AlecAivazis/survey.v1 v1.8.2
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/go-playground/validator.v9 v9.26.0
)

@ -39,6 +39,8 @@ var (
AppID string
// Color is the identification of different experimental group in one caster cluster.
Color string
// DiscoveryNodes is seed nodes.
DiscoveryNodes string
)
func init() {
@ -57,6 +59,7 @@ func addFlag(fs *flag.FlagSet) {
fs.StringVar(&AppID, "appid", os.Getenv("APP_ID"), "appid is global unique application id, register by service tree. or use APP_ID env variable.")
fs.StringVar(&DeployEnv, "deploy.env", defaultString("DEPLOY_ENV", _deployEnv), "deploy env. or use DEPLOY_ENV env variable, value: dev/fat1/uat/pre/prod etc.")
fs.StringVar(&Color, "deploy.color", os.Getenv("DEPLOY_COLOR"), "deploy.color is the identification of different experimental group.")
fs.StringVar(&DiscoveryNodes, "discovery.nodes", os.Getenv("DISCOVERY_NODES"), "discovery.nodes is seed nodes. value: 127.0.0.1:7171,127.0.0.2:7171 etc.")
}
func defaultString(env, value string) string {

@ -82,8 +82,9 @@ func TestFileEvent(t *testing.T) {
cli, err := NewFile(path)
assert.Nil(t, err)
assert.NotNil(t, cli)
time.Sleep(time.Millisecond * 100)
ch := cli.WatchEvent(context.Background(), "test.toml", "abc.toml")
time.Sleep(time.Millisecond)
time.Sleep(time.Millisecond * 100)
ioutil.WriteFile(path+"test.toml", []byte(`hello`), 0644)
timeout := time.NewTimer(time.Second)
select {
@ -93,16 +94,4 @@ func TestFileEvent(t *testing.T) {
assert.Equal(t, EventUpdate, ev.Event)
assert.Equal(t, "hello", ev.Value)
}
ioutil.WriteFile(path+"abc.toml", []byte(`test`), 0644)
select {
case <-timeout.C:
t.Fatalf("run test timeout")
case ev := <-ch:
assert.Equal(t, EventUpdate, ev.Event)
assert.Equal(t, "test", ev.Value)
}
content1, _ := cli.Get("test.toml").String()
assert.Equal(t, "hello", content1)
content2, _ := cli.Get("abc.toml").String()
assert.Equal(t, "test", content2)
}

@ -16,14 +16,14 @@ var _schema = "tidb://"
func (db *DB) nodeList() (nodes []string) {
var (
insMap map[string][]*naming.Instance
insZone *naming.InstancesInfo
ins []*naming.Instance
ok bool
)
if insMap, ok = db.dis.Fetch(context.Background()); !ok {
if insZone, ok = db.dis.Fetch(context.Background()); !ok {
return
}
if ins, ok = insMap[env.Zone]; !ok || len(ins) == 0 {
if ins, ok = insZone.Instances[env.Zone]; !ok || len(ins) == 0 {
return
}
for _, in := range ins {

@ -5,27 +5,21 @@ import (
"time"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"
"github.com/bilibili/kratos/pkg/ecode/types"
)
func TestEqual(t *testing.T) {
convey.Convey("Equal", t, func(ctx convey.C) {
ctx.Convey("When err1=Error(RequestErr, 'test') and err2=Errorf(RequestErr, 'test')", func(ctx convey.C) {
err1 := Error(RequestErr, "test")
err2 := Errorf(RequestErr, "test")
ctx.Convey("Then err1=err2, err1 != nil", func(ctx convey.C) {
ctx.So(err1, convey.ShouldResemble, err2)
ctx.So(err1, convey.ShouldNotBeNil)
})
})
})
// assert.True(t, OK.Equal(nil))
// assert.True(t, err1.Equal(err2))
// assert.False(t, err1.Equal(nil))
// assert.True(t, Equal(nil, nil))
var (
err1 = Error(RequestErr, "test")
err2 = Errorf(RequestErr, "test")
)
assert.Equal(t, err1, err2)
assert.True(t, OK.Equal(nil))
assert.True(t, err1.Equal(err2))
assert.False(t, err1.Equal(nil))
assert.True(t, Equal(nil, nil))
}
func TestDetail(t *testing.T) {

@ -50,6 +50,8 @@ var (
func init() {
addFlag(flag.CommandLine)
h = newHandlers(nil)
c = new(Config)
}
var (

@ -7,7 +7,6 @@ import (
"fmt"
"math/rand"
"net/url"
"os"
"strconv"
"strings"
"sync"
@ -18,10 +17,7 @@ import (
"github.com/bilibili/kratos/pkg/ecode"
"github.com/bilibili/kratos/pkg/log"
"github.com/bilibili/kratos/pkg/naming"
bm "github.com/bilibili/kratos/pkg/net/http/blademaster"
"github.com/bilibili/kratos/pkg/net/netutil"
"github.com/bilibili/kratos/pkg/net/netutil/breaker"
xstr "github.com/bilibili/kratos/pkg/str"
http "github.com/bilibili/kratos/pkg/net/http/blademaster"
xtime "github.com/bilibili/kratos/pkg/time"
)
@ -30,16 +26,12 @@ const (
_setURL = "http://%s/discovery/set"
_cancelURL = "http://%s/discovery/cancel"
_renewURL = "http://%s/discovery/renew"
_pollURL = "http://%s/discovery/polls"
_nodesURL = "http://%s/discovery/nodes"
_registerGap = 30 * time.Second
_statusUP = "1"
)
const (
_appid = "infra.discovery"
)
@ -51,116 +43,105 @@ var (
ErrDuplication = errors.New("discovery: instance duplicate registration")
)
var (
_once sync.Once
_builder naming.Builder
)
// Builder return default discvoery resolver builder.
func Builder() naming.Builder {
_once.Do(func() {
_builder = New(nil)
})
return _builder
}
// Build register resolver into default discovery.
func Build(id string) naming.Resolver {
return Builder().Build(id)
}
// Config discovery configures.
type Config struct {
Nodes []string
Region string
Zone string
Env string
Host string
}
type appData struct {
ZoneInstances map[string][]*naming.Instance `json:"zone_instances"`
Instances map[string][]*naming.Instance `json:"instances"`
LastTs int64 `json:"latest_timestamp"`
Err string `json:"err"`
}
// Discovery is discovery client.
type Discovery struct {
c *Config
once sync.Once
conf *Config
ctx context.Context
cancelFunc context.CancelFunc
httpClient *bm.Client
httpClient *http.Client
node atomic.Value
nodeIdx uint64
mutex sync.RWMutex
apps map[string]*appInfo
registry map[string]struct{}
lastHost string
cancelPolls context.CancelFunc
idx uint64
node atomic.Value
delete chan *appInfo
}
type appInfo struct {
resolver map[*Resolve]struct{}
zoneIns atomic.Value
resolver map[*Resolver]struct{}
lastTs int64 // latest timestamp
}
func fixConfig(c *Config) {
if len(c.Nodes) == 0 {
c.Nodes = []string{"NOTE: please config a default HOST"}
c.Nodes = strings.Split(env.DiscoveryNodes, ",")
}
if env.Zone != "" {
if c.Region == "" {
c.Region = env.Region
}
if c.Zone == "" {
c.Zone = env.Zone
}
if env.DeployEnv != "" {
if c.Env == "" {
c.Env = env.DeployEnv
}
if env.Hostname != "" {
if c.Host == "" {
c.Host = env.Hostname
} else {
c.Host, _ = os.Hostname()
}
}
var (
once sync.Once
_defaultDiscovery *Discovery
)
func initDefault() {
once.Do(func() {
_defaultDiscovery = New(nil)
})
}
// Builder return default discvoery resolver builder.
func Builder() naming.Builder {
if _defaultDiscovery == nil {
initDefault()
}
return _defaultDiscovery
}
// Build register resolver into default discovery.
func Build(id string) naming.Resolver {
if _defaultDiscovery == nil {
initDefault()
}
return _defaultDiscovery.Build(id)
}
// New new a discovery client.
func New(c *Config) (d *Discovery) {
if c == nil {
c = &Config{}
c = new(Config)
}
fixConfig(c)
ctx, cancel := context.WithCancel(context.Background())
d = &Discovery{
c: c,
ctx: ctx,
cancelFunc: cancel,
conf: c,
apps: map[string]*appInfo{},
registry: map[string]struct{}{},
delete: make(chan *appInfo, 10),
}
// httpClient
cfg := &bm.ClientConfig{
cfg := &http.ClientConfig{
Dial: xtime.Duration(3 * time.Second),
Timeout: xtime.Duration(40 * time.Second),
Breaker: &breaker.Config{
Window: 100,
Sleep: 3,
Bucket: 10,
Ratio: 0.5,
Request: 100,
},
}
d.httpClient = bm.NewClient(cfg)
KeepAlive: xtime.Duration(40 * time.Second),
}
d.httpClient = http.NewClient(cfg)
// discovery self
resolver := d.Build(_appid)
event := resolver.Watch()
_, ok := <-event
@ -169,7 +150,7 @@ func New(c *Config) (d *Discovery) {
}
ins, ok := resolver.Fetch(context.Background())
if ok {
d.newSelf(ins)
d.newSelf(ins.Instances)
}
go d.selfproc(resolver, event)
return
@ -183,13 +164,13 @@ func (d *Discovery) selfproc(resolver naming.Resolver, event <-chan struct{}) {
}
zones, ok := resolver.Fetch(context.Background())
if ok {
d.newSelf(zones)
d.newSelf(zones.Instances)
}
}
}
func (d *Discovery) newSelf(zones map[string][]*naming.Instance) {
ins, ok := zones[d.conf.Zone]
ins, ok := zones[d.c.Zone]
if !ok {
return
}
@ -203,22 +184,22 @@ func (d *Discovery) newSelf(zones map[string][]*naming.Instance) {
}
}
// diff old nodes
olds, ok := d.node.Load().([]string)
if ok {
var diff int
var olds int
for _, n := range nodes {
for _, o := range olds {
if node, ok := d.node.Load().([]string); ok {
for _, o := range node {
if o == n {
diff++
olds++
break
}
}
}
if len(nodes) == diff {
return
}
if len(nodes) == olds {
return
}
rand.Shuffle(len(nodes), func(i, j int) {
// FIXME: we should use rand.Shuffle() in golang 1.10
shuffle(len(nodes), func(i, j int) {
nodes[i], nodes[j] = nodes[j], nodes[i]
})
d.node.Store(nodes)
@ -226,7 +207,7 @@ func (d *Discovery) newSelf(zones map[string][]*naming.Instance) {
// Build disovery resovler builder.
func (d *Discovery) Build(appid string) naming.Resolver {
r := &Resolver{
r := &Resolve{
id: appid,
d: d,
event: make(chan struct{}, 1),
@ -235,7 +216,7 @@ func (d *Discovery) Build(appid string) naming.Resolver {
app, ok := d.apps[appid]
if !ok {
app = &appInfo{
resolver: make(map[*Resolver]struct{}),
resolver: make(map[*Resolve]struct{}),
}
d.apps[appid] = app
cancel := d.cancelPolls
@ -263,32 +244,32 @@ func (d *Discovery) Scheme() string {
return "discovery"
}
// Resolver discveory resolver.
type Resolver struct {
// Resolve discveory resolver.
type Resolve struct {
id string
event chan struct{}
d *Discovery
}
// Watch watch instance.
func (r *Resolver) Watch() <-chan struct{} {
func (r *Resolve) Watch() <-chan struct{} {
return r.event
}
// Fetch fetch resolver instance.
func (r *Resolver) Fetch(c context.Context) (ins map[string][]*naming.Instance, ok bool) {
func (r *Resolve) Fetch(ctx context.Context) (ins *naming.InstancesInfo, ok bool) {
r.d.mutex.RLock()
app, ok := r.d.apps[r.id]
r.d.mutex.RUnlock()
if ok {
ins, ok = app.zoneIns.Load().(map[string][]*naming.Instance)
ins, ok = app.zoneIns.Load().(*naming.InstancesInfo)
return
}
return
}
// Close close resolver.
func (r *Resolver) Close() error {
func (r *Resolve) Close() error {
r.d.mutex.Lock()
if app, ok := r.d.apps[r.id]; ok && len(app.resolver) != 0 {
delete(app.resolver, r)
@ -298,23 +279,11 @@ func (r *Resolver) Close() error {
return nil
}
func (d *Discovery) pickNode() string {
nodes, ok := d.node.Load().([]string)
if !ok || len(nodes) == 0 {
return d.conf.Nodes[d.idx%uint64(len(d.conf.Nodes))]
}
return nodes[d.idx%uint64(len(nodes))]
}
func (d *Discovery) switchNode() {
atomic.AddUint64(&d.idx, 1)
}
// Reload reload the config
func (d *Discovery) Reload(c *Config) {
fixConfig(c)
d.mutex.Lock()
d.conf = c
d.c = c
d.mutex.Unlock()
}
@ -325,7 +294,7 @@ func (d *Discovery) Close() error {
}
// Register Register an instance with discovery and renew automatically
func (d *Discovery) Register(c context.Context, ins *naming.Instance) (cancelFunc context.CancelFunc, err error) {
func (d *Discovery) Register(ctx context.Context, ins *naming.Instance) (cancelFunc context.CancelFunc, err error) {
d.mutex.Lock()
if _, ok := d.registry[ins.AppID]; ok {
err = ErrDuplication
@ -336,13 +305,15 @@ func (d *Discovery) Register(c context.Context, ins *naming.Instance) (cancelFun
if err != nil {
return
}
if err = d.register(c, ins); err != nil {
ctx, cancel := context.WithCancel(d.ctx)
if err = d.register(ctx, ins); err != nil {
d.mutex.Lock()
delete(d.registry, ins.AppID)
d.mutex.Unlock()
cancel()
return
}
ctx, cancel := context.WithCancel(d.ctx)
ch := make(chan struct{}, 1)
cancelFunc = context.CancelFunc(func() {
cancel()
@ -355,10 +326,10 @@ func (d *Discovery) Register(c context.Context, ins *naming.Instance) (cancelFun
select {
case <-ticker.C:
if err := d.renew(ctx, ins); err != nil && ecode.NothingFound.Equal(err) {
d.register(ctx, ins)
_ = d.register(ctx, ins)
}
case <-ctx.Done():
d.cancel(ins)
_ = d.cancel(ins)
ch <- struct{}{}
return
}
@ -367,148 +338,146 @@ func (d *Discovery) Register(c context.Context, ins *naming.Instance) (cancelFun
return
}
// Set set ins status and metadata.
func (d *Discovery) Set(ins *naming.Instance) error {
return d.set(context.Background(), ins)
}
// cancel Remove the registered instance from discovery
func (d *Discovery) cancel(ins *naming.Instance) (err error) {
// register Register an instance with discovery
func (d *Discovery) register(ctx context.Context, ins *naming.Instance) (err error) {
d.mutex.RLock()
conf := d.conf
c := d.c
d.mutex.RUnlock()
var metadata []byte
if ins.Metadata != nil {
if metadata, err = json.Marshal(ins.Metadata); err != nil {
log.Error("discovery:register instance Marshal metadata(%v) failed!error(%v)", ins.Metadata, err)
}
}
res := new(struct {
Code int `json:"code"`
Message string `json:"message"`
})
uri := fmt.Sprintf(_cancelURL, d.pickNode())
params := d.newParams(conf)
uri := fmt.Sprintf(_registerURL, d.pickNode())
params := d.newParams(c)
params.Set("appid", ins.AppID)
// request
if err = d.httpClient.Post(context.Background(), uri, "", params, &res); err != nil {
params.Set("addrs", strings.Join(ins.Addrs, ","))
params.Set("version", ins.Version)
params.Set("status", _statusUP)
params.Set("metadata", string(metadata))
if err = d.httpClient.Post(ctx, uri, "", params, &res); err != nil {
d.switchNode()
log.Error("discovery cancel client.Get(%v) env(%s) appid(%s) hostname(%s) error(%v)",
uri, conf.Env, ins.AppID, conf.Host, err)
log.Error("discovery: register client.Get(%v) zone(%s) env(%s) appid(%s) addrs(%v) error(%v)",
uri, c.Zone, c.Env, ins.AppID, ins.Addrs, err)
return
}
if ec := ecode.Int(res.Code); !ec.Equal(ecode.OK) {
log.Warn("discovery cancel client.Get(%v) env(%s) appid(%s) hostname(%s) code(%v)",
uri, conf.Env, ins.AppID, conf.Host, res.Code)
log.Warn("discovery: register client.Get(%v) env(%s) appid(%s) addrs(%v) code(%v)", uri, c.Env, ins.AppID, ins.Addrs, res.Code)
err = ec
return
}
log.Info("discovery cancel client.Get(%v) env(%s) appid(%s) hostname(%s) success",
uri, conf.Env, ins.AppID, conf.Host)
log.Info("discovery: register client.Get(%v) env(%s) appid(%s) addrs(%s) success", uri, c.Env, ins.AppID, ins.Addrs)
return
}
// register Register an instance with discovery
func (d *Discovery) register(ctx context.Context, ins *naming.Instance) (err error) {
// renew Renew an instance with discovery
func (d *Discovery) renew(ctx context.Context, ins *naming.Instance) (err error) {
d.mutex.RLock()
conf := d.conf
c := d.c
d.mutex.RUnlock()
var metadata []byte
if ins.Metadata != nil {
if metadata, err = json.Marshal(ins.Metadata); err != nil {
log.Error("discovery:register instance Marshal metadata(%v) failed!error(%v)", ins.Metadata, err)
}
}
res := new(struct {
Code int `json:"code"`
Message string `json:"message"`
})
uri := fmt.Sprintf(_registerURL, d.pickNode())
params := d.newParams(conf)
uri := fmt.Sprintf(_renewURL, d.pickNode())
params := d.newParams(c)
params.Set("appid", ins.AppID)
params.Set("addrs", strings.Join(ins.Addrs, ","))
params.Set("version", ins.Version)
params.Set("status", _statusUP)
params.Set("metadata", string(metadata))
if err = d.httpClient.Post(ctx, uri, "", params, &res); err != nil {
d.switchNode()
log.Error("discovery: register client.Get(%v) zone(%s) env(%s) appid(%s) addrs(%v) error(%v)",
uri, conf.Zone, conf.Env, ins.AppID, ins.Addrs, err)
log.Error("discovery: renew client.Get(%v) env(%s) appid(%s) hostname(%s) error(%v)",
uri, c.Env, ins.AppID, c.Host, err)
return
}
if ec := ecode.Int(res.Code); !ec.Equal(ecode.OK) {
log.Warn("discovery: register client.Get(%v) env(%s) appid(%s) addrs(%v) code(%v)",
uri, conf.Env, ins.AppID, ins.Addrs, res.Code)
err = ec
if ec.Equal(ecode.NothingFound) {
return
}
log.Error("discovery: renew client.Get(%v) env(%s) appid(%s) hostname(%s) code(%v)",
uri, c.Env, ins.AppID, c.Host, res.Code)
return
}
log.Info("discovery: register client.Get(%v) env(%s) appid(%s) addrs(%s) success",
uri, conf.Env, ins.AppID, ins.Addrs)
return
}
// rset set instance info with discovery
func (d *Discovery) set(ctx context.Context, ins *naming.Instance) (err error) {
// cancel Remove the registered instance from discovery
func (d *Discovery) cancel(ins *naming.Instance) (err error) {
d.mutex.RLock()
conf := d.conf
c := d.c
d.mutex.RUnlock()
res := new(struct {
Code int `json:"code"`
Message string `json:"message"`
})
uri := fmt.Sprintf(_setURL, d.pickNode())
params := d.newParams(conf)
uri := fmt.Sprintf(_cancelURL, d.pickNode())
params := d.newParams(c)
params.Set("appid", ins.AppID)
params.Set("version", ins.Version)
params.Set("status", strconv.FormatInt(ins.Status, 10))
if ins.Metadata != nil {
var metadata []byte
if metadata, err = json.Marshal(ins.Metadata); err != nil {
log.Error("discovery:set instance Marshal metadata(%v) failed!error(%v)", ins.Metadata, err)
}
params.Set("metadata", string(metadata))
}
if err = d.httpClient.Post(ctx, uri, "", params, &res); err != nil {
// request
if err = d.httpClient.Post(context.TODO(), uri, "", params, &res); err != nil {
d.switchNode()
log.Error("discovery: set client.Get(%v) zone(%s) env(%s) appid(%s) addrs(%v) error(%v)",
uri, conf.Zone, conf.Env, ins.AppID, ins.Addrs, err)
log.Error("discovery cancel client.Get(%v) env(%s) appid(%s) hostname(%s) error(%v)",
uri, c.Env, ins.AppID, c.Host, err)
return
}
if ec := ecode.Int(res.Code); !ec.Equal(ecode.OK) {
log.Warn("discovery: set client.Get(%v) env(%s) appid(%s) addrs(%v) code(%v)",
uri, conf.Env, ins.AppID, ins.Addrs, res.Code)
log.Warn("discovery cancel client.Get(%v) env(%s) appid(%s) hostname(%s) code(%v)",
uri, c.Env, ins.AppID, c.Host, res.Code)
err = ec
return
}
log.Info("discovery: set client.Get(%v) env(%s) appid(%s) addrs(%s) success",
uri+"?"+params.Encode(), conf.Env, ins.AppID, ins.Addrs)
log.Info("discovery cancel client.Get(%v) env(%s) appid(%s) hostname(%s) success",
uri, c.Env, ins.AppID, c.Host)
return
}
// renew Renew an instance with discovery
func (d *Discovery) renew(ctx context.Context, ins *naming.Instance) (err error) {
// Set set ins status and metadata.
func (d *Discovery) Set(ins *naming.Instance) error {
return d.set(context.Background(), ins)
}
// set set instance info with discovery
func (d *Discovery) set(ctx context.Context, ins *naming.Instance) (err error) {
d.mutex.RLock()
conf := d.conf
conf := d.c
d.mutex.RUnlock()
res := new(struct {
Code int `json:"code"`
Message string `json:"message"`
})
uri := fmt.Sprintf(_renewURL, d.pickNode())
uri := fmt.Sprintf(_setURL, d.pickNode())
params := d.newParams(conf)
params.Set("appid", ins.AppID)
params.Set("version", ins.Version)
params.Set("status", _statusUP)
if ins.Metadata != nil {
var metadata []byte
if metadata, err = json.Marshal(ins.Metadata); err != nil {
log.Error("discovery:set instance Marshal metadata(%v) failed!error(%v)", ins.Metadata, err)
return
}
params.Set("metadata", string(metadata))
}
if err = d.httpClient.Post(ctx, uri, "", params, &res); err != nil {
d.switchNode()
log.Error("discovery: renew client.Get(%v) env(%s) appid(%s) hostname(%s) error(%v)",
uri, conf.Env, ins.AppID, conf.Host, err)
log.Error("discovery: set client.Get(%v) zone(%s) env(%s) appid(%s) addrs(%v) error(%v)",
uri, conf.Zone, conf.Env, ins.AppID, ins.Addrs, err)
return
}
if ec := ecode.Int(res.Code); !ec.Equal(ecode.OK) {
log.Warn("discovery: set client.Get(%v) env(%s) appid(%s) addrs(%v) code(%v)",
uri, conf.Env, ins.AppID, ins.Addrs, res.Code)
err = ec
if ec.Equal(ecode.NothingFound) {
return
}
log.Error("discovery: renew client.Get(%v) env(%s) appid(%s) hostname(%s) code(%v)",
uri, conf.Env, ins.AppID, conf.Host, res.Code)
return
}
log.Info("discovery: set client.Get(%v) env(%s) appid(%s) addrs(%s) success", uri+"?"+params.Encode(), conf.Env, ins.AppID, ins.Addrs)
return
}
@ -518,7 +487,6 @@ func (d *Discovery) serverproc() {
ctx context.Context
cancel context.CancelFunc
)
bc := netutil.DefaultBackoffConfig
ticker := time.NewTicker(time.Minute * 30)
defer ticker.Stop()
for {
@ -531,16 +499,17 @@ func (d *Discovery) serverproc() {
select {
case <-d.ctx.Done():
return
case <-ticker.C:
default:
}
apps, err := d.polls(ctx, d.pickNode())
apps, err := d.polls(ctx)
if err != nil {
d.switchNode()
if ctx.Err() == context.Canceled {
ctx = nil
continue
}
time.Sleep(bc.Backoff(retry))
time.Sleep(time.Second)
retry++
continue
}
@ -549,38 +518,23 @@ func (d *Discovery) serverproc() {
}
}
func (d *Discovery) nodes() (nodes []string) {
res := new(struct {
Code int `json:"code"`
Data []struct {
Addr string `json:"addr"`
} `json:"data"`
})
uri := fmt.Sprintf(_nodesURL, d.pickNode())
if err := d.httpClient.Get(d.ctx, uri, "", nil, res); err != nil {
d.switchNode()
log.Error("discovery: consumer client.Get(%v)error(%+v)", uri, err)
return
}
if ec := ecode.Int(res.Code); !ec.Equal(ecode.OK) {
log.Error("discovery: consumer client.Get(%v) error(%v)", uri, res.Code)
return
}
if len(res.Data) == 0 {
log.Warn("discovery: get nodes(%s) failed,no nodes found!", uri)
return
func (d *Discovery) pickNode() string {
nodes, ok := d.node.Load().([]string)
if !ok || len(nodes) == 0 {
return d.c.Nodes[rand.Intn(len(d.c.Nodes))]
}
nodes = make([]string, 0, len(res.Data))
for i := range res.Data {
nodes = append(nodes, res.Data[i].Addr)
return nodes[atomic.LoadUint64(&d.nodeIdx)%uint64(len(nodes))]
}
return
func (d *Discovery) switchNode() {
atomic.AddUint64(&d.nodeIdx, 1)
}
func (d *Discovery) polls(ctx context.Context, host string) (apps map[string]appData, err error) {
func (d *Discovery) polls(ctx context.Context) (apps map[string]*naming.InstancesInfo, err error) {
var (
lastTs []int64
appid []string
lastTss []int64
appIDs []string
host = d.pickNode()
changed bool
)
if host != d.lastHost {
@ -588,46 +542,41 @@ func (d *Discovery) polls(ctx context.Context, host string) (apps map[string]app
changed = true
}
d.mutex.RLock()
conf := d.conf
c := d.c
for k, v := range d.apps {
if changed {
v.lastTs = 0
}
appid = append(appid, k)
lastTs = append(lastTs, v.lastTs)
appIDs = append(appIDs, k)
lastTss = append(lastTss, v.lastTs)
}
d.mutex.RUnlock()
if len(appid) == 0 {
if len(appIDs) == 0 {
return
}
uri := fmt.Sprintf(_pollURL, host)
res := new(struct {
Code int `json:"code"`
Message string `json:"message"`
Data map[string]appData `json:"data"`
Data map[string]*naming.InstancesInfo `json:"data"`
})
params := url.Values{}
params.Set("env", conf.Env)
params.Set("hostname", conf.Host)
params.Set("appid", strings.Join(appid, ","))
params.Set("latest_timestamp", xstr.JoinInts(lastTs))
params.Set("env", c.Env)
params.Set("hostname", c.Host)
for _, appid := range appIDs {
params.Add("appid", appid)
}
for _, ts := range lastTss {
params.Add("latest_timestamp", strconv.FormatInt(ts, 10))
}
if err = d.httpClient.Get(ctx, uri, "", params, res); err != nil {
d.switchNode()
log.Error("discovery: client.Get(%s) error(%+v)", uri+"?"+params.Encode(), err)
return
}
if ec := ecode.Int(res.Code); !ec.Equal(ecode.OK) {
if !ec.Equal(ecode.NotModified) {
log.Error("discovery: client.Get(%s) get error code(%d) message(%s)", uri+"?"+params.Encode(), res.Code, res.Message)
log.Error("discovery: client.Get(%s) get error code(%d)", uri+"?"+params.Encode(), res.Code)
err = ec
if ec.Equal(ecode.NothingFound) {
for appID, value := range res.Data {
if value.Err != "" {
errInfo := fmt.Sprintf("discovery: app(%s) on ENV(%s) %s!\n", appID, conf.Env, value.Err)
log.Error(errInfo)
fmt.Fprintf(os.Stderr, errInfo)
}
}
}
}
return
}
@ -639,18 +588,17 @@ func (d *Discovery) polls(ctx context.Context, host string) (apps map[string]app
return
}
}
log.Info("discovery: polls uri(%s)", uri+"?"+params.Encode())
log.Info("discovery: successfully polls(%s) instances (%s)", uri+"?"+params.Encode(), info)
apps = res.Data
return
}
func (d *Discovery) broadcast(apps map[string]appData) {
for id, v := range apps {
func (d *Discovery) broadcast(apps map[string]*naming.InstancesInfo) {
for appID, v := range apps {
var count int
for zone, ins := range v.ZoneInstances {
for zone, ins := range v.Instances {
if len(ins) == 0 {
delete(v.ZoneInstances, zone)
delete(v.Instances, zone)
}
count += len(ins)
}
@ -658,11 +606,11 @@ func (d *Discovery) broadcast(apps map[string]appData) {
continue
}
d.mutex.RLock()
app, ok := d.apps[id]
app, ok := d.apps[appID]
d.mutex.RUnlock()
if ok {
app.lastTs = v.LastTs
app.zoneIns.Store(v.ZoneInstances)
app.zoneIns.Store(v)
d.mutex.RLock()
for rs := range app.resolver {
select {
@ -675,10 +623,38 @@ func (d *Discovery) broadcast(apps map[string]appData) {
}
}
func (d *Discovery) newParams(conf *Config) url.Values {
func (d *Discovery) newParams(c *Config) url.Values {
params := url.Values{}
params.Set("zone", conf.Zone)
params.Set("env", conf.Env)
params.Set("hostname", conf.Host)
params.Set("region", c.Region)
params.Set("zone", c.Zone)
params.Set("env", c.Env)
params.Set("hostname", c.Host)
return params
}
var r = rand.New(rand.NewSource(time.Now().UnixNano()))
// shuffle pseudo-randomizes the order of elements.
// n is the number of elements. Shuffle panics if n < 0.
// swap swaps the elements with indexes i and j.
func shuffle(n int, swap func(i, j int)) {
if n < 0 {
panic("invalid argument to Shuffle")
}
// Fisher-Yates shuffle: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
// Shuffle really ought not be called with n that doesn't fit in 32 bits.
// Not only will it take a very long time, but with 2³¹! possible permutations,
// there's no way that any PRNG can have a big enough internal state to
// generate even a minuscule percentage of the possible permutations.
// Nevertheless, the right API signature accepts an int n, so handle it as best we can.
i := n - 1
for ; i > 1<<31-1-1; i-- {
j := int(r.Int63n(int64(i + 1)))
swap(i, j)
}
for ; i > 0; i-- {
j := int(r.Int31n(int32(i + 1)))
swap(i, j)
}
}

@ -2,27 +2,30 @@ package naming
import (
"context"
"strconv"
)
// metadata common key
const (
MetaZone = "zone"
MetaCluster = "cluster"
MetaWeight = "weight"
MetaCluster = "cluster"
MetaZone = "zone"
MetaColor = "color"
)
// Instance represents a server the client connects to.
type Instance struct {
// Region is region.
Region string `json:"region"`
// Zone is IDC.
Zone string `json:"zone"`
// Env prod/pre/uat/fat1
// Env prod/preuat/fat1
Env string `json:"env"`
// AppID is mapping servicetree appid.
AppID string `json:"appid"`
// Hostname is hostname from docker.
Hostname string `json:"hostname"`
// Addrs is the adress of app instance
// Addrs is the address of app instance
// format: scheme://host
Addrs []string `json:"addrs"`
// Version is publishing version.
@ -32,20 +35,18 @@ type Instance struct {
// Metadata is the information associated with Addr, which may be used
// to make load balancing decision.
Metadata map[string]string `json:"metadata"`
// Status status
Status int64
}
// Resolver resolve naming service
type Resolver interface {
Fetch(context.Context) (map[string][]*Instance, bool)
Fetch(context.Context) (*InstancesInfo, bool)
Watch() <-chan struct{}
Close() error
}
// Registry Register an instance and renew automatically
// Registry Register an instance and renew automatically.
type Registry interface {
Register(context.Context, *Instance) (context.CancelFunc, error)
Register(ctx context.Context, ins *Instance) (cancel context.CancelFunc, err error)
Close() error
}
@ -54,3 +55,77 @@ type Builder interface {
Build(id string) Resolver
Scheme() string
}
// InstancesInfo instance info.
type InstancesInfo struct {
Instances map[string][]*Instance `json:"instances"`
LastTs int64 `json:"latest_timestamp"`
Scheduler []Zone `json:"scheduler"`
}
// Zone zone scheduler info.
type Zone struct {
Src string `json:"src"`
Dst map[string]int64 `json:"dst"`
}
// UseScheduler use scheduler info on instances.
// if instancesInfo contains scheduler info about zone,
// return releated zone's instances weighted by scheduler.
// if not,only zone instances be returned.
func (insInf *InstancesInfo) UseScheduler(zone string) (inss []*Instance) {
var scheduler struct {
zone []string
weights []int64
}
var oriWeights []int64
for _, sch := range insInf.Scheduler {
if sch.Src == zone {
for zone, schWeight := range sch.Dst {
if zins, ok := insInf.Instances[zone]; ok {
var totalWeight int64
for _, ins := range zins {
var weight int64
if weight, _ = strconv.ParseInt(ins.Metadata[MetaWeight], 10, 64); weight <= 0 {
weight = 10
}
totalWeight += weight
}
oriWeights = append(oriWeights, totalWeight)
inss = append(inss, zins...)
}
scheduler.weights = append(scheduler.weights, schWeight)
scheduler.zone = append(scheduler.zone, zone)
}
}
}
if len(inss) == 0 {
var ok bool
if inss, ok = insInf.Instances[zone]; ok {
return
}
for _, v := range insInf.Instances {
inss = append(inss, v...)
}
return
}
var comMulti int64 = 1
for _, weigth := range oriWeights {
comMulti *= weigth
}
var fixWeight = make(map[string]int64, len(scheduler.weights))
for i, zone := range scheduler.zone {
fixWeight[zone] = scheduler.weights[i] * comMulti / oriWeights[i]
}
for _, ins := range inss {
var weight int64
if weight, _ = strconv.ParseInt(ins.Metadata[MetaWeight], 10, 64); weight <= 0 {
weight = 10
}
if fix, ok := fixWeight[ins.Zone]; ok {
weight = weight * fix
}
ins.Metadata[MetaWeight] = strconv.FormatInt(weight, 10)
}
return
}

@ -43,6 +43,10 @@ var (
_defaultClient *Client
)
func init() {
resolver.Register(discovery.New(nil))
}
func baseMetadata() metadata.MD {
gmd := metadata.MD{nmd.Caller: []string{env.AppID}}
if env.Color != "" {

@ -77,7 +77,7 @@ func main() {
if file != "" {
content, err := ioutil.ReadFile(file)
if err != nil {
fmt.Println("ioutil.ReadFile %s failed!err:=%v", file, err)
fmt.Printf("ioutil.ReadFile(%s) error(%v)\n", file, err)
os.Exit(1)
}
if len(content) > 0 {

@ -140,24 +140,24 @@ func TestToEcode(t *testing.T) {
t.Run("input pb.Error and ecode.Status", func(t *testing.T) {
gst := status.New(codes.InvalidArgument, "requesterr")
gst, _ = gst.WithDetails(
&pb.Error{ErrCode: 1122, ErrMessage: "message"},
ecode.Errorf(ecode.AccessKeyErr, "AccessKeyErr").Proto(),
&pb.Error{ErrCode: 401, ErrMessage: "message"},
ecode.Errorf(ecode.Unauthorized, "Unauthorized").Proto(),
)
ec := ToEcode(gst)
assert.Equal(t, int(ecode.AccessKeyErr), ec.Code())
assert.Equal(t, "AccessKeyErr", ec.Message())
assert.Equal(t, int(ecode.Unauthorized), ec.Code())
assert.Equal(t, "Unauthorized", ec.Message())
})
t.Run("input encode.Status", func(t *testing.T) {
m := &timestamp.Timestamp{Seconds: time.Now().Unix()}
st, _ := ecode.Errorf(ecode.AccessKeyErr, "AccessKeyErr").WithDetails(m)
st, _ := ecode.Errorf(ecode.Unauthorized, "Unauthorized").WithDetails(m)
gst := status.New(codes.InvalidArgument, "requesterr")
gst, _ = gst.WithDetails(st.Proto())
ec := ToEcode(gst)
assert.Equal(t, int(ecode.AccessKeyErr), ec.Code())
assert.Equal(t, "AccessKeyErr", ec.Message())
assert.Equal(t, int(ecode.Unauthorized), ec.Code())
assert.Equal(t, "Unauthorized", ec.Message())
assert.Len(t, ec.Details(), 1)
assert.IsType(t, m, ec.Details()[0])
})

@ -43,21 +43,20 @@ func (d *Direct) Scheme() string {
return Name
}
// Watch a tree
// Watch a tree.
func (d *Direct) Watch() <-chan struct{} {
ch := make(chan struct{}, 1)
ch <- struct{}{}
return ch
}
//Unwatch a tree
// Unwatch a tree.
func (d *Direct) Unwatch(id string) {
}
//Fetch fetch isntances
func (d *Direct) Fetch(ctx context.Context) (insMap map[string][]*naming.Instance, found bool) {
//Fetch fetch isntances.
func (d *Direct) Fetch(ctx context.Context) (res *naming.InstancesInfo, found bool) {
var ins []*naming.Instance
addrs := strings.Split(d.id, ",")
for _, addr := range addrs {
ins = append(ins, &naming.Instance{
@ -67,7 +66,9 @@ func (d *Direct) Fetch(ctx context.Context) (insMap map[string][]*naming.Instanc
if len(ins) > 0 {
found = true
}
insMap = map[string][]*naming.Instance{env.Zone: ins}
res = &naming.InstancesInfo{
Instances: map[string][]*naming.Instance{env.Zone: ins},
}
return
}

@ -16,7 +16,7 @@ import (
"github.com/bilibili/kratos/pkg/naming"
wmeta "github.com/bilibili/kratos/pkg/net/rpc/warden/internal/metadata"
"github.com/dgryski/go-farm"
farm "github.com/dgryski/go-farm"
"github.com/pkg/errors"
"google.golang.org/grpc/resolver"
)
@ -128,20 +128,22 @@ func (r *Resolver) updateproc() {
return
}
}
if insMap, ok := r.nr.Fetch(context.Background()); ok {
instances, ok := insMap[r.zone]
if ins, ok := r.nr.Fetch(context.Background()); ok {
instances, ok := ins.Instances[r.zone]
if !ok {
for _, value := range insMap {
for _, value := range ins.Instances {
instances = append(instances, value...)
}
}
if r.subsetSize > 0 && len(instances) > 0 {
instances = r.subset(instances, env.Hostname, r.subsetSize)
}
if len(instances) > 0 {
r.newAddress(instances)
}
}
}
}
func (r *Resolver) subset(backends []*naming.Instance, clientID string, size int64) []*naming.Instance {
if len(backends) <= int(size) {

@ -2,6 +2,7 @@ package resolver
import (
"context"
"github.com/bilibili/kratos/pkg/conf/env"
"github.com/bilibili/kratos/pkg/naming"
)
@ -25,19 +26,18 @@ func (mb *mockDiscoveryBuilder) Scheme() string {
}
type mockDiscoveryResolver struct {
//instances map[string]*naming.Instance
d *mockDiscoveryBuilder
watchch chan struct{}
}
var _ naming.Resolver = &mockDiscoveryResolver{}
func (md *mockDiscoveryResolver) Fetch(ctx context.Context) (map[string][]*naming.Instance, bool) {
func (md *mockDiscoveryResolver) Fetch(ctx context.Context) (*naming.InstancesInfo, bool) {
zones := make(map[string][]*naming.Instance)
for _, v := range md.d.instances {
zones[v.Zone] = append(zones[v.Zone], v)
}
return zones, len(zones) > 0
return &naming.InstancesInfo{Instances: zones}, len(zones) > 0
}
func (md *mockDiscoveryResolver) Watch() <-chan struct{} {

@ -168,7 +168,8 @@ func TestErrorResolver(t *testing.T) {
resetCount()
}
func TestClusterResolver(t *testing.T) {
// FIXME
func testClusterResolver(t *testing.T) {
mockResolver := newMockDiscoveryBuilder()
resolver.Set(mockResolver)
mockResolver.registry(testAppID, "server1", "127.0.0.1:18081", map[string]string{"cluster": "c1"})
@ -200,7 +201,8 @@ func TestClusterResolver(t *testing.T) {
resetCount()
}
func TestNoClusterResolver(t *testing.T) {
// FIXME
func testNoClusterResolver(t *testing.T) {
mockResolver := newMockDiscoveryBuilder()
resolver.Set(mockResolver)
mockResolver.registry(testAppID, "server1", "127.0.0.1:18081", map[string]string{"cluster": "c1"})
@ -263,7 +265,8 @@ func TestZoneResolver(t *testing.T) {
resetCount()
}
func TestSubsetConn(t *testing.T) {
// FIXME
func testSubsetConn(t *testing.T) {
mockResolver := newMockDiscoveryBuilder()
resolver.Set(mockResolver)
mockResolver.registry(testAppID, "server1", "127.0.0.1:18081", map[string]string{})

@ -196,8 +196,8 @@ func Test_Warden(t *testing.T) {
xtrace.Init(&xtrace.Config{Addr: "127.0.0.1:9982", Timeout: xtime.Duration(time.Second * 3)})
go _testOnce.Do(runServer(t))
go runClient(context.Background(), &clientConfig, t, "trace_test", 0)
testTrace(t, 9982, false)
testInterceptorChain(t)
//testTrace(t, 9982, false)
//testInterceptorChain(t)
testValidation(t)
testServerRecovery(t)
testClientRecovery(t)
@ -243,7 +243,7 @@ func testAllErrorCase(t *testing.T) {
ec := ecode.Cause(err)
assert.Equal(t, ecode.Conflict.Code(), ec.Code())
// remove this assert in future
assert.Equal(t, "20024", ec.Message())
assert.Equal(t, "-409", ec.Message())
})
t.Run("pb_error_error", func(t *testing.T) {
_, err := runClient(ctx, &clientConfig, t, "pb_error_error", 0)

@ -91,6 +91,7 @@ func create() (err error) {
tpls[_tplTypeGRPCToml] = _tplGRPCToml
tpls[_tplTypeService] = _tplGPRCService
} else {
tpls[_tplTypeHTTPServer] = delgrpc(_tplHTTPServer)
tpls[_tplTypeService] = _tplService
tpls[_tplTypeMain] = delgrpc(_tplMain)
}
@ -134,6 +135,9 @@ func delgrpc(tpl string) string {
if strings.Contains(l, "warden") {
continue
}
if strings.Contains(l, "pb") {
continue
}
buf.WriteString(l)
buf.WriteString("\n")
}

Loading…
Cancel
Save