Skip to content

Commit

Permalink
reindex nodes in background
Browse files Browse the repository at this point in the history
  • Loading branch information
james03160927 committed Jan 13, 2025
1 parent 6719ba9 commit b2291b0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 19 deletions.
24 changes: 16 additions & 8 deletions integration-tests/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package integration
import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"registry-backend/config"
"registry-backend/drip"
authorization "registry-backend/server/middleware/authorization"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRegistryNode(t *testing.T) {
Expand Down Expand Up @@ -116,6 +118,18 @@ func TestRegistryNode(t *testing.T) {
assert.Equal(t, node.Repository, updatedResponse.Repository)
})

// Test reindexing nodes
t.Run("Reindex Nodes", func(t *testing.T) {
res, err := withMiddleware(authz, impl.ReindexNodes)(ctx, drip.ReindexNodesRequestObject{})
require.NoError(t, err, "Node reindexing failed")
assert.IsType(t, drip.ReindexNodes200Response{}, res)

time.Sleep(1 * time.Second)
nodes := impl.mockAlgolia.LastIndexedNodes
require.Equal(t, 1, len(nodes))
assert.Equal(t, *node.Id, nodes[0].ID)
})

// Test deleting the node
t.Run("Delete Node", func(t *testing.T) {
res, err := withMiddleware(authz, impl.DeleteNode)(ctx, drip.DeleteNodeRequestObject{
Expand All @@ -136,10 +150,4 @@ func TestRegistryNode(t *testing.T) {
assert.IsType(t, drip.DeleteNode204Response{}, res)
})

// Test reindexing nodes
t.Run("Reindex Nodes", func(t *testing.T) {
res, err := withMiddleware(authz, impl.ReindexNodes)(ctx, drip.ReindexNodesRequestObject{})
require.NoError(t, err, "Node reindexing failed")
assert.IsType(t, drip.ReindexNodes200Response{}, res)
})
}
9 changes: 5 additions & 4 deletions integration-tests/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ package integration
import (
"context"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"net"
"net/http"
"net/http/httptest"
Expand All @@ -20,6 +16,11 @@ import (
"runtime"
"strings"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"

"registry-backend/ent"
"registry-backend/ent/migrate"
"testing"
Expand Down
12 changes: 11 additions & 1 deletion logging/logging.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package drip_logging

import (
"github.com/rs/zerolog/log"
"context"
"os"
"sync"

"github.com/rs/zerolog/log"

"github.com/rs/zerolog"
"github.com/rs/zerolog/pkgerrors"
)
Expand Down Expand Up @@ -45,6 +47,14 @@ func SetupLogger() zerolog.Logger {
return log
}

func ReuseContextLogger(ctx context.Context, newCtx context.Context) context.Context {
l := zerolog.Ctx(ctx)
if l == nil {
return newCtx
}
return l.WithContext(newCtx)
}

func SetGlobalLogLevel(logLevel string) {
// Default to info level
defaultLevel := zerolog.InfoLevel
Expand Down
10 changes: 6 additions & 4 deletions server/implementation/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"registry-backend/ent/publisher"
"registry-backend/ent/schema"
"registry-backend/entity"
drip_logging "registry-backend/logging"
"registry-backend/mapper"
drip_services "registry-backend/services/registry"
"time"
Expand Down Expand Up @@ -998,13 +999,14 @@ func (s *DripStrictServerImplementation) ListAllNodeVersions(
}

func (s *DripStrictServerImplementation) ReindexNodes(ctx context.Context, request drip.ReindexNodesRequestObject) (res drip.ReindexNodesResponseObject, err error) {
err = s.RegistryService.ReindexAllNodes(ctx, s.Client)
reindexCtx := drip_logging.ReuseContextLogger(ctx, context.Background())
err = s.RegistryService.ReindexAllNodesBackground(reindexCtx, s.Client)
if err != nil {
log.Ctx(ctx).Error().Msgf("Failed to reindex all nodes w/ err: %v", err)
return drip.ReindexNodes500JSONResponse{Message: "Failed to reindex nodes", Error: err.Error()}, nil
log.Ctx(ctx).Error().Msgf("Failed to trigger reindex all nodes w/ err: %v", err)
return drip.ReindexNodes500JSONResponse{Message: "Failed to trigger reindex nodes", Error: err.Error()}, nil
}

log.Ctx(ctx).Info().Msgf("Reindex nodes successful")
log.Ctx(ctx).Info().Msgf("Triggering Reindex nodes successful")
return drip.ReindexNodes200Response{}, nil
}

Expand Down
25 changes: 23 additions & 2 deletions services/registry/registry_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"registry-backend/mapper"
drip_metric "registry-backend/server/middleware/metric"
"strings"
"sync"
"time"

"entgo.io/ent/dialect/sql"
Expand Down Expand Up @@ -1055,11 +1056,31 @@ func (s *RegistryService) ReindexAllNodes(ctx context.Context, client *ent.Clien
nvs = append(nvs, n.Edges.Versions...)
}

log.Ctx(ctx).Info().Msgf("reindexing %d number of n versions", len(nvs))
log.Ctx(ctx).Info().Msgf("reindexing %d number of node versions", len(nvs))
err = s.algolia.IndexNodeVersions(ctx, nvs...)
if err != nil {
return fmt.Errorf("failed to reindex all n versions: %w", err)
return fmt.Errorf("failed to reindex all node versions: %w", err)
}

return nil
}

var reindexLock = sync.Mutex{}

func (s *RegistryService) ReindexAllNodesBackground(ctx context.Context, client *ent.Client) (err error) {
if !reindexLock.TryLock() {
return fmt.Errorf("another reindex is in progress")
}
defer reindexLock.Unlock()

go func() {
err = s.ReindexAllNodes(ctx, client)
if err != nil {
log.Ctx(ctx).Err(err).Msg("failed to reindex all nodes in background")
}
log.Ctx(ctx).Info().Msg("reindexing nodes in background succesful")
}()

return nil
}

Expand Down

0 comments on commit b2291b0

Please sign in to comment.