-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
run multiple queries per table at once with boltdb-shipper
- Loading branch information
1 parent
89b8ae4
commit f8fea78
Showing
10 changed files
with
258 additions
and
34 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
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
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,50 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cortexproject/cortex/pkg/chunk" | ||
chunk_util "github.com/cortexproject/cortex/pkg/chunk/util" | ||
"github.com/cortexproject/cortex/pkg/util" | ||
) | ||
|
||
const maxQueriesPerGoroutine = 100 | ||
|
||
type TableQuerier interface { | ||
MultiQueries(ctx context.Context, queries []chunk.IndexQuery, callback chunk_util.Callback) error | ||
} | ||
|
||
// QueriesByTable groups and returns queries by tables. | ||
func QueriesByTable(queries []chunk.IndexQuery) map[string][]chunk.IndexQuery { | ||
queriesByTable := make(map[string][]chunk.IndexQuery) | ||
for _, query := range queries { | ||
if _, ok := queriesByTable[query.TableName]; !ok { | ||
queriesByTable[query.TableName] = []chunk.IndexQuery{} | ||
} | ||
|
||
queriesByTable[query.TableName] = append(queriesByTable[query.TableName], query) | ||
} | ||
|
||
return queriesByTable | ||
} | ||
|
||
func DoParallelQueries(ctx context.Context, tableQuerier TableQuerier, queries []chunk.IndexQuery, callback chunk_util.Callback) error { | ||
errs := make(chan error) | ||
|
||
for i := 0; i < len(queries); i += maxQueriesPerGoroutine { | ||
q := queries[i:util.Min(i+maxQueriesPerGoroutine, len(queries))] | ||
go func(queries []chunk.IndexQuery) { | ||
errs <- tableQuerier.MultiQueries(ctx, queries, callback) | ||
}(q) | ||
} | ||
|
||
var lastErr error | ||
for i := 0; i < len(queries); i += maxQueriesPerGoroutine { | ||
err := <-errs | ||
if err != nil { | ||
lastErr = err | ||
} | ||
} | ||
|
||
return lastErr | ||
} |
Oops, something went wrong.