Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auditing: Try get index before creating it. #139

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions auditing/meilisearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ type meiliAuditing struct {
}

var (
errAuditingIndexCreationDeadlineUnsufficient = errors.New("auditing index creation timed out, because meilisearch took too long. Consider increasing the timeout to prevent failing requests")
errAuditingIndexCreationDeadlineInsufficient = errors.New("auditing index creation timed out, because meilisearch took too long. Consider increasing the timeout to prevent failing requests")
)

const (
meiliIndexNameTimeSuffixSchema = "\\d\\d\\d\\d-\\d\\d(-\\d\\d(_\\d\\d)?)?"
meiliIndexCreationWaitTimeout = 30 * time.Second
meiliIndexCreationWaitInterval = 50 * time.Millisecond
meiliIndexCreationWaitInterval = 100 * time.Millisecond
)

func New(c Config) (Auditing, error) {
Expand Down Expand Up @@ -351,11 +351,25 @@ func (a *meiliAuditing) decodeEntry(doc map[string]any) Entry {
func (a *meiliAuditing) getLatestIndex() (*meilisearch.Index, error) {
a.indexLock.Lock()
defer a.indexLock.Unlock()

indexUid := indexName(a.indexPrefix, a.rotationInterval)
if a.index != nil && a.index.UID == indexUid {
return a.index, nil
}

var meiliError *meilisearch.Error
index, err := a.client.GetIndex(indexUid)

switch {
case err == nil:
a.index = index
return a.index, nil
case errors.As(err, &meiliError) && meiliError.ErrCode == meilisearch.MeilisearchApiError && meiliError.MeilisearchApiError.Code == "index_not_found":
// fallthrough
default:
return nil, err
}

a.log.Debug("auditing", "create new index", a.rotationInterval, "index", indexUid)
creationTask, err := a.client.CreateIndex(&meilisearch.IndexConfig{
Uid: indexUid,
Expand All @@ -365,7 +379,7 @@ func (a *meiliAuditing) getLatestIndex() (*meilisearch.Index, error) {
return nil, fmt.Errorf("failed to request create index (%s): %w", indexUid, err)
}

waitCtx, cancelFunc := context.WithTimeoutCause(context.Background(), meiliIndexCreationWaitTimeout, errAuditingIndexCreationDeadlineUnsufficient)
waitCtx, cancelFunc := context.WithTimeoutCause(context.Background(), meiliIndexCreationWaitTimeout, errAuditingIndexCreationDeadlineInsufficient)
defer cancelFunc()
_, err = a.client.WaitForTask(creationTask.TaskUID, meilisearch.WaitParams{
Context: waitCtx,
Expand All @@ -387,6 +401,7 @@ func (a *meiliAuditing) getLatestIndex() (*meilisearch.Index, error) {
a.log.Error("auditing", "failed to clean up indexes", err)
}
}()

return a.index, nil
}

Expand Down
4 changes: 2 additions & 2 deletions auditing/meilisearch_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func StartMeilisearch(t testing.TB) (container testcontainers.Container, c *conn

meiliContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "getmeili/meilisearch:v1.2.0",
Image: "getmeili/meilisearch:v1.7.1",
ExposedPorts: []string{"7700/tcp"},
Env: map[string]string{
"MEILI_MASTER_KEY": meilisearchMasterKey,
Expand Down Expand Up @@ -145,7 +145,7 @@ func TestAuditing_Meilisearch(t *testing.T) {
t: func(t *testing.T, a Auditing) {
entries, err := a.Search(EntryFilter{})
require.NoError(t, err)
assert.Len(t, entries, 0)
assert.Empty(t, entries)
},
},
{
Expand Down
Loading