|
| 1 | +// The MIT License (MIT) |
| 2 | + |
| 3 | +// Copyright (c) 2017-2020 Uber Technologies Inc. |
| 4 | + |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +package activecluster |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + "errors" |
| 28 | + |
| 29 | + "github.com/uber/cadence/common/cache" |
| 30 | + "github.com/uber/cadence/common/cluster" |
| 31 | + "github.com/uber/cadence/common/log" |
| 32 | + "github.com/uber/cadence/common/log/tag" |
| 33 | + "github.com/uber/cadence/common/metrics" |
| 34 | + "github.com/uber/cadence/common/types" |
| 35 | +) |
| 36 | + |
| 37 | +type DomainIDToDomainFn func(id string) (*cache.DomainCacheEntry, error) |
| 38 | + |
| 39 | +type manager struct { |
| 40 | + domainIDToDomainFn DomainIDToDomainFn |
| 41 | + clusterMetadata cluster.Metadata |
| 42 | + metricsCl metrics.Client |
| 43 | + logger log.Logger |
| 44 | +} |
| 45 | + |
| 46 | +func NewManager( |
| 47 | + domainIDToDomainFn DomainIDToDomainFn, |
| 48 | + clusterMetadata cluster.Metadata, |
| 49 | + metricsCl metrics.Client, |
| 50 | + logger log.Logger, |
| 51 | +) Manager { |
| 52 | + return &manager{ |
| 53 | + domainIDToDomainFn: domainIDToDomainFn, |
| 54 | + clusterMetadata: clusterMetadata, |
| 55 | + metricsCl: metricsCl, |
| 56 | + logger: logger.WithTags(tag.ComponentActiveRegionManager), |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func (m *manager) Start() { |
| 61 | + // TODO: implement this |
| 62 | +} |
| 63 | + |
| 64 | +func (m *manager) Stop() { |
| 65 | + // TODO: implement this |
| 66 | +} |
| 67 | + |
| 68 | +func (m *manager) LookupExternalEntity(ctx context.Context, entityType, entityKey string) (*LookupResult, error) { |
| 69 | + // TODO: implement this |
| 70 | + return nil, errors.New("not implemented") |
| 71 | +} |
| 72 | + |
| 73 | +func (m *manager) LookupExternalEntityOfNewWorkflow(ctx context.Context, req *types.HistoryStartWorkflowExecutionRequest) (*LookupResult, error) { |
| 74 | + d, err := m.domainIDToDomainFn(req.DomainUUID) |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + if !d.GetReplicationConfig().IsActiveActive() { |
| 80 | + // Not an active-active domain. return ActiveClusterName from domain entry |
| 81 | + return &LookupResult{ |
| 82 | + Region: d.GetReplicationConfig().ActiveClusterName, |
| 83 | + ClusterName: d.GetReplicationConfig().ActiveClusterName, |
| 84 | + FailoverVersion: d.GetFailoverVersion(), |
| 85 | + }, nil |
| 86 | + } |
| 87 | + |
| 88 | + wfID := req.StartRequest.WorkflowID |
| 89 | + return helperToBeRemoved(wfID) |
| 90 | +} |
| 91 | + |
| 92 | +func (m *manager) LookupWorkflow(ctx context.Context, domainID, wfID, rID string) (*LookupResult, error) { |
| 93 | + d, err := m.domainIDToDomainFn(domainID) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + |
| 98 | + if !d.GetReplicationConfig().IsActiveActive() { |
| 99 | + // Not an active-active domain. return ActiveClusterName from domain entry |
| 100 | + return &LookupResult{ |
| 101 | + Region: d.GetReplicationConfig().ActiveClusterName, |
| 102 | + ClusterName: d.GetReplicationConfig().ActiveClusterName, |
| 103 | + FailoverVersion: d.GetFailoverVersion(), |
| 104 | + }, nil |
| 105 | + } |
| 106 | + |
| 107 | + return helperToBeRemoved(wfID) |
| 108 | +} |
| 109 | + |
| 110 | +func helperToBeRemoved(wfID string) (*LookupResult, error) { |
| 111 | + // TODO: Remove below fake implementation and implement properly |
| 112 | + // - lookup active region given <domain id, wf id, run id> from executions table RowType=ActiveCluster. |
| 113 | + // - cache this info |
| 114 | + // - add metrics for cache hit/miss |
| 115 | + // - return cluster name |
| 116 | + if wfID == "wf1" { |
| 117 | + return &LookupResult{ |
| 118 | + Region: "cluster0", |
| 119 | + ClusterName: "cluster0", |
| 120 | + FailoverVersion: 1, |
| 121 | + }, nil |
| 122 | + } |
| 123 | + if wfID == "wf2" { |
| 124 | + return &LookupResult{ |
| 125 | + Region: "cluster1", |
| 126 | + ClusterName: "cluster1", |
| 127 | + FailoverVersion: 2, |
| 128 | + }, nil |
| 129 | + } |
| 130 | + |
| 131 | + return nil, errors.New("not implemented") |
| 132 | +} |
0 commit comments