filer/elastic: report a failed read as an error, not as a missing entry (#13334)

* filer/elastic: report a failed read as an error, not as a missing entry

FindEntry returned filer_pb.ErrNotFound for any error from Elasticsearch
other than not-found, so a lookup that timed out or met an unavailable
cluster answered that an existing entry was gone. It now returns the
error, as #13287 and #13289 made the TiKV, MongoDB, ArangoDB and RocksDB
stores do.

KvGet reported a value it could not decode as missing. It now returns the
decode error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer/elastic: keep a top-level directory whose lookup failed

DeleteEntry looked up a top-level entry to decide whether to drop its
index, and treated a failed lookup like a file: it deleted the entry and
left the directory's index behind. Return the lookup error instead, so the
delete fails and a retry drops the index.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-12-03 10:32:34 -08:00
1 parent 5dafe56a63
commit 370fd46ed9
3 files changed
+112 -3

No files matched your search

+7 -1
View File
@@ -4,6 +4,7 @@ package elastic
import (
"context"
"errors"
"fmt"
"math"
"strings"
@@ -169,6 +170,9 @@ func (store *ElasticStore) FindEntry(ctx context.Context, fullpath weed_util.Ful
if elastic.IsNotFound(err) {
return nil, filer_pb.ErrNotFound
}
if err != nil {
return nil, fmt.Errorf("find %s: %w", fullpath, err)
}
if searchResult != nil && searchResult.Found {
esEntry := &ESEntry{
ParentId: "",
@@ -177,7 +181,6 @@ func (store *ElasticStore) FindEntry(ctx context.Context, fullpath weed_util.Ful
err := jsoniter.Unmarshal(searchResult.Source, esEntry)
return esEntry.Entry, err
}
glog.ErrorfCtx(ctx, "find entry(%s),%v.", string(fullpath), err)
return nil, filer_pb.ErrNotFound
}
@@ -190,6 +193,9 @@ func (store *ElasticStore) DeleteEntry(ctx context.Context, fullpath weed_util.F
// If we delete an entry at the top level, we should attempt to delete the corresponding Elasticsearch index.
if strings.Count(strFullpath, "/") == 1 {
entry, err2 := store.FindEntry(ctx, fullpath)
if err2 != nil && !errors.Is(err2, filer_pb.ErrNotFound) {
return err2
}
if err2 == nil && entry.IsDirectory() {
bucketIndex := indexPrefix + strFullpath[1:]
store.deleteIndex(ctx, bucketIndex)
@@ -0,0 +1,102 @@
//go:build elastic
package elastic
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
elastic "github.com/olivere/elastic/v7"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
// storeAnswering returns a store whose cluster answers every request with
// status and body.
func storeAnswering(t *testing.T, status int, body string) *ElasticStore {
t.Helper()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
fmt.Fprint(w, body)
}))
t.Cleanup(server.Close)
client, err := elastic.NewClient(elastic.SetURL(server.URL), elastic.SetSniff(false), elastic.SetHealthcheck(false))
if err != nil {
t.Fatal(err)
}
return &ElasticStore{client: client}
}
func TestFindEntryReportsAFailedReadAsAnError(t *testing.T) {
store := storeAnswering(t, http.StatusServiceUnavailable, `{"error":{"type":"cluster_block_exception","reason":"blocked"},"status":503}`)
_, err := store.FindEntry(context.Background(), util.FullPath("/buckets/b/kept.bin"))
if err == nil {
t.Fatal("find succeeded although the cluster failed the read")
}
if errors.Is(err, filer_pb.ErrNotFound) {
t.Fatalf("a failed read was reported as a missing entry: %v", err)
}
}
func TestFindEntryReportsAMissingEntryAsNotFound(t *testing.T) {
for name, body := range map[string]string{
"missing document": `{"_index":".seaweedfs_buckets","_type":"_doc","_id":"x","found":false}`,
"missing index": `{"error":{"type":"index_not_found_exception","reason":"no such index"},"status":404}`,
} {
t.Run(name, func(t *testing.T) {
store := storeAnswering(t, http.StatusNotFound, body)
_, err := store.FindEntry(context.Background(), util.FullPath("/buckets/b/gone.bin"))
if !errors.Is(err, filer_pb.ErrNotFound) {
t.Fatalf("find = %v, want filer_pb.ErrNotFound", err)
}
})
}
}
func TestKvGetReportsAValueItCannotDecodeAsAnError(t *testing.T) {
store := storeAnswering(t, http.StatusOK, `{"_index":".seaweedfs_kv_entries","_type":"_doc","_id":"k","found":true,"_source":"not an object"}`)
_, err := store.KvGet(context.Background(), []byte("k"))
if err == nil {
t.Fatal("KvGet succeeded on a value it cannot decode")
}
if errors.Is(err, filer.ErrKvNotFound) {
t.Fatalf("a value that cannot be decoded was reported as missing: %v", err)
}
}
func TestDeleteEntryKeepsATopLevelDirectoryItCannotRead(t *testing.T) {
deleted := false
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == http.MethodDelete {
deleted = true
fmt.Fprint(w, `{"result":"deleted"}`)
return
}
w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprint(w, `{"error":{"type":"cluster_block_exception","reason":"blocked"},"status":503}`)
}))
t.Cleanup(server.Close)
client, err := elastic.NewClient(elastic.SetURL(server.URL), elastic.SetSniff(false), elastic.SetHealthcheck(false))
if err != nil {
t.Fatal(err)
}
store := &ElasticStore{client: client}
if err := store.DeleteEntry(context.Background(), util.FullPath("/topdir")); err == nil {
t.Fatal("delete succeeded although the directory could not be read")
}
if deleted {
t.Fatal("the entry was deleted without its index")
}
}
+3 -2
View File
@@ -43,9 +43,10 @@ func (store *ElasticStore) KvGet(ctx context.Context, key []byte) (value []byte,
}
if searchResult != nil && searchResult.Found {
esEntry := &ESKVEntry{}
if err := jsoniter.Unmarshal(searchResult.Source, esEntry); err == nil {
return esEntry.Value, nil
if err := jsoniter.Unmarshal(searchResult.Source, esEntry); err != nil {
return value, fmt.Errorf("decode key %q: %w", key, err)
}
return esEntry.Value, nil
}
return value, filer.ErrKvNotFound
}