forked from cadence-workflow/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support read from closed execution v2 (cadence-workflow#1470)
- Loading branch information
Showing
9 changed files
with
389 additions
and
57 deletions.
There are no files selected for viewing
297 changes: 297 additions & 0 deletions
297
common/persistence/cassandra/cassandraVisibilityPersistenceV2.go
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,297 @@ | ||
// Copyright (c) 2017 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 ( | ||
"fmt" | ||
"github.com/gocql/gocql" | ||
"github.com/uber-common/bark" | ||
workflow "github.com/uber/cadence/.gen/go/shared" | ||
p "github.com/uber/cadence/common/persistence" | ||
"github.com/uber/cadence/common/service/config" | ||
) | ||
|
||
const ( | ||
templateGetClosedWorkflowExecutionsV2 = `SELECT workflow_id, run_id, start_time, close_time, workflow_type_name, status, history_length ` + | ||
`FROM closed_executions_v2 ` + | ||
`WHERE domain_id = ? ` + | ||
`AND domain_partition IN (?) ` + | ||
`AND close_time >= ? ` + | ||
`AND close_time <= ? ` | ||
|
||
templateGetClosedWorkflowExecutionsByTypeV2 = `SELECT workflow_id, run_id, start_time, close_time, workflow_type_name, status, history_length ` + | ||
`FROM closed_executions_v2 ` + | ||
`WHERE domain_id = ? ` + | ||
`AND domain_partition = ? ` + | ||
`AND close_time >= ? ` + | ||
`AND close_time <= ? ` + | ||
`AND workflow_type_name = ? ` | ||
|
||
templateGetClosedWorkflowExecutionsByIDV2 = `SELECT workflow_id, run_id, start_time, close_time, workflow_type_name, status, history_length ` + | ||
`FROM closed_executions_v2 ` + | ||
`WHERE domain_id = ? ` + | ||
`AND domain_partition = ? ` + | ||
`AND close_time >= ? ` + | ||
`AND close_time <= ? ` + | ||
`AND workflow_id = ? ` | ||
|
||
templateGetClosedWorkflowExecutionsByStatusV2 = `SELECT workflow_id, run_id, start_time, close_time, workflow_type_name, status, history_length ` + | ||
`FROM closed_executions_v2 ` + | ||
`WHERE domain_id = ? ` + | ||
`AND domain_partition = ? ` + | ||
`AND close_time >= ? ` + | ||
`AND close_time <= ? ` + | ||
`AND status = ? ` | ||
) | ||
|
||
type ( | ||
cassandraVisibilityPersistenceV2 struct { | ||
cassandraStore | ||
lowConslevel gocql.Consistency | ||
persistence p.VisibilityManager | ||
} | ||
) | ||
|
||
// NewVisibilityPersistenceV2 create a wrapper of cassandra visibilityPersistence, with all list closed executions using v2 table | ||
func NewVisibilityPersistenceV2(persistence p.VisibilityManager, cfg *config.Cassandra, logger bark.Logger) (p.VisibilityManager, error) { | ||
cluster := NewCassandraCluster(cfg.Hosts, cfg.Port, cfg.User, cfg.Password, cfg.Datacenter) | ||
cluster.Keyspace = cfg.Keyspace | ||
cluster.ProtoVersion = cassandraProtoVersion | ||
cluster.Consistency = gocql.LocalQuorum | ||
cluster.SerialConsistency = gocql.LocalSerial | ||
cluster.Timeout = defaultSessionTimeout | ||
|
||
session, err := cluster.CreateSession() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &cassandraVisibilityPersistenceV2{ | ||
cassandraStore: cassandraStore{session: session, logger: logger}, | ||
lowConslevel: gocql.One, | ||
persistence: persistence, | ||
}, nil | ||
} | ||
|
||
// Close releases the resources held by this object | ||
func (v *cassandraVisibilityPersistenceV2) Close() { | ||
if v.session != nil { | ||
v.session.Close() | ||
} | ||
v.persistence.Close() | ||
} | ||
|
||
func (v *cassandraVisibilityPersistenceV2) GetName() string { | ||
return v.persistence.GetName() | ||
} | ||
|
||
func (v *cassandraVisibilityPersistenceV2) RecordWorkflowExecutionStarted( | ||
request *p.RecordWorkflowExecutionStartedRequest) error { | ||
return v.persistence.RecordWorkflowExecutionStarted(request) | ||
} | ||
|
||
func (v *cassandraVisibilityPersistenceV2) RecordWorkflowExecutionClosed( | ||
request *p.RecordWorkflowExecutionClosedRequest) error { | ||
return v.persistence.RecordWorkflowExecutionClosed(request) | ||
} | ||
|
||
func (v *cassandraVisibilityPersistenceV2) ListOpenWorkflowExecutions( | ||
request *p.ListWorkflowExecutionsRequest) (*p.ListWorkflowExecutionsResponse, error) { | ||
return v.persistence.ListOpenWorkflowExecutions(request) | ||
} | ||
|
||
func (v *cassandraVisibilityPersistenceV2) ListOpenWorkflowExecutionsByType( | ||
request *p.ListWorkflowExecutionsByTypeRequest) (*p.ListWorkflowExecutionsResponse, error) { | ||
return v.persistence.ListOpenWorkflowExecutionsByType(request) | ||
} | ||
|
||
func (v *cassandraVisibilityPersistenceV2) ListOpenWorkflowExecutionsByWorkflowID( | ||
request *p.ListWorkflowExecutionsByWorkflowIDRequest) (*p.ListWorkflowExecutionsResponse, error) { | ||
return v.persistence.ListOpenWorkflowExecutionsByWorkflowID(request) | ||
} | ||
|
||
func (v *cassandraVisibilityPersistenceV2) GetClosedWorkflowExecution( | ||
request *p.GetClosedWorkflowExecutionRequest) (*p.GetClosedWorkflowExecutionResponse, error) { | ||
return v.persistence.GetClosedWorkflowExecution(request) | ||
} | ||
|
||
func (v *cassandraVisibilityPersistenceV2) ListClosedWorkflowExecutions( | ||
request *p.ListWorkflowExecutionsRequest) (*p.ListWorkflowExecutionsResponse, error) { | ||
query := v.session.Query(templateGetClosedWorkflowExecutionsV2, | ||
request.DomainUUID, | ||
domainPartition, | ||
p.UnixNanoToDBTimestamp(request.EarliestStartTime), | ||
p.UnixNanoToDBTimestamp(request.LatestStartTime)).Consistency(v.lowConslevel) | ||
iter := query.PageSize(request.PageSize).PageState(request.NextPageToken).Iter() | ||
if iter == nil { | ||
// TODO: should return a bad request error if the token is invalid | ||
return nil, &workflow.InternalServiceError{ | ||
Message: "ListClosedWorkflowExecutions operation failed. Not able to create query iterator.", | ||
} | ||
} | ||
|
||
response := &p.ListWorkflowExecutionsResponse{} | ||
response.Executions = make([]*workflow.WorkflowExecutionInfo, 0) | ||
wfexecution, has := readClosedWorkflowExecutionRecord(iter) | ||
for has { | ||
response.Executions = append(response.Executions, wfexecution) | ||
wfexecution, has = readClosedWorkflowExecutionRecord(iter) | ||
} | ||
|
||
nextPageToken := iter.PageState() | ||
response.NextPageToken = make([]byte, len(nextPageToken)) | ||
copy(response.NextPageToken, nextPageToken) | ||
if err := iter.Close(); err != nil { | ||
if isThrottlingError(err) { | ||
return nil, &workflow.ServiceBusyError{ | ||
Message: fmt.Sprintf("ListClosedWorkflowExecutions operation failed. Error: %v", err), | ||
} | ||
} | ||
return nil, &workflow.InternalServiceError{ | ||
Message: fmt.Sprintf("ListClosedWorkflowExecutions operation failed. Error: %v", err), | ||
} | ||
} | ||
|
||
return response, nil | ||
} | ||
|
||
func (v *cassandraVisibilityPersistenceV2) ListClosedWorkflowExecutionsByType( | ||
request *p.ListWorkflowExecutionsByTypeRequest) (*p.ListWorkflowExecutionsResponse, error) { | ||
query := v.session.Query(templateGetClosedWorkflowExecutionsByTypeV2, | ||
request.DomainUUID, | ||
domainPartition, | ||
p.UnixNanoToDBTimestamp(request.EarliestStartTime), | ||
p.UnixNanoToDBTimestamp(request.LatestStartTime), | ||
request.WorkflowTypeName).Consistency(v.lowConslevel) | ||
iter := query.PageSize(request.PageSize).PageState(request.NextPageToken).Iter() | ||
if iter == nil { | ||
// TODO: should return a bad request error if the token is invalid | ||
return nil, &workflow.InternalServiceError{ | ||
Message: "ListClosedWorkflowExecutionsByType operation failed. Not able to create query iterator.", | ||
} | ||
} | ||
|
||
response := &p.ListWorkflowExecutionsResponse{} | ||
response.Executions = make([]*workflow.WorkflowExecutionInfo, 0) | ||
wfexecution, has := readClosedWorkflowExecutionRecord(iter) | ||
for has { | ||
response.Executions = append(response.Executions, wfexecution) | ||
wfexecution, has = readClosedWorkflowExecutionRecord(iter) | ||
} | ||
|
||
nextPageToken := iter.PageState() | ||
response.NextPageToken = make([]byte, len(nextPageToken)) | ||
copy(response.NextPageToken, nextPageToken) | ||
if err := iter.Close(); err != nil { | ||
if isThrottlingError(err) { | ||
return nil, &workflow.ServiceBusyError{ | ||
Message: fmt.Sprintf("ListClosedWorkflowExecutionsByType operation failed. Error: %v", err), | ||
} | ||
} | ||
return nil, &workflow.InternalServiceError{ | ||
Message: fmt.Sprintf("ListClosedWorkflowExecutionsByType operation failed. Error: %v", err), | ||
} | ||
} | ||
|
||
return response, nil | ||
} | ||
|
||
func (v *cassandraVisibilityPersistenceV2) ListClosedWorkflowExecutionsByWorkflowID( | ||
request *p.ListWorkflowExecutionsByWorkflowIDRequest) (*p.ListWorkflowExecutionsResponse, error) { | ||
query := v.session.Query(templateGetClosedWorkflowExecutionsByIDV2, | ||
request.DomainUUID, | ||
domainPartition, | ||
p.UnixNanoToDBTimestamp(request.EarliestStartTime), | ||
p.UnixNanoToDBTimestamp(request.LatestStartTime), | ||
request.WorkflowID).Consistency(v.lowConslevel) | ||
iter := query.PageSize(request.PageSize).PageState(request.NextPageToken).Iter() | ||
if iter == nil { | ||
// TODO: should return a bad request error if the token is invalid | ||
return nil, &workflow.InternalServiceError{ | ||
Message: "ListClosedWorkflowExecutionsByWorkflowID operation failed. Not able to create query iterator.", | ||
} | ||
} | ||
|
||
response := &p.ListWorkflowExecutionsResponse{} | ||
response.Executions = make([]*workflow.WorkflowExecutionInfo, 0) | ||
wfexecution, has := readClosedWorkflowExecutionRecord(iter) | ||
for has { | ||
response.Executions = append(response.Executions, wfexecution) | ||
wfexecution, has = readClosedWorkflowExecutionRecord(iter) | ||
} | ||
|
||
nextPageToken := iter.PageState() | ||
response.NextPageToken = make([]byte, len(nextPageToken)) | ||
copy(response.NextPageToken, nextPageToken) | ||
if err := iter.Close(); err != nil { | ||
if isThrottlingError(err) { | ||
return nil, &workflow.ServiceBusyError{ | ||
Message: fmt.Sprintf("ListClosedWorkflowExecutionsByWorkflowID operation failed. Error: %v", err), | ||
} | ||
} | ||
return nil, &workflow.InternalServiceError{ | ||
Message: fmt.Sprintf("ListClosedWorkflowExecutionsByWorkflowID operation failed. Error: %v", err), | ||
} | ||
} | ||
|
||
return response, nil | ||
} | ||
|
||
func (v *cassandraVisibilityPersistenceV2) ListClosedWorkflowExecutionsByStatus( | ||
request *p.ListClosedWorkflowExecutionsByStatusRequest) (*p.ListWorkflowExecutionsResponse, error) { | ||
query := v.session.Query(templateGetClosedWorkflowExecutionsByStatusV2, | ||
request.DomainUUID, | ||
domainPartition, | ||
p.UnixNanoToDBTimestamp(request.EarliestStartTime), | ||
p.UnixNanoToDBTimestamp(request.LatestStartTime), | ||
request.Status).Consistency(v.lowConslevel) | ||
iter := query.PageSize(request.PageSize).PageState(request.NextPageToken).Iter() | ||
if iter == nil { | ||
// TODO: should return a bad request error if the token is invalid | ||
return nil, &workflow.InternalServiceError{ | ||
Message: "ListClosedWorkflowExecutionsByStatus operation failed. Not able to create query iterator.", | ||
} | ||
} | ||
|
||
response := &p.ListWorkflowExecutionsResponse{} | ||
response.Executions = make([]*workflow.WorkflowExecutionInfo, 0) | ||
wfexecution, has := readClosedWorkflowExecutionRecord(iter) | ||
for has { | ||
response.Executions = append(response.Executions, wfexecution) | ||
wfexecution, has = readClosedWorkflowExecutionRecord(iter) | ||
} | ||
|
||
nextPageToken := iter.PageState() | ||
response.NextPageToken = make([]byte, len(nextPageToken)) | ||
copy(response.NextPageToken, nextPageToken) | ||
if err := iter.Close(); err != nil { | ||
if isThrottlingError(err) { | ||
return nil, &workflow.ServiceBusyError{ | ||
Message: fmt.Sprintf("ListClosedWorkflowExecutionsByStatus operation failed. Error: %v", err), | ||
} | ||
} | ||
return nil, &workflow.InternalServiceError{ | ||
Message: fmt.Sprintf("ListClosedWorkflowExecutionsByStatus operation failed. Error: %v", err), | ||
} | ||
} | ||
|
||
return response, nil | ||
} |
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
Oops, something went wrong.