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.

423 lines
13 KiB

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