Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
2,014 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
// 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 deletenamespace | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
|
||
enumspb "go.temporal.io/api/enums/v1" | ||
"go.temporal.io/api/serviceerror" | ||
|
||
"go.temporal.io/server/common/log" | ||
"go.temporal.io/server/common/log/tag" | ||
"go.temporal.io/server/common/metrics" | ||
"go.temporal.io/server/common/namespace" | ||
"go.temporal.io/server/common/persistence" | ||
) | ||
|
||
type ( | ||
activities struct { | ||
metadataManager persistence.MetadataManager | ||
metricsClient metrics.Client | ||
logger log.Logger | ||
} | ||
) | ||
|
||
func NewActivities( | ||
metadataManager persistence.MetadataManager, | ||
metricsClient metrics.Client, | ||
logger log.Logger, | ||
) *activities { | ||
return &activities{ | ||
metadataManager: metadataManager, | ||
metricsClient: metricsClient, | ||
logger: logger, | ||
} | ||
} | ||
|
||
func (a *activities) GetNamespaceIDActivity(_ context.Context, nsName namespace.Name) (namespace.ID, error) { | ||
getNamespaceRequest := &persistence.GetNamespaceRequest{ | ||
Name: nsName.String(), | ||
} | ||
|
||
getNamespaceResponse, err := a.metadataManager.GetNamespace(getNamespaceRequest) | ||
if err != nil { | ||
return namespace.EmptyID, err | ||
} | ||
|
||
if getNamespaceResponse.Namespace == nil || getNamespaceResponse.Namespace.Info == nil || getNamespaceResponse.Namespace.Info.Id == "" { | ||
return namespace.EmptyID, serviceerror.NewInternal("namespace info is corrupted") | ||
} | ||
|
||
return namespace.ID(getNamespaceResponse.Namespace.Info.Id), nil | ||
} | ||
|
||
func (a *activities) MarkNamespaceDeletedActivity(_ context.Context, nsName namespace.Name) error { | ||
getNamespaceRequest := &persistence.GetNamespaceRequest{ | ||
Name: nsName.String(), | ||
} | ||
|
||
metadata, err := a.metadataManager.GetMetadata() | ||
if err != nil { | ||
a.metricsClient.IncCounter(metrics.DeleteNamespaceWorkflowScope, metrics.ReadNamespaceFailuresCount) | ||
a.logger.Error("Unable to get cluster metadata.", tag.WorkflowNamespace(nsName.String()), tag.Error(err)) | ||
return err | ||
} | ||
|
||
ns, err := a.metadataManager.GetNamespace(getNamespaceRequest) | ||
if err != nil { | ||
a.metricsClient.IncCounter(metrics.DeleteNamespaceWorkflowScope, metrics.ReadNamespaceFailuresCount) | ||
a.logger.Error("Unable to get namespace details.", tag.WorkflowNamespace(nsName.String()), tag.Error(err)) | ||
return err | ||
} | ||
|
||
ns.Namespace.Info.State = enumspb.NAMESPACE_STATE_DELETED | ||
|
||
updateRequest := &persistence.UpdateNamespaceRequest{ | ||
Namespace: ns.Namespace, | ||
IsGlobalNamespace: ns.IsGlobalNamespace, | ||
NotificationVersion: metadata.NotificationVersion, | ||
} | ||
|
||
err = a.metadataManager.UpdateNamespace(updateRequest) | ||
if err != nil { | ||
a.metricsClient.IncCounter(metrics.DeleteNamespaceWorkflowScope, metrics.UpdateNamespaceFailuresCount) | ||
a.logger.Error("Unable to update namespace state to Deleted.", tag.WorkflowNamespace(nsName.String()), tag.Error(err)) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (a *activities) GenerateDeletedNamespaceNameActivity(_ context.Context, nsName namespace.Name) (namespace.Name, error) { | ||
var letters = []rune("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") | ||
const suffixLength = 5 | ||
rand.Seed(time.Now().UnixNano()) | ||
|
||
for { // Just in case. Generated name should always be unique after first attempt. | ||
b := make([]rune, suffixLength) | ||
for i := range b { | ||
b[i] = letters[rand.Intn(len(letters))] | ||
} | ||
|
||
newName := fmt.Sprintf("%s-deleted-%s", nsName, string(b)) | ||
|
||
_, err := a.metadataManager.GetNamespace(&persistence.GetNamespaceRequest{ | ||
Name: newName, | ||
}) | ||
switch err.(type) { | ||
case nil: | ||
a.logger.Warn("Regenerate namespace name due to collision.", tag.WorkflowNamespace(nsName.String()), tag.WorkflowNamespace(newName)) | ||
case *serviceerror.NotFound: | ||
a.logger.Info("Generated new name for deleted namespace.", tag.WorkflowNamespace(nsName.String()), tag.WorkflowNamespace(newName)) | ||
return namespace.Name(newName), nil | ||
default: | ||
a.metricsClient.IncCounter(metrics.DeleteNamespaceWorkflowScope, metrics.ReadNamespaceFailuresCount) | ||
a.logger.Error("Unable to get namespace details.", tag.WorkflowNamespace(nsName.String()), tag.Error(err)) | ||
return namespace.EmptyName, err | ||
} | ||
} | ||
} | ||
|
||
func (a *activities) RenameNamespaceActivity(_ context.Context, previousName namespace.Name, newName namespace.Name) error { | ||
renameNamespaceRequest := &persistence.RenameNamespaceRequest{ | ||
PreviousName: previousName.String(), | ||
NewName: newName.String(), | ||
} | ||
|
||
err := a.metadataManager.RenameNamespace(renameNamespaceRequest) | ||
if err != nil { | ||
a.metricsClient.IncCounter(metrics.DeleteNamespaceWorkflowScope, metrics.RenameNamespaceFailuresCount) | ||
a.logger.Error("Unable to rename namespace.", tag.WorkflowNamespace(previousName.String()), tag.Error(err)) | ||
return err | ||
} | ||
|
||
a.metricsClient.IncCounter(metrics.DeleteNamespaceWorkflowScope, metrics.RenameNamespaceSuccessCount) | ||
a.logger.Info("Namespace renamed successfully.", tag.WorkflowNamespace(previousName.String()), tag.WorkflowNamespace(newName.String())) | ||
return nil | ||
} |
Oops, something went wrong.