abstract_sql_store.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. package abstract_sql
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "github.com/seaweedfs/seaweedfs/weed/filer"
  7. "github.com/seaweedfs/seaweedfs/weed/glog"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  9. "github.com/seaweedfs/seaweedfs/weed/util"
  10. "strings"
  11. "sync"
  12. )
  13. type SqlGenerator interface {
  14. GetSqlInsert(tableName string) string
  15. GetSqlUpdate(tableName string) string
  16. GetSqlFind(tableName string) string
  17. GetSqlDelete(tableName string) string
  18. GetSqlDeleteFolderChildren(tableName string) string
  19. GetSqlListExclusive(tableName string) string
  20. GetSqlListInclusive(tableName string) string
  21. GetSqlCreateTable(tableName string) string
  22. GetSqlDropTable(tableName string) string
  23. }
  24. type AbstractSqlStore struct {
  25. SqlGenerator
  26. DB *sql.DB
  27. SupportBucketTable bool
  28. dbs map[string]bool
  29. dbsLock sync.Mutex
  30. }
  31. var _ filer.BucketAware = (*AbstractSqlStore)(nil)
  32. func (store *AbstractSqlStore) CanDropWholeBucket() bool {
  33. return store.SupportBucketTable
  34. }
  35. func (store *AbstractSqlStore) OnBucketCreation(bucket string) {
  36. store.dbsLock.Lock()
  37. defer store.dbsLock.Unlock()
  38. store.CreateTable(context.Background(), bucket)
  39. if store.dbs == nil {
  40. return
  41. }
  42. store.dbs[bucket] = true
  43. }
  44. func (store *AbstractSqlStore) OnBucketDeletion(bucket string) {
  45. store.dbsLock.Lock()
  46. defer store.dbsLock.Unlock()
  47. store.deleteTable(context.Background(), bucket)
  48. if store.dbs == nil {
  49. return
  50. }
  51. delete(store.dbs, bucket)
  52. }
  53. const (
  54. DEFAULT_TABLE = "filemeta"
  55. )
  56. type TxOrDB interface {
  57. ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
  58. QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
  59. QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
  60. }
  61. func (store *AbstractSqlStore) BeginTransaction(ctx context.Context) (context.Context, error) {
  62. tx, err := store.DB.BeginTx(ctx, &sql.TxOptions{
  63. Isolation: sql.LevelReadCommitted,
  64. ReadOnly: false,
  65. })
  66. if err != nil {
  67. return ctx, err
  68. }
  69. return context.WithValue(ctx, "tx", tx), nil
  70. }
  71. func (store *AbstractSqlStore) CommitTransaction(ctx context.Context) error {
  72. if tx, ok := ctx.Value("tx").(*sql.Tx); ok {
  73. return tx.Commit()
  74. }
  75. return nil
  76. }
  77. func (store *AbstractSqlStore) RollbackTransaction(ctx context.Context) error {
  78. if tx, ok := ctx.Value("tx").(*sql.Tx); ok {
  79. return tx.Rollback()
  80. }
  81. return nil
  82. }
  83. func (store *AbstractSqlStore) getTxOrDB(ctx context.Context, fullpath util.FullPath, isForChildren bool) (txOrDB TxOrDB, bucket string, shortPath util.FullPath, err error) {
  84. shortPath = fullpath
  85. bucket = DEFAULT_TABLE
  86. if tx, ok := ctx.Value("tx").(*sql.Tx); ok {
  87. txOrDB = tx
  88. } else {
  89. txOrDB = store.DB
  90. }
  91. if !store.SupportBucketTable {
  92. return
  93. }
  94. if !strings.HasPrefix(string(fullpath), "/buckets/") {
  95. return
  96. }
  97. // detect bucket
  98. bucketAndObjectKey := string(fullpath)[len("/buckets/"):]
  99. t := strings.Index(bucketAndObjectKey, "/")
  100. if t < 0 && !isForChildren {
  101. return
  102. }
  103. bucket = bucketAndObjectKey
  104. shortPath = "/"
  105. if t > 0 {
  106. bucket = bucketAndObjectKey[:t]
  107. shortPath = util.FullPath(bucketAndObjectKey[t:])
  108. }
  109. if isValidBucket(bucket) {
  110. store.dbsLock.Lock()
  111. defer store.dbsLock.Unlock()
  112. if store.dbs == nil {
  113. store.dbs = make(map[string]bool)
  114. }
  115. if _, found := store.dbs[bucket]; !found {
  116. if err = store.CreateTable(ctx, bucket); err == nil {
  117. store.dbs[bucket] = true
  118. }
  119. }
  120. }
  121. return
  122. }
  123. func (store *AbstractSqlStore) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
  124. db, bucket, shortPath, err := store.getTxOrDB(ctx, entry.FullPath, false)
  125. if err != nil {
  126. return fmt.Errorf("findDB %s : %v", entry.FullPath, err)
  127. }
  128. dir, name := shortPath.DirAndName()
  129. meta, err := entry.EncodeAttributesAndChunks()
  130. if err != nil {
  131. return fmt.Errorf("encode %s: %s", entry.FullPath, err)
  132. }
  133. if len(entry.GetChunks()) > filer.CountEntryChunksForGzip {
  134. meta = util.MaybeGzipData(meta)
  135. }
  136. res, err := db.ExecContext(ctx, store.GetSqlInsert(bucket), util.HashStringToLong(dir), name, dir, meta)
  137. if err == nil {
  138. return
  139. }
  140. if !strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  141. // return fmt.Errorf("insert: %s", err)
  142. // skip this since the error can be in a different language
  143. }
  144. // now the insert failed possibly due to duplication constraints
  145. glog.V(1).Infof("insert %s falls back to update: %v", entry.FullPath, err)
  146. res, err = db.ExecContext(ctx, store.GetSqlUpdate(bucket), meta, util.HashStringToLong(dir), name, dir)
  147. if err != nil {
  148. return fmt.Errorf("upsert %s: %s", entry.FullPath, err)
  149. }
  150. _, err = res.RowsAffected()
  151. if err != nil {
  152. return fmt.Errorf("upsert %s but no rows affected: %s", entry.FullPath, err)
  153. }
  154. return nil
  155. }
  156. func (store *AbstractSqlStore) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
  157. db, bucket, shortPath, err := store.getTxOrDB(ctx, entry.FullPath, false)
  158. if err != nil {
  159. return fmt.Errorf("findDB %s : %v", entry.FullPath, err)
  160. }
  161. dir, name := shortPath.DirAndName()
  162. meta, err := entry.EncodeAttributesAndChunks()
  163. if err != nil {
  164. return fmt.Errorf("encode %s: %s", entry.FullPath, err)
  165. }
  166. res, err := db.ExecContext(ctx, store.GetSqlUpdate(bucket), meta, util.HashStringToLong(dir), name, dir)
  167. if err != nil {
  168. return fmt.Errorf("update %s: %s", entry.FullPath, err)
  169. }
  170. _, err = res.RowsAffected()
  171. if err != nil {
  172. return fmt.Errorf("update %s but no rows affected: %s", entry.FullPath, err)
  173. }
  174. return nil
  175. }
  176. func (store *AbstractSqlStore) FindEntry(ctx context.Context, fullpath util.FullPath) (*filer.Entry, error) {
  177. db, bucket, shortPath, err := store.getTxOrDB(ctx, fullpath, false)
  178. if err != nil {
  179. return nil, fmt.Errorf("findDB %s : %v", fullpath, err)
  180. }
  181. dir, name := shortPath.DirAndName()
  182. row := db.QueryRowContext(ctx, store.GetSqlFind(bucket), util.HashStringToLong(dir), name, dir)
  183. var data []byte
  184. if err := row.Scan(&data); err != nil {
  185. if err == sql.ErrNoRows {
  186. return nil, filer_pb.ErrNotFound
  187. }
  188. return nil, fmt.Errorf("find %s: %v", fullpath, err)
  189. }
  190. entry := &filer.Entry{
  191. FullPath: fullpath,
  192. }
  193. if err := entry.DecodeAttributesAndChunks(util.MaybeDecompressData(data)); err != nil {
  194. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  195. }
  196. return entry, nil
  197. }
  198. func (store *AbstractSqlStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) error {
  199. db, bucket, shortPath, err := store.getTxOrDB(ctx, fullpath, false)
  200. if err != nil {
  201. return fmt.Errorf("findDB %s : %v", fullpath, err)
  202. }
  203. dir, name := shortPath.DirAndName()
  204. res, err := db.ExecContext(ctx, store.GetSqlDelete(bucket), util.HashStringToLong(dir), name, dir)
  205. if err != nil {
  206. return fmt.Errorf("delete %s: %s", fullpath, err)
  207. }
  208. _, err = res.RowsAffected()
  209. if err != nil {
  210. return fmt.Errorf("delete %s but no rows affected: %s", fullpath, err)
  211. }
  212. return nil
  213. }
  214. func (store *AbstractSqlStore) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) error {
  215. db, bucket, shortPath, err := store.getTxOrDB(ctx, fullpath, true)
  216. if err != nil {
  217. return fmt.Errorf("findDB %s : %v", fullpath, err)
  218. }
  219. if isValidBucket(bucket) && shortPath == "/" {
  220. if err = store.deleteTable(ctx, bucket); err == nil {
  221. store.dbsLock.Lock()
  222. delete(store.dbs, bucket)
  223. store.dbsLock.Unlock()
  224. return nil
  225. } else {
  226. return err
  227. }
  228. }
  229. glog.V(4).Infof("delete %s SQL %s %d", string(shortPath), store.GetSqlDeleteFolderChildren(bucket), util.HashStringToLong(string(shortPath)))
  230. res, err := db.ExecContext(ctx, store.GetSqlDeleteFolderChildren(bucket), util.HashStringToLong(string(shortPath)), string(shortPath))
  231. if err != nil {
  232. return fmt.Errorf("deleteFolderChildren %s: %s", fullpath, err)
  233. }
  234. _, err = res.RowsAffected()
  235. if err != nil {
  236. return fmt.Errorf("deleteFolderChildren %s but no rows affected: %s", fullpath, err)
  237. }
  238. return nil
  239. }
  240. func (store *AbstractSqlStore) ListDirectoryPrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
  241. db, bucket, shortPath, err := store.getTxOrDB(ctx, dirPath, true)
  242. if err != nil {
  243. return lastFileName, fmt.Errorf("findDB %s : %v", dirPath, err)
  244. }
  245. sqlText := store.GetSqlListExclusive(bucket)
  246. if includeStartFile {
  247. sqlText = store.GetSqlListInclusive(bucket)
  248. }
  249. rows, err := db.QueryContext(ctx, sqlText, util.HashStringToLong(string(shortPath)), startFileName, string(shortPath), prefix+"%", limit+1)
  250. if err != nil {
  251. return lastFileName, fmt.Errorf("list %s : %v", dirPath, err)
  252. }
  253. defer rows.Close()
  254. for rows.Next() {
  255. var name string
  256. var data []byte
  257. if err = rows.Scan(&name, &data); err != nil {
  258. glog.V(0).Infof("scan %s : %v", dirPath, err)
  259. return lastFileName, fmt.Errorf("scan %s: %v", dirPath, err)
  260. }
  261. lastFileName = name
  262. entry := &filer.Entry{
  263. FullPath: util.NewFullPath(string(dirPath), name),
  264. }
  265. if err = entry.DecodeAttributesAndChunks(util.MaybeDecompressData(data)); err != nil {
  266. glog.V(0).Infof("scan decode %s : %v", entry.FullPath, err)
  267. return lastFileName, fmt.Errorf("scan decode %s : %v", entry.FullPath, err)
  268. }
  269. if !eachEntryFunc(entry) {
  270. break
  271. }
  272. }
  273. return lastFileName, nil
  274. }
  275. func (store *AbstractSqlStore) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
  276. return store.ListDirectoryPrefixedEntries(ctx, dirPath, startFileName, includeStartFile, limit, "", nil)
  277. }
  278. func (store *AbstractSqlStore) Shutdown() {
  279. store.DB.Close()
  280. }
  281. func isValidBucket(bucket string) bool {
  282. return bucket != DEFAULT_TABLE && bucket != ""
  283. }
  284. func (store *AbstractSqlStore) CreateTable(ctx context.Context, bucket string) error {
  285. if !store.SupportBucketTable {
  286. return nil
  287. }
  288. _, err := store.DB.ExecContext(ctx, store.SqlGenerator.GetSqlCreateTable(bucket))
  289. return err
  290. }
  291. func (store *AbstractSqlStore) deleteTable(ctx context.Context, bucket string) error {
  292. if !store.SupportBucketTable {
  293. return nil
  294. }
  295. _, err := store.DB.ExecContext(ctx, store.SqlGenerator.GetSqlDropTable(bucket))
  296. return err
  297. }