fix(search): skip deletion of missing temporary indexes
This commit is contained in:
1 parent
66d3897f66
commit
f59d1e8d03
3 files changed
+67
-1
No files matched your search
@@ -175,7 +175,7 @@ func (ix *Indexer) Reindex(ctx context.Context) error {
|
||||
|
||||
func (ix *Indexer) rebuild(ctx context.Context, s indexSpec) error {
|
||||
next := s.uid + "_next"
|
||||
if err := ix.meili.call(ctx, http.MethodDelete, "/indexes/"+next, nil); err != nil && !errors.Is(err, ErrMeiliNotFound) {
|
||||
if err := ix.meili.deleteIndexIfExists(ctx, next); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ix.meili.call(ctx, http.MethodPost, "/indexes", map[string]any{"uid": next, "primaryKey": "id"}); err != nil {
|
||||
|
||||
@@ -114,6 +114,22 @@ func (m *Meili) call(ctx context.Context, method, path string, body any) error {
|
||||
return m.wait(ctx, t)
|
||||
}
|
||||
|
||||
// deleteIndexIfExists не создаёт заведомо провальную задачу удаления, если индекса нет.
|
||||
func (m *Meili) deleteIndexIfExists(ctx context.Context, uid string) error {
|
||||
path := "/indexes/" + uid
|
||||
if err := m.do(ctx, http.MethodGet, path, nil, nil); err != nil {
|
||||
if errors.Is(err, ErrMeiliNotFound) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
// Индекс мог исчезнуть между проверкой и выполнением асинхронной задачи.
|
||||
if err := m.call(ctx, http.MethodDelete, path, nil); err != nil && !errors.Is(err, ErrMeiliNotFound) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Healthy — Meilisearch отвечает.
|
||||
func (m *Meili) Healthy(ctx context.Context) error {
|
||||
return m.do(ctx, http.MethodGet, "/health", nil, nil)
|
||||
|
||||
@@ -5,9 +5,59 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDeleteIndexIfExists(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
lookupCode int
|
||||
task string
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "missing index", lookupCode: http.StatusNotFound},
|
||||
{name: "existing index", lookupCode: http.StatusOK, task: `{"status":"succeeded"}`},
|
||||
{name: "lookup failure", lookupCode: http.StatusServiceUnavailable, wantErr: true},
|
||||
{name: "delete failure", lookupCode: http.StatusOK, task: `{"status":"failed","error":{"code":"internal","message":"disk error"}}`, wantErr: true},
|
||||
{name: "concurrent deletion", lookupCode: http.StatusOK, task: `{"status":"failed","error":{"code":"index_not_found","message":"Index disappeared"}}`},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var requests []string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requests = append(requests, r.Method+" "+r.URL.Path)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.Method + " " + r.URL.Path {
|
||||
case "GET /indexes/tracks_next":
|
||||
w.WriteHeader(tc.lookupCode)
|
||||
_, _ = w.Write([]byte(`{}`))
|
||||
case "DELETE /indexes/tracks_next":
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
_, _ = w.Write([]byte(`{"taskUid":1}`))
|
||||
case "GET /tasks/1":
|
||||
_, _ = w.Write([]byte(tc.task))
|
||||
default:
|
||||
t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
err := NewMeili(srv.URL, "").deleteIndexIfExists(context.Background(), "tracks_next")
|
||||
if (err != nil) != tc.wantErr {
|
||||
t.Fatalf("err = %v, wantErr = %v", err, tc.wantErr)
|
||||
}
|
||||
wantRequests := []string{"GET /indexes/tracks_next"}
|
||||
if tc.lookupCode == http.StatusOK {
|
||||
wantRequests = append(wantRequests, "DELETE /indexes/tracks_next", "GET /tasks/1")
|
||||
}
|
||||
if !reflect.DeepEqual(requests, wantRequests) {
|
||||
t.Fatalf("requests = %v, want %v", requests, wantRequests)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// На чистом Meilisearch удаление несуществующего индекса падает в задаче, а не ответом 404.
|
||||
func TestCallMapsFailedIndexTaskToNotFound(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in new issue
Block a user