Skip to content
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
41 changes: 32 additions & 9 deletions integration_test/warehouse/warehouse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,17 @@ func TestUploads(t *testing.T) {
t.Run("tracks loading", func(t *testing.T) {
testCases := []struct {
batchStagingFiles bool
maxSizeInMB string
}{
{batchStagingFiles: false},
{batchStagingFiles: true},
{batchStagingFiles: true, maxSizeInMB: "100"},
{batchStagingFiles: true, maxSizeInMB: "0.00005"}, // Very low maxSizeInMB to ensure that staging files are not batched
}
for _, tc := range testCases {
if tc.batchStagingFiles {
t.Setenv(config.ConfigKeyToEnv(config.DefaultEnvPrefix, "Warehouse.enableV2NotifierJob"), "true")
t.Setenv(config.ConfigKeyToEnv(config.DefaultEnvPrefix, "Warehouse.loadFiles.queryWithUploadID.enable"), "true")
t.Setenv(config.ConfigKeyToEnv(config.DefaultEnvPrefix, "Warehouse.loadFiles.maxSizeInMB"), tc.maxSizeInMB)
}
db, minioResource, whClient := setupServer(t, false, nil, nil)

Expand All @@ -107,12 +110,10 @@ func TestUploads(t *testing.T) {
uuid.New().String(),
)
}), "\n")

require.NoError(t, whClient.Process(ctx, whclient.StagingFile{
stagingFile := whclient.StagingFile{
WorkspaceID: workspaceID,
SourceID: sourceID,
DestinationID: destinationID,
Location: prepareStagingFile(t, ctx, minioResource, eventsPayload).ObjectName,
TotalEvents: events,
FirstEventAt: time.Now().Format(misc.RFC3339Milli),
LastEventAt: time.Now().Add(time.Minute * 30).Format(misc.RFC3339Milli),
Expand All @@ -128,13 +129,25 @@ func TestUploads(t *testing.T) {
"received_at": "datetime",
},
},
}))
requireStagingFileEventsCount(t, ctx, db, events, []lo.Tuple2[string, any]{
}
stagingFile.Location = prepareStagingFileWithFileName(t, ctx, minioResource, eventsPayload, "staging1.json").ObjectName
require.NoError(t, whClient.Process(ctx, stagingFile))

// Create a new eventsPayload with different IDs for the second staging file
eventsPayload2 := strings.Join(lo.RepeatBy(events, func(int) string {
return fmt.Sprintf(`{"data":{"id":%q,"user_id":%q,"received_at":"2023-05-12T04:36:50.199Z"},"metadata":{"columns":{"id":"string","user_id":"string","received_at":"datetime"}, "table": "tracks"}}`,
uuid.New().String(),
uuid.New().String(),
)
}), "\n")
stagingFile.Location = prepareStagingFileWithFileName(t, ctx, minioResource, eventsPayload2, "staging2.json").ObjectName
require.NoError(t, whClient.Process(ctx, stagingFile))
requireStagingFileEventsCount(t, ctx, db, 2*events, []lo.Tuple2[string, any]{
{A: "source_id", B: sourceID},
{A: "destination_id", B: destinationID},
{A: "status", B: succeeded},
}...)
requireTableUploadEventsCount(t, ctx, db, events, []lo.Tuple2[string, any]{
requireTableUploadEventsCount(t, ctx, db, 2*events, []lo.Tuple2[string, any]{
{A: "status", B: exportedData},
{A: "wh_uploads.source_id", B: sourceID},
{A: "wh_uploads.destination_id", B: destinationID},
Expand All @@ -146,7 +159,7 @@ func TestUploads(t *testing.T) {
{A: "namespace", B: namespace},
{A: "status", B: exportedData},
}...)
requireDownstreamEventsCount(t, ctx, db, fmt.Sprintf("%s.%s", namespace, "tracks"), events)
requireDownstreamEventsCount(t, ctx, db, fmt.Sprintf("%s.%s", namespace, "tracks"), 2*events)
}
})
t.Run("user and identifies loading", func(t *testing.T) {
Expand Down Expand Up @@ -2204,8 +2217,18 @@ func prepareStagingFile(
payload string,
) filemanager.UploadedFile {
t.Helper()
return prepareStagingFileWithFileName(t, ctx, minioResource, payload, "staging.json")
}

filePath := path.Join(t.TempDir(), "staging.json")
func prepareStagingFileWithFileName(
t testing.TB,
ctx context.Context,
minioResource *minio.Resource,
payload string,
fileName string,
) filemanager.UploadedFile {
t.Helper()
filePath := path.Join(t.TempDir(), fileName)

gz, err := misc.CreateGZ(filePath)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion warehouse/internal/loadfiles/loadfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ func (lf *LoadFileGenerator) createUploadV2Jobs(ctx context.Context, job *model.
return fmt.Errorf("populating destination revision ID: %w", err)
}
g, gCtx := errgroup.WithContext(ctx)
stagingFileGroups := lf.GroupStagingFiles(stagingFiles, lf.Conf.GetInt("Warehouse.loadFiles.maxSizeInMB", 128))
stagingFileGroups := lf.GroupStagingFiles(stagingFiles, lf.Conf.GetInt("Warehouse.loadFiles.maxSizeInMB", 512))
for i, fileGroups := range lo.Chunk(stagingFileGroups, publishBatchSize) {
for j, group := range fileGroups {
lf.Logger.Infon("Processing chunk and group",
Expand Down
35 changes: 2 additions & 33 deletions warehouse/internal/repo/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,27 +119,12 @@ func (lf *LoadFiles) Get(ctx context.Context, uploadID int64, stagingFileIDs []i

func (lf *LoadFiles) getByUploadId(ctx context.Context, uploadID int64) ([]model.LoadFile, error) {
sqlStatement := `
WITH row_numbered_load_files AS (
SELECT
` + loadTableColumns + `,
row_number() OVER (
PARTITION BY
upload_id,
table_name
ORDER BY
id DESC
) AS row_number
` + loadTableColumns + `
FROM
` + loadTableName + `
WHERE
upload_id = $1
)
SELECT
` + loadTableColumns + `
FROM
row_numbered_load_files
WHERE
row_number = 1
ORDER BY
id ASC;
`
Expand Down Expand Up @@ -307,28 +292,12 @@ func (lf *LoadFiles) totalExportedEventsByUploadID(
) (int64, error) {
var count sql.NullInt64
sqlStatement := `
WITH row_numbered_load_files AS (
SELECT
total_events,
table_name,
row_number() OVER (
PARTITION BY
upload_id,
table_name
ORDER BY
id DESC
) AS row_number
COALESCE(sum(total_events), 0) AS total_events
FROM
` + loadTableName + `
WHERE
upload_id = $1
)
SELECT
COALESCE(sum(total_events), 0) AS total_events
FROM
row_numbered_load_files
WHERE
row_number = 1
AND
table_name != ALL($2);`

Expand Down
43 changes: 8 additions & 35 deletions warehouse/internal/repo/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func Test_LoadFiles_WithUploadID(t *testing.T) {

for i := 0; i < 10; i++ {
loadFile := model.LoadFile{
TableName: "table_name__" + strconv.Itoa(i),
TableName: "table_name__" + strconv.Itoa(i%3),
Location: "s3://bucket/path/to/file",
TotalRows: 10,
ContentLength: 1000,
Expand Down Expand Up @@ -208,37 +208,6 @@ func Test_LoadFiles_WithUploadID(t *testing.T) {
require.Len(t, loadFiles, 0)
require.NoError(t, err)
})

t.Run("get latest for stagingID", func(t *testing.T) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a valid test scenario anymore

var lastLoadFile model.LoadFile
var loadFiles []model.LoadFile
for i := 0; i < 10; i++ {
loadFile := model.LoadFile{
TableName: "table_name",
Location: fmt.Sprintf("s3://bucket/path/to/file/%d", i),
TotalRows: 10,
ContentLength: 1000,
DestinationRevisionID: "revision_id",
UploadID: &uploadID2,
UseRudderStorage: true,
SourceID: "source_id",
DestinationID: "destination_id",
DestinationType: "RS",
}
loadFiles = append(loadFiles, loadFile)
lastLoadFile = loadFile
}
err := r.Insert(ctx, loadFiles)
require.NoError(t, err)

gotLoadFiles, err := r.Get(ctx, uploadID2, []int64{})
require.NoError(t, err)

require.Len(t, gotLoadFiles, 1)
lastLoadFile.ID = gotLoadFiles[0].ID
lastLoadFile.CreatedAt = gotLoadFiles[0].CreatedAt
require.Equal(t, lastLoadFile, gotLoadFiles[0])
})
}

func TestLoadFiles_GetByID(t *testing.T) {
Expand Down Expand Up @@ -408,15 +377,17 @@ func TestLoadFiles_TotalExportedEvents_WithUploadID(t *testing.T) {
totalEvents := 0
uploadID := createUpload(t, ctx, db)
var skipTables []string
skipTablesTotalEvents := 0

for i := 0; i < stagingFilesCount; i++ {
for j := 0; j < loadFilesCount; j++ {
for k := 0; k < retriesCount; k++ {
tableName := "table_name_" + strconv.Itoa(i+1) + "_" + strconv.Itoa(j+1)
rows := (i + 1) + (j + 1) + (k + 1)
loadFiles = append(loadFiles, model.LoadFile{
TableName: tableName,
Location: "s3://bucket/path/to/file",
TotalRows: (i + 1) + (j + 1) + (k + 1),
TotalRows: rows,
ContentLength: 1000,
UploadID: &uploadID,
DestinationRevisionID: "revision_id",
Expand All @@ -428,9 +399,11 @@ func TestLoadFiles_TotalExportedEvents_WithUploadID(t *testing.T) {
if i != 0 || j != 0 {
// Skip every table except the first one
skipTables = append(skipTables, tableName)
} else {
skipTablesTotalEvents += rows
}
totalEvents += rows
}
totalEvents += (i + 1) + (j + 1) + retriesCount
}
}

Expand All @@ -450,7 +423,7 @@ func TestLoadFiles_TotalExportedEvents_WithUploadID(t *testing.T) {
t.Run("with skip tables", func(t *testing.T) {
exportedEvents, err := r.TotalExportedEvents(ctx, uploadID, []int64{}, skipTables)
require.NoError(t, err)
require.Equal(t, int64(retriesCount+2), exportedEvents)
require.Equal(t, int64(skipTablesTotalEvents), exportedEvents)
})
t.Run("context cancelled", func(t *testing.T) {
ctx, cancel := context.WithCancel(ctx)
Expand Down
20 changes: 3 additions & 17 deletions warehouse/internal/repo/table_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,27 +194,13 @@ func (tu *TableUploads) PopulateTotalEventsWithTx(ctx context.Context, tx *sqlmi
var queryArgs []any
if tu.queryLoadFilesWithUploadID.Load() {
subQuery = `
WITH row_numbered_load_files as (
SELECT
total_events,
row_number() OVER (
PARTITION BY upload_id,
table_name
ORDER BY
id DESC
) AS row_number
FROM
` + loadTableName + `
WHERE
upload_id = $1
AND table_name = $2
)
SELECT
sum(total_events) as total
FROM
row_numbered_load_files
` + loadTableName + `
WHERE
row_number = 1
upload_id = $1
AND table_name = $2
`
queryArgs = []any{
uploadId,
Expand Down
6 changes: 5 additions & 1 deletion warehouse/internal/repo/table_upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ func TestTableUploadRepo(t *testing.T) {
TableName: tables2[i],
TotalRows: i + 1,
UploadID: &uploadId,
}, model.LoadFile{
TableName: tables2[i],
TotalRows: i + 1,
UploadID: &uploadId,
})
}
insertErr := l.Insert(ctx, loadFiles)
Expand All @@ -395,7 +399,7 @@ func TestTableUploadRepo(t *testing.T) {
require.NoError(t, txErr)
for _, loadFile := range loadFiles {
tableName := loadFile.TableName
expectedTotal := int64(loadFile.TotalRows)
expectedTotal := 2 * int64(loadFile.TotalRows) // Since there are 2 load files for each table
tableUpload, getErr := r.GetByUploadIDAndTableName(ctx, uploadId, tableName)
require.NoError(t, getErr)
require.Equal(t, uploadId, tableUpload.UploadID)
Expand Down
19 changes: 3 additions & 16 deletions warehouse/router/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,27 +859,14 @@ func (job *UploadJob) GetLoadFilesMetadata(ctx context.Context, options whutils.
func (job *UploadJob) getLoadFilesMetadataQuery(tableFilterSQL, limitSQL string) string {
if job.config.queryLoadFilesWithUploadID.Load() {
return fmt.Sprintf(`
WITH row_numbered_load_files as (
SELECT
location,
metadata,
row_number() OVER (
PARTITION BY upload_id,
table_name
ORDER BY
id DESC
) AS row_number
FROM
%[1]s
WHERE upload_id = %[2]d %[3]s
)
SELECT
location,
metadata
FROM
row_numbered_load_files
%[1]s
WHERE
row_number = 1
upload_id = %[2]d
%[3]s
%[4]s;
`,
whutils.WarehouseLoadFilesTable,
Expand Down
34 changes: 33 additions & 1 deletion warehouse/router/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,13 +748,34 @@ func TestUploadJob_GetLoadFilesMetadata(t *testing.T) {
testCases := []struct {
name string
queryWithUploadID bool
tableName string
limit int64
expectedLoadFiles int
}{
{
name: "query with upload ID",
queryWithUploadID: true,
expectedLoadFiles: 4,
},
{
name: "query with upload ID and table name",
queryWithUploadID: true,
tableName: "test_table2",
expectedLoadFiles: 3,
},
{
name: "query with upload ID, table name and limit",
queryWithUploadID: true,
tableName: "test_table2",
limit: 2,
expectedLoadFiles: 2,
},
{
name: "query with upload ID and limit",
queryWithUploadID: true,
limit: 1,
expectedLoadFiles: 1,
},
{
name: "query with staging file IDs",
queryWithUploadID: false,
Expand Down Expand Up @@ -791,10 +812,21 @@ func TestUploadJob_GetLoadFilesMetadata(t *testing.T) {
UploadID: &job.upload.ID,
TableName: "test_table2",
},
{
UploadID: &job.upload.ID,
TableName: "test_table2",
},
{
UploadID: &job.upload.ID,
TableName: "test_table2",
},
}
err := repo.NewLoadFiles(db, conf).Insert(ctx, loadFiles)
require.NoError(t, err)
result, err := job.GetLoadFilesMetadata(ctx, warehouseutils.GetLoadFilesOptions{})
result, err := job.GetLoadFilesMetadata(ctx, warehouseutils.GetLoadFilesOptions{
Table: tc.tableName,
Limit: tc.limit,
})
require.NoError(t, err)
require.Equal(t, tc.expectedLoadFiles, len(result))
})
Expand Down