Files
Jesse HallamandGitHub 7e1936a976 MM-68248: Handle missing OpenSearch indexes gracefully before reindex (#36712)
* MM-68248: Handle missing indexes gracefully before reindex

OpenSearch v3 rejects _update_by_query and _delete_by_query with no index
argument (405), and returns index_not_found_exception (404) when querying
an exact index name that hasn't been created yet. Both arise before any
reindex has run, since indexes are created on first document write.

Return nil/empty instead of an error from all affected operations, and add
test coverage for each in the no-indexes state.

* MM-68248: Fix copy-paste operation names in DeleteFilesBatch

* MM-68248: Add i18n string for delete_files_batch error

* MM-68248: Also check error type in isIndexNotFound
2026-05-22 16:05:03 +00:00

308 lines
9.4 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.enterprise for license information.
package opensearch
import (
"bytes"
"context"
"encoding/json"
"os"
"strings"
"testing"
"github.com/opensearch-project/opensearch-go/v4"
"github.com/opensearch-project/opensearch-go/v4/opensearchapi"
"github.com/stretchr/testify/suite"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/v8/channels/api4"
"github.com/mattermost/mattermost/server/v8/enterprise/elasticsearch/common"
"github.com/mattermost/mattermost/server/v8/platform/shared/filestore"
"github.com/mattermost/mattermost/server/v8/platform/shared/filestore/mocks"
)
type OpensearchInterfaceTestSuite struct {
common.CommonTestSuite
th *api4.TestHelper
client *opensearchapi.Client
ctx context.Context
fileBackend filestore.FileBackend
}
func TestOpensearchInterfaceTestSuite(t *testing.T) {
testSuite := &OpensearchInterfaceTestSuite{
CommonTestSuite: common.CommonTestSuite{},
}
suite.Run(t, testSuite)
}
func (s *OpensearchInterfaceTestSuite) SetupSuite() {
if os.Getenv("IS_CI") == "true" {
os.Setenv("MM_ELASTICSEARCHSETTINGS_CONNECTIONURL", "http://opensearch:9201")
os.Setenv("MM_ELASTICSEARCHSETTINGS_BACKEND", "opensearch")
}
s.th = api4.SetupEnterprise(s.T()).InitBasic(s.T())
s.CommonTestSuite.TH = s.th
s.CommonTestSuite.GetDocumentFn = func(index, documentID string) (bool, json.RawMessage, error) {
resp, err := s.client.Document.Get(s.ctx, opensearchapi.DocumentGetReq{
Index: index,
DocumentID: documentID,
})
if resp == nil {
return false, nil, err
}
return resp.Found, resp.Source, err
}
s.CommonTestSuite.RefreshIndexFn = func() error {
_, err := s.client.Indices.Refresh(context.Background(), nil)
return err
}
s.CommonTestSuite.CreateIndexFn = func(index string) error {
_, err := s.client.Indices.Create(s.ctx, opensearchapi.IndicesCreateReq{
Index: index,
})
return err
}
s.CommonTestSuite.GetIndexFn = func(indexPattern string) ([]string, error) {
res, err := s.client.Indices.Get(s.ctx, opensearchapi.IndicesGetReq{
Indices: []string{indexPattern},
})
if err != nil {
return nil, err
}
var names []string
for name := range res.Indices {
names = append(names, name)
}
return names, nil
}
// Set up the state for the tests.
s.th.App.UpdateConfig(func(cfg *model.Config) {
if os.Getenv("IS_CI") == "true" {
*cfg.ElasticsearchSettings.ConnectionURL = "http://opensearch:9201"
} else {
*cfg.ElasticsearchSettings.ConnectionURL = "http://localhost:9201"
}
*cfg.ElasticsearchSettings.Backend = model.ElasticsearchSettingsOSBackend
*cfg.ElasticsearchSettings.EnableIndexing = true
*cfg.ElasticsearchSettings.EnableSearching = true
*cfg.ElasticsearchSettings.EnableAutocomplete = true
*cfg.ElasticsearchSettings.LiveIndexingBatchSize = 1
*cfg.SqlSettings.DisableDatabaseSearch = true
})
s.th.App.Srv().SetLicense(model.NewTestLicense())
if s.fileBackend == nil {
s.fileBackend = &mocks.FileBackend{}
}
// Initialise other stuff for the test.
s.client = createTestClient(s.T(), s.th.Context, s.th.App.Config(), s.th.App.FileBackend())
s.ctx = context.Background()
// Register search engine
s.th.App.SearchEngine().RegisterElasticsearchEngine(&OpensearchInterfaceImpl{Platform: s.th.Server.Platform()})
}
func (s *OpensearchInterfaceTestSuite) TearDownSuite() {
if os.Getenv("IS_CI") == "true" {
os.Setenv("MM_ELASTICSEARCHSETTINGS_CONNECTIONURL", "http://elasticsearch:9201")
os.Unsetenv("MM_ELASTICSEARCHSETTINGS_BACKEND")
}
}
func (s *OpensearchInterfaceTestSuite) SetupTest() {
if strings.Contains(s.T().Name(), "CJK") {
s.th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ElasticsearchSettings.EnableCJKAnalyzers = true
})
} else {
s.th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ElasticsearchSettings.EnableCJKAnalyzers = false
})
}
s.CommonTestSuite.ESImpl = s.th.App.SearchEngine().ElasticsearchEngine
if s.CommonTestSuite.ESImpl.IsActive() {
appErr := s.CommonTestSuite.ESImpl.Stop()
s.Require().Nil(appErr)
}
s.Require().Nil(s.CommonTestSuite.ESImpl.Start(context.Background()))
s.Nil(s.CommonTestSuite.ESImpl.PurgeIndexes(s.th.Context))
s.NoError(s.RefreshIndexFn())
}
func (s *OpensearchInterfaceTestSuite) TestSyncBulkIndexChannels() {
s.Run("Should index multiple channels successfully", func() {
// Create test channels
channel1 := &model.Channel{
TeamId: s.th.BasicTeam.Id,
Type: model.ChannelTypeOpen,
Name: "test-channel-1",
DisplayName: "Test Channel 1",
}
channel1.PreSave()
channel2 := &model.Channel{
TeamId: s.th.BasicTeam.Id,
Type: model.ChannelTypePrivate,
Name: "test-channel-2",
DisplayName: "Test Channel 2",
}
channel2.PreSave()
channels := []*model.Channel{channel1, channel2}
// Mock getUserIDsForChannel function
getUserIDsForChannel := func(channel *model.Channel) ([]string, error) {
return []string{s.th.BasicUser.Id, s.th.BasicUser2.Id}, nil
}
teamMemberIDs := []string{s.th.BasicUser.Id, s.th.BasicUser2.Id}
// Test the bulk indexing
appErr := s.CommonTestSuite.ESImpl.SyncBulkIndexChannels(s.th.Context, channels, getUserIDsForChannel, teamMemberIDs)
s.Require().Nil(appErr)
// Refresh the index to ensure data is searchable
s.Require().NoError(s.CommonTestSuite.RefreshIndexFn())
// Verify both channels are indexed
found, _, err := s.CommonTestSuite.GetDocumentFn("channels", channel1.Id)
s.Require().NoError(err)
s.Require().True(found)
found, _, err = s.CommonTestSuite.GetDocumentFn("channels", channel2.Id)
s.Require().NoError(err)
s.Require().True(found)
})
s.Run("Should handle empty channels list", func() {
getUserIDsForChannel := func(channel *model.Channel) ([]string, error) {
return []string{}, nil
}
appErr := s.CommonTestSuite.ESImpl.SyncBulkIndexChannels(s.th.Context, []*model.Channel{}, getUserIDsForChannel, []string{})
s.Require().Nil(appErr)
})
s.Run("Should handle getUserIDsForChannel error", func() {
channel := &model.Channel{
TeamId: s.th.BasicTeam.Id,
Type: model.ChannelTypeOpen,
Name: "test-channel-error",
DisplayName: "Test Channel Error",
}
channel.PreSave()
getUserIDsForChannel := func(channel *model.Channel) ([]string, error) {
return nil, model.NewAppError("TestError", "test.error", nil, "", 500)
}
appErr := s.CommonTestSuite.ESImpl.SyncBulkIndexChannels(s.th.Context, []*model.Channel{channel}, getUserIDsForChannel, []string{})
s.Require().NotNil(appErr)
s.Require().Contains(appErr.Error(), "test.error")
})
}
// TestNoIndexesGracefulHandling verifies that write and search operations
// return nil/empty (not an error) when no indexes exist yet. This covers the
// state before any reindex has run: the index templates are present but the
// actual indexes have never been created.
func (s *OpensearchInterfaceTestSuite) TestNoIndexesGracefulHandling() {
// SetupTest already calls PurgeIndexes, so there are no indexes at this point.
impl := s.CommonTestSuite.ESImpl
rctx := s.th.Context
s.Run("BackfillPostsChannelType", func() {
appErr := impl.BackfillPostsChannelType(rctx, []string{"channel1", "channel2"}, "O")
s.Nil(appErr)
})
s.Run("DeleteChannelPosts", func() {
appErr := impl.DeleteChannelPosts(rctx, s.th.BasicChannel.Id)
s.Nil(appErr)
})
s.Run("DeleteUserPosts", func() {
appErr := impl.DeleteUserPosts(rctx, s.th.BasicUser.Id)
s.Nil(appErr)
})
s.Run("UpdatePostsChannelTypeByChannelId", func() {
appErr := impl.UpdatePostsChannelTypeByChannelId(rctx, s.th.BasicChannel.Id, "O")
s.Nil(appErr)
})
s.Run("SearchFiles", func() {
channels := model.ChannelList{s.th.BasicChannel}
params := model.ParseSearchParams("test", 0)
fileIDs, appErr := impl.SearchFiles(channels, params, 0, 20)
s.Nil(appErr)
s.Empty(fileIDs)
})
s.Run("DeletePostFiles", func() {
appErr := impl.DeletePostFiles(rctx, s.th.BasicPost.Id)
s.Nil(appErr)
})
s.Run("DeleteUserFiles", func() {
appErr := impl.DeleteUserFiles(rctx, s.th.BasicUser.Id)
s.Nil(appErr)
})
s.Run("DeleteFilesBatch", func() {
appErr := impl.DeleteFilesBatch(rctx, model.GetMillis(), 1000)
s.Nil(appErr)
})
}
func (s *OpensearchInterfaceTestSuite) TestTemplateCreationClientError() {
s.Run("Should handle error with CausedBy information from opensearch", func() {
// Invalid template request that will trigger an error with caused_by
invalidTemplateBody := map[string]any{
"index_patterns": []string{"test-invalid-*"},
"template": map[string]any{
"settings": map[string]any{
"analysis": map[string]any{
"analyzer": map[string]any{
"my_analyzer": map[string]any{
"type": "custom",
"tokenizer": "nonexistent_tokenizer",
},
},
},
},
},
}
templateBytes, err := json.Marshal(invalidTemplateBody)
s.Require().NoError(err)
_, err = s.client.IndexTemplate.Create(s.ctx, opensearchapi.IndexTemplateCreateReq{
IndexTemplate: "test-invalid-template",
Body: bytes.NewReader(templateBytes),
})
var osErr *opensearch.StructError
s.Require().ErrorAs(err, &osErr)
s.Require().NotNil(osErr.Err.CausedBy, "Expected CausedBy to be present")
s.Require().NotEmpty(osErr.Err.CausedBy.Type)
s.Require().NotEmpty(osErr.Err.CausedBy.Reason)
// clean up after test
_, _ = s.client.IndexTemplate.Delete(s.ctx, opensearchapi.IndexTemplateDeleteReq{
IndexTemplate: "test-invalid-template",
})
})
}