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

[Feature] add transaction hook #525

Merged
merged 13 commits into from
Mar 15, 2019

Conversation

github-ygy
Copy link
Contributor

@github-ygy github-ygy commented Mar 5, 2019

Ⅰ. Describe what this PR did

Feature add transaction hook

Ⅱ. Does this pull request fix one issue?

add transaction hook in the Transaction Life Cycle

Ⅲ. Why don't you add test cases (unit test/integration test)?

add test in TransactionHookManagerTest
add test in TransactionTemplateTest

Ⅳ. Describe how to verify it

Ⅴ. Special notes for reviews

@codecov-io
Copy link

codecov-io commented Mar 5, 2019

Codecov Report

Merging #525 into develop will increase coverage by 0.38%.
The diff coverage is 68.75%.

Impacted file tree graph

@@              Coverage Diff              @@
##             develop     #525      +/-   ##
=============================================
+ Coverage      31.89%   32.28%   +0.38%     
- Complexity       775      798      +23     
=============================================
  Files            200      202       +2     
  Lines           7948     8032      +84     
  Branches         939      941       +2     
=============================================
+ Hits            2535     2593      +58     
- Misses          5155     5178      +23     
- Partials         258      261       +3
Impacted Files Coverage Δ Complexity Δ
...car/tm/api/transaction/TransactionHookAdapter.java 12.5% <12.5%> (ø) 1 <1> (?)
...car/tm/api/transaction/TransactionHookManager.java 66.66% <66.66%> (ø) 5 <5> (?)
...m/alibaba/fescar/tm/api/TransactionalTemplate.java 75% <75.34%> (+6.57%) 19 <17> (+17) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4f40469...c512cd7. Read the comment docs.

Copy link
Contributor

@sharajava sharajava left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if hooks are really needed by users. Can you share some ideas about hook use cases?

And also, the hook mechanism here is doing something just like FailureHandler. I think we can replace FailureHandler with TransactionHook after some enhancement on this.

}
// 4. everything is fine, commit.
try {
triggerBeforeCommit();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception should be properly handled. I don't think users would like to fail committing by their hook.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it in next commit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the hook help us to add more function in the transaction life cycle
like
1.trace the transaction execute status
2. customer logger to help they find the bad transaction
3.some case we need to do something when the commit all success

* @author guoyao
* @date 2019/3/4
*/
public interface TransactionHook {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a transaction hook, why not also including before/after rollback?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea .i will add it


}

try {
triggerAfterCommit();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge after tx.commit()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea ,done

Copy link
Contributor

@jovany-wang jovany-wang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left some comments about code style.

import com.alibaba.fescar.tm.api.transaction.TransactionHookManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already format

*/
package com.alibaba.fescar.tm.api.transaction;

import java.util.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use *

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy that

if (transactionHook == null) {
throw new NullPointerException("transactionHook must not be null");
}
List<TransactionHook> transactionHooks=hooksLocal.get();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the spaces around =

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

*/
public class TransactionTemplateTest {

private static final String DEFAULT_XID="123456789";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -26,7 +26,7 @@
*/
public final class TransactionHookManager {

private static final ThreadLocal<List<TransactionHook>> hooksLocal=new ThreadLocal<>();
private static final ThreadLocal<List<TransactionHook>> hooksLocal = new ThreadLocal<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call a static final member as uppercase:

private static final ThreadLocal<List<TransactionHook>> LOCAL_HOOKS = new ThreadLocal<>();

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use spi to load the transaction hook instead of a thread local holder?

@slievrly
Copy link
Member

@jovany-wang

Copy link
Contributor

@jovany-wang jovany-wang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM!

@slievrly slievrly merged commit d9d4793 into apache:develop Mar 15, 2019
@wangliang181230 wangliang181230 added this to the 0.3.* milestone Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants