Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
2
22a7f0099
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Summer2022
22a7f0099
Commits
0804c09c
Commit
0804c09c
authored
5 years ago
by
vito.he
Browse files
Options
Downloads
Patches
Plain Diff
modify zk ListenServiceEvent
parent
82fe0798
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
remoting/zookeeper/client.go
+3
-2
3 additions, 2 deletions
remoting/zookeeper/client.go
remoting/zookeeper/listener.go
+54
-47
54 additions, 47 deletions
remoting/zookeeper/listener.go
remoting/zookeeper/listener_test.go
+1
-2
1 addition, 2 deletions
remoting/zookeeper/listener_test.go
with
58 additions
and
51 deletions
remoting/zookeeper/client.go
+
3
−
2
View file @
0804c09c
...
...
@@ -43,6 +43,7 @@ const (
var
(
errNilZkClientConn
=
perrors
.
New
(
"zookeeperclient{conn} is nil"
)
errNilChildren
=
perrors
.
Errorf
(
"has none children"
)
)
// ZookeeperClient ...
...
...
@@ -513,7 +514,7 @@ func (z *ZookeeperClient) GetChildrenW(path string) ([]string, <-chan zk.Event,
return
nil
,
nil
,
perrors
.
Errorf
(
"path{%s} has none children"
,
path
)
}
if
len
(
children
)
==
0
{
return
nil
,
nil
,
p
err
ors
.
Errorf
(
"path{%s} has none children"
,
path
)
return
nil
,
nil
,
err
NilChildren
}
return
children
,
event
,
nil
...
...
@@ -544,7 +545,7 @@ func (z *ZookeeperClient) GetChildren(path string) ([]string, error) {
return
nil
,
perrors
.
Errorf
(
"path{%s} has none children"
,
path
)
}
if
len
(
children
)
==
0
{
return
nil
,
p
err
ors
.
Errorf
(
"path{%s} has none children"
,
path
)
return
nil
,
err
NilChildren
}
return
children
,
nil
...
...
This diff is collapsed.
Click to expand it.
remoting/zookeeper/listener.go
+
54
−
47
View file @
0804c09c
...
...
@@ -19,7 +19,6 @@ package zookeeper
import
(
"path"
"strings"
"sync"
"time"
)
...
...
@@ -111,8 +110,16 @@ func (l *ZkEventListener) handleZkNodeEvent(zkPath string, children []string, li
newChildren
,
err
:=
l
.
client
.
GetChildren
(
zkPath
)
if
err
!=
nil
{
logger
.
Errorf
(
"path{%s} child nodes changed, zk.Children() = error{%v}"
,
zkPath
,
perrors
.
WithStack
(
err
))
return
if
err
==
errNilChildren
{
content
,
_
,
err
:=
l
.
client
.
Conn
.
Get
(
zkPath
)
if
err
!=
nil
{
logger
.
Errorf
(
"Get new node path {%v} 's content error,message is {%v}"
,
zkPath
,
perrors
.
WithStack
(
err
))
}
listener
.
DataChange
(
remoting
.
Event
{
Path
:
zkPath
,
Action
:
remoting
.
EventTypeUpdate
,
Content
:
string
(
content
)})
}
else
{
logger
.
Errorf
(
"path{%s} child nodes changed, zk.Children() = error{%v}"
,
zkPath
,
perrors
.
WithStack
(
err
))
}
}
// a node was added -- listen the new node
...
...
@@ -272,50 +279,50 @@ func timeSecondDuration(sec int) time.Duration {
// |
// --------> ListenServiceNodeEvent
func
(
l
*
ZkEventListener
)
ListenServiceEvent
(
zkPath
string
,
listener
remoting
.
DataListener
)
{
var
(
err
error
dubboPath
string
children
[]
string
)
zkPath
=
strings
.
ReplaceAll
(
zkPath
,
"$"
,
"%24"
)
l
.
pathMapLock
.
Lock
()
_
,
ok
:=
l
.
pathMap
[
zkPath
]
l
.
pathMapLock
.
Unlock
()
if
ok
{
logger
.
Warnf
(
"@zkPath %s has already been listened."
,
zkPath
)
return
}
l
.
pathMapLock
.
Lock
()
l
.
pathMap
[
zkPath
]
=
struct
{}{}
l
.
pathMapLock
.
Unlock
()
logger
.
Infof
(
"listen dubbo provider path{%s} event and wait to get all provider zk nodes"
,
zkPath
)
children
,
err
=
l
.
client
.
GetChildren
(
zkPath
)
if
err
!=
nil
{
children
=
nil
logger
.
Warnf
(
"fail to get children of zk path{%s}"
,
zkPath
)
}
for
_
,
c
:=
range
children
{
// listen l service node
dubboPath
=
path
.
Join
(
zkPath
,
c
)
content
,
_
,
err
:=
l
.
client
.
Conn
.
Get
(
dubboPath
)
if
err
!=
nil
{
logger
.
Errorf
(
"Get new node path {%v} 's content error,message is {%v}"
,
dubboPath
,
perrors
.
WithStack
(
err
))
}
if
!
listener
.
DataChange
(
remoting
.
Event
{
Path
:
dubboPath
,
Action
:
remoting
.
EventTypeAdd
,
Content
:
string
(
content
)})
{
continue
}
logger
.
Infof
(
"listen dubbo service key{%s}"
,
dubboPath
)
go
func
(
zkPath
string
,
listener
remoting
.
DataListener
)
{
if
l
.
ListenServiceNodeEvent
(
zkPath
)
{
listener
.
DataChange
(
remoting
.
Event
{
Path
:
zkPath
,
Action
:
remoting
.
EventTypeDel
})
}
logger
.
Warnf
(
"listenSelf(zk path{%s}) goroutine exit now"
,
zkPath
)
}(
dubboPath
,
listener
)
}
//
var (
//
err error
//
dubboPath string
//
children []string
//
)
//
//
zkPath = strings.ReplaceAll(zkPath, "$", "%24")
//
l.pathMapLock.Lock()
//
_, ok := l.pathMap[zkPath]
//
l.pathMapLock.Unlock()
//
if ok {
//
logger.Warnf("@zkPath %s has already been listened.", zkPath)
//
return
//
}
//
//
l.pathMapLock.Lock()
//
l.pathMap[zkPath] = struct{}{}
//
l.pathMapLock.Unlock()
//
//
logger.Infof("listen dubbo provider path{%s} event and wait to get all provider zk nodes", zkPath)
//
children, err = l.client.GetChildren(zkPath)
//
if err != nil {
//
children = nil
//
logger.Warnf("fail to get children of zk path{%s}", zkPath)
//
}
//
//
for _, c := range children {
//
// listen l service node
//
dubboPath = path.Join(zkPath, c)
//
content, _, err := l.client.Conn.Get(dubboPath)
//
if err != nil {
//
logger.Errorf("Get new node path {%v} 's content error,message is {%v}", dubboPath, perrors.WithStack(err))
//
}
//
if !listener.DataChange(remoting.Event{Path: dubboPath, Action: remoting.EventTypeAdd, Content: string(content)}) {
//
continue
//
}
//
logger.Infof("listen dubbo service key{%s}", dubboPath)
//
go func(zkPath string, listener remoting.DataListener) {
//
if l.ListenServiceNodeEvent(zkPath) {
//
listener.DataChange(remoting.Event{Path: zkPath, Action: remoting.EventTypeDel})
//
}
//
logger.Warnf("listenSelf(zk path{%s}) goroutine exit now", zkPath)
//
}(dubboPath, listener)
//
}
logger
.
Infof
(
"listen dubbo path{%s}"
,
zkPath
)
go
func
(
zkPath
string
,
listener
remoting
.
DataListener
)
{
...
...
This diff is collapsed.
Click to expand it.
remoting/zookeeper/listener_test.go
+
1
−
2
View file @
0804c09c
...
...
@@ -97,12 +97,11 @@ func TestListener(t *testing.T) {
listener
:=
NewZkEventListener
(
client
)
dataListener
:=
&
mockDataListener
{
client
:
client
,
changedData
:
changedData
,
wait
:
&
wait
}
listener
.
ListenServiceEvent
(
"/dubbo"
,
dataListener
)
time
.
Sleep
(
1
*
time
.
Second
)
_
,
err
:=
client
.
Conn
.
Set
(
"/dubbo/dubbo.properties"
,
[]
byte
(
changedData
),
1
)
assert
.
NoError
(
t
,
err
)
wait
.
Wait
()
assert
.
Equal
(
t
,
changedData
,
dataListener
.
eventList
[
1
]
.
Content
)
client
.
Close
()
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment