-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: router lifecycle to handle router error
- Loading branch information
Showing
5 changed files
with
286 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package router | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/rudderlabs/rudder-go-kit/config" | ||
"github.com/rudderlabs/rudder-go-kit/logger" | ||
"github.com/rudderlabs/rudder-go-kit/stats" | ||
"github.com/rudderlabs/rudder-server/services/controlplane" | ||
"github.com/rudderlabs/rudder-server/services/notifier" | ||
"github.com/rudderlabs/rudder-server/utils/types" | ||
"github.com/rudderlabs/rudder-server/warehouse/bcm" | ||
"github.com/rudderlabs/rudder-server/warehouse/encoding" | ||
"github.com/rudderlabs/rudder-server/warehouse/integrations/middleware/sqlquerywrapper" | ||
"github.com/rudderlabs/rudder-server/warehouse/multitenant" | ||
) | ||
|
||
type Factory struct { | ||
reporting types.Reporting | ||
conf *config.Config | ||
logger logger.Logger | ||
statsFactory stats.Stats | ||
db *sqlquerywrapper.DB | ||
notifier *notifier.Notifier | ||
tenantManager *multitenant.Manager | ||
controlPlaneClient *controlplane.Client | ||
bcManager *bcm.BackendConfigManager | ||
encodingFactory *encoding.Factory | ||
triggerStore *sync.Map | ||
createUploadAlways createUploadAlwaysLoader | ||
} | ||
|
||
func (f *Factory) New(destType string) *Router { | ||
return New( | ||
f.reporting, | ||
destType, | ||
f.conf, | ||
f.logger.Child("router"), | ||
f.statsFactory, | ||
f.db, | ||
f.notifier, | ||
f.tenantManager, | ||
f.controlPlaneClient, | ||
f.bcManager, | ||
f.encodingFactory, | ||
f.triggerStore, | ||
f.createUploadAlways, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package router | ||
|
||
import ( | ||
"context" | ||
"slices" | ||
|
||
"golang.org/x/sync/errgroup" | ||
|
||
backendconfig "github.com/rudderlabs/rudder-server/backend-config" | ||
warehouseutils "github.com/rudderlabs/rudder-server/warehouse/utils" | ||
) | ||
|
||
type configWatcher interface { | ||
WatchConfig(ctx context.Context) <-chan map[string]backendconfig.ConfigT | ||
} | ||
|
||
type LifecycleManager struct { | ||
watcher configWatcher | ||
handler func(ctx context.Context, destType string) error | ||
|
||
destTypes map[string]struct{} | ||
} | ||
|
||
func NewLifecycleManager( | ||
watcher configWatcher, | ||
onDestType func(ctx context.Context, destType string) error, | ||
) *LifecycleManager { | ||
return &LifecycleManager{ | ||
watcher: watcher, | ||
handler: onDestType, | ||
destTypes: make(map[string]struct{}), | ||
} | ||
} | ||
|
||
func (lm *LifecycleManager) Run(ctx context.Context) error { | ||
g, ctx := errgroup.WithContext(ctx) | ||
|
||
ch := lm.watcher.WatchConfig(ctx) | ||
g.Go(func() error { | ||
for configData := range ch { | ||
destTypes := lm.newDestTypes(configData) | ||
for _, d := range destTypes { | ||
g.Go(func() error { | ||
return lm.handler(ctx, d) | ||
}) | ||
} | ||
} | ||
return nil | ||
}) | ||
|
||
return g.Wait() | ||
} | ||
|
||
func (lm *LifecycleManager) newDestTypes( | ||
configMap map[string]backendconfig.ConfigT, | ||
) []string { | ||
var newDestTypes []string | ||
|
||
for _, wConfig := range configMap { | ||
for _, source := range wConfig.Sources { | ||
for _, destination := range source.Destinations { | ||
|
||
destType := destination.DestinationDefinition.Name | ||
|
||
if !slices.Contains(warehouseutils.WarehouseDestinations, destType) { | ||
continue | ||
} | ||
|
||
_, ok := lm.destTypes[destType] | ||
if ok { | ||
continue | ||
} | ||
|
||
lm.destTypes[destType] = struct{}{} | ||
|
||
newDestTypes = append(newDestTypes, destType) | ||
} | ||
} | ||
} | ||
|
||
return newDestTypes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package router | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
|
||
backendconfig "github.com/rudderlabs/rudder-server/backend-config" | ||
"github.com/rudderlabs/rudder-server/utils/pubsub" | ||
whutils "github.com/rudderlabs/rudder-server/warehouse/utils" | ||
) | ||
|
||
func TestLifecycle(t *testing.T) { | ||
t.Run("normal lifecycle", func(t *testing.T) { | ||
ps := &configPubSub{} | ||
runningDestTypes := []string{} | ||
|
||
wg := sync.WaitGroup{} | ||
|
||
lm := NewLifecycleManager(ps, func(ctx context.Context, destType string) error { | ||
runningDestTypes = append(runningDestTypes, destType) | ||
wg.Done() | ||
<-ctx.Done() | ||
return nil | ||
}) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
done := make(chan error, 1) | ||
go func() { | ||
defer close(done) | ||
done <- lm.Run(ctx) | ||
}() | ||
|
||
wg.Add(1) | ||
ps.PublishConfig(configGen(t, whutils.POSTGRES, whutils.POSTGRES, "CLOUD_DESTINATION")) | ||
wg.Wait() | ||
require.Equal(t, []string{whutils.POSTGRES}, runningDestTypes) | ||
|
||
wg.Add(1) | ||
ps.PublishConfig(configGen(t, whutils.POSTGRES, whutils.SNOWFLAKE)) | ||
wg.Wait() | ||
require.Equal(t, []string{whutils.POSTGRES, whutils.SNOWFLAKE}, runningDestTypes) | ||
|
||
cancel() | ||
err := <-done | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("error in handler", func(t *testing.T) { | ||
ps := &configPubSub{} | ||
wg := sync.WaitGroup{} | ||
triggerErr := make(chan error) | ||
|
||
lm := NewLifecycleManager(ps, func(ctx context.Context, destType string) error { | ||
wg.Done() | ||
|
||
return <-triggerErr | ||
}) | ||
|
||
done := make(chan error, 1) | ||
go func() { | ||
defer close(done) | ||
done <- lm.Run(context.Background()) | ||
}() | ||
|
||
t.Log("setup a handler") | ||
wg.Add(1) | ||
ps.PublishConfig(configGen(t, whutils.POSTGRES)) | ||
wg.Wait() | ||
|
||
triggerErr <- fmt.Errorf("some error") | ||
|
||
require.Equal(t, fmt.Errorf("some error"), <-done) | ||
}) | ||
} | ||
|
||
type configPubSub struct { | ||
ps pubsub.PublishSubscriber | ||
} | ||
|
||
func (cp *configPubSub) WatchConfig(ctx context.Context) <-chan map[string]backendconfig.ConfigT { | ||
chIn := cp.ps.Subscribe(ctx, "test_config") | ||
chOut := make(chan map[string]backendconfig.ConfigT) | ||
go func() { | ||
for data := range chIn { | ||
input := data.Data.(map[string]backendconfig.ConfigT) | ||
chOut <- input | ||
} | ||
close(chOut) | ||
}() | ||
|
||
return chOut | ||
} | ||
|
||
func (cp *configPubSub) PublishConfig(config map[string]backendconfig.ConfigT) { | ||
cp.ps.Publish("test_config", config) | ||
} | ||
|
||
func configGen(t testing.TB, destType ...string) map[string]backendconfig.ConfigT { | ||
t.Helper() | ||
|
||
dsts := []backendconfig.DestinationT{} | ||
for _, dt := range destType { | ||
dsts = append(dsts, backendconfig.DestinationT{ | ||
ID: uuid.NewString(), | ||
Enabled: true, | ||
DestinationDefinition: backendconfig.DestinationDefinitionT{ | ||
Name: dt, | ||
}, | ||
}) | ||
} | ||
|
||
wID := uuid.NewString() | ||
return map[string]backendconfig.ConfigT{ | ||
wID: { | ||
WorkspaceID: wID, | ||
Sources: []backendconfig.SourceT{ | ||
{ | ||
ID: uuid.NewString(), | ||
Enabled: true, | ||
Destinations: dsts, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |