Skip to content

Commit

Permalink
Auditing: Try get index before creating it. (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored Apr 3, 2024
1 parent 74e9c4c commit 2322a38
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
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

0 comments on commit 2322a38

Please sign in to comment.