Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
丁松杰
/
Pole
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
b7b7cb3c
authored
5 years ago
by
dingsongjie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化
parent
ede3fb7e
master
…
v1.0.0
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
77 additions
and
39 deletions
src/Pole.Sagas.Server/Services/SagaService.cs
src/Pole.Sagas.Storage.PostgreSql/PostgreSqlSagaStorage.cs
src/Pole.Sagas/Core/Abstraction/IEventSender.cs
src/Pole.Sagas/Core/ActivityStatus.cs
src/Pole.Sagas/Core/EventSender.cs
src/Pole.Sagas/Core/ISagaStorage.cs
src/Pole.Sagas/Core/Saga.cs
src/Pole.Sagas/Core/SagaStatus.cs
src/Pole.Sagas/Protos/saga.proto
src/Pole.Sagas.Server/Services/SagaService.cs
View file @
b7b7cb3c
...
@@ -48,7 +48,7 @@ namespace Pole.Sagas.Server.Services
...
@@ -48,7 +48,7 @@ namespace Pole.Sagas.Server.Services
CommonResponse
commonResponse
=
new
CommonResponse
();
CommonResponse
commonResponse
=
new
CommonResponse
();
try
try
{
{
await
sagaStorage
.
ActivityE
nd
ed
(
request
.
ActivityId
,
request
.
ResultData
.
ToByteArray
());
await
sagaStorage
.
ActivityE
xecut
ed
(
request
.
ActivityId
,
request
.
ResultData
.
ToByteArray
());
commonResponse
.
IsSuccess
=
true
;
commonResponse
.
IsSuccess
=
true
;
}
}
catch
(
Exception
ex
)
catch
(
Exception
ex
)
...
@@ -62,7 +62,7 @@ namespace Pole.Sagas.Server.Services
...
@@ -62,7 +62,7 @@ namespace Pole.Sagas.Server.Services
CommonResponse
commonResponse
=
new
CommonResponse
();
CommonResponse
commonResponse
=
new
CommonResponse
();
try
try
{
{
await
sagaStorage
.
ActivityExecuteAborted
(
request
.
ActivityId
,
request
.
Errors
);
await
sagaStorage
.
ActivityExecuteAborted
(
request
.
ActivityId
);
commonResponse
.
IsSuccess
=
true
;
commonResponse
.
IsSuccess
=
true
;
}
}
catch
(
Exception
ex
)
catch
(
Exception
ex
)
...
@@ -76,7 +76,7 @@ namespace Pole.Sagas.Server.Services
...
@@ -76,7 +76,7 @@ namespace Pole.Sagas.Server.Services
CommonResponse
commonResponse
=
new
CommonResponse
();
CommonResponse
commonResponse
=
new
CommonResponse
();
try
try
{
{
await
sagaStorage
.
ActivityExecuteOvertime
(
request
.
ActivityId
,
request
.
SagaId
,
request
.
Errors
);
await
sagaStorage
.
ActivityExecuteOvertime
(
request
.
ActivityId
);
commonResponse
.
IsSuccess
=
true
;
commonResponse
.
IsSuccess
=
true
;
}
}
catch
(
Exception
ex
)
catch
(
Exception
ex
)
...
@@ -90,7 +90,7 @@ namespace Pole.Sagas.Server.Services
...
@@ -90,7 +90,7 @@ namespace Pole.Sagas.Server.Services
CommonResponse
commonResponse
=
new
CommonResponse
();
CommonResponse
commonResponse
=
new
CommonResponse
();
try
try
{
{
await
sagaStorage
.
ActivityExecut
eStarted
(
request
.
ActivityId
,
request
.
SagaId
,
request
.
TimeOutSeconds
,
request
.
ParameterData
.
ToByteArray
(),
request
.
Order
,
Convert
.
ToDateTime
(
request
.
AddTime
));
await
sagaStorage
.
ActivityExecut
ing
(
request
.
ActivityId
,
request
.
SagaId
,
request
.
ParameterData
.
ToByteArray
(),
request
.
Order
,
Convert
.
ToDateTime
(
request
.
AddTime
));
commonResponse
.
IsSuccess
=
true
;
commonResponse
.
IsSuccess
=
true
;
}
}
catch
(
Exception
ex
)
catch
(
Exception
ex
)
...
@@ -156,6 +156,20 @@ namespace Pole.Sagas.Server.Services
...
@@ -156,6 +156,20 @@ namespace Pole.Sagas.Server.Services
}
}
return
commonResponse
;
return
commonResponse
;
}
}
public
override
async
Task
<
CommonResponse
>
ActivityCompensating
(
ActivityCompensatingRequest
request
,
ServerCallContext
context
)
{
CommonResponse
commonResponse
=
new
CommonResponse
();
try
{
await
sagaStorage
.
ActivityCompensating
(
request
.
ActivityId
);
commonResponse
.
IsSuccess
=
true
;
}
catch
(
Exception
ex
)
{
commonResponse
.
Errors
=
CombineError
(
ex
);
}
return
commonResponse
;
}
private
string
CombineError
(
Exception
exception
)
private
string
CombineError
(
Exception
exception
)
{
{
return
exception
.
InnerException
!=
null
?
exception
.
InnerException
.
Message
+
exception
.
StackTrace
:
exception
.
Message
+
exception
.
StackTrace
;
return
exception
.
InnerException
!=
null
?
exception
.
InnerException
.
Message
+
exception
.
StackTrace
:
exception
.
Message
+
exception
.
StackTrace
;
...
...
This diff is collapsed.
Click to expand it.
src/Pole.Sagas.Storage.PostgreSql/PostgreSqlSagaStorage.cs
View file @
b7b7cb3c
...
@@ -67,22 +67,49 @@ $"UPDATE {activityTableName} SET \"Status\"=@Status WHERE \"Id\" = @Id";
...
@@ -67,22 +67,49 @@ $"UPDATE {activityTableName} SET \"Status\"=@Status WHERE \"Id\" = @Id";
}
}
}
}
public
Task
ActivityEnd
ed
(
string
activityId
,
byte
[]
resultData
)
public
async
Task
ActivityExecut
ed
(
string
activityId
,
byte
[]
resultData
)
{
{
throw
new
NotImplementedException
();
using
(
var
connection
=
new
NpgsqlConnection
(
poleSagasStoragePostgreSqlOption
.
ConnectionString
))
{
var
updateActivitySql
=
$"UPDATE
{
activityTableName
}
SET \"Status\"=@Status \"ResultData\"=@ResultData WHERE \"Id\" = @Id"
;
await
connection
.
ExecuteAsync
(
updateActivitySql
,
new
{
Id
=
activityId
,
Status
=
nameof
(
ActivityStatus
.
Executed
)
});
}
}
}
public
Task
ActivityExecuteAborted
(
string
activityId
,
string
errors
)
public
async
Task
ActivityExecuteAborted
(
string
activityId
)
{
{
throw
new
NotImplementedException
();
using
(
var
connection
=
new
NpgsqlConnection
(
poleSagasStoragePostgreSqlOption
.
ConnectionString
))
{
var
updateActivitySql
=
$"UPDATE
{
activityTableName
}
SET \"Status\"=@Status WHERE \"Id\" = @Id"
;
await
connection
.
ExecuteAsync
(
updateActivitySql
,
new
{
Id
=
activityId
,
Status
=
nameof
(
ActivityStatus
.
ExecuteAborted
)
});
}
}
}
public
Task
ActivityExecuteOvertime
(
string
activityId
,
string
sagaId
,
string
errors
)
public
async
Task
ActivityExecuteOvertime
(
string
activityId
)
{
{
throw
new
NotImplementedException
();
using
(
var
connection
=
new
NpgsqlConnection
(
poleSagasStoragePostgreSqlOption
.
ConnectionString
))
{
var
updateActivitySql
=
$"UPDATE
{
activityTableName
}
SET \"Status\"=@Status WHERE \"Id\" = @Id"
;
await
connection
.
ExecuteAsync
(
updateActivitySql
,
new
{
Id
=
activityId
,
Status
=
nameof
(
ActivityStatus
.
ExecutingOvertime
)
});
}
}
}
public
Task
ActivityExecut
eStarted
(
string
activityId
,
string
sagaId
,
int
timeOutSeconds
,
byte
[]
ParameterData
,
int
order
,
DateTime
addTime
)
public
Task
ActivityExecut
ing
(
string
activityId
,
string
sagaId
,
byte
[]
ParameterData
,
int
order
,
DateTime
addTime
)
{
{
throw
new
NotImplementedException
();
throw
new
NotImplementedException
();
}
}
...
@@ -106,5 +133,10 @@ $"UPDATE {activityTableName} SET \"Status\"=@Status WHERE \"Id\" = @Id";
...
@@ -106,5 +133,10 @@ $"UPDATE {activityTableName} SET \"Status\"=@Status WHERE \"Id\" = @Id";
{
{
throw
new
NotImplementedException
();
throw
new
NotImplementedException
();
}
}
public
Task
ActivityCompensating
(
string
activityId
)
{
throw
new
NotImplementedException
();
}
}
}
}
}
This diff is collapsed.
Click to expand it.
src/Pole.Sagas/Core/Abstraction/IEventSender.cs
View file @
b7b7cb3c
...
@@ -7,13 +7,13 @@ namespace Pole.Sagas.Core.Abstraction
...
@@ -7,13 +7,13 @@ namespace Pole.Sagas.Core.Abstraction
{
{
Task
SagaStarted
(
string
sagaId
,
string
serviceName
,
DateTime
addTime
);
Task
SagaStarted
(
string
sagaId
,
string
serviceName
,
DateTime
addTime
);
Task
SagaEnded
(
string
sagaId
,
DateTime
ExpiresAt
);
Task
SagaEnded
(
string
sagaId
,
DateTime
ExpiresAt
);
Task
ActivityExecuting
(
string
activityId
,
string
sagaId
,
int
timeOutSeconds
,
byte
[]
parameterData
,
int
order
,
DateTime
addTime
);
Task
ActivityExecuting
(
string
activityId
,
string
sagaId
,
byte
[]
parameterData
,
int
order
,
DateTime
addTime
);
Task
ActivityRetried
(
string
activityId
,
string
status
,
int
retries
,
ActivityRetryType
retryType
);
Task
ActivityRetried
(
string
activityId
,
string
status
,
int
retries
,
ActivityRetryType
retryType
);
Task
ActivityExecuteAborted
(
string
activityId
,
string
errors
);
Task
ActivityExecuteAborted
(
string
activityId
);
Task
ActivityCompensateAborted
(
string
activityId
,
string
sagaId
,
string
errors
);
Task
ActivityCompensateAborted
(
string
activityId
,
string
sagaId
,
string
errors
);
Task
ActivityExecuted
(
string
activityId
,
byte
[]
resultData
);
Task
ActivityExecuted
(
string
activityId
,
byte
[]
resultData
);
Task
ActivityCompensated
(
string
activityId
);
Task
ActivityCompensated
(
string
activityId
);
Task
ActivityExecuteOvertime
(
string
activityId
,
string
sagaId
,
string
errors
);
Task
ActivityExecuteOvertime
(
string
activityId
);
Task
ActivityRevoked
(
string
activityId
);
Task
ActivityRevoked
(
string
activityId
);
Task
ActivityCompensating
(
string
activityId
);
Task
ActivityCompensating
(
string
activityId
);
}
}
...
...
This diff is collapsed.
Click to expand it.
src/Pole.Sagas/Core/ActivityStatus.cs
View file @
b7b7cb3c
...
@@ -14,6 +14,6 @@ namespace Pole.Sagas.Core
...
@@ -14,6 +14,6 @@ namespace Pole.Sagas.Core
ExecuteAborted
,
ExecuteAborted
,
Revoked
,
Revoked
,
CompensateAborted
,
CompensateAborted
,
Overtime
Executing
Overtime
}
}
}
}
This diff is collapsed.
Click to expand it.
src/Pole.Sagas/Core/EventSender.cs
View file @
b7b7cb3c
...
@@ -56,12 +56,11 @@ namespace Pole.Sagas.Core
...
@@ -56,12 +56,11 @@ namespace Pole.Sagas.Core
}
}
}
}
public
async
Task
ActivityExecuteAborted
(
string
activityId
,
string
errors
)
public
async
Task
ActivityExecuteAborted
(
string
activityId
)
{
{
var
result
=
await
sagaClient
.
ActivityExecuteAbortedAsync
(
new
Server
.
Grpc
.
ActivityExecuteAbortedRequest
var
result
=
await
sagaClient
.
ActivityExecuteAbortedAsync
(
new
Server
.
Grpc
.
ActivityExecuteAbortedRequest
{
{
ActivityId
=
activityId
,
ActivityId
=
activityId
Errors
=
errors
});
});
if
(!
result
.
IsSuccess
)
if
(!
result
.
IsSuccess
)
{
{
...
@@ -84,7 +83,7 @@ namespace Pole.Sagas.Core
...
@@ -84,7 +83,7 @@ namespace Pole.Sagas.Core
}
}
}
}
public
async
Task
ActivityExecuting
(
string
activityId
,
string
sagaId
,
int
timeoutSeconds
,
byte
[]
parameterData
,
int
order
,
DateTime
addTime
)
public
async
Task
ActivityExecuting
(
string
activityId
,
string
sagaId
,
byte
[]
parameterData
,
int
order
,
DateTime
addTime
)
{
{
var
result
=
await
sagaClient
.
ActivityExecutingAsync
(
new
Server
.
Grpc
.
ActivityExecutingRequest
var
result
=
await
sagaClient
.
ActivityExecutingAsync
(
new
Server
.
Grpc
.
ActivityExecutingRequest
{
{
...
@@ -93,7 +92,6 @@ namespace Pole.Sagas.Core
...
@@ -93,7 +92,6 @@ namespace Pole.Sagas.Core
Order
=
order
,
Order
=
order
,
ParameterData
=
Google
.
Protobuf
.
ByteString
.
CopyFrom
(
parameterData
),
ParameterData
=
Google
.
Protobuf
.
ByteString
.
CopyFrom
(
parameterData
),
SagaId
=
sagaId
,
SagaId
=
sagaId
,
TimeOutSeconds
=
timeoutSeconds
});
});
if
(!
result
.
IsSuccess
)
if
(!
result
.
IsSuccess
)
{
{
...
@@ -128,13 +126,11 @@ namespace Pole.Sagas.Core
...
@@ -128,13 +126,11 @@ namespace Pole.Sagas.Core
}
}
}
}
public
async
Task
ActivityExecuteOvertime
(
string
activityId
,
string
sagaId
,
string
errors
)
public
async
Task
ActivityExecuteOvertime
(
string
activityId
)
{
{
var
result
=
await
sagaClient
.
ActivityExecuteOvertimeAsync
(
new
Server
.
Grpc
.
ActivityExecuteOvertimeRequest
var
result
=
await
sagaClient
.
ActivityExecuteOvertimeAsync
(
new
Server
.
Grpc
.
ActivityExecuteOvertimeRequest
{
{
SagaId
=
sagaId
,
ActivityId
=
activityId
,
ActivityId
=
activityId
,
Errors
=
errors
});
});
if
(!
result
.
IsSuccess
)
if
(!
result
.
IsSuccess
)
{
{
...
...
This diff is collapsed.
Click to expand it.
src/Pole.Sagas/Core/ISagaStorage.cs
View file @
b7b7cb3c
...
@@ -10,13 +10,14 @@ namespace Pole.Sagas.Core
...
@@ -10,13 +10,14 @@ namespace Pole.Sagas.Core
{
{
Task
SagaStarted
(
string
sagaId
,
string
serviceName
,
DateTime
addTime
);
Task
SagaStarted
(
string
sagaId
,
string
serviceName
,
DateTime
addTime
);
Task
SagaEnded
(
string
sagaId
,
DateTime
ExpiresAt
);
Task
SagaEnded
(
string
sagaId
,
DateTime
ExpiresAt
);
Task
ActivityExecut
eStarted
(
string
activityId
,
string
sagaId
,
int
timeOutSeconds
,
byte
[]
ParameterData
,
int
order
,
DateTime
addTime
);
Task
ActivityExecut
ing
(
string
activityId
,
string
sagaId
,
byte
[]
ParameterData
,
int
order
,
DateTime
addTime
);
Task
ActivityRetried
(
string
activityId
,
string
status
,
int
retries
,
ActivityRetryType
retryType
);
Task
ActivityRetried
(
string
activityId
,
string
status
,
int
retries
,
ActivityRetryType
retryType
);
Task
ActivityExecuteAborted
(
string
activityId
,
string
errors
);
Task
ActivityExecuteAborted
(
string
activityId
);
Task
ActivityCompensateAborted
(
string
activityId
,
string
sagaId
,
string
errors
);
Task
ActivityCompensateAborted
(
string
activityId
,
string
sagaId
,
string
errors
);
Task
ActivityE
nd
ed
(
string
activityId
,
byte
[]
resultData
);
Task
ActivityE
xecut
ed
(
string
activityId
,
byte
[]
resultData
);
Task
ActivityCompensated
(
string
activityId
);
Task
ActivityCompensated
(
string
activityId
);
Task
ActivityExecuteOvertime
(
string
activityId
,
string
sagaId
,
string
errors
);
Task
ActivityExecuteOvertime
(
string
activityId
);
Task
ActivityRevoked
(
string
activityId
);
Task
ActivityRevoked
(
string
activityId
);
Task
ActivityCompensating
(
string
activityId
);
}
}
}
}
This diff is collapsed.
Click to expand it.
src/Pole.Sagas/Core/Saga.cs
View file @
b7b7cb3c
...
@@ -142,7 +142,7 @@ namespace Pole.Sagas.Core
...
@@ -142,7 +142,7 @@ namespace Pole.Sagas.Core
try
try
{
{
var
bytesContent
=
serializer
.
SerializeToUtf8Bytes
(
activityWapper
.
DataObj
,
activityWapper
.
ActivityDataType
);
var
bytesContent
=
serializer
.
SerializeToUtf8Bytes
(
activityWapper
.
DataObj
,
activityWapper
.
ActivityDataType
);
await
eventSender
.
ActivityExecuting
(
activityId
,
Id
,
activityWapper
.
TimeOutSeconds
,
bytesContent
,
activityWapper
.
Order
,
DateTime
.
UtcNow
);
await
eventSender
.
ActivityExecuting
(
activityId
,
Id
,
bytesContent
,
activityWapper
.
Order
,
DateTime
.
UtcNow
);
var
result
=
await
activityWapper
.
InvokeExecute
();
var
result
=
await
activityWapper
.
InvokeExecute
();
if
(!
result
.
IsSuccess
)
if
(!
result
.
IsSuccess
)
{
{
...
@@ -171,7 +171,7 @@ namespace Pole.Sagas.Core
...
@@ -171,7 +171,7 @@ namespace Pole.Sagas.Core
IsSuccess
=
false
,
IsSuccess
=
false
,
Errors
=
errors
Errors
=
errors
};
};
await
eventSender
.
ActivityExecuteOvertime
(
activityId
,
Id
,
errors
);
await
eventSender
.
ActivityExecuteOvertime
(
activityId
);
// 超时的时候 需要首先补偿这个超时的操作
// 超时的时候 需要首先补偿这个超时的操作
return
await
CompensateActivity
(
result
,
currentExecuteOrder
+
1
);
return
await
CompensateActivity
(
result
,
currentExecuteOrder
+
1
);
}
}
...
@@ -183,7 +183,7 @@ namespace Pole.Sagas.Core
...
@@ -183,7 +183,7 @@ namespace Pole.Sagas.Core
IsSuccess
=
false
,
IsSuccess
=
false
,
Errors
=
errors
Errors
=
errors
};
};
await
eventSender
.
ActivityExecuteAborted
(
activityId
,
errors
);
await
eventSender
.
ActivityExecuteAborted
(
activityId
);
// 出错的时候 需要首先补偿这个出错的操作
// 出错的时候 需要首先补偿这个出错的操作
return
await
CompensateActivity
(
result
,
currentExecuteOrder
+
1
);
return
await
CompensateActivity
(
result
,
currentExecuteOrder
+
1
);
}
}
...
...
This diff is collapsed.
Click to expand it.
src/Pole.Sagas/Core/SagaStatus.cs
View file @
b7b7cb3c
...
@@ -8,7 +8,6 @@ namespace Pole.Sagas.Core
...
@@ -8,7 +8,6 @@ namespace Pole.Sagas.Core
{
{
Started
,
Started
,
Ended
,
Ended
,
Error
,
Error
Overtime
}
}
}
}
This diff is collapsed.
Click to expand it.
src/Pole.Sagas/Protos/saga.proto
View file @
b7b7cb3c
...
@@ -35,10 +35,9 @@ message SagaEndedRequest {
...
@@ -35,10 +35,9 @@ message SagaEndedRequest {
message
ActivityExecutingRequest
{
message
ActivityExecutingRequest
{
string
activityId
=
1
;
string
activityId
=
1
;
string
sagaId
=
2
;
string
sagaId
=
2
;
int32
timeOutSeconds
=
3
;
bytes
parameterData
=
3
;
bytes
parameterData
=
4
;
int32
order
=
4
;
int32
order
=
5
;
string
addTime
=
5
;
string
addTime
=
6
;
}
}
message
ActivityRetriedRequest
{
message
ActivityRetriedRequest
{
string
activityId
=
1
;
string
activityId
=
1
;
...
@@ -52,7 +51,6 @@ message ActivityRetriedRequest {
...
@@ -52,7 +51,6 @@ message ActivityRetriedRequest {
}
}
message
ActivityExecuteAbortedRequest
{
message
ActivityExecuteAbortedRequest
{
string
activityId
=
1
;
string
activityId
=
1
;
string
errors
=
2
;
}
}
message
ActivityCompensateAbortedRequest
{
message
ActivityCompensateAbortedRequest
{
string
activityId
=
1
;
string
activityId
=
1
;
...
@@ -68,8 +66,6 @@ message ActivityCompensatedRequest {
...
@@ -68,8 +66,6 @@ message ActivityCompensatedRequest {
}
}
message
ActivityExecuteOvertimeRequest
{
message
ActivityExecuteOvertimeRequest
{
string
activityId
=
1
;
string
activityId
=
1
;
string
sagaId
=
2
;
string
errors
=
3
;
}
}
message
ActivityRevokedRequest
{
message
ActivityRevokedRequest
{
string
activityId
=
1
;
string
activityId
=
1
;
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment