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.

221 lines
6.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package cassandra
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "github.com/gocql/gocql"
  7. "time"
  8. "github.com/seaweedfs/seaweedfs/weed/filer"
  9. "github.com/seaweedfs/seaweedfs/weed/glog"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  11. "github.com/seaweedfs/seaweedfs/weed/util"
  12. )
  13. func init() {
  14. filer.Stores = append(filer.Stores, &CassandraStore{})
  15. }
  16. type CassandraStore struct {
  17. cluster *gocql.ClusterConfig
  18. session *gocql.Session
  19. superLargeDirectoryHash map[string]string
  20. }
  21. func (store *CassandraStore) GetName() string {
  22. return "cassandra"
  23. }
  24. func (store *CassandraStore) Initialize(configuration util.Configuration, prefix string) (err error) {
  25. return store.initialize(
  26. configuration.GetString(prefix+"keyspace"),
  27. configuration.GetStringSlice(prefix+"hosts"),
  28. configuration.GetString(prefix+"username"),
  29. configuration.GetString(prefix+"password"),
  30. configuration.GetStringSlice(prefix+"superLargeDirectories"),
  31. configuration.GetString(prefix+"localDC"),
  32. configuration.GetInt(prefix+"connection_timeout_millisecond"),
  33. )
  34. }
  35. func (store *CassandraStore) isSuperLargeDirectory(dir string) (dirHash string, isSuperLargeDirectory bool) {
  36. dirHash, isSuperLargeDirectory = store.superLargeDirectoryHash[dir]
  37. return
  38. }
  39. func (store *CassandraStore) initialize(keyspace string, hosts []string, username string, password string, superLargeDirectories []string, localDC string, timeout int) (err error) {
  40. store.cluster = gocql.NewCluster(hosts...)
  41. if username != "" && password != "" {
  42. store.cluster.Authenticator = gocql.PasswordAuthenticator{Username: username, Password: password}
  43. }
  44. store.cluster.Keyspace = keyspace
  45. store.cluster.Timeout = time.Duration(timeout) * time.Millisecond
  46. glog.V(0).Infof("timeout = %d", timeout)
  47. fallback := gocql.RoundRobinHostPolicy()
  48. if localDC != "" {
  49. fallback = gocql.DCAwareRoundRobinPolicy(localDC)
  50. }
  51. store.cluster.PoolConfig.HostSelectionPolicy = gocql.TokenAwareHostPolicy(fallback)
  52. store.cluster.Consistency = gocql.LocalQuorum
  53. store.session, err = store.cluster.CreateSession()
  54. if err != nil {
  55. glog.V(0).Infof("Failed to open cassandra store, hosts %v, keyspace %s", hosts, keyspace)
  56. }
  57. // set directory hash
  58. store.superLargeDirectoryHash = make(map[string]string)
  59. existingHash := make(map[string]string)
  60. for _, dir := range superLargeDirectories {
  61. // adding dir hash to avoid duplicated names
  62. dirHash := util.Md5String([]byte(dir))[:4]
  63. store.superLargeDirectoryHash[dir] = dirHash
  64. if existingDir, found := existingHash[dirHash]; found {
  65. glog.Fatalf("directory %s has the same hash as %s", dir, existingDir)
  66. }
  67. existingHash[dirHash] = dir
  68. }
  69. return
  70. }
  71. func (store *CassandraStore) BeginTransaction(ctx context.Context) (context.Context, error) {
  72. return ctx, nil
  73. }
  74. func (store *CassandraStore) CommitTransaction(ctx context.Context) error {
  75. return nil
  76. }
  77. func (store *CassandraStore) RollbackTransaction(ctx context.Context) error {
  78. return nil
  79. }
  80. func (store *CassandraStore) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
  81. dir, name := entry.FullPath.DirAndName()
  82. if dirHash, ok := store.isSuperLargeDirectory(dir); ok {
  83. dir, name = dirHash+name, ""
  84. }
  85. meta, err := entry.EncodeAttributesAndChunks()
  86. if err != nil {
  87. return fmt.Errorf("encode %s: %s", entry.FullPath, err)
  88. }
  89. if len(entry.GetChunks()) > filer.CountEntryChunksForGzip {
  90. meta = util.MaybeGzipData(meta)
  91. }
  92. if err := store.session.Query(
  93. "INSERT INTO filemeta (directory,name,meta) VALUES(?,?,?) USING TTL ? ",
  94. dir, name, meta, entry.TtlSec).Exec(); err != nil {
  95. return fmt.Errorf("insert %s: %s", entry.FullPath, err)
  96. }
  97. return nil
  98. }
  99. func (store *CassandraStore) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
  100. return store.InsertEntry(ctx, entry)
  101. }
  102. func (store *CassandraStore) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer.Entry, err error) {
  103. dir, name := fullpath.DirAndName()
  104. if dirHash, ok := store.isSuperLargeDirectory(dir); ok {
  105. dir, name = dirHash+name, ""
  106. }
  107. var data []byte
  108. if err := store.session.Query(
  109. "SELECT meta FROM filemeta WHERE directory=? AND name=?",
  110. dir, name).Scan(&data); err != nil {
  111. if errors.Is(err, gocql.ErrNotFound) {
  112. return nil, filer_pb.ErrNotFound
  113. }
  114. return nil, err
  115. }
  116. entry = &filer.Entry{
  117. FullPath: fullpath,
  118. }
  119. err = entry.DecodeAttributesAndChunks(util.MaybeDecompressData(data))
  120. if err != nil {
  121. return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
  122. }
  123. return entry, nil
  124. }
  125. func (store *CassandraStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) error {
  126. dir, name := fullpath.DirAndName()
  127. if dirHash, ok := store.isSuperLargeDirectory(dir); ok {
  128. dir, name = dirHash+name, ""
  129. }
  130. if err := store.session.Query(
  131. "DELETE FROM filemeta WHERE directory=? AND name=?",
  132. dir, name).Exec(); err != nil {
  133. return fmt.Errorf("delete %s : %v", fullpath, err)
  134. }
  135. return nil
  136. }
  137. func (store *CassandraStore) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) error {
  138. if _, ok := store.isSuperLargeDirectory(string(fullpath)); ok {
  139. return nil // filer.ErrUnsupportedSuperLargeDirectoryListing
  140. }
  141. if err := store.session.Query(
  142. "DELETE FROM filemeta WHERE directory=?",
  143. fullpath).Exec(); err != nil {
  144. return fmt.Errorf("delete %s : %v", fullpath, err)
  145. }
  146. return nil
  147. }
  148. func (store *CassandraStore) ListDirectoryPrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
  149. return lastFileName, filer.ErrUnsupportedListDirectoryPrefixed
  150. }
  151. func (store *CassandraStore) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
  152. if _, ok := store.isSuperLargeDirectory(string(dirPath)); ok {
  153. return // nil, filer.ErrUnsupportedSuperLargeDirectoryListing
  154. }
  155. cqlStr := "SELECT NAME, meta FROM filemeta WHERE directory=? AND name>? ORDER BY NAME ASC LIMIT ?"
  156. if includeStartFile {
  157. cqlStr = "SELECT NAME, meta FROM filemeta WHERE directory=? AND name>=? ORDER BY NAME ASC LIMIT ?"
  158. }
  159. var data []byte
  160. var name string
  161. iter := store.session.Query(cqlStr, string(dirPath), startFileName, limit+1).Iter()
  162. for iter.Scan(&name, &data) {
  163. entry := &filer.Entry{
  164. FullPath: util.NewFullPath(string(dirPath), name),
  165. }
  166. lastFileName = name
  167. if decodeErr := entry.DecodeAttributesAndChunks(util.MaybeDecompressData(data)); decodeErr != nil {
  168. err = decodeErr
  169. glog.V(0).Infof("list %s : %v", entry.FullPath, err)
  170. break
  171. }
  172. if !eachEntryFunc(entry) {
  173. break
  174. }
  175. }
  176. if err := iter.Close(); err != nil {
  177. glog.V(0).Infof("list iterator close: %v", err)
  178. }
  179. return lastFileName, err
  180. }
  181. func (store *CassandraStore) Shutdown() {
  182. store.session.Close()
  183. }