Skip to content

Commit 895e035

Browse files
authored
Merge pull request #33 from bilibili/v1.1
V1.1
2 parents f49d4a1 + 655e8a4 commit 895e035

27 files changed

+510
-604
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
#### Discovery
2+
##### Version 1.1.0
3+
> 1. use kratos pkg
4+
> 2. replace gin by kratos/bm
5+
> 3. fix poll return nil when be canceled.
6+
> 4. add init protect mode
7+
> 5. supoort set.
8+
29
##### Version 1.0.2
310
> 1.fix nodesproc. get all zone nodes.
411

cmd/discovery/discovery-example.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# 同一discovery集群的所有node节点地址,包含本node
33
nodes = ["127.0.0.1:7171"]
4-
4+
enableprotect=false
55
# 本可用区zone(一般指机房)标识
66
[env]
77
region = "sh"
@@ -19,10 +19,12 @@ DeployEnv = "dev"
1919
# 注意:ip别配置为0.0.0.0或者127.0.0.1
2020
[httpServer]
2121
addr = "127.0.0.1:7171"
22+
timeout="40s"
2223

2324
# 当前节点同步其他节点使用的http client
2425
# dial 连接建立超时时间
2526
# keepAlive 连接复用保持时间
2627
[httpClient]
2728
dial = "1s"
2829
keepAlive = "120s"
30+
timeout="40s"

cmd/discovery/main.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,29 @@ import (
1010
"github.com/bilibili/discovery/conf"
1111
"github.com/bilibili/discovery/discovery"
1212
"github.com/bilibili/discovery/http"
13-
14-
log "github.com/golang/glog"
13+
log "github.com/bilibili/kratos/pkg/log"
1514
)
1615

