-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
实现全局锁在 zookeeper下的持久化, 完善 ZookeeperRepository 的相关方法, 并编写相关测试用例 #362
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,11 +62,11 @@ | |
public class ZookeeperRepository implements HmilyRepository { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ZookeeperRepository.class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 在idea添加设置,空行也要缩进 |
||
|
||
private static final CountDownLatch LATCH = new CountDownLatch(1); | ||
|
||
private static volatile ZooKeeper zooKeeper; | ||
|
||
private HmilySerializer hmilySerializer; | ||
|
||
private HmilyRepositoryNode node; | ||
|
@@ -223,7 +223,7 @@ public int removeHmilyTransactionByDate(final Date date) { | |
return dateParam.after(hmilyTransaction.getUpdateTime()) && hmilyTransaction.getStatus() == HmilyActionEnum.DELETE.getCode(); | ||
}, date); | ||
} | ||
|
||
@Override | ||
public int createHmilyParticipant(final HmilyParticipant hmilyParticipant) throws HmilyRepositoryException { | ||
try { | ||
|
@@ -288,7 +288,7 @@ public boolean existHmilyParticipantByTransId(final Long transId) { | |
return transIdParam.compareTo(hmilyParticipant.getTransId()) == 0; | ||
}, transId); | ||
} | ||
|
||
@Override | ||
public int updateHmilyParticipantStatus(final Long participantId, final Integer status) throws HmilyRepositoryException { | ||
String path = node.getHmilyParticipantRealPath(participantId); | ||
|
@@ -440,25 +440,61 @@ public int updateHmilyParticipantUndoStatus(final Long undoId, final Integer sta | |
} | ||
return HmilyRepository.FAIL_ROWS; | ||
} | ||
|
||
@Override | ||
public int writeHmilyLocks(final Collection<HmilyLock> locks) { | ||
// TODO | ||
return 0; | ||
for (HmilyLock lock : locks) { | ||
String path = node.getHmilyLockRealPath(lock.getLockId().replace("/", "-")); | ||
try { | ||
create(node.getHmilyLockRootPath()); | ||
Stat stat = zooKeeper.exists(path, false); | ||
if (stat == null) { | ||
zooKeeper.create(path, hmilySerializer.serialize(lock), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); | ||
} else { | ||
zooKeeper.setData(path, hmilySerializer.serialize(lock), stat.getVersion()); | ||
} | ||
} catch (KeeperException | InterruptedException e) { | ||
throw new HmilyException(e); | ||
} | ||
} | ||
return locks.size(); | ||
} | ||
|
||
@Override | ||
public int releaseHmilyLocks(final Collection<HmilyLock> locks) { | ||
// TODO | ||
return 0; | ||
for (HmilyLock lock : locks) { | ||
String path = node.getHmilyLockRealPath(lock.getLockId().replace("/", "-")); | ||
try { | ||
if (checkPath(path, false)) { | ||
return FAIL_ROWS; | ||
} | ||
zooKeeper.delete(path, -1); | ||
} catch (InterruptedException | KeeperException e) { | ||
LOGGER.error("removeHmilyLock occur a exception", e); | ||
return HmilyRepository.FAIL_ROWS; | ||
} | ||
} | ||
return locks.size(); | ||
} | ||
|
||
@Override | ||
public Optional<HmilyLock> findHmilyLockById(final String lockId) { | ||
// TODO | ||
String path = node.getHmilyLockRealPath(lockId.replace("/", "-")); | ||
try { | ||
if (checkPath(path, false)) { | ||
return Optional.empty(); | ||
} | ||
byte[] data = zooKeeper.getData(path, false, null); | ||
if (data == null || data.length == 0) { | ||
return Optional.empty(); | ||
} | ||
return Optional.of(hmilySerializer.deSerialize(data, HmilyLock.class)); | ||
} catch (KeeperException | InterruptedException e) { | ||
LOGGER.error("findHmilyLockById occur a exception", e); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
private void connect(final HmilyZookeeperConfig config) { | ||
try { | ||
zooKeeper = new ZooKeeper(config.getHost(), config.getSessionTimeOut(), watchedEvent -> { | ||
|
@@ -467,9 +503,9 @@ private void connect(final HmilyZookeeperConfig config) { | |
} | ||
}); | ||
LATCH.await(); | ||
Stat stat = zooKeeper.exists(node.getRootPathPrefix(), false); | ||
Stat stat = zooKeeper.exists("/" + node.getRootPathPrefix(), false); | ||
if (stat == null) { | ||
zooKeeper.create(node.getRootPathPrefix(), node.getRootPathPrefix().getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); | ||
zooKeeper.create("/" + node.getRootPathPrefix(), node.getRootPathPrefix().getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); | ||
} | ||
} catch (Exception e) { | ||
throw new HmilyRuntimeException(e); | ||
|
@@ -581,18 +617,18 @@ private <T> int removeByFilter(final String path, final Class<T> deserializeClas | |
} | ||
return HmilyRepository.FAIL_ROWS; | ||
} | ||
|
||
/** | ||
* The type Path tokenizer. | ||
*/ | ||
static class PathTokenizer { | ||
|
||
private String path = ""; | ||
|
||
private String[] nodes; | ||
|
||
private int index; | ||
|
||
/** | ||
* Instantiates a new Path tokenizer. | ||
* | ||
|
@@ -607,7 +643,7 @@ static class PathTokenizer { | |
index = 1; | ||
} | ||
} | ||
|
||
/** | ||
* Next path string. | ||
* | ||
|
@@ -618,7 +654,7 @@ public String nextPath() { | |
index++; | ||
return path; | ||
} | ||
|
||
/** | ||
* Has next boolean. | ||
* | ||
|
@@ -628,14 +664,14 @@ public boolean hasNext() { | |
return index < nodes.length; | ||
} | ||
} | ||
|
||
/** | ||
* The interface Filter. | ||
* | ||
* @param <T> the type parameter | ||
*/ | ||
interface Filter<T> { | ||
|
||
/** | ||
* Filter boolean. | ||
* | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里为什么要去掉lombok自动生成构造函数,而自己去写
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修改