rride
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
ZNodeName sequence = (ZNodeName) o;
if (!name.equals(sequence.name))
return false;
return true;
}
@Override
public int hashCode() {
return name.hashCode() + 37;
}
public int compareTo(ZNodeName that) {
int s1 = this.sequence;
int s2 = that.sequence;
if (s1 == -1 && s2 == -1) {
return this.name.compareTo(that.name);
}
if (s1 == -1) {
return -1;
} else if (s2 == -1) {
return 1;
} else {
return s1 - s2;
}
}
/**
* Returns the name of the znode
*/
public String getName() {
return name;
}
/**
* Returns the sequence number
*/
public int getZNodeName() {
return sequence;
}
/**
* Returns the text prefix before the sequence number
*/
public String getPrefix() {
return prefix;
}
}
PS:这个ZNodeName类是被我修改过的,官方的代码比较有问题,官方的先用了节点路径的前缀prefix比较,再去比较sequence序号是不对的,这样会导致sessionid小的总是能拿到锁。应该直接比较全局有序的sequence序号,小的先拿到锁,先到先得。
Zookeeper统一操作ZooKeeperOperation接口:
public interface ZooKeeperOperation {
/**
* Performs the operation - which may be involved multiple times if the connection
* to ZooKeeper closes during this operation
*
* @return the result of the operation or null
* @throws KeeperException
* @throws InterruptedException
*/
public boolean execute() throws KeeperException, InterruptedException;
}
因为Zookeeper的操作会失败,这个类封装了多次尝试:
/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.zookeeper.recipes.lock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;
import org.apache.zookeeper.recipes.lock.ZooKeeperOperation;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* A base class for protocol implementations which provides a number of higher
* level helper methods for working with ZooKeeper along with retrying synchronous
* operations if the connection to ZooKeeper closes such as
* {@link #retryOperation(ZooKeeperOperation)}
*
*/
class ProtocolSupport {
private static final Logger LOG = LoggerFactory.getLogger(ProtocolSupport.class);
protected final ZooKeeper zookeeper;
private AtomicBoolean closed = new AtomicBoolean(false);
private long retryDelay = 500L;
private int retryCount = 10;
private List<ACL&