1716
func main() {
1817
flag.Parse()
1918
if err := conf.Init(); err != nil {
20-
log.Errorf("conf.Init() error(%v)", err)
19+
log.Error("conf.Init() error(%v)", err)
2120
panic(err)
2221
}
22+
log.Init(conf.Conf.Log)
2323
dis, cancel := discovery.New(conf.Conf)
2424
http.Init(conf.Conf, dis)
2525
// init signal
2626
c := make(chan os.Signal, 1)
2727
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
2828
for {
2929
s := <-c
30-
log.Infof("discovery get a signal %s", s.String())
30+
log.Info("discovery get a signal %s", s.String())
3131
switch s {
3232
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
3333
cancel()
3434
time.Sleep(time.Second)
3535
log.Info("discovery quit !!!")
36-
log.Flush()
3736
return
3837
case syscall.SIGHUP:
3938
default:

conf/conf.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"io/ioutil"
66
"os"
77

8-
"github.com/bilibili/discovery/lib/http"
9-
108
"github.com/BurntSushi/toml"
9+
log "github.com/bilibili/kratos/pkg/log"
10+
http "github.com/bilibili/kratos/pkg/net/http/blademaster"
1111
)
1212

1313
var (
@@ -27,21 +27,20 @@ func init() {
2727
hostname = os.Getenv("HOSTNAME")
2828
}
2929
flag.StringVar(&confPath, "conf", "discovery-example.toml", "config path")
30-
flag.StringVar(&region, "region", os.Getenv("REGION"), "avaliable region. or use REGION env variable, value: sh etc.")
31-
flag.StringVar(&zone, "zone", os.Getenv("ZONE"), "avaliable zone. or use ZONE env variable, value: sh001/sh002 etc.")
32-
flag.StringVar(&deployEnv, "deploy.env", os.Getenv("DEPLOY_ENV"), "deploy env. or use DEPLOY_ENV env variable, value: dev/fat1/uat/pre/prod etc.")
3330
flag.StringVar(&hostname, "hostname", hostname, "machine hostname")
3431
flag.StringVar(&schedulerPath, "scheduler", "scheduler.json", "scheduler info")
3532
}
3633

3734
// Config config.
3835
type Config struct {
39-
Nodes []string
40-
Zones map[string][]string
41-
HTTPServer *ServerConfig
42-
HTTPClient *http.ClientConfig
43-
Env *Env
44-
Scheduler []byte
36+
Nodes []string
37+
Zones map[string][]string
38+
HTTPServer *http.ServerConfig
39+
HTTPClient *http.ClientConfig
40+
Env *Env
41+
Log *log.Config
42+
Scheduler []byte
43+
EnableProtect bool
4544
}
4645

4746
// Fix fix env config.
@@ -72,11 +71,6 @@ type Env struct {
7271
DeployEnv string
7372
}
7473

75-
// ServerConfig Http Servers conf.
76-
type ServerConfig struct {
77-
Addr string
78-
}
79-
8074
// Init init conf
8175
func Init() (err error) {
8276
if _, err = toml.DecodeFile(confPath, &Conf); err != nil {

discovery/discovery.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,39 @@ package discovery
33
import (
44
"context"
55
"sync/atomic"
6+
"time"
67

78
"github.com/bilibili/discovery/conf"
8-
"github.com/bilibili/discovery/lib/http"
99
"github.com/bilibili/discovery/registry"
10+
http "github.com/bilibili/kratos/pkg/net/http/blademaster"
1011
)
1112

1213
// Discovery discovery.
1314
type Discovery struct {
14-
c *conf.Config
15-
client *http.Client
16-
registry *registry.Registry
17-
nodes atomic.Value
15+
c *conf.Config
16+
protected bool
17+
client *http.Client
18+
registry *registry.Registry
19+
nodes atomic.Value
1820
}
1921

2022
// New get a discovery.
2123
func New(c *conf.Config) (d *Discovery, cancel context.CancelFunc) {
2224
d = &Discovery{
23-
c: c,
24-
client: http.NewClient(c.HTTPClient),
25-
registry: registry.NewRegistry(c),
25+
protected: c.EnableProtect,
26+
c: c,
27+
client: http.NewClient(c.HTTPClient),
28+
registry: registry.NewRegistry(c),
2629
}
2730
d.nodes.Store(registry.NewNodes(c))
2831
d.syncUp()
2932
cancel = d.regSelf()
3033
go d.nodesproc()
34+
go d.exitProtect()
3135
return
3236
}
37+
38+
func (d *Discovery) exitProtect() {
39+
time.Sleep(time.Second * 90)
40+
d.protected = false
41+
}

discovery/register.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,37 @@ package discovery
33
import (
44
"context"
55

6-
"github.com/bilibili/discovery/errors"
76
"github.com/bilibili/discovery/model"
87
"github.com/bilibili/discovery/registry"
8+
"github.com/bilibili/kratos/pkg/ecode"
99

10-
log "github.com/golang/glog"
10+
log "github.com/bilibili/kratos/pkg/log"
1111
)
1212

1313
// Register a new instance.
14-
func (d *Discovery) Register(c context.Context, ins *model.Instance, latestTimestamp int64, replication bool) {
14+
func (d *Discovery) Register(c context.Context, ins *model.Instance, latestTimestamp int64, replication bool, fromzone bool) {
1515
_ = d.registry.Register(ins, latestTimestamp)
1616
if !replication {
17-
_ = d.nodes.Load().(*registry.Nodes).Replicate(c, model.Register, ins, ins.Zone != d.c.Env.Zone)
17+
_ = d.nodes.Load().(*registry.Nodes).Replicate(c, model.Register, ins, fromzone)
1818
}
1919
}
2020

2121
// Renew marks the given instance of the given app name as renewed, and also marks whether it originated from replication.
2222
func (d *Discovery) Renew(c context.Context, arg *model.ArgRenew) (i *model.Instance, err error) {
2323
i, ok := d.registry.Renew(arg)
2424
if !ok {
25-
err = errors.NothingFound
26-
log.Errorf("renew appid(%s) hostname(%s) zone(%s) env(%s) error", arg.AppID, arg.Hostname, arg.Zone, arg.Env)
25+
err = ecode.NothingFound
26+
log.Error("renew appid(%s) hostname(%s) zone(%s) env(%s) error", arg.AppID, arg.Hostname, arg.Zone, arg.Env)
2727
return
2828
}
2929
if !arg.Replication {
3030
_ = d.nodes.Load().(*registry.Nodes).Replicate(c, model.Renew, i, arg.Zone != d.c.Env.Zone)
3131
return
3232
}
3333
if arg.DirtyTimestamp > i.DirtyTimestamp {
34-
err = errors.NothingFound
34+
err = ecode.NothingFound
3535
} else if arg.DirtyTimestamp < i.DirtyTimestamp {
36-
err = errors.Conflict
36+
err = ecode.Conflict
3737
}
3838
return
3939
}
@@ -42,8 +42,8 @@ func (d *Discovery) Renew(c context.Context, arg *model.ArgRenew) (i *model.Inst
4242
func (d *Discovery) Cancel(c context.Context, arg *model.ArgCancel) (err error) {
4343
i, ok := d.registry.Cancel(arg)
4444
if !ok {
45-
err = errors.NothingFound
46-
log.Errorf("cancel appid(%s) hostname(%s) error", arg.AppID, arg.Hostname)
45+
err = ecode.NothingFound
46+
log.Error("cancel appid(%s) hostname(%s) error", arg.AppID, arg.Hostname)
4747
return
4848
}
4949
if !arg.Replication {
@@ -68,7 +68,7 @@ func (d *Discovery) Fetchs(c context.Context, arg *model.ArgFetchs) (is map[stri
6868
for _, appid := range arg.AppID {
6969
i, err := d.registry.Fetch(arg.Zone, arg.Env, appid, 0, arg.Status)
7070
if err != nil {
71-
log.Errorf("Fetchs fetch appid(%v) err", err)
71+
log.Error("Fetchs fetch appid(%v) err", err)
7272
continue
7373
}
7474
is[appid] = i
@@ -77,7 +77,7 @@ func (d *Discovery) Fetchs(c context.Context, arg *model.ArgFetchs) (is map[stri
7777
}
7878

7979
// Polls hangs request and then write instances when that has changes, or return NotModified.
80-
func (d *Discovery) Polls(c context.Context, arg *model.ArgPolls) (ch chan map[string]*model.InstanceInfo, new bool, err error) {
80+
func (d *Discovery) Polls(c context.Context, arg *model.ArgPolls) (ch chan map[string]*model.InstanceInfo, new bool, miss string, err error) {
8181
return d.registry.Polls(arg)
8282
}
8383

@@ -94,7 +94,10 @@ func (d *Discovery) Nodes(c context.Context) (nsi []*model.Node) {
9494
// Set set metadata,color,status of instance.
9595
func (d *Discovery) Set(c context.Context, arg *model.ArgSet) (err error) {
9696
if !d.registry.Set(arg) {
97-
err = errors.ParamsErr
97+
err = ecode.RequestErr
98+
}
99+
if !arg.Replication {
100+
d.nodes.Load().(*registry.Nodes).ReplicateSet(c, arg, arg.FromZone)
98101
}
99102
return
100103
}

0 commit comments

Comments
 (0)