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

Rft : Optimize lock for zookeeper registry #578

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions registry/zookeeper/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func (r *zkRegistry) DoRegister(root string, node string) error {
func (r *zkRegistry) DoUnregister(root string, node string) error {
r.cltLock.Lock()
defer r.cltLock.Unlock()

if !r.ZkClient().ZkConnValid() {
return perrors.Errorf("zk client is not valid.")
}
Expand Down
30 changes: 15 additions & 15 deletions remoting/zookeeper/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var (
type ZookeeperClient struct {
name string
ZkAddrs []string
sync.Mutex // for conn
sync.RWMutex // for conn
Conn *zk.Conn
Timeout time.Duration
exit chan struct{}
Expand Down Expand Up @@ -419,9 +419,9 @@ func (z *ZookeeperClient) CreateWithValue(basePath string, value []byte) error {
for _, str := range strings.Split(basePath, "/")[1:] {
tmpPath = path.Join(tmpPath, "/", str)
err = errNilZkClientConn
z.Lock()
z.RLock()
conn := z.Conn
zouyx marked this conversation as resolved.
Show resolved Hide resolved
z.Unlock()
z.RUnlock()
if conn != nil {
_, err = conn.Create(tmpPath, value, 0, zk.WorldACL(zk.PermAll))
}
Expand All @@ -446,9 +446,9 @@ func (z *ZookeeperClient) Delete(basePath string) error {
)

err = errNilZkClientConn
z.Lock()
z.RLock()
conn := z.Conn
zouyx marked this conversation as resolved.
Show resolved Hide resolved
z.Unlock()
z.RUnlock()
if conn != nil {
err = conn.Delete(basePath, -1)
}
Expand All @@ -468,9 +468,9 @@ func (z *ZookeeperClient) RegisterTemp(basePath string, node string) (string, er
err = errNilZkClientConn
data = []byte("")
zkPath = path.Join(basePath) + "/" + node
z.Lock()
z.RLock()
conn := z.Conn
zouyx marked this conversation as resolved.
Show resolved Hide resolved
z.Unlock()
z.RUnlock()
if conn != nil {
tmpPath, err = conn.Create(zkPath, data, zk.FlagEphemeral, zk.WorldACL(zk.PermAll))
}
Expand All @@ -493,9 +493,9 @@ func (z *ZookeeperClient) RegisterTempSeq(basePath string, data []byte) (string,
)

err = errNilZkClientConn
z.Lock()
z.RLock()
zouyx marked this conversation as resolved.
Show resolved Hide resolved
conn := z.Conn
z.Unlock()
z.RUnlock()
if conn != nil {
tmpPath, err = conn.Create(
path.Join(basePath)+"/",
Expand Down Expand Up @@ -526,9 +526,9 @@ func (z *ZookeeperClient) GetChildrenW(path string) ([]string, <-chan zk.Event,
)

err = errNilZkClientConn
z.Lock()
z.RLock()
conn := z.Conn
z.Unlock()
z.RUnlock()
if conn != nil {
children, stat, watcher, err = conn.ChildrenW(path)
}
Expand Down Expand Up @@ -562,9 +562,9 @@ func (z *ZookeeperClient) GetChildren(path string) ([]string, error) {
)

err = errNilZkClientConn
z.Lock()
z.RLock()
conn := z.Conn
z.Unlock()
z.RUnlock()
if conn != nil {
children, stat, err = conn.Children(path)
}
Expand Down Expand Up @@ -595,9 +595,9 @@ func (z *ZookeeperClient) ExistW(zkPath string) (<-chan zk.Event, error) {
)

err = errNilZkClientConn
z.Lock()
z.RLock()
conn := z.Conn
z.Unlock()
z.RUnlock()
if conn != nil {
exist, _, watcher, err = conn.ExistsW(zkPath)
}
Expand Down