Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8dfc0c2

Browse files
Merge pull request #5 from catcherwong/feat/mongobarrier
feat: support mongo barrier
2 parents ff01961 + 713fcb3 commit 8dfc0c2

File tree

10 files changed

+236
-7
lines changed

10 files changed

+236
-7
lines changed

‎.github/workflows/release_stable.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ jobs:
2121
dotnet build --configuration Release --source https://api.nuget.org/v3/index.json src/Dtmgrpc/Dtmgrpc.csproj
2222
dotnet build --configuration Release --source https://api.nuget.org/v3/index.json src/DtmCommon/DtmCommon.csproj
2323
dotnet build --configuration Release --source https://api.nuget.org/v3/index.json src/DtmSERedisBarrier/DtmSERedisBarrier.csproj
24+
dotnet build --configuration Release --source https://api.nuget.org/v3/index.json src/DtmMongoBarrier/DtmMongoBarrier.csproj
2425
- name: Pack with dotnet
2526
run: |
2627
dotnet pack src/Dtmgrpc/Dtmgrpc.csproj -o /home/runner/work/nugetpkgs -c Release --no-build
2728
dotnet pack src/DtmCommon/DtmCommon.csproj -o /home/runner/work/nugetpkgs -c Release --no-build
2829
dotnet pack src/DtmSERedisBarrier/DtmSERedisBarrier.csproj -o /home/runner/work/nugetpkgs -c Release --no-build
30+
dotnet pack src/DtmMongoBarrier/DtmMongoBarrier.csproj -o /home/runner/work/nugetpkgs -c Release --no-build
2931
- name: Upload artifact
3032
uses: actions/upload-artifact@v1
3133
with:

‎.github/workflows/release_unstable.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ jobs:
2121
dotnet build --configuration Release --source https://api.nuget.org/v3/index.json src/Dtmgrpc/Dtmgrpc.csproj
2222
dotnet build --configuration Release --source https://api.nuget.org/v3/index.json src/DtmCommon/DtmCommon.csproj
2323
dotnet build --configuration Release --source https://api.nuget.org/v3/index.json src/DtmSERedisBarrier/DtmSERedisBarrier.csproj
24+
dotnet build --configuration Release --source https://api.nuget.org/v3/index.json src/DtmMongoBarrier/DtmMongoBarrier.csproj
2425
- name: Pack with dotnet
2526
run: |
2627
ver=alpha`date +%Y%m%d%H%M%S`
2728
dotnet pack src/Dtmgrpc/Dtmgrpc.csproj --version-suffix $ver -o /home/runner/work/nugetpkgs -c Release --no-build
2829
dotnet pack src/DtmCommon/DtmCommon.csproj --version-suffix $ver -o /home/runner/work/nugetpkgs -c Release --no-build
2930
dotnet pack src/DtmSERedisBarrier/DtmSERedisBarrier.csproj --version-suffix $ver -o /home/runner/work/nugetpkgs -c Release --no-build
31+
dotnet pack src/DtmMongoBarrier/DtmMongoBarrier.csproj --version-suffix $ver -o /home/runner/work/nugetpkgs -c Release --no-build
3032
- name: Upload artifact
3133
uses: actions/upload-artifact@v1
3234
with:

