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.

318 lines
9.5 KiB

  1. //go:build elastic
  2. // +build elastic
  3. package elastic
  4. import (
  5. "context"
  6. "fmt"
  7. "math"
  8. "strings"
  9. jsoniter "github.com/json-iterator/go"
  10. elastic "github.com/olivere/elastic/v7"
  11. "github.com/seaweedfs/seaweedfs/weed/filer"
  12. "github.com/seaweedfs/seaweedfs/weed/glog"
  13. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  14. weed_util "github.com/seaweedfs/seaweedfs/weed/util"
  15. )
  16. var (
  17. indexType = "_doc"
  18. indexPrefix = ".seaweedfs_"
  19. indexKV = ".seaweedfs_kv_entries"
  20. kvMappings = ` {
  21. "mappings": {
  22. "enabled": false,
  23. "properties": {
  24. "Value":{
  25. "type": "binary"
  26. }
  27. }
  28. }
  29. }`
  30. )
  31. type ESEntry struct {
  32. ParentId string `json:"ParentId"`
  33. Entry *filer.Entry
  34. }
  35. type ESKVEntry struct {
  36. Value []byte `json:"Value"`
  37. }
  38. func init() {
  39. filer.Stores = append(filer.Stores, &ElasticStore{})
  40. }
  41. type ElasticStore struct {
  42. client *elastic.Client
  43. maxPageSize int
  44. }
  45. func (store *ElasticStore) GetName() string {
  46. return "elastic7"
  47. }
  48. func (store *ElasticStore) Initialize(configuration weed_util.Configuration, prefix string) (err error) {
  49. options := []elastic.ClientOptionFunc{}
  50. servers := configuration.GetStringSlice(prefix + "servers")
  51. options = append(options, elastic.SetURL(servers...))
  52. username := configuration.GetString(prefix + "username")
  53. password := configuration.GetString(prefix + "password")
  54. if username != "" && password != "" {
  55. options = append(options, elastic.SetBasicAuth(username, password))
  56. }
  57. options = append(options, elastic.SetSniff(configuration.GetBool(prefix+"sniff_enabled")))
  58. options = append(options, elastic.SetHealthcheck(configuration.GetBool(prefix+"healthcheck_enabled")))
  59. store.maxPageSize = configuration.GetInt(prefix + "index.max_result_window")
  60. if store.maxPageSize <= 0 {
  61. store.maxPageSize = 10000
  62. }
  63. glog.Infof("filer store elastic endpoints: %v.", servers)
  64. return store.initialize(options)
  65. }
  66. func (store *ElasticStore) initialize(options []elastic.ClientOptionFunc) (err error) {
  67. ctx := context.Background()
  68. store.client, err = elastic.NewClient(options...)
  69. if err != nil {
  70. return fmt.Errorf("init elastic %v.", err)
  71. }
  72. if ok, err := store.client.IndexExists(indexKV).Do(ctx); err == nil && !ok {
  73. _, err = store.client.CreateIndex(indexKV).Body(kvMappings).Do(ctx)
  74. if err != nil {
  75. return fmt.Errorf("create index(%s) %v.", indexKV, err)
  76. }
  77. }
  78. return nil
  79. }
  80. func (store *ElasticStore) BeginTransaction(ctx context.Context) (context.Context, error) {
  81. return ctx, nil
  82. }
  83. func (store *ElasticStore) CommitTransaction(ctx context.Context) error {
  84. return nil
  85. }
  86. func (store *ElasticStore) RollbackTransaction(ctx context.Context) error {
  87. return nil
  88. }
  89. func (store *ElasticStore) ListDirectoryPrefixedEntries(ctx context.Context, dirPath weed_util.FullPath, startFileName string, includeStartFile bool, limit int64, prefix string, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
  90. return lastFileName, filer.ErrUnsupportedListDirectoryPrefixed
  91. }
  92. func (store *ElasticStore) InsertEntry(ctx context.Context, entry *filer.Entry) (err error) {
  93. index := getIndex(entry.FullPath, false)
  94. dir, _ := entry.FullPath.DirAndName()
  95. id := weed_util.Md5String([]byte(entry.FullPath))
  96. esEntry := &ESEntry{
  97. ParentId: weed_util.Md5String([]byte(dir)),
  98. Entry: entry,
  99. }
  100. value, err := jsoniter.Marshal(esEntry)
  101. if err != nil {
  102. glog.Errorf("insert entry(%s) %v.", string(entry.FullPath), err)
  103. return fmt.Errorf("insert entry %v.", err)
  104. }
  105. _, err = store.client.Index().
  106. Index(index).
  107. Type(indexType).
  108. Id(id).
  109. BodyJson(string(value)).
  110. Do(ctx)
  111. if err != nil {
  112. glog.Errorf("insert entry(%s) %v.", string(entry.FullPath), err)
  113. return fmt.Errorf("insert entry %v.", err)
  114. }
  115. return nil
  116. }
  117. func (store *ElasticStore) UpdateEntry(ctx context.Context, entry *filer.Entry) (err error) {
  118. return store.InsertEntry(ctx, entry)
  119. }
  120. func (store *ElasticStore) FindEntry(ctx context.Context, fullpath weed_util.FullPath) (entry *filer.Entry, err error) {
  121. index := getIndex(fullpath, false)
  122. id := weed_util.Md5String([]byte(fullpath))
  123. searchResult, err := store.client.Get().
  124. Index(index).
  125. Type(indexType).
  126. Id(id).
  127. Do(ctx)
  128. if elastic.IsNotFound(err) {
  129. return nil, filer_pb.ErrNotFound
  130. }
  131. if searchResult != nil && searchResult.Found {
  132. esEntry := &ESEntry{
  133. ParentId: "",
  134. Entry: &filer.Entry{},
  135. }
  136. err := jsoniter.Unmarshal(searchResult.Source, esEntry)
  137. return esEntry.Entry, err
  138. }
  139. glog.Errorf("find entry(%s),%v.", string(fullpath), err)
  140. return nil, filer_pb.ErrNotFound
  141. }
  142. func (store *ElasticStore) DeleteEntry(ctx context.Context, fullpath weed_util.FullPath) (err error) {
  143. index := getIndex(fullpath, false)
  144. id := weed_util.Md5String([]byte(fullpath))
  145. strFullpath := string(fullpath)
  146. // A top-level subdirectory refers to an Elasticsearch index.
  147. // If we delete an entry at the top level, we should attempt to delete the corresponding Elasticsearch index.
  148. if strings.Count(strFullpath, "/") == 1 {
  149. entry, err2 := store.FindEntry(ctx, fullpath)
  150. if err2 == nil && entry.IsDirectory() {
  151. bucketIndex := indexPrefix + strFullpath[1:]
  152. store.deleteIndex(ctx, bucketIndex)
  153. }
  154. }
  155. return store.deleteEntry(ctx, index, id)
  156. }
  157. func (store *ElasticStore) deleteIndex(ctx context.Context, index string) (err error) {
  158. deleteResult, err := store.client.DeleteIndex(index).Do(ctx)
  159. if elastic.IsNotFound(err) || (err == nil && deleteResult.Acknowledged) {
  160. return nil
  161. }
  162. glog.Errorf("delete index(%s) %v.", index, err)
  163. return err
  164. }
  165. func (store *ElasticStore) deleteEntry(ctx context.Context, index, id string) (err error) {
  166. deleteResult, err := store.client.Delete().
  167. Index(index).
  168. Type(indexType).
  169. Id(id).
  170. Do(ctx)
  171. if err == nil {
  172. if deleteResult.Result == "deleted" || deleteResult.Result == "not_found" {
  173. return nil
  174. }
  175. }
  176. glog.Errorf("delete entry(index:%s,_id:%s) %v.", index, id, err)
  177. return fmt.Errorf("delete entry %v.", err)
  178. }
  179. func (store *ElasticStore) DeleteFolderChildren(ctx context.Context, fullpath weed_util.FullPath) (err error) {
  180. _, err = store.ListDirectoryEntries(ctx, fullpath, "", false, math.MaxInt32, func(entry *filer.Entry) bool {
  181. if err := store.DeleteEntry(ctx, entry.FullPath); err != nil {
  182. glog.Errorf("elastic delete %s: %v.", entry.FullPath, err)
  183. return false
  184. }
  185. return true
  186. })
  187. return
  188. }
  189. func (store *ElasticStore) ListDirectoryEntries(ctx context.Context, dirPath weed_util.FullPath, startFileName string, includeStartFile bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
  190. return store.listDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit, eachEntryFunc)
  191. }
  192. func (store *ElasticStore) listDirectoryEntries(
  193. ctx context.Context, fullpath weed_util.FullPath, startFileName string, inclusive bool, limit int64, eachEntryFunc filer.ListEachEntryFunc) (lastFileName string, err error) {
  194. first := true
  195. index := getIndex(fullpath, true)
  196. nextStart := ""
  197. parentId := weed_util.Md5String([]byte(fullpath))
  198. if _, err = store.client.Refresh(index).Do(ctx); err != nil {
  199. if elastic.IsNotFound(err) {
  200. store.client.CreateIndex(index).Do(ctx)
  201. return
  202. }
  203. }
  204. for {
  205. result := &elastic.SearchResult{}
  206. if (startFileName == "" && first) || inclusive {
  207. if result, err = store.search(ctx, index, parentId); err != nil {
  208. glog.Errorf("search (%s,%s,%t,%d) %v.", string(fullpath), startFileName, inclusive, limit, err)
  209. return
  210. }
  211. } else {
  212. fullPath := string(fullpath) + "/" + startFileName
  213. if !first {
  214. fullPath = nextStart
  215. }
  216. after := weed_util.Md5String([]byte(fullPath))
  217. if result, err = store.searchAfter(ctx, index, parentId, after); err != nil {
  218. glog.Errorf("searchAfter (%s,%s,%t,%d) %v.", string(fullpath), startFileName, inclusive, limit, err)
  219. return
  220. }
  221. }
  222. first = false
  223. for _, hit := range result.Hits.Hits {
  224. esEntry := &ESEntry{
  225. ParentId: "",
  226. Entry: &filer.Entry{},
  227. }
  228. if err := jsoniter.Unmarshal(hit.Source, esEntry); err == nil {
  229. limit--
  230. if limit < 0 {
  231. return lastFileName, nil
  232. }
  233. nextStart = string(esEntry.Entry.FullPath)
  234. fileName := esEntry.Entry.FullPath.Name()
  235. if fileName == startFileName && !inclusive {
  236. continue
  237. }
  238. if !eachEntryFunc(esEntry.Entry) {
  239. break
  240. }
  241. lastFileName = fileName
  242. }
  243. }
  244. if len(result.Hits.Hits) < store.maxPageSize {
  245. break
  246. }
  247. }
  248. return
  249. }
  250. func (store *ElasticStore) search(ctx context.Context, index, parentId string) (result *elastic.SearchResult, err error) {
  251. if count, err := store.client.Count(index).Do(ctx); err == nil && count == 0 {
  252. return &elastic.SearchResult{
  253. Hits: &elastic.SearchHits{
  254. Hits: make([]*elastic.SearchHit, 0)},
  255. }, nil
  256. }
  257. queryResult, err := store.client.Search().
  258. Index(index).
  259. Query(elastic.NewMatchQuery("ParentId", parentId)).
  260. Size(store.maxPageSize).
  261. Sort("_id", false).
  262. Do(ctx)
  263. return queryResult, err
  264. }
  265. func (store *ElasticStore) searchAfter(ctx context.Context, index, parentId, after string) (result *elastic.SearchResult, err error) {
  266. queryResult, err := store.client.Search().
  267. Index(index).
  268. Query(elastic.NewMatchQuery("ParentId", parentId)).
  269. SearchAfter(after).
  270. Size(store.maxPageSize).
  271. Sort("_id", false).
  272. Do(ctx)
  273. return queryResult, err
  274. }
  275. func (store *ElasticStore) Shutdown() {
  276. store.client.Stop()
  277. }
  278. func getIndex(fullpath weed_util.FullPath, isDirectory bool) string {
  279. path := strings.Split(string(fullpath), "/")
  280. if isDirectory && len(path) >= 2 {
  281. return indexPrefix + strings.ToLower(path[1])
  282. }
  283. if len(path) > 2 {
  284. return indexPrefix + strings.ToLower(path[1])
  285. }
  286. if len(path) == 2 {
  287. return indexPrefix
  288. }
  289. return ""
  290. }