保存不确定部署处置重新核查实现及测试检查点
This commit is contained in:
1 parent
7fa5e9b3d2
commit
f9cd19f631
29 files changed
+907
-49
No files matched your search
@@ -30,7 +30,7 @@ func validRecoveryRequest(request Request, nonce string) bool {
|
||||
return request.Cursor == "" && validRecoveryID(request.TransactionID) && validRecoveryAction(request.RecoveryAction) && request.DispositionID == "" && request.ConfirmationToken == ""
|
||||
case "recovery_execute":
|
||||
return request.Cursor == "" && request.TransactionID == "" && request.RecoveryAction == "" && validRecoverySecret(request.DispositionID) && validRecoverySecret(request.ConfirmationToken)
|
||||
case "recovery_result":
|
||||
case "recovery_result", "recovery_reconcile_prepare":
|
||||
return request.Cursor == "" && request.TransactionID == "" && request.RecoveryAction == "" && validRecoverySecret(request.DispositionID) && request.ConfirmationToken == ""
|
||||
}
|
||||
return false
|
||||
|
||||
@@ -21,6 +21,10 @@ type RecoveryDispositionSession interface {
|
||||
GetRecoveryDisposition(context.Context, string) (deployment.RecoveryDispositionSummary, error)
|
||||
}
|
||||
|
||||
type RecoveryReconciliationSession interface {
|
||||
PrepareRecoveryReconciliation(context.Context, string) (RecoveryPreparation, error)
|
||||
}
|
||||
|
||||
func validRecoveryAction(action deployment.RecoveryAction) bool {
|
||||
return action == deployment.RecoveryActionRecord || action == deployment.RecoveryActionCommit || action == deployment.RecoveryActionRollback
|
||||
}
|
||||
@@ -60,6 +64,11 @@ func validDispositionResponse(request Request, response Response) bool {
|
||||
return response.Disposition == nil && validPreparation(response.Preparation) &&
|
||||
response.Preparation.Summary.TransactionID == request.TransactionID && response.Preparation.Summary.Action == request.RecoveryAction
|
||||
}
|
||||
if request.Action == "recovery_reconcile_prepare" {
|
||||
return response.Disposition == nil && validPreparation(response.Preparation) &&
|
||||
response.Preparation.Summary.SourceDispositionID == request.DispositionID &&
|
||||
response.Preparation.DispositionID != request.DispositionID
|
||||
}
|
||||
return response.Preparation == nil && response.Disposition != nil && response.Disposition.Validate() == nil && response.Disposition.ID == request.DispositionID
|
||||
}
|
||||
|
||||
@@ -70,6 +79,14 @@ func dispatchRecoveryDisposition(ctx context.Context, session RecoverySession, r
|
||||
}
|
||||
var err error
|
||||
switch request.Action {
|
||||
case "recovery_reconcile_prepare":
|
||||
reconciler, ok := session.(RecoveryReconciliationSession)
|
||||
if !ok {
|
||||
return ErrUnavailable
|
||||
}
|
||||
var value RecoveryPreparation
|
||||
value, err = reconciler.PrepareRecoveryReconciliation(ctx, request.DispositionID)
|
||||
response.Preparation = &value
|
||||
case "recovery_prepare":
|
||||
var value RecoveryPreparation
|
||||
value, err = actions.PrepareRecovery(ctx, request.TransactionID, request.RecoveryAction)
|
||||
@@ -101,6 +118,14 @@ func (c *Client) PrepareRecovery(id string, action deployment.RecoveryAction) (R
|
||||
return *response.Preparation, nil
|
||||
}
|
||||
|
||||
func (c *Client) PrepareRecoveryReconciliation(sourceID string) (RecoveryPreparation, error) {
|
||||
response, err := c.recoveryExchange(Request{Action: "recovery_reconcile_prepare", DispositionID: sourceID})
|
||||
if err != nil {
|
||||
return RecoveryPreparation{}, err
|
||||
}
|
||||
return *response.Preparation, nil
|
||||
}
|
||||
|
||||
func (c *Client) ExecuteRecovery(id, token string) (deployment.RecoveryDispositionSummary, error) {
|
||||
response, err := c.recoveryExchange(Request{Action: "recovery_execute", DispositionID: id, ConfirmationToken: token})
|
||||
if err != nil {
|
||||
|
||||
@@ -89,6 +89,7 @@ func TestDispositionRequestFieldIsolation(t *testing.T) {
|
||||
{Action: "recovery_prepare", TransactionID: "tx-1", RecoveryAction: deployment.RecoveryActionRecord},
|
||||
{Action: "recovery_execute", DispositionID: strings.Repeat("b", 64), ConfirmationToken: strings.Repeat("c", 64)},
|
||||
{Action: "recovery_result", DispositionID: strings.Repeat("b", 64)},
|
||||
{Action: "recovery_reconcile_prepare", DispositionID: strings.Repeat("b", 64)},
|
||||
}
|
||||
fields := []struct {
|
||||
name string
|
||||
@@ -101,7 +102,7 @@ func TestDispositionRequestFieldIsolation(t *testing.T) {
|
||||
{"cursor", func(r *Request) { r.Cursor = "tx-0" }, map[string]bool{"recovery_list": true}},
|
||||
{"transaction", func(r *Request) { r.TransactionID = "tx-1" }, map[string]bool{"recovery_inspect": true, "recovery_prepare": true}},
|
||||
{"action", func(r *Request) { r.RecoveryAction = deployment.RecoveryActionRecord }, map[string]bool{"recovery_prepare": true}},
|
||||
{"id", func(r *Request) { r.DispositionID = strings.Repeat("b", 64) }, map[string]bool{"recovery_execute": true, "recovery_result": true}},
|
||||
{"id", func(r *Request) { r.DispositionID = strings.Repeat("b", 64) }, map[string]bool{"recovery_execute": true, "recovery_result": true, "recovery_reconcile_prepare": true}},
|
||||
{"token", func(r *Request) { r.ConfirmationToken = strings.Repeat("c", 64) }, map[string]bool{"recovery_execute": true}},
|
||||
}
|
||||
for _, request := range requests {
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package localreset
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/deployment"
|
||||
)
|
||||
|
||||
type reconciliationSession struct { dispositionTestSession }
|
||||
|
||||
func (s *reconciliationSession) PrepareRecoveryReconciliation(ctx context.Context, sourceID string) (RecoveryPreparation, error) {
|
||||
value, err := s.PrepareRecovery(ctx, "tx-1", deployment.RecoveryActionVerifyCommit)
|
||||
value.Summary.SourceDispositionID = sourceID
|
||||
value.Summary.Phase = deployment.TransactionPhaseCommitVerified
|
||||
s.prepared = value
|
||||
return value, err
|
||||
}
|
||||
|
||||
func (s *reconciliationSession) ExecuteRecovery(ctx context.Context, id, token string) (deployment.RecoveryDispositionSummary, error) {
|
||||
sourceID := s.prepared.Summary.SourceDispositionID
|
||||
value, err := s.dispositionTestSession.ExecuteRecovery(ctx, id, token)
|
||||
if err != nil { return value, err }
|
||||
value.SourceDispositionID, value.ResultCode = sourceID, deployment.RecoveryResultCommitted
|
||||
s.result = value
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func TestRecoveryReconciliationProtocolAndQuery(t *testing.T) {
|
||||
session := &reconciliationSession{}
|
||||
_, conn, done := recoveryConnection(t, session)
|
||||
client, err := Begin(t.Context(), conn)
|
||||
if err != nil { t.Fatal(err) }
|
||||
sourceID := strings.Repeat("a", 64)
|
||||
prepared, err := client.PrepareRecoveryReconciliation(sourceID)
|
||||
if err != nil || prepared.Summary.SourceDispositionID != sourceID { t.Fatal("原处置绑定丢失", err) }
|
||||
result, err := client.ExecuteRecovery(prepared.DispositionID, prepared.Token)
|
||||
if err != nil || result.SourceDispositionID != sourceID || result.Status != "succeeded" { t.Fatal("重新核查结果无效", err) }
|
||||
queried, err := client.GetRecoveryDisposition(result.ID)
|
||||
if err != nil || queried != result { t.Fatal("查询改变了关联结果", err) }
|
||||
if _, err := client.ExecuteRecovery(prepared.DispositionID, prepared.Token); err == nil { t.Fatal("旧令牌被重放") }
|
||||
_ = client.Close()
|
||||
<-done
|
||||
if session.prepares.Load() != 1 || session.executes.Load() != 2 || session.queries.Load() != 1 || session.resets.Load() != 0 { t.Fatal("调用了错误业务入口") }
|
||||
}
|
||||
|
||||
func TestRecoveryReconciliationRejectsUnboundResponse(t *testing.T) {
|
||||
sourceID := strings.Repeat("a", 64)
|
||||
session := &reconciliationSession{}
|
||||
prepared, err := session.PrepareRecoveryReconciliation(t.Context(), sourceID)
|
||||
if err != nil { t.Fatal(err) }
|
||||
request := Request{Action: "recovery_reconcile_prepare", DispositionID: sourceID}
|
||||
for _, mode := range []string{"source", "action", "same-id", "transaction", "mixed", "expired-range"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
value := prepared
|
||||
response := Response{Preparation: &value}
|
||||
switch mode {
|
||||
case "source": value.Summary.SourceDispositionID = strings.Repeat("d", 64)
|
||||
case "action": value.Summary.Action = deployment.RecoveryActionCommit
|
||||
case "same-id": value.DispositionID = sourceID
|
||||
case "transaction": value.Summary.TransactionID = "../bad"
|
||||
case "mixed": response.Disposition = &deployment.RecoveryDispositionSummary{}
|
||||
case "expired-range": value.Summary.ExpiresAt = value.Summary.ObservedAt.Add(2*time.Minute)
|
||||
}
|
||||
if validDispositionResponse(request, response) { t.Fatal("接受了混合或无绑定准备响应") }
|
||||
})
|
||||
}
|
||||
if validRecoveryAction(deployment.RecoveryActionVerifyCommit) || validRecoveryAction(deployment.RecoveryActionVerifyRollback) { t.Fatal("普通准备允许绕过原处置绑定") }
|
||||
}
|
||||
|
||||
func TestRecoveryReconciliationOldServerFailsClosed(t *testing.T) {
|
||||
session := &dispositionTestSession{}
|
||||
_, conn, done := recoveryConnection(t, session)
|
||||
client, err := Begin(t.Context(), conn)
|
||||
if err != nil { t.Fatal(err) }
|
||||
if _, err := client.PrepareRecoveryReconciliation(strings.Repeat("a", 64)); err == nil { t.Fatal("旧服务错误地支持了重新核查") }
|
||||
_ = client.Close()
|
||||
<-done
|
||||
if session.prepares.Load() != 0 || session.executes.Load() != 0 { t.Fatal("旧服务错误地执行了原动作") }
|
||||
}
|
||||
@@ -25,7 +25,7 @@ func (s *Server) handleRecovery(ctx context.Context, conn net.Conn, reader *bufi
|
||||
case "recovery_inspect":
|
||||
inspection, readErr := session.InspectRecovery(ctx, request.TransactionID)
|
||||
err, response.Inspection = readErr, &inspection
|
||||
case "recovery_prepare", "recovery_execute", "recovery_result":
|
||||
case "recovery_prepare", "recovery_execute", "recovery_result", "recovery_reconcile_prepare":
|
||||
err = dispatchRecoveryDisposition(ctx, session, request, &response)
|
||||
default:
|
||||
err = ErrProtocol
|
||||
|
||||
Reference in new issue
Block a user