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

index.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package db
  5. import (
  6. "context"
  7. "errors"
  8. "fmt"
  9. "code.gitea.io/gitea/modules/setting"
  10. )
  11. // ResourceIndex represents a resource index which could be used as issue/release and others
  12. // We can create different tables i.e. issue_index, release_index and etc.
  13. type ResourceIndex struct {
  14. GroupID int64 `xorm:"pk"`
  15. MaxIndex int64 `xorm:"index"`
  16. }
  17. // UpsertResourceIndex the function will not return until it acquires the lock or receives an error.
  18. func UpsertResourceIndex(e Engine, tableName string, groupID int64) (err error) {
  19. // An atomic UPSERT operation (INSERT/UPDATE) is the only operation
  20. // that ensures that the key is actually locked.
  21. switch {
  22. case setting.Database.UseSQLite3 || setting.Database.UsePostgreSQL:
  23. _, err = e.Exec(fmt.Sprintf("INSERT INTO %s (group_id, max_index) "+
  24. "VALUES (?,1) ON CONFLICT (group_id) DO UPDATE SET max_index = %s.max_index+1",
  25. tableName, tableName), groupID)
  26. case setting.Database.UseMySQL:
  27. _, err = e.Exec(fmt.Sprintf("INSERT INTO %s (group_id, max_index) "+
  28. "VALUES (?,1) ON DUPLICATE KEY UPDATE max_index = max_index+1", tableName),
  29. groupID)
  30. case setting.Database.UseMSSQL:
  31. // https://weblogs.sqlteam.com/dang/2009/01/31/upsert-race-condition-with-merge/
  32. _, err = e.Exec(fmt.Sprintf("MERGE %s WITH (HOLDLOCK) as target "+
  33. "USING (SELECT ? AS group_id) AS src "+
  34. "ON src.group_id = target.group_id "+
  35. "WHEN MATCHED THEN UPDATE SET target.max_index = target.max_index+1 "+
  36. "WHEN NOT MATCHED THEN INSERT (group_id, max_index) "+
  37. "VALUES (src.group_id, 1);", tableName),
  38. groupID)
  39. default:
  40. return fmt.Errorf("database type not supported")
  41. }
  42. return
  43. }
  44. var (
  45. // ErrResouceOutdated represents an error when request resource outdated
  46. ErrResouceOutdated = errors.New("resource outdated")
  47. // ErrGetResourceIndexFailed represents an error when resource index retries 3 times
  48. ErrGetResourceIndexFailed = errors.New("get resource index failed")
  49. )
  50. const (
  51. // MaxDupIndexAttempts max retry times to create index
  52. MaxDupIndexAttempts = 3
  53. )
  54. // GetNextResourceIndex retried 3 times to generate a resource index
  55. func GetNextResourceIndex(tableName string, groupID int64) (int64, error) {
  56. for i := 0; i < MaxDupIndexAttempts; i++ {
  57. idx, err := getNextResourceIndex(tableName, groupID)
  58. if err == ErrResouceOutdated {
  59. continue
  60. }
  61. if err != nil {
  62. return 0, err
  63. }
  64. return idx, nil
  65. }
  66. return 0, ErrGetResourceIndexFailed
  67. }
  68. // DeleteResouceIndex delete resource index
  69. func DeleteResouceIndex(ctx context.Context, tableName string, groupID int64) error {
  70. _, err := Exec(ctx, fmt.Sprintf("DELETE FROM %s WHERE group_id=?", tableName), groupID)
  71. return err
  72. }
  73. // getNextResourceIndex return the next index
  74. func getNextResourceIndex(tableName string, groupID int64) (int64, error) {
  75. sess := x.NewSession()
  76. defer sess.Close()
  77. if err := sess.Begin(); err != nil {
  78. return 0, err
  79. }
  80. var preIdx int64
  81. _, err := sess.SQL(fmt.Sprintf("SELECT max_index FROM %s WHERE group_id = ?", tableName), groupID).Get(&preIdx)
  82. if err != nil {
  83. return 0, err
  84. }
  85. if err := UpsertResourceIndex(sess, tableName, groupID); err != nil {
  86. return 0, err
  87. }
  88. var curIdx int64
  89. has, err := sess.SQL(fmt.Sprintf("SELECT max_index FROM %s WHERE group_id = ? AND max_index=?", tableName), groupID, preIdx+1).Get(&curIdx)
  90. if err != nil {
  91. return 0, err
  92. }
  93. if !has {
  94. return 0, ErrResouceOutdated
  95. }
  96. if err := sess.Commit(); err != nil {
  97. return 0, err
  98. }
  99. return curIdx, nil
  100. }