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.

413 lines
12 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/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.Chunks) > 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. func (store *YdbStore) CanDropWholeBucket() bool {
  293. return store.SupportBucketTable
  294. }
  295. func (store *YdbStore) OnBucketCreation(bucket string) {
  296. store.dbsLock.Lock()
  297. defer store.dbsLock.Unlock()
  298. if err := store.createTable(context.Background(),
  299. path.Join(store.tablePathPrefix, bucket)); err != nil {
  300. glog.Errorf("createTable %s: %v", bucket, err)
  301. }
  302. if store.dbs == nil {
  303. return
  304. }
  305. store.dbs[bucket] = true
  306. }
  307. func (store *YdbStore) OnBucketDeletion(bucket string) {
  308. store.dbsLock.Lock()
  309. defer store.dbsLock.Unlock()
  310. if err := store.deleteTable(context.Background(),
  311. path.Join(store.tablePathPrefix, bucket)); err != nil {
  312. glog.Errorf("deleteTable %s: %v", bucket, err)
  313. }
  314. if store.dbs == nil {
  315. return
  316. }
  317. delete(store.dbs, bucket)
  318. }
  319. func (store *YdbStore) createTable(ctx context.Context, prefix string) error {
  320. return store.DB.Table().Do(ctx, func(ctx context.Context, s table.Session) error {
  321. return s.CreateTable(ctx, path.Join(prefix, abstract_sql.DEFAULT_TABLE), createTableOptions()...)
  322. })
  323. }
  324. func (store *YdbStore) deleteTable(ctx context.Context, prefix string) error {
  325. if !store.SupportBucketTable {
  326. return nil
  327. }
  328. if err := store.DB.Table().Do(ctx, func(ctx context.Context, s table.Session) error {
  329. return s.DropTable(ctx, path.Join(prefix, abstract_sql.DEFAULT_TABLE))
  330. }); err != nil {
  331. return err
  332. }
  333. glog.V(4).Infof("deleted table %s", prefix)
  334. return nil
  335. }
  336. func (store *YdbStore) getPrefix(ctx context.Context, dir *string) (tablePathPrefix *string, shortDir *string) {
  337. tablePathPrefix = &store.tablePathPrefix
  338. shortDir = dir
  339. if !store.SupportBucketTable {
  340. return
  341. }
  342. prefixBuckets := store.dirBuckets + "/"
  343. if strings.HasPrefix(*dir, prefixBuckets) {
  344. // detect bucket
  345. bucketAndDir := (*dir)[len(prefixBuckets):]
  346. var bucket string
  347. if t := strings.Index(bucketAndDir, "/"); t > 0 {
  348. bucket = bucketAndDir[:t]
  349. } else if t < 0 {
  350. bucket = bucketAndDir
  351. }
  352. if bucket == "" {
  353. return
  354. }
  355. store.dbsLock.Lock()
  356. defer store.dbsLock.Unlock()
  357. tablePathPrefixWithBucket := path.Join(store.tablePathPrefix, bucket)
  358. if _, found := store.dbs[bucket]; !found {
  359. if err := store.createTable(ctx, tablePathPrefixWithBucket); err == nil {
  360. store.dbs[bucket] = true
  361. glog.V(4).Infof("created table %s", tablePathPrefixWithBucket)
  362. } else {
  363. glog.Errorf("createTable %s: %v", tablePathPrefixWithBucket, err)
  364. }
  365. }
  366. tablePathPrefix = &tablePathPrefixWithBucket
  367. }
  368. return
  369. }