mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
**What changed?** gogo/protobuf has been replaced with Google's official go compiler. **Why?** gogo/protobuf has been deprecated for some time and the community is moving on, building new tools (like vtproto) atop google's v2 compiler. **How did you test it?** `make test` **Potential risks** 1. The change from embedded gogo-generated-structs to google-generated-pointers-to-structs created a risk of nil pointer exceptions. I've fixed all the ones our tests found but it's possible there are more lurking in the new code. 2. This change may cause our performance to decrease. Certainly encoding/deconding of proto objects will become slower, but the overuse of pointers by the google compiler may negatively affect our overall performance. We'll need to keep an eye on the GC stats 3. This breaks the HTTP API. We will not support [shortand payload encoding](https://github.com/temporalio/proposals/blob/master/api/http-api.md#payload-formatting) in this first pass; that will come once this initial work is in testing. **Breaking changes for developers** - `*time.Time` in proto structs will now be [timestamppb.Timestamp](https://pkg.go.dev/google.golang.org/[email protected]/types/known/timestamppb#section-documentation) - `*time.Duration` will now be [durationpb.Duration](https://pkg.go.dev/google.golang.org/protobuf/types/known/durationpb) - V2-generated structs embed locks, so you cannot dereference them. `go vet` will scream at you about this. If you need a copy, use `proto.Clone`. - If the performance of this sucks then I will either update our code generator to add shallow-clone methods or hand-roll the ones we need - Proto enums will, when formatted to JSON, now be in `SCREAMING_SNAKE_CASE` rather than `PascalCase`. We decided (in discussion with the SDK team) that now was as good a time as any to rip the bandage off. - Proto objects, or objects embedding protos, cannot be compared using `reflect.DeepEqual` or _anything_ that uses it. This includes `testify` and `mock` equality testers! - You will need to use the `common/testing/protorequire`, `common/testing/protoassert`, or `common/testing/protomock` packages instead. I've implemented proto-compatible matchers and assertions there for all cases I've encountered - If you need `reflect.DeepEqual` for any reason you can use `go.temporal.io/api/temporalproto.DeepEqual` instead Note that history loading will not be impacted by the JSON changes: I rewrote history loading to dynamically fix incoming history JSON data (like all our other sdks); you can find this code in [my fork of our go API](https://github.com/tdeebswihart/temporal-api-go/blob/master/internal/temporalhistoryv1/load.go) alongside its tests. **🚨Sharp Edges Introduced🚨** Beware `*timestamppb.Timestamp.AsTime()`. If you need to extract a time value from a proto time (timestamppb) **always** make sure to check whether it's nil first. When the proto object is `nil` `AsTime()` will return a non-zero time at the proto epoch: UTC midnight on January 1, 1970. I've made this mistake multiple times during this transition and each time it's been a pain to debug **Is hotfix candidate?** No.
714 lines
21 KiB
Go
714 lines
21 KiB
Go
// The MIT License
|
|
//
|
|
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
|
|
//
|
|
// Copyright (c) 2020 Uber Technologies, Inc.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
|
|
package cassandra
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
enumspb "go.temporal.io/api/enums/v1"
|
|
"go.temporal.io/api/serviceerror"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
"go.temporal.io/server/common/convert"
|
|
"go.temporal.io/server/common/log"
|
|
p "go.temporal.io/server/common/persistence"
|
|
"go.temporal.io/server/common/persistence/nosql/nosqlplugin/cassandra/gocql"
|
|
"go.temporal.io/server/common/primitives/timestamp"
|
|
)
|
|
|
|
const (
|
|
templateCreateTaskQuery = `INSERT INTO tasks (` +
|
|
`namespace_id, task_queue_name, task_queue_type, type, task_id, task, task_encoding) ` +
|
|
`VALUES(?, ?, ?, ?, ?, ?, ?)`
|
|
|
|
templateCreateTaskWithTTLQuery = `INSERT INTO tasks (` +
|
|
`namespace_id, task_queue_name, task_queue_type, type, task_id, task, task_encoding) ` +
|
|
`VALUES(?, ?, ?, ?, ?, ?, ?) USING TTL ?`
|
|
|
|
templateGetTasksQuery = `SELECT task_id, task, task_encoding ` +
|
|
`FROM tasks ` +
|
|
`WHERE namespace_id = ? ` +
|
|
`and task_queue_name = ? ` +
|
|
`and task_queue_type = ? ` +
|
|
`and type = ? ` +
|
|
`and task_id >= ? ` +
|
|
`and task_id < ?`
|
|
|
|
templateCompleteTaskQuery = `DELETE FROM tasks ` +
|
|
`WHERE namespace_id = ? ` +
|
|
`and task_queue_name = ? ` +
|
|
`and task_queue_type = ? ` +
|
|
`and type = ? ` +
|
|
`and task_id = ?`
|
|
|
|
templateCompleteTasksLessThanQuery = `DELETE FROM tasks ` +
|
|
`WHERE namespace_id = ? ` +
|
|
`AND task_queue_name = ? ` +
|
|
`AND task_queue_type = ? ` +
|
|
`AND type = ? ` +
|
|
`AND task_id < ? `
|
|
|
|
templateGetTaskQueueQuery = `SELECT ` +
|
|
`range_id, ` +
|
|
`task_queue, ` +
|
|
`task_queue_encoding ` +
|
|
`FROM tasks ` +
|
|
`WHERE namespace_id = ? ` +
|
|
`and task_queue_name = ? ` +
|
|
`and task_queue_type = ? ` +
|
|
`and type = ? ` +
|
|
`and task_id = ?`
|
|
|
|
templateInsertTaskQueueQuery = `INSERT INTO tasks (` +
|
|
`namespace_id, ` +
|
|
`task_queue_name, ` +
|
|
`task_queue_type, ` +
|
|
`type, ` +
|
|
`task_id, ` +
|
|
`range_id, ` +
|
|
`task_queue, ` +
|
|
`task_queue_encoding ` +
|
|
`) VALUES (?, ?, ?, ?, ?, ?, ?, ?) IF NOT EXISTS`
|
|
|
|
templateUpdateTaskQueueQuery = `UPDATE tasks SET ` +
|
|
`range_id = ?, ` +
|
|
`task_queue = ?, ` +
|
|
`task_queue_encoding = ? ` +
|
|
`WHERE namespace_id = ? ` +
|
|
`and task_queue_name = ? ` +
|
|
`and task_queue_type = ? ` +
|
|
`and type = ? ` +
|
|
`and task_id = ? ` +
|
|
`IF range_id = ?`
|
|
|
|
templateUpdateTaskQueueQueryWithTTLPart1 = `INSERT INTO tasks (` +
|
|
`namespace_id, ` +
|
|
`task_queue_name, ` +
|
|
`task_queue_type, ` +
|
|
`type, ` +
|
|
`task_id ` +
|
|
`) VALUES (?, ?, ?, ?, ?) USING TTL ?`
|
|
|
|
templateUpdateTaskQueueQueryWithTTLPart2 = `UPDATE tasks USING TTL ? SET ` +
|
|
`range_id = ?, ` +
|
|
`task_queue = ?, ` +
|
|
`task_queue_encoding = ? ` +
|
|
`WHERE namespace_id = ? ` +
|
|
`and task_queue_name = ? ` +
|
|
`and task_queue_type = ? ` +
|
|
`and type = ? ` +
|
|
`and task_id = ? ` +
|
|
`IF range_id = ?`
|
|
|
|
templateDeleteTaskQueueQuery = `DELETE FROM tasks ` +
|
|
`WHERE namespace_id = ? ` +
|
|
`AND task_queue_name = ? ` +
|
|
`AND task_queue_type = ? ` +
|
|
`AND type = ? ` +
|
|
`AND task_id = ? ` +
|
|
`IF range_id = ?`
|
|
|
|
templateGetTaskQueueUserDataQuery = `SELECT data, data_encoding, version
|
|
FROM task_queue_user_data
|
|
WHERE namespace_id = ? AND build_id = ''
|
|
AND task_queue_name = ?`
|
|
|
|
templateUpdateTaskQueueUserDataQuery = `UPDATE task_queue_user_data SET
|
|
data = ?,
|
|
data_encoding = ?,
|
|
version = ?
|
|
WHERE namespace_id = ?
|
|
AND build_id = ''
|
|
AND task_queue_name = ?
|
|
IF version = ?`
|
|
|
|
templateInsertTaskQueueUserDataQuery = `INSERT INTO task_queue_user_data
|
|
(namespace_id, build_id, task_queue_name, data, data_encoding, version) VALUES
|
|
(? , '' , ? , ? , ? , 1 ) IF NOT EXISTS`
|
|
|
|
templateInsertBuildIdTaskQueueMappingQuery = `INSERT INTO task_queue_user_data
|
|
(namespace_id, build_id, task_queue_name) VALUES
|
|
(? , ? , ?)`
|
|
templateDeleteBuildIdTaskQueueMappingQuery = `DELETE FROM task_queue_user_data
|
|
WHERE namespace_id = ? AND build_id = ? AND task_queue_name = ?`
|
|
templateListTaskQueueUserDataQuery = `SELECT task_queue_name, data, data_encoding, version FROM task_queue_user_data WHERE namespace_id = ? AND build_id = ''`
|
|
templateListTaskQueueNamesByBuildIdQuery = `SELECT task_queue_name FROM task_queue_user_data WHERE namespace_id = ? AND build_id = ?`
|
|
templateCountTaskQueueByBuildIdQuery = `SELECT COUNT(*) FROM task_queue_user_data WHERE namespace_id = ? AND build_id = ?`
|
|
|
|
// Not much of a need to make this configurable, we're just reading some strings
|
|
listTaskQueueNamesByBuildIdPageSize = 100
|
|
)
|
|
|
|
type (
|
|
MatchingTaskStore struct {
|
|
Session gocql.Session
|
|
Logger log.Logger
|
|
}
|
|
)
|
|
|
|
func NewMatchingTaskStore(
|
|
session gocql.Session,
|
|
logger log.Logger,
|
|
) *MatchingTaskStore {
|
|
return &MatchingTaskStore{
|
|
Session: session,
|
|
Logger: logger,
|
|
}
|
|
}
|
|
|
|
func (d *MatchingTaskStore) CreateTaskQueue(
|
|
ctx context.Context,
|
|
request *p.InternalCreateTaskQueueRequest,
|
|
) error {
|
|
query := d.Session.Query(templateInsertTaskQueueQuery,
|
|
request.NamespaceID,
|
|
request.TaskQueue,
|
|
request.TaskType,
|
|
rowTypeTaskQueue,
|
|
taskQueueTaskID,
|
|
request.RangeID,
|
|
request.TaskQueueInfo.Data,
|
|
request.TaskQueueInfo.EncodingType.String(),
|
|
).WithContext(ctx)
|
|
|
|
previous := make(map[string]interface{})
|
|
applied, err := query.MapScanCAS(previous)
|
|
if err != nil {
|
|
return gocql.ConvertError("CreateTaskQueue", err)
|
|
}
|
|
|
|
if !applied {
|
|
previousRangeID := previous["range_id"]
|
|
return &p.ConditionFailedError{
|
|
Msg: fmt.Sprintf("CreateTaskQueue: TaskQueue:%v, TaskQueueType:%v, PreviousRangeID:%v",
|
|
request.TaskQueue, request.TaskType, previousRangeID),
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (d *MatchingTaskStore) GetTaskQueue(
|
|
ctx context.Context,
|
|
request *p.InternalGetTaskQueueRequest,
|
|
) (*p.InternalGetTaskQueueResponse, error) {
|
|
query := d.Session.Query(templateGetTaskQueueQuery,
|
|
request.NamespaceID,
|
|
request.TaskQueue,
|
|
request.TaskType,
|
|
rowTypeTaskQueue,
|
|
taskQueueTaskID,
|
|
).WithContext(ctx)
|
|
|
|
var rangeID int64
|
|
var tlBytes []byte
|
|
var tlEncoding string
|
|
if err := query.Scan(&rangeID, &tlBytes, &tlEncoding); err != nil {
|
|
return nil, gocql.ConvertError("GetTaskQueue", err)
|
|
}
|
|
|
|
return &p.InternalGetTaskQueueResponse{
|
|
RangeID: rangeID,
|
|
TaskQueueInfo: p.NewDataBlob(tlBytes, tlEncoding),
|
|
}, nil
|
|
}
|
|
|
|
// UpdateTaskQueue update task queue
|
|
func (d *MatchingTaskStore) UpdateTaskQueue(
|
|
ctx context.Context,
|
|
request *p.InternalUpdateTaskQueueRequest,
|
|
) (*p.UpdateTaskQueueResponse, error) {
|
|
var err error
|
|
var applied bool
|
|
previous := make(map[string]interface{})
|
|
if request.TaskQueueKind == enumspb.TASK_QUEUE_KIND_STICKY { // if task_queue is sticky, then update with TTL
|
|
if request.ExpiryTime == nil {
|
|
return nil, serviceerror.NewInternal("ExpiryTime cannot be nil for sticky task queue")
|
|
}
|
|
expiryTTL := convert.Int64Ceil(time.Until(timestamp.TimeValue(request.ExpiryTime)).Seconds())
|
|
if expiryTTL >= maxCassandraTTL {
|
|
expiryTTL = maxCassandraTTL
|
|
}
|
|
batch := d.Session.NewBatch(gocql.LoggedBatch).WithContext(ctx)
|
|
batch.Query(templateUpdateTaskQueueQueryWithTTLPart1,
|
|
request.NamespaceID,
|
|
request.TaskQueue,
|
|
request.TaskType,
|
|
rowTypeTaskQueue,
|
|
taskQueueTaskID,
|
|
expiryTTL,
|
|
)
|
|
batch.Query(templateUpdateTaskQueueQueryWithTTLPart2,
|
|
expiryTTL,
|
|
request.RangeID,
|
|
request.TaskQueueInfo.Data,
|
|
request.TaskQueueInfo.EncodingType.String(),
|
|
request.NamespaceID,
|
|
request.TaskQueue,
|
|
request.TaskType,
|
|
rowTypeTaskQueue,
|
|
taskQueueTaskID,
|
|
request.PrevRangeID,
|
|
)
|
|
applied, _, err = d.Session.MapExecuteBatchCAS(batch, previous)
|
|
} else {
|
|
query := d.Session.Query(templateUpdateTaskQueueQuery,
|
|
request.RangeID,
|
|
request.TaskQueueInfo.Data,
|
|
request.TaskQueueInfo.EncodingType.String(),
|
|
request.NamespaceID,
|
|
request.TaskQueue,
|
|
request.TaskType,
|
|
rowTypeTaskQueue,
|
|
taskQueueTaskID,
|
|
request.PrevRangeID,
|
|
).WithContext(ctx)
|
|
applied, err = query.MapScanCAS(previous)
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, gocql.ConvertError("UpdateTaskQueue", err)
|
|
}
|
|
|
|
if !applied {
|
|
var columns []string
|
|
for k, v := range previous {
|
|
columns = append(columns, fmt.Sprintf("%s=%v", k, v))
|
|
}
|
|
|
|
return nil, &p.ConditionFailedError{
|
|
Msg: fmt.Sprintf("Failed to update task queue. name: %v, type: %v, rangeID: %v, columns: (%v)",
|
|
request.TaskQueue, request.TaskType, request.RangeID, strings.Join(columns, ",")),
|
|
}
|
|
}
|
|
|
|
return &p.UpdateTaskQueueResponse{}, nil
|
|
}
|
|
|
|
func (d *MatchingTaskStore) ListTaskQueue(
|
|
_ context.Context,
|
|
_ *p.ListTaskQueueRequest,
|
|
) (*p.InternalListTaskQueueResponse, error) {
|
|
return nil, serviceerror.NewUnavailable("unsupported operation")
|
|
}
|
|
|
|
func (d *MatchingTaskStore) DeleteTaskQueue(
|
|
ctx context.Context,
|
|
request *p.DeleteTaskQueueRequest,
|
|
) error {
|
|
query := d.Session.Query(
|
|
templateDeleteTaskQueueQuery,
|
|
request.TaskQueue.NamespaceID,
|
|
request.TaskQueue.TaskQueueName,
|
|
request.TaskQueue.TaskQueueType,
|
|
rowTypeTaskQueue,
|
|
taskQueueTaskID,
|
|
request.RangeID,
|
|
).WithContext(ctx)
|
|
previous := make(map[string]interface{})
|
|
applied, err := query.MapScanCAS(previous)
|
|
if err != nil {
|
|
return gocql.ConvertError("DeleteTaskQueue", err)
|
|
}
|
|
if !applied {
|
|
return &p.ConditionFailedError{
|
|
Msg: fmt.Sprintf("DeleteTaskQueue operation failed: expected_range_id=%v but found %+v", request.RangeID, previous),
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CreateTasks add tasks
|
|
func (d *MatchingTaskStore) CreateTasks(
|
|
ctx context.Context,
|
|
request *p.InternalCreateTasksRequest,
|
|
) (*p.CreateTasksResponse, error) {
|
|
batch := d.Session.NewBatch(gocql.LoggedBatch).WithContext(ctx)
|
|
namespaceID := request.NamespaceID
|
|
taskQueue := request.TaskQueue
|
|
taskQueueType := request.TaskType
|
|
|
|
for _, task := range request.Tasks {
|
|
ttl := GetTaskTTL(task.ExpiryTime)
|
|
|
|
if ttl <= 0 || ttl > maxCassandraTTL {
|
|
batch.Query(templateCreateTaskQuery,
|
|
namespaceID,
|
|
taskQueue,
|
|
taskQueueType,
|
|
rowTypeTask,
|
|
task.TaskId,
|
|
task.Task.Data,
|
|
task.Task.EncodingType.String())
|
|
} else {
|
|
batch.Query(templateCreateTaskWithTTLQuery,
|
|
namespaceID,
|
|
taskQueue,
|
|
taskQueueType,
|
|
rowTypeTask,
|
|
task.TaskId,
|
|
task.Task.Data,
|
|
task.Task.EncodingType.String(),
|
|
ttl)
|
|
}
|
|
}
|
|
|
|
// The following query is used to ensure that range_id didn't change
|
|
batch.Query(templateUpdateTaskQueueQuery,
|
|
request.RangeID,
|
|
request.TaskQueueInfo.Data,
|
|
request.TaskQueueInfo.EncodingType.String(),
|
|
namespaceID,
|
|
taskQueue,
|
|
taskQueueType,
|
|
rowTypeTaskQueue,
|
|
taskQueueTaskID,
|
|
request.RangeID,
|
|
)
|
|
|
|
previous := make(map[string]interface{})
|
|
applied, _, err := d.Session.MapExecuteBatchCAS(batch, previous)
|
|
if err != nil {
|
|
return nil, gocql.ConvertError("CreateTasks", err)
|
|
}
|
|
if !applied {
|
|
rangeID := previous["range_id"]
|
|
return nil, &p.ConditionFailedError{
|
|
Msg: fmt.Sprintf("Failed to create task. TaskQueue: %v, taskQueueType: %v, rangeID: %v, db rangeID: %v",
|
|
taskQueue, taskQueueType, request.RangeID, rangeID),
|
|
}
|
|
}
|
|
|
|
return &p.CreateTasksResponse{}, nil
|
|
}
|
|
|
|
func GetTaskTTL(expireTime *timestamppb.Timestamp) int64 {
|
|
var ttl int64 = 0
|
|
if expireTime != nil && !expireTime.AsTime().IsZero() {
|
|
expiryTtl := convert.Int64Ceil(time.Until(expireTime.AsTime()).Seconds())
|
|
|
|
// 0 means no ttl, we dont want that.
|
|
// Todo: Come back and correctly ignore expired in-memory tasks before persisting
|
|
if expiryTtl < 1 {
|
|
expiryTtl = 1
|
|
}
|
|
|
|
ttl = expiryTtl
|
|
}
|
|
return ttl
|
|
}
|
|
|
|
// GetTasks get a task
|
|
func (d *MatchingTaskStore) GetTasks(
|
|
ctx context.Context,
|
|
request *p.GetTasksRequest,
|
|
) (*p.InternalGetTasksResponse, error) {
|
|
// Reading taskqueue tasks need to be quorum level consistent, otherwise we could lose tasks
|
|
query := d.Session.Query(templateGetTasksQuery,
|
|
request.NamespaceID,
|
|
request.TaskQueue,
|
|
request.TaskType,
|
|
rowTypeTask,
|
|
request.InclusiveMinTaskID,
|
|
request.ExclusiveMaxTaskID,
|
|
).WithContext(ctx)
|
|
iter := query.PageSize(request.PageSize).PageState(request.NextPageToken).Iter()
|
|
|
|
response := &p.InternalGetTasksResponse{}
|
|
task := make(map[string]interface{})
|
|
for iter.MapScan(task) {
|
|
_, ok := task["task_id"]
|
|
if !ok { // no tasks, but static column record returned
|
|
continue
|
|
}
|
|
|
|
rawTask, ok := task["task"]
|
|
if !ok {
|
|
return nil, newFieldNotFoundError("task", task)
|
|
}
|
|
taskVal, ok := rawTask.([]byte)
|
|
if !ok {
|
|
var byteSliceType []byte
|
|
return nil, newPersistedTypeMismatchError("task", byteSliceType, rawTask, task)
|
|
|
|
}
|
|
|
|
rawEncoding, ok := task["task_encoding"]
|
|
if !ok {
|
|
return nil, newFieldNotFoundError("task_encoding", task)
|
|
}
|
|
encodingVal, ok := rawEncoding.(string)
|
|
if !ok {
|
|
var byteSliceType []byte
|
|
return nil, newPersistedTypeMismatchError("task_encoding", byteSliceType, rawEncoding, task)
|
|
}
|
|
response.Tasks = append(response.Tasks, p.NewDataBlob(taskVal, encodingVal))
|
|
|
|
task = make(map[string]interface{}) // Reinitialize map as initialized fails on unmarshalling
|
|
}
|
|
if len(iter.PageState()) > 0 {
|
|
response.NextPageToken = iter.PageState()
|
|
}
|
|
|
|
if err := iter.Close(); err != nil {
|
|
return nil, serviceerror.NewUnavailable(fmt.Sprintf("GetTasks operation failed. Error: %v", err))
|
|
}
|
|
return response, nil
|
|
}
|
|
|
|
// CompleteTask delete a task
|
|
func (d *MatchingTaskStore) CompleteTask(
|
|
ctx context.Context,
|
|
request *p.CompleteTaskRequest,
|
|
) error {
|
|
tli := request.TaskQueue
|
|
query := d.Session.Query(templateCompleteTaskQuery,
|
|
tli.NamespaceID,
|
|
tli.TaskQueueName,
|
|
tli.TaskQueueType,
|
|
rowTypeTask,
|
|
request.TaskID,
|
|
).WithContext(ctx)
|
|
|
|
err := query.Exec()
|
|
if err != nil {
|
|
return gocql.ConvertError("CompleteTask", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// CompleteTasksLessThan deletes all tasks less than the given task id. This API ignores the
|
|
// Limit request parameter i.e. either all tasks leq the task_id will be deleted or an error will
|
|
// be returned to the caller
|
|
func (d *MatchingTaskStore) CompleteTasksLessThan(
|
|
ctx context.Context,
|
|
request *p.CompleteTasksLessThanRequest,
|
|
) (int, error) {
|
|
query := d.Session.Query(
|
|
templateCompleteTasksLessThanQuery,
|
|
request.NamespaceID,
|
|
request.TaskQueueName,
|
|
request.TaskType,
|
|
rowTypeTask,
|
|
request.ExclusiveMaxTaskID,
|
|
).WithContext(ctx)
|
|
err := query.Exec()
|
|
if err != nil {
|
|
return 0, gocql.ConvertError("CompleteTasksLessThan", err)
|
|
}
|
|
return p.UnknownNumRowsAffected, nil
|
|
}
|
|
|
|
func (d *MatchingTaskStore) GetTaskQueueUserData(
|
|
ctx context.Context,
|
|
request *p.GetTaskQueueUserDataRequest,
|
|
) (*p.InternalGetTaskQueueUserDataResponse, error) {
|
|
query := d.Session.Query(templateGetTaskQueueUserDataQuery,
|
|
request.NamespaceID,
|
|
request.TaskQueue,
|
|
).WithContext(ctx)
|
|
var version int64
|
|
var userDataBytes []byte
|
|
var encoding string
|
|
if err := query.Scan(&userDataBytes, &encoding, &version); err != nil {
|
|
return nil, gocql.ConvertError("GetTaskQueueData", err)
|
|
}
|
|
|
|
return &p.InternalGetTaskQueueUserDataResponse{
|
|
Version: version,
|
|
UserData: p.NewDataBlob(userDataBytes, encoding),
|
|
}, nil
|
|
}
|
|
|
|
func (d *MatchingTaskStore) UpdateTaskQueueUserData(
|
|
ctx context.Context,
|
|
request *p.InternalUpdateTaskQueueUserDataRequest,
|
|
) error {
|
|
batch := d.Session.NewBatch(gocql.UnloggedBatch).WithContext(ctx)
|
|
|
|
if request.Version == 0 {
|
|
batch.Query(templateInsertTaskQueueUserDataQuery,
|
|
request.NamespaceID,
|
|
request.TaskQueue,
|
|
request.UserData.Data,
|
|
request.UserData.EncodingType.String(),
|
|
)
|
|
} else {
|
|
batch.Query(templateUpdateTaskQueueUserDataQuery,
|
|
request.UserData.Data,
|
|
request.UserData.EncodingType.String(),
|
|
request.Version+1,
|
|
request.NamespaceID,
|
|
request.TaskQueue,
|
|
request.Version,
|
|
)
|
|
}
|
|
for _, buildId := range request.BuildIdsAdded {
|
|
batch.Query(templateInsertBuildIdTaskQueueMappingQuery, request.NamespaceID, buildId, request.TaskQueue)
|
|
}
|
|
for _, buildId := range request.BuildIdsRemoved {
|
|
batch.Query(templateDeleteBuildIdTaskQueueMappingQuery, request.NamespaceID, buildId, request.TaskQueue)
|
|
}
|
|
|
|
previous := make(map[string]interface{})
|
|
applied, iter, err := d.Session.MapExecuteBatchCAS(batch, previous)
|
|
|
|
if err != nil {
|
|
return gocql.ConvertError("UpdateTaskQueueUserData", err)
|
|
}
|
|
|
|
// We only care about the conflict in the first query
|
|
err = iter.Close()
|
|
if err != nil {
|
|
return gocql.ConvertError("UpdateTaskQueueUserData", err)
|
|
}
|
|
|
|
if !applied {
|
|
var columns []string
|
|
for k, v := range previous {
|
|
columns = append(columns, fmt.Sprintf("%s=%v", k, v))
|
|
}
|
|
|
|
return &p.ConditionFailedError{
|
|
Msg: fmt.Sprintf("Failed to update task queue. name: %v, version: %v, columns: (%v)",
|
|
request.TaskQueue, request.Version, strings.Join(columns, ",")),
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (d *MatchingTaskStore) ListTaskQueueUserDataEntries(ctx context.Context, request *p.ListTaskQueueUserDataEntriesRequest) (*p.InternalListTaskQueueUserDataEntriesResponse, error) {
|
|
query := d.Session.Query(templateListTaskQueueUserDataQuery, request.NamespaceID).WithContext(ctx)
|
|
iter := query.PageSize(request.PageSize).PageState(request.NextPageToken).Iter()
|
|
|
|
response := &p.InternalListTaskQueueUserDataEntriesResponse{}
|
|
row := make(map[string]interface{})
|
|
for iter.MapScan(row) {
|
|
taskQueueRaw, ok := row["task_queue_name"]
|
|
if !ok {
|
|
return nil, newFieldNotFoundError("task_queue_name", row)
|
|
}
|
|
taskQueue, ok := taskQueueRaw.(string)
|
|
if !ok {
|
|
return nil, newPersistedTypeMismatchError("task_queue_name", taskQueue, taskQueueRaw, row)
|
|
}
|
|
|
|
dataRaw, ok := row["data"]
|
|
if !ok {
|
|
return nil, newFieldNotFoundError("data", row)
|
|
}
|
|
data, ok := dataRaw.([]byte)
|
|
if !ok {
|
|
return nil, newPersistedTypeMismatchError("data", data, dataRaw, row)
|
|
}
|
|
|
|
dataEncodingRaw, ok := row["data_encoding"]
|
|
if !ok {
|
|
return nil, newFieldNotFoundError("data_encoding", row)
|
|
}
|
|
dataEncoding, ok := dataEncodingRaw.(string)
|
|
if !ok {
|
|
return nil, newPersistedTypeMismatchError("data_encoding", dataEncoding, dataEncodingRaw, row)
|
|
}
|
|
|
|
versionRaw, ok := row["version"]
|
|
if !ok {
|
|
return nil, newFieldNotFoundError("version", row)
|
|
}
|
|
version, ok := versionRaw.(int64)
|
|
if !ok {
|
|
return nil, newPersistedTypeMismatchError("version", version, versionRaw, row)
|
|
}
|
|
|
|
response.Entries = append(response.Entries, p.InternalTaskQueueUserDataEntry{TaskQueue: taskQueue, Data: p.NewDataBlob(data, dataEncoding), Version: version})
|
|
|
|
row = make(map[string]interface{}) // Reinitialize map as initialized fails on unmarshalling
|
|
}
|
|
if len(iter.PageState()) > 0 {
|
|
response.NextPageToken = iter.PageState()
|
|
}
|
|
|
|
if err := iter.Close(); err != nil {
|
|
return nil, serviceerror.NewUnavailable(fmt.Sprintf("ListTaskQueueUserDataEntries operation failed. Error: %v", err))
|
|
}
|
|
return response, nil
|
|
}
|
|
|
|
func (d *MatchingTaskStore) GetTaskQueuesByBuildId(ctx context.Context, request *p.GetTaskQueuesByBuildIdRequest) ([]string, error) {
|
|
query := d.Session.Query(templateListTaskQueueNamesByBuildIdQuery, request.NamespaceID, request.BuildID).WithContext(ctx)
|
|
iter := query.PageSize(listTaskQueueNamesByBuildIdPageSize).Iter()
|
|
|
|
var taskQueues []string
|
|
row := make(map[string]interface{})
|
|
|
|
for {
|
|
for iter.MapScan(row) {
|
|
taskQueueRaw, ok := row["task_queue_name"]
|
|
if !ok {
|
|
return nil, newFieldNotFoundError("task_queue_name", row)
|
|
}
|
|
taskQueue, ok := taskQueueRaw.(string)
|
|
if !ok {
|
|
var stringType string
|
|
return nil, newPersistedTypeMismatchError("task_queue_name", stringType, taskQueueRaw, row)
|
|
}
|
|
|
|
taskQueues = append(taskQueues, taskQueue)
|
|
|
|
row = make(map[string]interface{}) // Reinitialize map as initialized fails on unmarshalling
|
|
}
|
|
if len(iter.PageState()) == 0 {
|
|
break
|
|
}
|
|
}
|
|
|
|
if err := iter.Close(); err != nil {
|
|
return nil, serviceerror.NewUnavailable(fmt.Sprintf("GetTaskQueuesByBuildId operation failed. Error: %v", err))
|
|
}
|
|
return taskQueues, nil
|
|
}
|
|
|
|
func (d *MatchingTaskStore) CountTaskQueuesByBuildId(ctx context.Context, request *p.CountTaskQueuesByBuildIdRequest) (int, error) {
|
|
var count int
|
|
query := d.Session.Query(templateCountTaskQueueByBuildIdQuery, request.NamespaceID, request.BuildID).WithContext(ctx)
|
|
err := query.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
func (d *MatchingTaskStore) GetName() string {
|
|
return cassandraPersistenceName
|
|
}
|
|
|
|
func (d *MatchingTaskStore) Close() {
|
|
if d.Session != nil {
|
|
d.Session.Close()
|
|
}
|
|
}
|