-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
optimize: refactor the redis lock string to hash #3016
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #3016 +/- ##
=============================================
+ Coverage 50.39% 50.43% +0.03%
- Complexity 3092 3100 +8
=============================================
Files 599 599
Lines 19531 19488 -43
Branches 2412 2360 -52
=============================================
- Hits 9843 9829 -14
+ Misses 8697 8644 -53
- Partials 991 1015 +24 |
list change hash xid branch1 branch1_info branch2 branch2_info..... del: competition lock: reentrant lock: |
lockMap.put(TABLE_NAME, value.getTableName()); | ||
lockMap.put(ROW_KEY, value.getRowKey()); | ||
lockMap.put(PK, value.getPk()); | ||
pipeline.hmset(key, lockMap); |
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.
cannot play the role of competition lock
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.
fixed.
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.
please check my message
String key = readyKeys.get(i); | ||
if (result != 1) { | ||
status = result; | ||
if (partitions.contains(FAILED)) { |
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.
partitions.get(i).contains
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.
finished
if (successSet.size() > 0) { | ||
Pipeline pipeline2 = jedis.pipelined(); | ||
successSet.forEach(locKey -> | ||
pipeline2.hdel(locKey, XID, TRANSACTION_ID, BRANCH_ID, RESOURCE_ID, |
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.
del successSet. toArray (new String[0])
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.
finished
|
||
Pipeline pipeline1 = jedis.pipelined(); | ||
needLockKeys.stream().forEachOrdered( | ||
needLockKey -> pipeline1.hmget(needLockKey, XID, TABLE_NAME, PK, BRANCH_ID)); |
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.
only need to xid
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.
finished
readyKeys.add(key); | ||
}); | ||
List<Object> results = pipeline.syncAndReturnAll(); | ||
for (int i = 0; i < results.size(); i++) { | ||
Long result = (long)results.get(i); | ||
List<Integer> results2 = (List<Integer>) (List) results; |
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.
List< Integer > results = (List) (List) pipeline.syncAndReturnAll();
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.
finished
needReleaseKeys.stream() | ||
.forEach(key -> pipelined.hmget(key, XID, TABLE_NAME, PK, BRANCH_ID)); | ||
List<Object> existedObjs = pipelined.syncAndReturnAll(); | ||
List<List<String>> existedLockInfos = (List<List<String>>) (List) existedObjs; |
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.
the lock can only be actively unlocked by the owner without having to retrieve it again
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.
finished
pipeline.sync(); | ||
Pipeline pipelined = jedis.pipelined(); | ||
needReleaseKeys.stream() | ||
.forEach(key -> pipelined.hmget(key, XID, TABLE_NAME, PK, BRANCH_ID)); |
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.
only need to xid
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.
finished
|
||
Pipeline pipelined1 = jedis.pipelined(); | ||
needReleaseKeys.stream().forEach(key -> | ||
pipelined1.hdel(key, XID, TRANSACTION_ID, BRANCH_ID, RESOURCE_ID, |
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.
del needReleaseKeys. toArray (new String[0])
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.
finished
String[] keys = rowKeyStr.split(SEMICOLON); | ||
Arrays.asList(keys).stream().forEach(rowKey -> { | ||
if (StringUtils.isNotEmpty(rowKey)) { | ||
pipelined.hdel(rowKey, XID, TRANSACTION_ID, BRANCH_ID, |
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.
del rowkey
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.
finished
} | ||
}); | ||
} else { | ||
pipelined.hdel(rowKeyStr, XID, TRANSACTION_ID, BRANCH_ID, RESOURCE_ID, |
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.
del rowkey
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.
finished
Pipeline pipeline = jedis.pipelined(); | ||
lockKeys.stream().forEach(key -> pipeline.hget(key, XID)); | ||
List<Object> existedRowLockXid = pipeline.syncAndReturnAll(); | ||
List<String> existedXids = (List<String>) (List) existedRowLockXid; |
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.
List existedXids = (List) (List) pipeline.syncAndReturnAll();
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.
finished
String xidLockKey = buildXidLockKey(needLockXid); | ||
StringJoiner lockKeysString = new StringJoiner(SEMICOLON); | ||
needLockKeys.stream().forEach(lockKey -> lockKeysString.add(lockKey)); | ||
jedis.hsetnx(xidLockKey, branchId.toString(), lockKeysString.toString()); |
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.
hset enough
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.
finished
Pipeline pipeline = jedis.pipelined(); | ||
lockKeys.stream().forEach(key -> pipeline.hget(key, XID)); | ||
List<String> existedXids = (List<String>) (List) pipeline.syncAndReturnAll(); | ||
return existedXids.stream().allMatch(existedXid -> xid.equals(existedXid)); |
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.
please pay attention to this problem: list All elements are null
existedXids.size>0
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.
finished
@@ -60,131 +82,139 @@ | |||
*/ | |||
public RedisLocker() { | |||
logQueryLimit = | |||
ConfigurationFactory.getInstance().getInt(ConfigurationKeys.STORE_REDIS_QUERY_LIMIT, DEFAULT_QUERY_LIMIT); | |||
ConfigurationFactory.getInstance() |
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.
don't need the logQueryLimit
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.
finished
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.
LGTM
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.
Note that the structure is not compatible
LGTM
/** the start time of transaction */ | ||
public static final String START_TIME = "start-time"; | ||
/** | ||
* The constant ; |
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.
Can be completed or deleted
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.
finished
@@ -15,23 +15,21 @@ | |||
*/ | |||
package io.seata.server.storage.redis.lock; | |||
|
|||
import static io.seata.common.Constants.SEMICOLON; | |||
|
|||
import com.google.common.collect.Lists; |
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.
Can be placed below
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.
It seemed that the java package was on the top
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.
It seemed that the java package was on the top
finished
locks = | ||
locks.stream().filter(LambdaUtils.distinctByKey(LockDO::getRowKey)).collect(Collectors.toList()); | ||
List<LockDO> needLockDOS = convertToLockDO(rowLocks); | ||
if (needLockDOS.size() >= 1) { |
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.
Remove equal to
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.
finished
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.
The meaning here is the same as before, so be it
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.
The meaning here is the same as before, so be it
finished
return DEFAULT_REDIS_SEATA_LOCK_XID_PREFIX + xid; | ||
} | ||
|
||
private String getLockKey(String rowKey) { | ||
private String buildLockKey(String rowKey) { | ||
return DEFAULT_REDIS_SEATA_LOCK_PREFIX + rowKey; |
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.
Change the names of these two keys for structure error
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.
Change the names of these two keys for structure error
finished
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.
LGTM for @ph3636
Ⅰ. Describe what this PR did
redis lock data structor ,use hash to replace string
Ⅱ. Does this pull request fix one issue?
fixes #3008
Ⅲ. Why don't you add test cases (unit test/integration test)?
Ⅳ. Describe how to verify it
Ⅴ. Special notes for reviews