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.
389 lines
12 KiB
Go
389 lines
12 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 namespace
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"golang.org/x/exp/maps"
|
|
|
|
enumspb "go.temporal.io/api/enums/v1"
|
|
namespacepb "go.temporal.io/api/namespace/v1"
|
|
"go.temporal.io/api/serviceerror"
|
|
"go.temporal.io/server/api/adminservice/v1"
|
|
persistencespb "go.temporal.io/server/api/persistence/v1"
|
|
"go.temporal.io/server/common"
|
|
"go.temporal.io/server/common/persistence"
|
|
"go.temporal.io/server/common/util"
|
|
)
|
|
|
|
type (
|
|
// Mutation changes a Namespace "in-flight" during a Clone operation.
|
|
Mutation interface {
|
|
apply(*persistence.GetNamespaceResponse)
|
|
}
|
|
|
|
// BadBinaryError is an error type carrying additional information about
|
|
// when/why/who configured a given checksum as being bad.
|
|
BadBinaryError struct {
|
|
cksum string
|
|
info *namespacepb.BadBinaryInfo
|
|
}
|
|
|
|
// ID is the unique identifier type for a Namespace.
|
|
ID string
|
|
|
|
// Name is a user-supplied nickname for a Namespace.
|
|
Name string
|
|
|
|
// Namespaces is a *Namespace slice
|
|
Namespaces []*Namespace
|
|
|
|
// Namespace contains the info and config for a namespace
|
|
Namespace struct {
|
|
info *persistencespb.NamespaceInfo
|
|
config *persistencespb.NamespaceConfig
|
|
replicationConfig *persistencespb.NamespaceReplicationConfig
|
|
configVersion int64
|
|
failoverVersion int64
|
|
isGlobalNamespace bool
|
|
failoverNotificationVersion int64
|
|
notificationVersion int64
|
|
|
|
customSearchAttributesMapper CustomSearchAttributesMapper
|
|
}
|
|
|
|
CustomSearchAttributesMapper struct {
|
|
fieldToAlias map[string]string
|
|
aliasToField map[string]string
|
|
}
|
|
)
|
|
|
|
const (
|
|
EmptyName Name = ""
|
|
EmptyID ID = ""
|
|
)
|
|
|
|
func NewID() ID {
|
|
return ID(uuid.NewString())
|
|
}
|
|
|
|
func FromPersistentState(record *persistence.GetNamespaceResponse) *Namespace {
|
|
return &Namespace{
|
|
info: record.Namespace.Info,
|
|
config: record.Namespace.Config,
|
|
replicationConfig: record.Namespace.ReplicationConfig,
|
|
configVersion: record.Namespace.ConfigVersion,
|
|
failoverVersion: record.Namespace.FailoverVersion,
|
|
isGlobalNamespace: record.IsGlobalNamespace,
|
|
failoverNotificationVersion: record.Namespace.FailoverNotificationVersion,
|
|
notificationVersion: record.NotificationVersion,
|
|
customSearchAttributesMapper: CustomSearchAttributesMapper{
|
|
fieldToAlias: record.Namespace.Config.CustomSearchAttributeAliases,
|
|
aliasToField: util.InverseMap(record.Namespace.Config.CustomSearchAttributeAliases),
|
|
},
|
|
}
|
|
}
|
|
|
|
func FromAdminClientApiResponse(response *adminservice.GetNamespaceResponse) *Namespace {
|
|
info := &persistencespb.NamespaceInfo{
|
|
Id: response.GetInfo().GetId(),
|
|
Name: response.GetInfo().GetName(),
|
|
State: response.GetInfo().GetState(),
|
|
Description: response.GetInfo().GetDescription(),
|
|
Owner: response.GetInfo().GetOwnerEmail(),
|
|
Data: response.GetInfo().GetData(),
|
|
}
|
|
config := &persistencespb.NamespaceConfig{
|
|
Retention: response.GetConfig().GetWorkflowExecutionRetentionTtl(),
|
|
HistoryArchivalState: response.GetConfig().GetHistoryArchivalState(),
|
|
HistoryArchivalUri: response.GetConfig().GetHistoryArchivalUri(),
|
|
VisibilityArchivalState: response.GetConfig().GetVisibilityArchivalState(),
|
|
VisibilityArchivalUri: response.GetConfig().GetVisibilityArchivalUri(),
|
|
CustomSearchAttributeAliases: response.GetConfig().GetCustomSearchAttributeAliases(),
|
|
}
|
|
replicationConfig := &persistencespb.NamespaceReplicationConfig{
|
|
ActiveClusterName: response.GetReplicationConfig().GetActiveClusterName(),
|
|
State: response.GetReplicationConfig().GetState(),
|
|
Clusters: ConvertClusterReplicationConfigFromProto(response.GetReplicationConfig().GetClusters()),
|
|
FailoverHistory: convertFailoverHistoryToPersistenceProto(response.GetFailoverHistory()),
|
|
}
|
|
return &Namespace{
|
|
info: info,
|
|
config: config,
|
|
replicationConfig: replicationConfig,
|
|
configVersion: response.GetConfigVersion(),
|
|
failoverVersion: response.GetFailoverVersion(),
|
|
isGlobalNamespace: response.GetIsGlobalNamespace(),
|
|
}
|
|
}
|
|
|
|
func (ns *Namespace) Clone(ms ...Mutation) *Namespace {
|
|
newns := *ns
|
|
r := persistence.GetNamespaceResponse{
|
|
Namespace: &persistencespb.NamespaceDetail{
|
|
Info: common.CloneProto(newns.info),
|
|
Config: common.CloneProto(newns.config),
|
|
ReplicationConfig: common.CloneProto(newns.replicationConfig),
|
|
ConfigVersion: newns.configVersion,
|
|
FailoverNotificationVersion: newns.failoverNotificationVersion,
|
|
FailoverVersion: newns.failoverVersion,
|
|
},
|
|
IsGlobalNamespace: newns.isGlobalNamespace,
|
|
NotificationVersion: newns.notificationVersion,
|
|
}
|
|
for _, m := range ms {
|
|
m.apply(&r)
|
|
}
|
|
return FromPersistentState(&r)
|
|
}
|
|
|
|
// VisibilityArchivalState observes the visibility archive configuration (state
|
|
// and URI) for this namespace.
|
|
func (ns *Namespace) VisibilityArchivalState() ArchivalConfigState {
|
|
return ArchivalConfigState{
|
|
State: ns.config.VisibilityArchivalState,
|
|
URI: ns.config.VisibilityArchivalUri,
|
|
}
|
|
}
|
|
|
|
// HistoryArchivalState observes the history archive configuration (state and
|
|
// URI) for this namespace.
|
|
func (ns *Namespace) HistoryArchivalState() ArchivalConfigState {
|
|
return ArchivalConfigState{
|
|
State: ns.config.HistoryArchivalState,
|
|
URI: ns.config.HistoryArchivalUri,
|
|
}
|
|
}
|
|
|
|
// VerifyBinaryChecksum returns an error if the provided checksum is one of this
|
|
// namespace's configured bad binary checksums. The returned error (if any) will
|
|
// be unwrappable as BadBinaryError.
|
|
func (ns *Namespace) VerifyBinaryChecksum(cksum string) error {
|
|
badBinMap := ns.config.GetBadBinaries().GetBinaries()
|
|
if badBinMap == nil {
|
|
return nil
|
|
}
|
|
if info, ok := badBinMap[cksum]; ok {
|
|
return BadBinaryError{cksum: cksum, info: info}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ID observes this namespace's permanent unique identifier in string form.
|
|
func (ns *Namespace) ID() ID {
|
|
if ns.info == nil {
|
|
return ID("")
|
|
}
|
|
return ID(ns.info.Id)
|
|
}
|
|
|
|
// Name observes this namespace's configured name.
|
|
func (ns *Namespace) Name() Name {
|
|
if ns.info == nil {
|
|
return Name("")
|
|
}
|
|
return Name(ns.info.Name)
|
|
}
|
|
|
|
func (ns *Namespace) State() enumspb.NamespaceState {
|
|
if ns.replicationConfig == nil {
|
|
return enumspb.NAMESPACE_STATE_UNSPECIFIED
|
|
}
|
|
return ns.info.State
|
|
}
|
|
|
|
func (ns *Namespace) ReplicationState() enumspb.ReplicationState {
|
|
if ns.replicationConfig == nil {
|
|
return enumspb.REPLICATION_STATE_UNSPECIFIED
|
|
}
|
|
return ns.replicationConfig.State
|
|
}
|
|
|
|
// ActiveClusterName observes the name of the cluster that is currently active
|
|
// for this namspace.
|
|
func (ns *Namespace) ActiveClusterName() string {
|
|
if ns.replicationConfig == nil {
|
|
return ""
|
|
}
|
|
return ns.replicationConfig.ActiveClusterName
|
|
}
|
|
|
|
// ClusterNames observes the names of the clusters to which this namespace is
|
|
// replicated.
|
|
func (ns *Namespace) ClusterNames() []string {
|
|
// copy slice to preserve immutability
|
|
out := make([]string, len(ns.replicationConfig.Clusters))
|
|
copy(out, ns.replicationConfig.Clusters)
|
|
return out
|
|
}
|
|
|
|
// IsOnCluster returns true is namespace is registered on cluster otherwise false.
|
|
func (ns *Namespace) IsOnCluster(clusterName string) bool {
|
|
for _, namespaceCluster := range ns.replicationConfig.Clusters {
|
|
if namespaceCluster == clusterName {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// ConfigVersion return the namespace config version
|
|
func (ns *Namespace) ConfigVersion() int64 {
|
|
return ns.configVersion
|
|
}
|
|
|
|
// FailoverVersion return the namespace failover version
|
|
func (ns *Namespace) FailoverVersion() int64 {
|
|
return ns.failoverVersion
|
|
}
|
|
|
|
// IsGlobalNamespace returns whether the namespace is a global namespace.
|
|
// Being a global namespace doesn't necessarily mean that there are multiple registered clusters for it, only that it
|
|
// has a failover version. To determine whether operations should be replicated for a namespace, see ReplicationPolicy.
|
|
func (ns *Namespace) IsGlobalNamespace() bool {
|
|
return ns.isGlobalNamespace
|
|
}
|
|
|
|
// FailoverNotificationVersion return the global notification version of when failover happened
|
|
func (ns *Namespace) FailoverNotificationVersion() int64 {
|
|
return ns.failoverNotificationVersion
|
|
}
|
|
|
|
// NotificationVersion return the global notification version of when namespace changed
|
|
func (ns *Namespace) NotificationVersion() int64 {
|
|
return ns.notificationVersion
|
|
}
|
|
|
|
// ActiveInCluster returns whether the namespace is active, i.e. non global
|
|
// namespace or global namespace which active cluster is the provided cluster
|
|
func (ns *Namespace) ActiveInCluster(clusterName string) bool {
|
|
if !ns.isGlobalNamespace {
|
|
// namespace is not a global namespace, meaning namespace is always
|
|
// "active" within each cluster
|
|
return true
|
|
}
|
|
return clusterName == ns.ActiveClusterName()
|
|
}
|
|
|
|
// ReplicationPolicy return the derived workflow replication policy
|
|
func (ns *Namespace) ReplicationPolicy() ReplicationPolicy {
|
|
// frontend guarantee that the clusters always contains the active
|
|
// namespace, so if the # of clusters is 1 then we do not need to send out
|
|
// any events for replication
|
|
if ns.isGlobalNamespace && len(ns.replicationConfig.Clusters) > 1 {
|
|
return ReplicationPolicyMultiCluster
|
|
}
|
|
return ReplicationPolicyOneCluster
|
|
}
|
|
|
|
func (ns *Namespace) GetCustomData(key string) string {
|
|
if ns.info.Data == nil {
|
|
return ""
|
|
}
|
|
return ns.info.Data[key]
|
|
}
|
|
|
|
// Retention returns retention duration for this namespace.
|
|
func (ns *Namespace) Retention() time.Duration {
|
|
if ns.config.Retention == nil {
|
|
return 0
|
|
}
|
|
|
|
return ns.config.Retention.AsDuration()
|
|
}
|
|
|
|
func (ns *Namespace) CustomSearchAttributesMapper() CustomSearchAttributesMapper {
|
|
return ns.customSearchAttributesMapper
|
|
}
|
|
|
|
// Error returns the reason associated with this bad binary.
|
|
func (e BadBinaryError) Error() string {
|
|
return e.info.Reason
|
|
}
|
|
|
|
// Reason returns the reason associated with this bad binary.
|
|
func (e BadBinaryError) Reason() string {
|
|
return e.info.Reason
|
|
}
|
|
|
|
// Operator returns the operator associated with this bad binary.
|
|
func (e BadBinaryError) Operator() string {
|
|
return e.info.Operator
|
|
}
|
|
|
|
// Created returns the time at which this bad binary was declared to be bad.
|
|
func (e BadBinaryError) Created() time.Time {
|
|
return e.info.CreateTime.AsTime()
|
|
}
|
|
|
|
// Checksum observes the binary checksum that caused this error.
|
|
func (e BadBinaryError) Checksum() string {
|
|
return e.cksum
|
|
}
|
|
|
|
func (id ID) String() string {
|
|
return string(id)
|
|
}
|
|
|
|
func (id ID) IsEmpty() bool {
|
|
return id == EmptyID
|
|
}
|
|
|
|
func (n Name) String() string {
|
|
return string(n)
|
|
}
|
|
|
|
func (n Name) IsEmpty() bool {
|
|
return n == EmptyName
|
|
}
|
|
|
|
func (m *CustomSearchAttributesMapper) GetAlias(fieldName string, namespace string) (string, error) {
|
|
alias, ok := m.fieldToAlias[fieldName]
|
|
if !ok {
|
|
return "", serviceerror.NewInvalidArgument(
|
|
fmt.Sprintf("Namespace %s has no mapping defined for field name %s", namespace, fieldName),
|
|
)
|
|
}
|
|
return alias, nil
|
|
}
|
|
|
|
func (m *CustomSearchAttributesMapper) GetFieldName(alias string, namespace string) (string, error) {
|
|
fieldName, ok := m.aliasToField[alias]
|
|
if !ok {
|
|
return "", serviceerror.NewInvalidArgument(
|
|
fmt.Sprintf("Namespace %s has no mapping defined for search attribute %s", namespace, alias),
|
|
)
|
|
}
|
|
return fieldName, nil
|
|
}
|
|
|
|
func (m *CustomSearchAttributesMapper) FieldToAliasMap() map[string]string {
|
|
return maps.Clone(m.fieldToAlias)
|
|
}
|