‎Dtmgrpc.sln‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "protos", "protos", "{A0C2C4
2222
EndProject
2323
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DtmCommon", "src\DtmCommon\DtmCommon.csproj", "{7823B7E5-77D4-49FB-9FEB-50F924D8F6E0}"
2424
EndProject
25-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DtmSERedisBarrier", "src\DtmSERedisBarrier\DtmSERedisBarrier.csproj", "{6F18F233-3FB1-4596-94A3-544B47DE9A0B}"
25+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DtmSERedisBarrier", "src\DtmSERedisBarrier\DtmSERedisBarrier.csproj", "{6F18F233-3FB1-4596-94A3-544B47DE9A0B}"
26+
EndProject
27+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DtmMongoBarrier", "src\DtmMongoBarrier\DtmMongoBarrier.csproj", "{3A7F9FCE-E5A2-4D7F-8CEB-C9EED9376693}"
2628
EndProject
2729
Global
2830
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -54,6 +56,10 @@ Global
5456
{6F18F233-3FB1-4596-94A3-544B47DE9A0B}.Debug|Any CPU.Build.0 = Debug|Any CPU
5557
{6F18F233-3FB1-4596-94A3-544B47DE9A0B}.Release|Any CPU.ActiveCfg = Release|Any CPU
5658
{6F18F233-3FB1-4596-94A3-544B47DE9A0B}.Release|Any CPU.Build.0 = Release|Any CPU
59+
{3A7F9FCE-E5A2-4D7F-8CEB-C9EED9376693}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
60+
{3A7F9FCE-E5A2-4D7F-8CEB-C9EED9376693}.Debug|Any CPU.Build.0 = Debug|Any CPU
61+
{3A7F9FCE-E5A2-4D7F-8CEB-C9EED9376693}.Release|Any CPU.ActiveCfg = Release|Any CPU
62+
{3A7F9FCE-E5A2-4D7F-8CEB-C9EED9376693}.Release|Any CPU.Build.0 = Release|Any CPU
5763
EndGlobalSection
5864
GlobalSection(SolutionProperties) = preSolution
5965
HideSolutionNode = FALSE
@@ -66,6 +72,7 @@ Global
6672
{A0C2C43F-3B7C-4E09-B7F0-CA0C9CCC34D1} = {9E0177CB-0836-4959-A672-BCC0FE6943FB}
6773
{7823B7E5-77D4-49FB-9FEB-50F924D8F6E0} = {A8C60387-7535-4481-A68D-8D69EF997BB8}
6874
{6F18F233-3FB1-4596-94A3-544B47DE9A0B} = {A8C60387-7535-4481-A68D-8D69EF997BB8}
75+
{3A7F9FCE-E5A2-4D7F-8CEB-C9EED9376693} = {A8C60387-7535-4481-A68D-8D69EF997BB8}
6976
EndGlobalSection
7077
GlobalSection(ExtensibilityGlobals) = postSolution
7178
SolutionGuid = {5B86B52A-82F8-4AE2-956B-30B95BACCC27}

‎src/DtmCommon/Barrier/BranchBarrier.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,15 @@ public async Task<string> QueryPrepared(DbConnection db)
138138
/// <param name="op"></param>
139139
/// <param name="originAffected"></param>
140140
/// <returns></returns>
141-
private bool IsNullCompensation(string op, int originAffected)
141+
internal bool IsNullCompensation(string op, int originAffected)
142142
=> (op.Equals(Cancel) || op.Equals(Compensate)) && originAffected > 0;
143143

144144
/// <summary>
145145
/// 这个是重复请求或者悬挂
146146
/// </summary>
147147
/// <param name="currentAffected"></param>
148148
/// <returns></returns>
149-
private bool IsDuplicateOrPend(int currentAffected)
149+
internal bool IsDuplicateOrPend(int currentAffected)
150150
=> currentAffected == 0;
151151

152152
/// <summary>
@@ -156,7 +156,7 @@ private bool IsDuplicateOrPend(int currentAffected)
156156
/// <param name="op">op</param>
157157
/// <param name="currentAffected">currentAffected</param>
158158
/// <returns></returns>
159-
private bool IsMsgRejected(string err, string op, int currentAffected)
159+
internal bool IsMsgRejected(string err, string op, int currentAffected)
160160
=> string.IsNullOrWhiteSpace(err) && op.Equals(Constant.TYPE_MSG) && currentAffected == 0;
161161

162162
public bool IsInValid()

‎src/DtmCommon/DtmCommon.csproj‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<RepositoryUrl>https://github.com/catcherwong/dtmgrpc-csharp</RepositoryUrl>
77
<Description>a c# client for distributed transaction framework dtm. 分布式事务管理器dtm的c#客户端</Description>
88
<PackageTags>dtm,csharp,distributed transaction,tcc,saga,msg</PackageTags>
9-
<VersionPrefix>1.1.0</VersionPrefix>
9+
<VersionPrefix>1.1.1</VersionPrefix>
1010
<VersionSuffix></VersionSuffix>
1111
<Authors>catcherwong</Authors>
1212
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
@@ -18,6 +18,7 @@
1818
<InternalsVisibleTo Include="Dtmgrpc.Tests" />
1919
<InternalsVisibleTo Include="Dtmcli" />
2020
<InternalsVisibleTo Include="Dtmcli.Tests" />
21+
<InternalsVisibleTo Include="DtmMongoBarrier" />
2122
</ItemGroup>
2223

2324
<ItemGroup>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace DtmMongoBarrier
2+
{
3+
[MongoDB.Bson.Serialization.Attributes.BsonIgnoreExtraElements]
4+
internal class DtmBarrierDocument
5+
{
6+
[MongoDB.Bson.Serialization.Attributes.BsonElement("trans_type")]
7+
public string TransType { get; set; }
8+
9+
[MongoDB.Bson.Serialization.Attributes.BsonElement("gid")]
10+
public string GId { get; set; }
11+
12+
[MongoDB.Bson.Serialization.Attributes.BsonElement("branch_id")]
13+
public string BranchId { get; set; }
14+
15+
[MongoDB.Bson.Serialization.Attributes.BsonElement("op")]
16+
public string Op { get; set; }
17+
18+
[MongoDB.Bson.Serialization.Attributes.BsonElement("barrier_id")]
19+
public string BarrierId { get; set; }
20+
21+
[MongoDB.Bson.Serialization.Attributes.BsonElement("reason")]
22+
public string Reason { get; set; }
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.1;net5.0;net6.0</TargetFrameworks>
5+
<PackageProjectUrl>https://github.com/catcherwong/dtmgrpc-csharp</PackageProjectUrl>
6+
<RepositoryUrl>https://github.com/catcherwong/dtmgrpc-csharp</RepositoryUrl>
7+
<Description>a c# client for distributed transaction framework dtm. 分布式事务管理器dtm的c#客户端</Description>
8+
<PackageTags>dtm,csharp,distributed transaction,tcc,saga,msg</PackageTags>
9+
<VersionPrefix>1.1.1</VersionPrefix>
10+
<VersionSuffix></VersionSuffix>
11+
<Authors>catcherwong</Authors>
12+
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
13+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
14+
</PropertyGroup>
15+
16+
<ItemGroup>
17+
<PackageReference Include="MongoDB.Driver" Version="2.15.1" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\DtmCommon\DtmCommon.csproj" />
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
using DtmCommon;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.Logging;
4+
using System;
5+
using MongoDB.Driver;
6+
using System.Linq;
7+
using System.Collections.Generic;
8+
9+
namespace DtmMongoBarrier
10+
{
11+
public static class MongoBranchBarrier
12+
{
13+
public static async Task MongoCall(this BranchBarrier bb, IMongoClient mc, Func<IClientSessionHandle, Task> busiCall)
14+
{
15+
bb.BarrierID = bb.BarrierID + 1;
16+
var bid = bb.BarrierID.ToString().PadLeft(2, '0');
17+
18+
var session = await mc.StartSessionAsync();
19+
20+
session.StartTransaction();
21+
22+
try
23+
{
24+
var originOp = Constant.Barrier.OpDict.TryGetValue(bb.Op, out var ot) ? ot : string.Empty;
25+
26+
var (originAffected, oErr) = await MongoInsertBarrier(bb, session, bb.BranchID, originOp, bid, bb.Op);
27+
var (currentAffected, rErr) = await MongoInsertBarrier(bb, session, bb.BranchID, bb.Op, bid, bb.Op);
28+
29+
bb?.Logger?.LogDebug("mongo originAffected: {originAffected} currentAffected: {currentAffected}", originAffected, currentAffected);
30+
31+
if (bb.IsMsgRejected(rErr, bb.Op, currentAffected))
32+
throw new DtmDuplicatedException();
33+
34+
var isNullCompensation = bb.IsNullCompensation(bb.Op, originAffected);
35+
var isDuplicateOrPend = bb.IsDuplicateOrPend(currentAffected);
36+
37+
if (isNullCompensation || isDuplicateOrPend)
38+
{
39+
bb?.Logger?.LogInformation("mongo Will not exec busiCall, isNullCompensation={isNullCompensation}, isDuplicateOrPend={isDuplicateOrPend}", isNullCompensation, isDuplicateOrPend);
40+
await session.CommitTransactionAsync();
41+
return;
42+
}
43+
44+
try
45+
{
46+
await busiCall.Invoke(session);
47+
}
48+
catch
49+
{
50+
throw;
51+
}
52+
53+
await session.CommitTransactionAsync();
54+
}
55+
catch (Exception ex)
56+
{
57+
bb?.Logger?.LogError(ex, "Mongo Call error, gid={gid}, trans_type={trans_type}", bb.Gid, bb.TransType);
58+
59+
await session.AbortTransactionAsync();
60+
61+
throw;
62+
}
63+
}
64+
65+
public static async Task<string> MongoQueryPrepared(this BranchBarrier bb, IMongoClient mc)
66+
{
67+
var session = await mc.StartSessionAsync();
68+
69+
try
70+
{
71+
await MongoInsertBarrier(
72+
bb,
73+
session,
74+
Constant.Barrier.MSG_BRANCHID,
75+
Constant.TYPE_MSG,
76+
Constant.Barrier.MSG_BARRIER_ID,
77+
Constant.Barrier.MSG_BARRIER_REASON);
78+
}
79+
catch (Exception ex)
80+
{
81+
bb?.Logger?.LogWarning(ex, "Mongo Insert Barrier error, gid={gid}", bb.Gid);
82+
return ex.Message;
83+
}
84+
85+
var reason = string.Empty;
86+
87+
try
88+
{
89+
var fs = bb.DtmOptions.BarrierTableName.Split('.');
90+
var barrier = session.Client.GetDatabase(fs[0]).GetCollection<DtmBarrierDocument>(fs[1]);
91+
92+
var filter = BuildFilters(bb.Gid, Constant.Barrier.MSG_BRANCHID, Constant.TYPE_MSG, Constant.Barrier.MSG_BARRIER_ID);
93+
var cursor = await barrier.FindAsync<DtmBarrierDocument>(filter);
94+
var res = await cursor.ToListAsync();
95+
96+
if (res != null && res.Any())
97+
{
98+
reason = res.First().Reason;
99+
if (reason.Equals(Constant.Barrier.MSG_BARRIER_REASON)) return Constant.ResultFailure;
100+
}
101+
}
102+
catch (Exception ex)
103+
{
104+
bb?.Logger?.LogWarning(ex, "Mongo Query Prepared error, gid={gid}", bb.Gid);
105+
return ex.Message;
106+
}
107+
108+
return string.Empty;
109+
}
110+
111+
private static async Task<(int, string)> MongoInsertBarrier(BranchBarrier bb, IClientSessionHandle session, string branchId, string op, string bid, string reason)
112+
{
113+
var err = string.Empty;
114+
if (session == null) return (-1, err);
115+
if (string.IsNullOrWhiteSpace(op)) return (0, err);
116+
117+
var fs = bb.DtmOptions.BarrierTableName.Split('.');
118+
var barrier = session.Client.GetDatabase(fs[0]).GetCollection<DtmBarrierDocument>(fs[1]);
119+
120+
List<DtmBarrierDocument> res = null;
121+
122+
try
123+
{
124+
var filter = BuildFilters(bb.Gid, branchId, op, bid);
125+
var cursor = await barrier.FindAsync<DtmBarrierDocument>(filter);
126+
res = await cursor.ToListAsync();
127+
}
128+
catch (Exception ex)
129+
{
130+
err = ex.Message;
131+
bb?.Logger?.LogDebug(ex, "Find document exception here, gid={gid}, branchId={branchId}, op={op}, bid={bid}", bb.Gid, branchId, op, bid);
132+
}
133+
134+
if (res == null || res.Count <= 0)
135+
{
136+
try
137+
{
138+
await barrier.InsertOneAsync(new DtmBarrierDocument
139+
{
140+
TransType = bb.TransType,
141+
GId = bb.Gid,
142+
BranchId = bb.BranchID,
143+
Op = op,
144+
BarrierId = bid,
145+
Reason = reason
146+
});
147+
}
148+
catch (Exception ex)
149+
{
150+
err = ex.Message;
151+
}
152+
153+
return (1, err);
154+
}
155+
156+
return (0, err);
157+
}
158+
159+
private static FilterDefinition<DtmBarrierDocument> BuildFilters(string gid, string branchId, string op, string barrierId)
160+
{
161+
return new FilterDefinitionBuilder<DtmBarrierDocument>().And(
162+
Builders<DtmBarrierDocument>.Filter.Eq(x => x.GId, gid),
163+
Builders<DtmBarrierDocument>.Filter.Eq(x => x.BranchId, branchId),
164+
Builders<DtmBarrierDocument>.Filter.Eq(x => x.Op, op),
165+
Builders<DtmBarrierDocument>.Filter.Eq(x => x.BarrierId, barrierId)
166+
);
167+
}
168+
}
169+
}

‎src/DtmSERedisBarrier/DtmSERedisBarrier.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<RepositoryUrl>https://github.com/catcherwong/dtmgrpc-csharp</RepositoryUrl>
77
<Description>a c# client for distributed transaction framework dtm. 分布式事务管理器dtm的c#客户端</Description>
88
<PackageTags>dtm,csharp,distributed transaction,tcc,saga,msg</PackageTags>
9-
<VersionPrefix>1.1.0</VersionPrefix>
9+
<VersionPrefix>1.1.1</VersionPrefix>
1010
<VersionSuffix></VersionSuffix>
1111
<Authors>catcherwong</Authors>
1212
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>

‎src/Dtmgrpc/Dtmgrpc.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<RepositoryUrl>https://github.com/catcherwong/dtmgrpc-csharp</RepositoryUrl>
77
<Description>a c# client for distributed transaction framework dtm. 分布式事务管理器dtm的c#客户端</Description>
88
<PackageTags>dtm,csharp,distributed transaction,tcc,saga,msg</PackageTags>
9-
<VersionPrefix>1.1.0</VersionPrefix>
9+
<VersionPrefix>1.1.1</VersionPrefix>
1010
<VersionSuffix></VersionSuffix>
1111
<Authors>catcherwong</Authors>
1212
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /