You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
243 lines
8.2 KiB
243 lines
8.2 KiB
package azuresink
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore/streaming"
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
|
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
|
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/appendblob"
|
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob"
|
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/bloberror"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/replication/repl_util"
|
|
"github.com/seaweedfs/seaweedfs/weed/replication/sink"
|
|
"github.com/seaweedfs/seaweedfs/weed/replication/source"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
)
|
|
|
|
type AzureSink struct {
|
|
client *azblob.Client
|
|
container string
|
|
dir string
|
|
filerSource *source.FilerSource
|
|
isIncremental bool
|
|
}
|
|
|
|
func init() {
|
|
sink.Sinks = append(sink.Sinks, &AzureSink{})
|
|
}
|
|
|
|
func (g *AzureSink) GetName() string {
|
|
return "azure"
|
|
}
|
|
|
|
func (g *AzureSink) GetSinkToDirectory() string {
|
|
return g.dir
|
|
}
|
|
|
|
func (g *AzureSink) IsIncremental() bool {
|
|
return g.isIncremental
|
|
}
|
|
|
|
func (g *AzureSink) Initialize(configuration util.Configuration, prefix string) error {
|
|
g.isIncremental = configuration.GetBool(prefix + "is_incremental")
|
|
return g.initialize(
|
|
configuration.GetString(prefix+"account_name"),
|
|
configuration.GetString(prefix+"account_key"),
|
|
configuration.GetString(prefix+"container"),
|
|
configuration.GetString(prefix+"directory"),
|
|
)
|
|
}
|
|
|
|
func (g *AzureSink) SetSourceFiler(s *source.FilerSource) {
|
|
g.filerSource = s
|
|
}
|
|
|
|
func (g *AzureSink) initialize(accountName, accountKey, container, dir string) error {
|
|
g.container = container
|
|
g.dir = dir
|
|
|
|
// Create credential and client
|
|
credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create Azure credential with account name:%s: %w", accountName, err)
|
|
}
|
|
|
|
serviceURL := fmt.Sprintf("https://%s.blob.core.windows.net/", accountName)
|
|
client, err := azblob.NewClientWithSharedKeyCredential(serviceURL, credential, &azblob.ClientOptions{
|
|
ClientOptions: azcore.ClientOptions{
|
|
Retry: policy.RetryOptions{
|
|
MaxRetries: 3, // Reasonable retry count - aggressive retries mask configuration errors
|
|
TryTimeout: 10 * time.Second, // Reduced from 1 minute to fail faster on auth issues
|
|
RetryDelay: 1 * time.Second,
|
|
MaxRetryDelay: 10 * time.Second,
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create Azure client: %w", err)
|
|
}
|
|
|
|
g.client = client
|
|
|
|
// Validate that the container exists early to catch configuration errors
|
|
containerClient := client.ServiceClient().NewContainerClient(container)
|
|
_, err = containerClient.GetProperties(context.Background(), nil)
|
|
if err != nil {
|
|
if bloberror.HasCode(err, bloberror.ContainerNotFound) {
|
|
return fmt.Errorf("Azure container '%s' does not exist. Please create it first", container)
|
|
}
|
|
return fmt.Errorf("failed to validate Azure container '%s': %w", container, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (g *AzureSink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool, signatures []int32) error {
|
|
|
|
key = cleanKey(key)
|
|
|
|
if isDirectory {
|
|
key = key + "/"
|
|
}
|
|
|
|
blobClient := g.client.ServiceClient().NewContainerClient(g.container).NewBlobClient(key)
|
|
_, err := blobClient.Delete(context.Background(), &blob.DeleteOptions{
|
|
DeleteSnapshots: to.Ptr(blob.DeleteSnapshotsOptionTypeInclude),
|
|
})
|
|
if err != nil {
|
|
// Make delete idempotent - don't return error if blob doesn't exist
|
|
if bloberror.HasCode(err, bloberror.BlobNotFound) {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("azure delete %s/%s: %w", g.container, key, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (g *AzureSink) CreateEntry(key string, entry *filer_pb.Entry, signatures []int32) error {
|
|
|
|
key = cleanKey(key)
|
|
|
|
if entry.IsDirectory {
|
|
return nil
|
|
}
|
|
|
|
totalSize := filer.FileSize(entry)
|
|
chunkViews := filer.ViewFromChunks(context.Background(), g.filerSource.LookupFileId, entry.GetChunks(), 0, int64(totalSize))
|
|
|
|
// Create append blob client
|
|
appendBlobClient := g.client.ServiceClient().NewContainerClient(g.container).NewAppendBlobClient(key)
|
|
|
|
// Try to create the blob first (without access conditions for initial creation)
|
|
_, err := appendBlobClient.Create(context.Background(), nil)
|
|
|
|
needsWrite := true
|
|
if err != nil {
|
|
if bloberror.HasCode(err, bloberror.BlobAlreadyExists) {
|
|
// Handle existing blob - check if overwrite is needed and perform it if necessary
|
|
var handleErr error
|
|
needsWrite, handleErr = g.handleExistingBlob(appendBlobClient, key, entry)
|
|
if handleErr != nil {
|
|
return handleErr
|
|
}
|
|
} else {
|
|
return fmt.Errorf("azure create append blob %s/%s: %w", g.container, key, err)
|
|
}
|
|
}
|
|
|
|
// If we don't need to write (blob is up-to-date), return early
|
|
if !needsWrite {
|
|
return nil
|
|
}
|
|
|
|
writeFunc := func(data []byte) error {
|
|
_, writeErr := appendBlobClient.AppendBlock(context.Background(), streaming.NopCloser(bytes.NewReader(data)), &appendblob.AppendBlockOptions{})
|
|
return writeErr
|
|
}
|
|
|
|
if len(entry.Content) > 0 {
|
|
return writeFunc(entry.Content)
|
|
}
|
|
|
|
if err := repl_util.CopyFromChunkViews(chunkViews, g.filerSource, writeFunc); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// handleExistingBlob determines whether an existing blob needs to be overwritten and performs the overwrite if necessary.
|
|
// It returns:
|
|
// - needsWrite: true if the caller should write data to the blob, false if the blob is already up-to-date
|
|
// - error: any error encountered during the operation
|
|
func (g *AzureSink) handleExistingBlob(appendBlobClient *appendblob.Client, key string, entry *filer_pb.Entry) (needsWrite bool, err error) {
|
|
// Get the blob's properties to decide whether to overwrite.
|
|
props, propErr := appendBlobClient.GetProperties(context.Background(), nil)
|
|
|
|
// Check if we can skip writing based on modification time.
|
|
if entry.Attributes != nil && entry.Attributes.Mtime > 0 && propErr == nil && props.LastModified != nil && props.ContentLength != nil {
|
|
remoteMtime := props.LastModified.Unix()
|
|
localMtime := entry.Attributes.Mtime
|
|
// Skip if remote is newer or same, AND has content.
|
|
if remoteMtime >= localMtime && *props.ContentLength > 0 {
|
|
glog.V(2).Infof("skip overwriting %s/%s: remote is up-to-date (remote mtime: %d >= local mtime: %d, size: %d)",
|
|
g.container, key, remoteMtime, localMtime, *props.ContentLength)
|
|
return false, nil
|
|
}
|
|
}
|
|
|
|
// Blob is empty or outdated - we need to delete and recreate it.
|
|
// Use ETag for a conditional delete to avoid race conditions.
|
|
deleteOpts := &blob.DeleteOptions{}
|
|
if propErr == nil && props.ETag != nil {
|
|
deleteOpts.AccessConditions = &blob.AccessConditions{
|
|
ModifiedAccessConditions: &blob.ModifiedAccessConditions{
|
|
IfMatch: props.ETag,
|
|
},
|
|
}
|
|
}
|
|
|
|
// Delete existing blob with conditional delete.
|
|
_, delErr := appendBlobClient.Delete(context.Background(), deleteOpts)
|
|
if delErr != nil {
|
|
// If the precondition fails, the blob was modified by another process after we checked it.
|
|
// Failing here is safe; replication will retry.
|
|
if bloberror.HasCode(delErr, bloberror.ConditionNotMet) {
|
|
return false, fmt.Errorf("azure blob %s/%s was modified concurrently, preventing overwrite: %w", g.container, key, delErr)
|
|
}
|
|
// Ignore BlobNotFound, as the goal is to delete it anyway.
|
|
if !bloberror.HasCode(delErr, bloberror.BlobNotFound) {
|
|
return false, fmt.Errorf("azure delete existing blob %s/%s: %w", g.container, key, delErr)
|
|
}
|
|
}
|
|
|
|
// Recreate the blob.
|
|
_, createErr := appendBlobClient.Create(context.Background(), nil)
|
|
if createErr != nil {
|
|
// It's possible another process recreated it after our delete.
|
|
// Failing is safe, as a retry of the whole function will handle it.
|
|
return false, fmt.Errorf("azure recreate append blob %s/%s: %w", g.container, key, createErr)
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
func (g *AzureSink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool, signatures []int32) (foundExistingEntry bool, err error) {
|
|
key = cleanKey(key)
|
|
return true, g.CreateEntry(key, newEntry, signatures)
|
|
}
|
|
|
|
func cleanKey(key string) string {
|
|
return strings.TrimPrefix(key, "/")
|
|
}
|