ydb_store.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. //go:build ydb
  2. // +build ydb
  3. package ydb
  4. import (
  5. "context"
  6. "fmt"
  7. "github.com/seaweedfs/seaweedfs/weed/filer"
  8. "github.com/seaweedfs/seaweedfs/weed/filer/abstract_sql"
  9. "github.com/seaweedfs/seaweedfs/weed/glog"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  11. "github.com/seaweedfs/seaweedfs/weed/util"
  12. environ "github.com/ydb-platform/ydb-go-sdk-auth-environ"
  13. "github.com/ydb-platform/ydb-go-sdk/v3"
  14. "github.com/ydb-platform/ydb-go-sdk/v3/sugar"
  15. "github.com/ydb-platform/ydb-go-sdk/v3/table"
  16. "github.com/ydb-platform/ydb-go-sdk/v3/table/result"
  17. "github.com/ydb-platform/ydb-go-sdk/v3/table/result/named"
  18. "github.com/ydb-platform/ydb-go-sdk/v3/table/types"
  19. "os"
  20. "path"
  21. "strings"
  22. "sync"
  23. "time"
  24. )
  25. const (
  26. defaultDialTimeOut = 10
  27. )
  28. var (
  29. roTX = table.TxControl(
  30. table.BeginTx(table.WithOnlineReadOnly()),
  31. table.CommitTx(),
  32. )
  33. rwTX = table.DefaultTxControl()
  34. )
  35. type YdbStore struct {
  36. DB ydb.Connection
  37. dirBuckets string
  38. tablePathPrefix string
  39. SupportBucketTable bool
  40. dbs map[string]bool
  41. dbsLock sync.Mutex
  42. }
  43. func init() {
  44. filer.Stores = append(filer.Stores, &YdbStore{})
  45. }
  46. func (store *YdbStore) GetName() string {
  47. return "ydb"
  48. }
  49. func (store *YdbStore) Initialize(configuration util.Configuration, prefix string) (err error) {
  50. return store.initialize(
  51. configuration.GetString("filer.options.buckets_folder"),
  52. configuration.GetString(prefix+"dsn"),
  53. configuration.GetString(prefix+"prefix"),
  54. configuration.GetBool(prefix+"useBucketPrefix"),
  55. configuration.GetInt(prefix+"dialTimeOut"),
  56. configuration.GetInt(prefix+"poolSizeLimit"),
  57. )
  58. }
  59. func (store *YdbStore) initialize(dirBuckets string, dsn string, tablePathPrefix string, useBucketPrefix bool, dialTimeOut int, poolSizeLimit int) (err error) {
  60. store.dirBuckets = dirBuckets
  61. store.SupportBucketTable = useBucketPrefix
  62. if store.SupportBucketTable {
  63. glog.V(0).Infof("enabled BucketPrefix")
  64. }
  65. store.dbs = make(map[string]bool)
  66. ctx, cancel := context.WithCancel(context.Background())
  67. defer cancel()
  68. if dialTimeOut == 0 {
  69. dialTimeOut = defaultDialTimeOut
  70. }
  71. opts := []ydb.Option{
  72. ydb.WithDialTimeout(time.Duration(dialTimeOut) * time.Second),
  73. environ.WithEnvironCredentials(ctx),
  74. }
  75. if poolSizeLimit > 0 {
  76. opts = append(opts, ydb.WithSessionPoolSizeLimit(poolSizeLimit))
  77. }
  78. if dsn == "" {
  79. dsn = os.Getenv("YDB_CONNECTION_STRING")
  80. }
  81. store.DB, err = ydb.Open(ctx, dsn, opts...)
  82. if err != nil {
  83. if store.DB != nil {
  84. _ = store.DB.Close(ctx)
  85. store.DB = nil
  86. }
  87. return fmt.Errorf("can not connect to %s error: %v", dsn, err)
  88. }
  89. store.tablePathPrefix = path.Join(store.DB.Name(), tablePathPrefix)
  90. if err = sugar.MakeRecursive(ctx, store.DB, store.tablePathPrefix); err != nil {
  91. return fmt.Errorf("MakeRecursive %s : %v", store.tablePathPrefix, err)
  92. }
  93. if err = store.createTable(ctx, store.tablePathPrefix); err != nil {
  94. glog.Errorf("createTable %s: %v", store.tablePathPrefix, err)
  95. }
  96. return err
  97. }
  98. func (store *YdbStore) doTxOrDB(ctx context.Context, query *string, params *table.QueryParameters, tc *table.TransactionControl, processResultFunc func(res result.Result) error) (err error) {
  99. var res result.Result
  100. if tx, ok := ctx.Value("tx").(table.Transaction); ok {
  101. res, err = tx.Execute(ctx, *query, params)
  102. if err != nil {
  103. return fmt.Errorf("execute transaction: %v", err)
  104. }
  105. } else {
  106. err = store.DB.Table().Do(ctx, func(ctx context.Context, s table.Session) (err error) {
  107. _, res, err = s.Execute(ctx, tc, *query, params)
  108. if err != nil {
  109. return fmt.Errorf("execute statement: %v", err)
  110. }
  111. return nil
  112. })
  113. }
  114. if err != nil {
  115. return err
  116. }
  117. if res != nil {
  118. defer func() { _ = res.Close() }()
  119. if processResultFunc != nil {
  120. if err = processResultFunc(res); err != nil {
  121. return fmt.Errorf("process result: %v", err)
  122. }
  123. }
  124. }
  125. return err
  126. }
  127. func (store *YdbStore) insertOrUpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
  128. dir, name := entry.FullPath.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. tablePathPrefix, shortDir := store.getPrefix(ctx, &dir)
  137. fileMeta := FileMeta{util.HashStringToLong(dir), name, *shortDir, meta}
  138. return store.doTxOrDB(ctx, withPragma(tablePathPrefix, upsertQuery), fileMeta.queryParameters(entry.TtlSec), rwTX, nil)
  139. }
  140. func (store *YdbStore) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
  141. return store.insertOrUpdateEntry(ctx, entry)
  142. }
  143. func (store *YdbStore) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
  144. return store.insertOrUpdateEntry(ctx, entry)
  145. }
  146. func (store *YdbStore) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer.Entry, err error) {
  147. dir, name := fullpath.DirAndName()
  148. var data []byte
  149. entryFound := false
  150. tablePathPrefix, shortDir := store.getPrefix(ctx, &dir)
  151. query := withPragma(tablePathPrefix, findQuery)
  152. queryParams := table.NewQueryParameters(
  153. table.ValueParam("$dir_hash", types.Int64Value(util.HashStringToLong(*shortDir))),
  154. table.ValueParam("$name", types.UTF8Value(name)))
  155. err = store.doTxOrDB(ctx, query, queryParams, roTX, func(res result.Result) error {
  156. if !res.NextResultSet(ctx) || !res.HasNextRow() {
  157. return nil
  158. }
  159. for res.NextRow() {
  160. if err = res.ScanNamed(named.OptionalWithDefault("meta", &data)); err != nil {
  161. return fmt.Errorf("scanNamed %s : %v", fullpath, err)
  162. }
  163. entryFound = true
  164. return nil
  165. }
  166. return res.Err()
  167. })
  168. if err != nil {
  169. return nil, err
  170. }
  171. if !entryFound {
  172. return nil, filer_pb.ErrNotFound
  173. }
  174. entry = &filer.Entry{
  175. FullPath: fullpath,
  176. }
  177. if err := entry.DecodeAttributesAndChunks(util.MaybeDecompressData(data)); err != nil {
  178. return nil, fmt.Errorf("decode %s : %v", fullpath, err)
  179. }
  180. return entry, nil
  181. }
  182. func (store *YdbStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) (err error) {
  183. dir, name := fullpath.DirAndName()
  184. tablePathPrefix, shortDir := store.getPrefix(ctx, &dir)
  185. query := withPragma(tablePathPrefix, deleteQuery)
  186. queryParams := table.NewQueryParameters(
  187. table.ValueParam("$dir_hash", types.Int64Value(util.HashStringToLong(*shortDir))),
  188. table.ValueParam("$name", types.UTF8Value(name)))
  189. return store.doTxOrDB(ctx, query, queryParams, rwTX, nil)
  190. }
  191. func (store *YdbStore) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) (err error) {
  192. dir, _ := fullpath.DirAndName()
  193. tablePathPrefix, shortDir := store.getPrefix(ctx, &dir)
  194. query := withPragma(tablePathPrefix, deleteFolderChildrenQuery)
  195. queryParams := table.NewQueryParameters(
  196. table.ValueParam("$dir_hash", types.Int64Value(util.HashStringToLong(*shortDir))),
  197. table.ValueParam("$directory", types.UTF8Value(*shortDir)))
  198. return store.doTxOrDB(ctx, query, queryParams, rwTX, nil)
  199. }
  200. func (store *YdbStore) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
  201. return store.ListDirectoryPrefixedEntries(ctx, dirPath, startFileName, includeStartFile, limit, "", eachEntryFunc)
  202. }
  203. func (store *YdbStore) ListDirectoryPrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
  204. dir := string(dirPath)
  205. tablePathPrefix, shortDir := store.getPrefix(ctx, &dir)
  206. var query *string
  207. if includeStartFile {
  208. query = withPragma(tablePathPrefix, listInclusiveDirectoryQuery)
  209. } else {
  210. query = withPragma(tablePathPrefix, listDirectoryQuery)
  211. }
  212. truncated := true
  213. eachEntryFuncIsNotBreake := true
  214. entryCount := int64(0)
  215. for truncated && eachEntryFuncIsNotBreake {
  216. if lastFileName != "" {
  217. startFileName = lastFileName
  218. if includeStartFile {
  219. query = withPragma(tablePathPrefix, listDirectoryQuery)
  220. }
  221. }
  222. restLimit := limit - entryCount
  223. queryParams := table.NewQueryParameters(
  224. table.ValueParam("$dir_hash", types.Int64Value(util.HashStringToLong(*shortDir))),
  225. table.ValueParam("$directory", types.UTF8Value(*shortDir)),
  226. table.ValueParam("$start_name", types.UTF8Value(startFileName)),
  227. table.ValueParam("$prefix", types.UTF8Value(prefix+"%")),
  228. table.ValueParam("$limit", types.Uint64Value(uint64(restLimit))),
  229. )
  230. err = store.doTxOrDB(ctx, query, queryParams, roTX, func(res result.Result) error {
  231. var name string
  232. var data []byte
  233. if !res.NextResultSet(ctx) || !res.HasNextRow() {
  234. truncated = false
  235. return nil
  236. }
  237. truncated = res.CurrentResultSet().Truncated()
  238. for res.NextRow() {
  239. if err := res.ScanNamed(
  240. named.OptionalWithDefault("name", &name),
  241. named.OptionalWithDefault("meta", &data)); err != nil {
  242. return fmt.Errorf("list scanNamed %s : %v", dir, err)
  243. }
  244. lastFileName = name
  245. entry := &filer.Entry{
  246. FullPath: util.NewFullPath(dir, name),
  247. }
  248. if err = entry.DecodeAttributesAndChunks(util.MaybeDecompressData(data)); err != nil {
  249. return fmt.Errorf("scan decode %s : %v", entry.FullPath, err)
  250. }
  251. if !eachEntryFunc(entry) {
  252. eachEntryFuncIsNotBreake = false
  253. break
  254. }
  255. entryCount += 1
  256. }
  257. return res.Err()
  258. })
  259. }
  260. if err != nil {
  261. return lastFileName, err
  262. }
  263. return lastFileName, nil
  264. }
  265. func (store *YdbStore) BeginTransaction(ctx context.Context) (context.Context, error) {
  266. session, err := store.DB.Table().CreateSession(ctx)
  267. if err != nil {
  268. return ctx, err
  269. }
  270. tx, err := session.BeginTransaction(ctx, table.TxSettings(table.WithSerializableReadWrite()))
  271. if err != nil {
  272. return ctx, err
  273. }
  274. return context.WithValue(ctx, "tx", tx), nil
  275. }
  276. func (store *YdbStore) CommitTransaction(ctx context.Context) error {
  277. if tx, ok := ctx.Value("tx").(table.Transaction); ok {
  278. _, err := tx.CommitTx(ctx)
  279. return err
  280. }
  281. return nil
  282. }
  283. func (store *YdbStore) RollbackTransaction(ctx context.Context) error {
  284. if tx, ok := ctx.Value("tx").(table.Transaction); ok {
  285. return tx.Rollback(ctx)
  286. }
  287. return nil
  288. }
  289. func (store *YdbStore) Shutdown() {
  290. _ = store.DB.Close(context.Background())
  291. }
  292. var _ filer.BucketAware = (*YdbStore)(nil)
  293. func (store *YdbStore) CanDropWholeBucket() bool {
  294. return store.SupportBucketTable
  295. }
  296. func (store *YdbStore) OnBucketCreation(bucket string) {
  297. store.dbsLock.Lock()
  298. defer store.dbsLock.Unlock()
  299. if err := store.createTable(context.Background(),
  300. path.Join(store.tablePathPrefix, bucket)); err != nil {
  301. glog.Errorf("createTable %s: %v", bucket, err)
  302. }
  303. if store.dbs == nil {
  304. return
  305. }
  306. store.dbs[bucket] = true
  307. }
  308. func (store *YdbStore) OnBucketDeletion(bucket string) {
  309. store.dbsLock.Lock()
  310. defer store.dbsLock.Unlock()
  311. if err := store.deleteTable(context.Background(),
  312. path.Join(store.tablePathPrefix, bucket)); err != nil {
  313. glog.Errorf("deleteTable %s: %v", bucket, err)
  314. }
  315. if store.dbs == nil {
  316. return
  317. }
  318. delete(store.dbs, bucket)
  319. }
  320. func (store *YdbStore) createTable(ctx context.Context, prefix string) error {
  321. return store.DB.Table().Do(ctx, func(ctx context.Context, s table.Session) error {
  322. return s.CreateTable(ctx, path.Join(prefix, abstract_sql.DEFAULT_TABLE), createTableOptions()...)
  323. })
  324. }
  325. func (store *YdbStore) deleteTable(ctx context.Context, prefix string) error {
  326. if !store.SupportBucketTable {
  327. return nil
  328. }
  329. if err := store.DB.Table().Do(ctx, func(ctx context.Context, s table.Session) error {
  330. return s.DropTable(ctx, path.Join(prefix, abstract_sql.DEFAULT_TABLE))
  331. }); err != nil {
  332. return err
  333. }
  334. glog.V(4).Infof("deleted table %s", prefix)
  335. return nil
  336. }
  337. func (store *YdbStore) getPrefix(ctx context.Context, dir *string) (tablePathPrefix *string, shortDir *string) {
  338. tablePathPrefix = &store.tablePathPrefix
  339. shortDir = dir
  340. if !store.SupportBucketTable {
  341. return
  342. }
  343. prefixBuckets := store.dirBuckets + "/"
  344. if strings.HasPrefix(*dir, prefixBuckets) {
  345. // detect bucket
  346. bucketAndDir := (*dir)[len(prefixBuckets):]
  347. var bucket string
  348. if t := strings.Index(bucketAndDir, "/"); t > 0 {
  349. bucket = bucketAndDir[:t]
  350. } else if t < 0 {
  351. bucket = bucketAndDir
  352. }
  353. if bucket == "" {
  354. return
  355. }
  356. store.dbsLock.Lock()
  357. defer store.dbsLock.Unlock()
  358. tablePathPrefixWithBucket := path.Join(store.tablePathPrefix, bucket)
  359. if _, found := store.dbs[bucket]; !found {
  360. if err := store.createTable(ctx, tablePathPrefixWithBucket); err == nil {
  361. store.dbs[bucket] = true
  362. glog.V(4).Infof("created table %s", tablePathPrefixWithBucket)
  363. } else {
  364. glog.Errorf("createTable %s: %v", tablePathPrefixWithBucket, err)
  365. }
  366. }
  367. tablePathPrefix = &tablePathPrefixWithBucket
  368. }
  369. return
  370. }