2018-05-27 15:01:15 +08:00
|
|
|
package cassandra
|
|
|
|
|
|
|
|
import (
|
2019-03-16 06:55:34 +08:00
|
|
|
"context"
|
2018-05-27 15:01:15 +08:00
|
|
|
"fmt"
|
2020-03-08 09:01:39 +08:00
|
|
|
"github.com/gocql/gocql"
|
2022-01-18 14:41:39 +08:00
|
|
|
"time"
|
2020-03-08 09:01:39 +08:00
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2018-05-27 15:01:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-09-01 15:21:19 +08:00
|
|
|
filer.Stores = append(filer.Stores, &CassandraStore{})
|
2018-05-27 15:01:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type CassandraStore struct {
|
2020-12-22 18:26:05 +08:00
|
|
|
cluster *gocql.ClusterConfig
|
|
|
|
session *gocql.Session
|
|
|
|
superLargeDirectoryHash map[string]string
|
2018-05-27 15:01:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *CassandraStore) GetName() string {
|
|
|
|
return "cassandra"
|
|
|
|
}
|
|
|
|
|
2020-01-30 01:09:55 +08:00
|
|
|
func (store *CassandraStore) Initialize(configuration util.Configuration, prefix string) (err error) {
|
2018-05-27 15:01:15 +08:00
|
|
|
return store.initialize(
|
2020-01-30 01:09:55 +08:00
|
|
|
configuration.GetString(prefix+"keyspace"),
|
|
|
|
configuration.GetStringSlice(prefix+"hosts"),
|
2020-09-25 11:22:47 +08:00
|
|
|
configuration.GetString(prefix+"username"),
|
|
|
|
configuration.GetString(prefix+"password"),
|
2020-12-22 18:26:05 +08:00
|
|
|
configuration.GetStringSlice(prefix+"superLargeDirectories"),
|
2021-07-03 04:02:26 +08:00
|
|
|
configuration.GetString(prefix+"localDC"),
|
2022-01-18 14:41:39 +08:00
|
|
|
configuration.GetInt(prefix+"connection_timeout_millisecond"),
|
2018-05-27 15:01:15 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-12-22 18:26:05 +08:00
|
|
|
func (store *CassandraStore) isSuperLargeDirectory(dir string) (dirHash string, isSuperLargeDirectory bool) {
|
|
|
|
dirHash, isSuperLargeDirectory = store.superLargeDirectoryHash[dir]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-18 14:41:39 +08:00
|
|
|
func (store *CassandraStore) initialize(keyspace string, hosts []string, username string, password string, superLargeDirectories []string, localDC string, timeout int) (err error) {
|
2018-05-27 15:01:15 +08:00
|
|
|
store.cluster = gocql.NewCluster(hosts...)
|
2020-09-25 11:22:47 +08:00
|
|
|
if username != "" && password != "" {
|
|
|
|
store.cluster.Authenticator = gocql.PasswordAuthenticator{Username: username, Password: password}
|
|
|
|
}
|
2018-05-27 15:01:15 +08:00
|
|
|
store.cluster.Keyspace = keyspace
|
2022-01-18 14:41:39 +08:00
|
|
|
store.cluster.Timeout = time.Duration(timeout) * time.Millisecond
|
|
|
|
glog.V(0).Infof("timeout = %d", timeout)
|
2021-07-03 04:02:26 +08:00
|
|
|
fallback := gocql.RoundRobinHostPolicy()
|
|
|
|
if localDC != "" {
|
|
|
|
fallback = gocql.DCAwareRoundRobinPolicy(localDC)
|
|
|
|
}
|
|
|
|
store.cluster.PoolConfig.HostSelectionPolicy = gocql.TokenAwareHostPolicy(fallback)
|
2018-05-27 15:01:15 +08:00
|
|
|
store.cluster.Consistency = gocql.LocalQuorum
|
2021-07-03 04:02:26 +08:00
|
|
|
|
2018-05-27 15:01:15 +08:00
|
|
|
store.session, err = store.cluster.CreateSession()
|
|
|
|
if err != nil {
|
|
|
|
glog.V(0).Infof("Failed to open cassandra store, hosts %v, keyspace %s", hosts, keyspace)
|
|
|
|
}
|
2020-12-22 18:26:05 +08:00
|
|
|
|
|
|
|
// set directory hash
|
|
|
|
store.superLargeDirectoryHash = make(map[string]string)
|
|
|
|
existingHash := make(map[string]string)
|
|
|
|
for _, dir := range superLargeDirectories {
|
|
|
|
// adding dir hash to avoid duplicated names
|
|
|
|
dirHash := util.Md5String([]byte(dir))[:4]
|
|
|
|
store.superLargeDirectoryHash[dir] = dirHash
|
|
|
|
if existingDir, found := existingHash[dirHash]; found {
|
|
|
|
glog.Fatalf("directory %s has the same hash as %s", dir, existingDir)
|
|
|
|
}
|
|
|
|
existingHash[dirHash] = dir
|
|
|
|
}
|
2018-05-27 15:01:15 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-31 14:09:16 +08:00
|
|
|
func (store *CassandraStore) BeginTransaction(ctx context.Context) (context.Context, error) {
|
2019-03-31 14:08:29 +08:00
|
|
|
return ctx, nil
|
|
|
|
}
|
2019-03-31 14:09:16 +08:00
|
|
|
func (store *CassandraStore) CommitTransaction(ctx context.Context) error {
|
2019-03-31 14:08:29 +08:00
|
|
|
return nil
|
|
|
|
}
|
2019-03-31 14:09:16 +08:00
|
|
|
func (store *CassandraStore) RollbackTransaction(ctx context.Context) error {
|
2019-03-31 14:08:29 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 15:21:19 +08:00
|
|
|
func (store *CassandraStore) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
|
2018-05-27 15:01:15 +08:00
|
|
|
|
|
|
|
dir, name := entry.FullPath.DirAndName()
|
2020-12-22 18:26:05 +08:00
|
|
|
if dirHash, ok := store.isSuperLargeDirectory(dir); ok {
|
|
|
|
dir, name = dirHash+name, ""
|
|
|
|
}
|
|
|
|
|
2018-05-27 15:01:15 +08:00
|
|
|
meta, err := entry.EncodeAttributesAndChunks()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("encode %s: %s", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
2022-05-02 01:28:55 +08:00
|
|
|
if len(entry.Chunks) > filer.CountEntryChunksForGzip {
|
2020-09-04 02:00:20 +08:00
|
|
|
meta = util.MaybeGzipData(meta)
|
|
|
|
}
|
|
|
|
|
2018-05-27 15:01:15 +08:00
|
|
|
if err := store.session.Query(
|
2019-01-09 12:26:19 +08:00
|
|
|
"INSERT INTO filemeta (directory,name,meta) VALUES(?,?,?) USING TTL ? ",
|
|
|
|
dir, name, meta, entry.TtlSec).Exec(); err != nil {
|
2018-05-27 15:01:15 +08:00
|
|
|
return fmt.Errorf("insert %s: %s", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 15:21:19 +08:00
|
|
|
func (store *CassandraStore) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
|
2018-05-27 15:01:15 +08:00
|
|
|
|
2019-03-16 06:55:34 +08:00
|
|
|
return store.InsertEntry(ctx, entry)
|
2018-05-27 15:01:15 +08:00
|
|
|
}
|
|
|
|
|
2020-09-01 15:21:19 +08:00
|
|
|
func (store *CassandraStore) FindEntry(ctx context.Context, fullpath util.FullPath) (entry *filer.Entry, err error) {
|
2018-05-27 15:01:15 +08:00
|
|
|
|
|
|
|
dir, name := fullpath.DirAndName()
|
2020-12-22 18:26:05 +08:00
|
|
|
if dirHash, ok := store.isSuperLargeDirectory(dir); ok {
|
|
|
|
dir, name = dirHash+name, ""
|
|
|
|
}
|
|
|
|
|
2018-05-27 15:01:15 +08:00
|
|
|
var data []byte
|
|
|
|
if err := store.session.Query(
|
|
|
|
"SELECT meta FROM filemeta WHERE directory=? AND name=?",
|
2021-07-21 05:47:39 +08:00
|
|
|
dir, name).Scan(&data); err != nil {
|
2018-05-27 15:01:15 +08:00
|
|
|
if err != gocql.ErrNotFound {
|
2020-03-08 09:01:39 +08:00
|
|
|
return nil, filer_pb.ErrNotFound
|
2018-05-27 15:01:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data) == 0 {
|
2020-03-08 09:01:39 +08:00
|
|
|
return nil, filer_pb.ErrNotFound
|
2018-05-27 15:01:15 +08:00
|
|
|
}
|
|
|
|
|
2020-09-01 15:21:19 +08:00
|
|
|
entry = &filer.Entry{
|
2018-05-27 15:01:15 +08:00
|
|
|
FullPath: fullpath,
|
|
|
|
}
|
2020-09-04 02:00:20 +08:00
|
|
|
err = entry.DecodeAttributesAndChunks(util.MaybeDecompressData(data))
|
2018-05-27 15:01:15 +08:00
|
|
|
if err != nil {
|
|
|
|
return entry, fmt.Errorf("decode %s : %v", entry.FullPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry, nil
|
|
|
|
}
|
|
|
|
|
2020-03-23 15:01:34 +08:00
|
|
|
func (store *CassandraStore) DeleteEntry(ctx context.Context, fullpath util.FullPath) error {
|
2018-05-27 15:01:15 +08:00
|
|
|
|
|
|
|
dir, name := fullpath.DirAndName()
|
2020-12-22 18:26:05 +08:00
|
|
|
if dirHash, ok := store.isSuperLargeDirectory(dir); ok {
|
|
|
|
dir, name = dirHash+name, ""
|
|
|
|
}
|
2018-05-27 15:01:15 +08:00
|
|
|
|
|
|
|
if err := store.session.Query(
|
|
|
|
"DELETE FROM filemeta WHERE directory=? AND name=?",
|
|
|
|
dir, name).Exec(); err != nil {
|
2018-05-31 11:24:57 +08:00
|
|
|
return fmt.Errorf("delete %s : %v", fullpath, err)
|
2019-12-13 16:23:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-22 23:23:20 +08:00
|
|
|
func (store *CassandraStore) DeleteFolderChildren(ctx context.Context, fullpath util.FullPath) error {
|
2020-12-22 18:26:05 +08:00
|
|
|
if _, ok := store.isSuperLargeDirectory(string(fullpath)); ok {
|
|
|
|
return nil // filer.ErrUnsupportedSuperLargeDirectoryListing
|
|
|
|
}
|
2019-12-13 16:23:05 +08:00
|
|
|
|
|
|
|
if err := store.session.Query(
|
|
|
|
"DELETE FROM filemeta WHERE directory=?",
|
|
|
|
fullpath).Exec(); err != nil {
|
|
|
|
return fmt.Errorf("delete %s : %v", fullpath, err)
|
2018-05-27 15:01:15 +08:00
|
|
|
}
|
|
|
|
|
2018-05-31 11:24:57 +08:00
|
|
|
return nil
|
2018-05-27 15:01:15 +08:00
|
|
|
}
|
|
|
|
|
2021-01-16 15:56:24 +08:00
|
|
|
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) {
|
|
|
|
return lastFileName, filer.ErrUnsupportedListDirectoryPrefixed
|
2020-08-06 01:19:16 +08:00
|
|
|
}
|
|
|
|
|
2021-01-16 15:56:24 +08:00
|
|
|
func (store *CassandraStore) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
|
2018-05-27 15:01:15 +08:00
|
|
|
|
2021-01-15 14:38:34 +08:00
|
|
|
if _, ok := store.isSuperLargeDirectory(string(dirPath)); ok {
|
2020-12-22 18:26:05 +08:00
|
|
|
return // nil, filer.ErrUnsupportedSuperLargeDirectoryListing
|
|
|
|
}
|
|
|
|
|
2018-05-27 15:01:15 +08:00
|
|
|
cqlStr := "SELECT NAME, meta FROM filemeta WHERE directory=? AND name>? ORDER BY NAME ASC LIMIT ?"
|
2021-01-15 14:38:34 +08:00
|
|
|
if includeStartFile {
|
2018-05-27 15:01:15 +08:00
|
|
|
cqlStr = "SELECT NAME, meta FROM filemeta WHERE directory=? AND name>=? ORDER BY NAME ASC LIMIT ?"
|
|
|
|
}
|
|
|
|
|
|
|
|
var data []byte
|
|
|
|
var name string
|
2021-01-15 14:38:34 +08:00
|
|
|
iter := store.session.Query(cqlStr, string(dirPath), startFileName, limit+1).Iter()
|
2018-05-27 15:01:15 +08:00
|
|
|
for iter.Scan(&name, &data) {
|
2020-09-01 15:21:19 +08:00
|
|
|
entry := &filer.Entry{
|
2021-01-15 14:38:34 +08:00
|
|
|
FullPath: util.NewFullPath(string(dirPath), name),
|
2018-05-27 15:01:15 +08:00
|
|
|
}
|
2021-01-16 15:56:24 +08:00
|
|
|
lastFileName = name
|
2020-09-04 02:00:20 +08:00
|
|
|
if decodeErr := entry.DecodeAttributesAndChunks(util.MaybeDecompressData(data)); decodeErr != nil {
|
2018-05-27 15:01:15 +08:00
|
|
|
err = decodeErr
|
|
|
|
glog.V(0).Infof("list %s : %v", entry.FullPath, err)
|
|
|
|
break
|
|
|
|
}
|
2021-01-16 15:56:24 +08:00
|
|
|
if !eachEntryFunc(entry) {
|
|
|
|
break
|
|
|
|
}
|
2018-05-27 15:01:15 +08:00
|
|
|
}
|
|
|
|
if err := iter.Close(); err != nil {
|
|
|
|
glog.V(0).Infof("list iterator close: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-01-16 15:56:24 +08:00
|
|
|
return lastFileName, err
|
2018-05-27 15:01:15 +08:00
|
|
|
}
|
2020-03-15 11:30:26 +08:00
|
|
|
|
|
|
|
func (store *CassandraStore) Shutdown() {
|
|
|
|
store.session.Close()
|
|
|
|
}
|