You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

fence_api.go 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package fence
  18. import (
  19. "context"
  20. "database/sql"
  21. "fmt"
  22. "github.com/seata/seata-go/pkg/common/errors"
  23. "github.com/seata/seata-go/pkg/common/log"
  24. "github.com/seata/seata-go/pkg/rm/tcc/fence/enum"
  25. "github.com/seata/seata-go/pkg/rm/tcc/fence/handler"
  26. "github.com/seata/seata-go/pkg/tm"
  27. )
  28. // WithFence This method is a suspended API interface that asserts the phase timing of a transaction
  29. // and performs corresponding database operations to ensure transaction consistency
  30. // case 1: if fencePhase is FencePhaseNotExist, will return a fence not found error.
  31. // case 2: if fencePhase is FencePhasePrepare, will do prepare fence operation.
  32. // case 3: if fencePhase is FencePhaseCommit, will do commit fence operation.
  33. // case 4: if fencePhase is FencePhaseRollback, will do rollback fence operation.
  34. // case 5: if fencePhase not in above case, will return a fence phase illegal error.
  35. func WithFence(ctx context.Context, tx *sql.Tx, callback func() error) (err error) {
  36. fp := tm.GetFencePhase(ctx)
  37. h := handler.GetFenceHandler()
  38. switch {
  39. case fp == enum.FencePhaseNotExist:
  40. err = errors.NewTccFenceError(
  41. errors.FencePhaseError,
  42. fmt.Sprintf("xid %s, tx name %s, fence phase not exist", tm.GetXID(ctx), tm.GetTxName(ctx)),
  43. nil,
  44. )
  45. case fp == enum.FencePhasePrepare:
  46. err = h.PrepareFence(ctx, tx, callback)
  47. case fp == enum.FencePhaseCommit:
  48. err = h.CommitFence(ctx, tx, callback)
  49. case fp == enum.FencePhaseRollback:
  50. err = h.RollbackFence(ctx, tx, callback)
  51. default:
  52. err = errors.NewTccFenceError(
  53. errors.FencePhaseError,
  54. fmt.Sprintf("fence phase: %v illegal", fp),
  55. nil,
  56. )
  57. }
  58. if err != nil {
  59. log.Error(err)
  60. }
  61. return
  62. }