Skip to content

Commit f3ccb8b

Browse files
authored
Merge pull request #405 from weibocom/dev
merge dev & release
2 parents a6f5d1c + b035daf commit f3ccb8b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+3099
-468
lines changed

.github/workflows/test.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
testing:
2020
strategy:
2121
matrix:
22-
go-version: [1.12.x,1.13.x,1.14.x,1.15.x,1.16.x,1.17.x,1.18.x,1.19.x,1.20.x,1.21.x]
22+
go-version: [1.16.x,1.17.x,1.18.x,1.19.x,1.20.x,1.21.x]
2323
platform: [ubuntu-latest]
2424
runs-on: ${{ matrix.platform }}
2525
steps:
@@ -45,10 +45,10 @@ jobs:
4545
name: codecov
4646
runs-on: ubuntu-latest
4747
steps:
48-
- name: Set up Go 1.15
48+
- name: Set up Go 1.16
4949
uses: actions/setup-go@v3
5050
with:
51-
go-version: 1.15.x
51+
go-version: 1.16.x
5252
id: go
5353

5454
- name: Checkout code

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ zj*
2424
# gdb
2525
*.gdb_history
2626
__*
27+
*.log
2728

2829
# motan-go
2930
*.pid
@@ -40,4 +41,4 @@ main/magent*
4041
log/log.test*
4142
go.sum
4243
agent_runtime
43-
test/
44+
test/

agent.go

+87-27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"github.com/weibocom/motan-go/endpoint"
77
vlog "github.com/weibocom/motan-go/log"
8+
"github.com/weibocom/motan-go/meta"
9+
"github.com/weibocom/motan-go/provider"
810
"gopkg.in/yaml.v2"
911
"io/ioutil"
1012
"net"
@@ -193,6 +195,8 @@ func (a *Agent) StartMotanAgentFromConfig(config *cfg.Config) {
193195
return
194196
}
195197
fmt.Println("init agent context success.")
198+
// initialize meta package
199+
meta.Initialize(a.Context)
196200
a.initParam()
197201
a.SetSanpshotConf()
198202
a.initAgentURL()
@@ -225,7 +229,7 @@ func (a *Agent) StartMotanAgentFromConfig(config *cfg.Config) {
225229

226230
func (a *Agent) startRegistryFailback() {
227231
vlog.Infoln("start agent failback")
228-
ticker := time.NewTicker(registry.DefaultFailbackInterval * time.Millisecond)
232+
ticker := time.NewTicker(time.Duration(registry.GetFailbackInterval()) * time.Millisecond)
229233
defer ticker.Stop()
230234
for range ticker.C {
231235
a.registryLock.Lock()
@@ -265,22 +269,8 @@ func (a *Agent) GetRegistryStatus() []map[string]*motan.RegistryStatus {
265269
}
266270

267271
func (a *Agent) registerStatusSampler() {
268-
metrics.RegisterStatusSampleFunc("memory", func() int64 {
269-
p, _ := process.NewProcess(int32(os.Getpid()))
270-
memInfo, err := p.MemoryInfo()
271-
if err != nil {
272-
return 0
273-
}
274-
return int64(memInfo.RSS >> 20)
275-
})
276-
metrics.RegisterStatusSampleFunc("cpu", func() int64 {
277-
p, _ := process.NewProcess(int32(os.Getpid()))
278-
cpuPercent, err := p.CPUPercent()
279-
if err != nil {
280-
return 0
281-
}
282-
return int64(cpuPercent)
283-
})
272+
metrics.RegisterStatusSampleFunc("memory", GetRssMemory)
273+
metrics.RegisterStatusSampleFunc("cpu", GetCpuPercent)
284274
metrics.RegisterStatusSampleFunc("goroutine_count", func() int64 {
285275
return int64(runtime.NumGoroutine())
286276
})
@@ -289,6 +279,24 @@ func (a *Agent) registerStatusSampler() {
289279
})
290280
}
291281

282+
func GetRssMemory() int64 {
283+
p, _ := process.NewProcess(int32(os.Getpid()))
284+
memInfo, err := p.MemoryInfo()
285+
if err != nil {
286+
return 0
287+
}
288+
return int64(memInfo.RSS >> 20)
289+
}
290+
291+
func GetCpuPercent() int64 {
292+
p, _ := process.NewProcess(int32(os.Getpid()))
293+
cpuPercent, err := p.CPUPercent()
294+
if err != nil {
295+
return 0
296+
}
297+
return int64(cpuPercent)
298+
}
299+
292300
func (a *Agent) initStatus() {
293301
if a.recover {
294302
a.recoverStatus()
@@ -371,7 +379,7 @@ func (a *Agent) initParam() {
371379
port = defaultPort
372380
}
373381

374-
mPort := *motan.Mport
382+
mPort := motan.GetMport()
375383
if mPort == 0 {
376384
if envMPort, ok := os.LookupEnv("mport"); ok {
377385
if envMPortInt, err := strconv.Atoi(envMPort); err == nil {
@@ -629,6 +637,7 @@ func (a *Agent) initAgentURL() {
629637
} else {
630638
agentURL.Parameters[motan.ApplicationKey] = agentURL.Group
631639
}
640+
motan.SetApplication(agentURL.Parameters[motan.ApplicationKey])
632641
if agentURL.Group == "" {
633642
agentURL.Group = defaultAgentGroup
634643
agentURL.Parameters[motan.ApplicationKey] = defaultAgentGroup
@@ -652,8 +661,9 @@ func (a *Agent) initAgentURL() {
652661
func (a *Agent) startAgent() {
653662
url := a.agentURL.Copy()
654663
url.Port = a.port
664+
url.Protocol = mserver.Motan2
655665
handler := &agentMessageHandler{agent: a}
656-
server := &mserver.MotanServer{URL: url}
666+
server := defaultExtFactory.GetServer(url)
657667
server.SetMessageHandler(handler)
658668
vlog.Infof("Motan agent is started. port:%d", a.port)
659669
fmt.Println("Motan agent start.")
@@ -673,7 +683,7 @@ func (a *Agent) registerAgent() {
673683
if agentURL.Host == "" {
674684
agentURL.Host = motan.GetLocalIP()
675685
}
676-
if registryURL, regexit := a.Context.RegistryURLs[reg]; regexit {
686+
if registryURL, regExist := a.Context.RegistryURLs[reg]; regExist {
677687
registry := a.extFactory.GetRegistry(registryURL)
678688
if registry != nil {
679689
vlog.Infof("agent register in registry:%s, agent url:%s", registry.GetURL().GetIdentity(), agentURL.GetIdentity())
@@ -697,6 +707,16 @@ type agentMessageHandler struct {
697707
agent *Agent
698708
}
699709

710+
func (a *agentMessageHandler) GetName() string {
711+
return "agentMessageHandler"
712+
}
713+
714+
func (a *agentMessageHandler) GetRuntimeInfo() map[string]interface{} {
715+
info := map[string]interface{}{}
716+
info[motan.RuntimeMessageHandlerTypeKey] = a.GetName()
717+
return info
718+
}
719+
700720
func (a *agentMessageHandler) clusterCall(request motan.Request, ck string, motanCluster *cluster.MotanCluster) (res motan.Response) {
701721
// fill default request info
702722
fillDefaultReqInfo(request, motanCluster.GetURL())
@@ -747,8 +767,8 @@ func (a *agentMessageHandler) httpCall(request motan.Request, ck string, httpClu
747767
}
748768
httpRequest := fasthttp.AcquireRequest()
749769
httpResponse := fasthttp.AcquireResponse()
770+
// do not release http response
750771
defer fasthttp.ReleaseRequest(httpRequest)
751-
defer fasthttp.ReleaseResponse(httpResponse)
752772
httpRequest.Header.Del("Host")
753773
httpRequest.SetHost(originalService)
754774
httpRequest.URI().SetPath(request.GetMethod())
@@ -886,6 +906,9 @@ func (a *Agent) unavailableAllServices() {
886906
func (a *Agent) doExportService(url *motan.URL) {
887907
a.svcLock.Lock()
888908
defer a.svcLock.Unlock()
909+
if _, ok := a.serviceExporters.Load(url.GetIdentityWithRegistry()); ok {
910+
return
911+
}
889912

890913
globalContext := a.Context
891914
exporter := &mserver.DefaultExporter{}
@@ -932,11 +955,38 @@ func (a *Agent) doExportService(url *motan.URL) {
932955
}
933956

934957
type serverAgentMessageHandler struct {
935-
providers *motan.CopyOnWriteMap
958+
providers *motan.CopyOnWriteMap
959+
frameworkProviders *motan.CopyOnWriteMap
960+
}
961+
962+
func (sa *serverAgentMessageHandler) GetName() string {
963+
return "serverAgentMessageHandler"
964+
}
965+
966+
func (sa *serverAgentMessageHandler) GetRuntimeInfo() map[string]interface{} {
967+
info := map[string]interface{}{}
968+
info[motan.RuntimeMessageHandlerTypeKey] = sa.GetName()
969+
providersInfo := map[string]interface{}{}
970+
sa.providers.Range(func(k, v interface{}) bool {
971+
provider, ok := v.(motan.Provider)
972+
if !ok {
973+
return true
974+
}
975+
providersInfo[k.(string)] = provider.GetRuntimeInfo()
976+
return true
977+
})
978+
info[motan.RuntimeProvidersKey] = providersInfo
979+
return info
936980
}
937981

938982
func (sa *serverAgentMessageHandler) Initialize() {
939983
sa.providers = motan.NewCopyOnWriteMap()
984+
sa.frameworkProviders = motan.NewCopyOnWriteMap()
985+
sa.initFrameworkServiceProvider()
986+
}
987+
988+
func (sa *serverAgentMessageHandler) initFrameworkServiceProvider() {
989+
sa.frameworkProviders.Store(meta.MetaServiceName, &provider.MetaProvider{})
940990
}
941991

942992
func getServiceKey(group, path string) string {
@@ -954,15 +1004,22 @@ func (sa *serverAgentMessageHandler) Call(request motan.Request) (res motan.Resp
9541004
group = request.GetAttachment(motan.GroupKey)
9551005
}
9561006
serviceKey := getServiceKey(group, request.GetServiceName())
1007+
if mfs := request.GetAttachment(mpro.MFrameworkService); mfs != "" {
1008+
if fp, ok := sa.frameworkProviders.Load(request.GetServiceName()); ok {
1009+
return fp.(motan.Provider).Call(request)
1010+
}
1011+
//throw specific exception to avoid triggering forced fusing on the client side。
1012+
return motan.BuildExceptionResponse(request.GetRequestID(), &motan.Exception{ErrCode: 501, ErrMsg: motan.ServiceNotSupport, ErrType: motan.ServiceException})
1013+
}
9571014
if p := sa.providers.LoadOrNil(serviceKey); p != nil {
9581015
p := p.(motan.Provider)
9591016
res = p.Call(request)
9601017
res.GetRPCContext(true).GzipSize = int(p.GetURL().GetIntValue(motan.GzipSizeKey, 0))
9611018
return res
9621019
}
963-
vlog.Errorf("not found provider for %s", motan.GetReqInfo(request))
1020+
vlog.Errorf("%s%s, %s", motan.ProviderNotExistPrefix, serviceKey, motan.GetReqInfo(request))
9641021
atomic.AddInt64(&notFoundProviderCount, 1)
965-
return motan.BuildExceptionResponse(request.GetRequestID(), &motan.Exception{ErrCode: 500, ErrMsg: "not found provider for " + serviceKey, ErrType: motan.ServiceException})
1022+
return motan.BuildExceptionResponse(request.GetRequestID(), &motan.Exception{ErrCode: motan.EProviderNotExist, ErrMsg: motan.ProviderNotExistPrefix + serviceKey, ErrType: motan.ServiceException})
9661023
}
9671024

9681025
func (sa *serverAgentMessageHandler) AddProvider(p motan.Provider) error {
@@ -1119,13 +1176,12 @@ func (a *Agent) startMServer() {
11191176
for kk, vv := range v {
11201177
handlers[kk] = vv
11211178
}
1122-
11231179
}
11241180
}
11251181
for k, v := range handlers {
11261182
a.mhandle(k, v)
11271183
}
1128-
1184+
var mPort int
11291185
var managementListener net.Listener
11301186
if managementUnixSockAddr := a.agentURL.GetParam(motan.ManagementUnixSockKey, ""); managementUnixSockAddr != "" {
11311187
listener, err := motan.ListenUnixSock(managementUnixSockAddr)
@@ -1159,19 +1215,21 @@ func (a *Agent) startMServer() {
11591215
managementListener = motan.TCPKeepAliveListener{TCPListener: listener.(*net.TCPListener)}
11601216
break
11611217
}
1218+
mPort = a.mport
11621219
if managementListener == nil {
11631220
vlog.Warningf("start management server failed for port range %s", startAndPortStr)
11641221
return
11651222
}
11661223
} else {
11671224
listener, err := net.Listen("tcp", ":"+strconv.Itoa(a.mport))
1225+
mPort = a.mport
11681226
if err != nil {
11691227
vlog.Infof("listen manage port %d failed:%s", a.mport, err.Error())
11701228
return
11711229
}
11721230
managementListener = motan.TCPKeepAliveListener{TCPListener: listener.(*net.TCPListener)}
11731231
}
1174-
1232+
motan.SetMport(mPort)
11751233
vlog.Infof("start listen manage for address: %s", managementListener.Addr().String())
11761234
err := http.Serve(managementListener, nil)
11771235
if err != nil {
@@ -1191,8 +1249,10 @@ func (a *Agent) mhandle(k string, h http.Handler) {
11911249
setAgentLock.Unlock()
11921250
}
11931251
http.HandleFunc(k, func(w http.ResponseWriter, r *http.Request) {
1252+
vlog.Infof("mport request: %s, address: %s", r.URL.Path, r.RemoteAddr)
11941253
if !PermissionCheck(r) {
11951254
w.Write([]byte("need permission!"))
1255+
vlog.Warningf("mport request no permission: %s, address: %s", r.URL.Path, r.RemoteAddr)
11961256
return
11971257
}
11981258
defer func() {

0 commit comments

Comments
 (0)