-
Notifications
You must be signed in to change notification settings - Fork 8.8k
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: reduce the number of lock conflict exception #1469
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a01ce0e
optimize: reduce number of lock conflict exception by retrying global…
ggndnn 452dcf4
Merge branch 'develop' into optimize_issue_1438
slievrly 7ae3eb3
Merge branch 'develop' into optimize_issue_1438
zjinlei 987054b
Merge branch 'develop' into optimize_issue_1438
xingfudeshi 254d6bd
Merge branch 'develop' into optimize_issue_1438
xingfudeshi b0921b3
Merge branch 'develop' into optimize_issue_1438
xingfudeshi 14c9f3a
Merge branch 'develop' into optimize_issue_1438
xingfudeshi 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
87 changes: 87 additions & 0 deletions
87
rm-datasource/src/test/java/io/seata/rm/datasource/ConnectionProxyTest.java
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* Licensed 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 io.seata.rm.datasource; | ||
|
||
import io.seata.core.exception.TransactionException; | ||
import io.seata.core.exception.TransactionExceptionCode; | ||
import io.seata.core.model.BranchType; | ||
import io.seata.core.model.ResourceManager; | ||
import io.seata.rm.DefaultResourceManager; | ||
import io.seata.rm.datasource.exec.LockConflictException; | ||
import io.seata.rm.datasource.exec.LockWaitTimeoutException; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Modifier; | ||
|
||
/** | ||
* ConnectionProxy test | ||
* | ||
* @author ggndnn | ||
*/ | ||
public class ConnectionProxyTest { | ||
private DataSourceProxy dataSourceProxy; | ||
|
||
private final static String TEST_RESOURCE_ID = "testResourceId"; | ||
|
||
private final static String TEST_XID = "testXid"; | ||
|
||
private Field branchRollbackFlagField; | ||
|
||
@BeforeEach | ||
public void initBeforeEach() throws Exception { | ||
branchRollbackFlagField = ConnectionProxy.LockRetryPolicy.class.getDeclaredField("LOCK_RETRY_POLICY_BRANCH_ROLLBACK_ON_CONFLICT"); | ||
Field modifiersField = Field.class.getDeclaredField("modifiers"); | ||
modifiersField.setAccessible(true); | ||
modifiersField.setInt(branchRollbackFlagField, branchRollbackFlagField.getModifiers() & ~Modifier.FINAL); | ||
branchRollbackFlagField.setAccessible(true); | ||
boolean branchRollbackFlag = (boolean) branchRollbackFlagField.get(null); | ||
Assertions.assertTrue(branchRollbackFlag); | ||
|
||
dataSourceProxy = Mockito.mock(DataSourceProxy.class); | ||
Mockito.when(dataSourceProxy.getResourceId()) | ||
.thenReturn(TEST_RESOURCE_ID); | ||
ResourceManager rm = Mockito.mock(ResourceManager.class); | ||
Mockito.when(rm.branchRegister(BranchType.AT, dataSourceProxy.getResourceId(), null, TEST_XID, null, null)) | ||
.thenThrow(new TransactionException(TransactionExceptionCode.LockKeyConflict)); | ||
DefaultResourceManager defaultResourceManager = DefaultResourceManager.get(); | ||
Assertions.assertNotNull(defaultResourceManager); | ||
DefaultResourceManager.mockResourceManager(BranchType.AT, rm); | ||
} | ||
|
||
@Test | ||
public void testLockRetryPolicyRollbackOnConflict() throws Exception { | ||
boolean oldBranchRollbackFlag = (boolean) branchRollbackFlagField.get(null); | ||
branchRollbackFlagField.set(null, true); | ||
ConnectionProxy connectionProxy = new ConnectionProxy(dataSourceProxy, null); | ||
connectionProxy.bind(TEST_XID); | ||
Assertions.assertThrows(LockConflictException.class, connectionProxy::commit); | ||
branchRollbackFlagField.set(null, oldBranchRollbackFlag); | ||
} | ||
|
||
@Test | ||
public void testLockRetryPolicyNotRollbackOnConflict() throws Exception { | ||
boolean oldBranchRollbackFlag = (boolean) branchRollbackFlagField.get(null); | ||
branchRollbackFlagField.set(null, false); | ||
ConnectionProxy connectionProxy = new ConnectionProxy(dataSourceProxy, null); | ||
connectionProxy.bind(TEST_XID); | ||
Assertions.assertThrows(LockWaitTimeoutException.class, connectionProxy::commit); | ||
branchRollbackFlagField.set(null, oldBranchRollbackFlag); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
rm-datasource/src/test/java/io/seata/rm/datasource/exec/AbstractDMLBaseExecutorTest.java
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* Licensed 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 io.seata.rm.datasource.exec; | ||
|
||
import io.seata.rm.datasource.ConnectionContext; | ||
import io.seata.rm.datasource.ConnectionProxy; | ||
import io.seata.rm.datasource.PreparedStatementProxy; | ||
import io.seata.rm.datasource.sql.SQLInsertRecognizer; | ||
import io.seata.rm.datasource.sql.struct.TableMeta; | ||
import io.seata.rm.datasource.sql.struct.TableRecords; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Modifier; | ||
import java.sql.Connection; | ||
|
||
/** | ||
* AbstractDMLBaseExecutor test | ||
* | ||
* @author ggndnn | ||
*/ | ||
public class AbstractDMLBaseExecutorTest { | ||
private ConnectionProxy connectionProxy; | ||
|
||
private AbstractDMLBaseExecutor executor; | ||
|
||
private Field branchRollbackFlagField; | ||
|
||
@BeforeEach | ||
public void initBeforeEach() throws Exception { | ||
branchRollbackFlagField = ConnectionProxy.LockRetryPolicy.class.getDeclaredField("LOCK_RETRY_POLICY_BRANCH_ROLLBACK_ON_CONFLICT"); | ||
Field modifiersField = Field.class.getDeclaredField("modifiers"); | ||
modifiersField.setAccessible(true); | ||
modifiersField.setInt(branchRollbackFlagField, branchRollbackFlagField.getModifiers() & ~Modifier.FINAL); | ||
branchRollbackFlagField.setAccessible(true); | ||
boolean branchRollbackFlag = (boolean) branchRollbackFlagField.get(null); | ||
Assertions.assertTrue(branchRollbackFlag); | ||
|
||
Connection targetConnection = Mockito.mock(Connection.class); | ||
connectionProxy = Mockito.mock(ConnectionProxy.class); | ||
Mockito.doThrow(new LockConflictException()) | ||
.when(connectionProxy).commit(); | ||
Mockito.when(connectionProxy.getAutoCommit()) | ||
.thenReturn(Boolean.TRUE); | ||
Mockito.when(connectionProxy.getTargetConnection()) | ||
.thenReturn(targetConnection); | ||
Mockito.when(connectionProxy.getContext()) | ||
.thenReturn(new ConnectionContext()); | ||
PreparedStatementProxy statementProxy = Mockito.mock(PreparedStatementProxy.class); | ||
Mockito.when(statementProxy.getConnectionProxy()) | ||
.thenReturn(connectionProxy); | ||
StatementCallback statementCallback = Mockito.mock(StatementCallback.class); | ||
SQLInsertRecognizer sqlInsertRecognizer = Mockito.mock(SQLInsertRecognizer.class); | ||
TableMeta tableMeta = Mockito.mock(TableMeta.class); | ||
executor = Mockito.spy(new InsertExecutor(statementProxy, statementCallback, sqlInsertRecognizer)); | ||
Mockito.doReturn(tableMeta) | ||
.when(executor).getTableMeta(); | ||
TableRecords tableRecords = new TableRecords(); | ||
Mockito.doReturn(tableRecords) | ||
.when(executor).beforeImage(); | ||
Mockito.doReturn(tableRecords) | ||
.when(executor).afterImage(tableRecords); | ||
} | ||
|
||
@Test | ||
public void testLockRetryPolicyRollbackOnConflict() throws Exception { | ||
boolean oldBranchRollbackFlag = (boolean) branchRollbackFlagField.get(null); | ||
branchRollbackFlagField.set(null, true); | ||
Assertions.assertThrows(LockWaitTimeoutException.class, executor::execute); | ||
Mockito.verify(connectionProxy, Mockito.times(1)) | ||
.rollback(); | ||
Mockito.verify(connectionProxy.getTargetConnection(), Mockito.atLeastOnce()) | ||
.rollback(); | ||
branchRollbackFlagField.set(null, oldBranchRollbackFlag); | ||
} | ||
|
||
@Test | ||
public void testLockRetryPolicyNotRollbackOnConflict() throws Throwable { | ||
boolean oldBranchRollbackFlag = (boolean) branchRollbackFlagField.get(null); | ||
branchRollbackFlagField.set(null, false); | ||
Assertions.assertThrows(LockConflictException.class, executor::execute); | ||
Mockito.verify(connectionProxy, Mockito.times(1)) | ||
.rollback(); | ||
Mockito.verify(connectionProxy.getTargetConnection(), Mockito.never()) | ||
.rollback(); | ||
branchRollbackFlagField.set(null, oldBranchRollbackFlag); | ||
} | ||
} |
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.
just used for LOCK_RETRY_POLICY_BRANCH_ROLLBACK_ON_CONFLICT=false
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.
Should it be okay to do
ConnectionProxy#rollback
in both cases?