github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/externalresource/internal/bucket/resource_desc.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package bucket
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  
    20  	brStorage "github.com/pingcap/tidb/br/pkg/storage"
    21  	"github.com/pingcap/tiflow/engine/pkg/externalresource/internal"
    22  	resModel "github.com/pingcap/tiflow/engine/pkg/externalresource/model"
    23  )
    24  
    25  var _ internal.ResourceDescriptor = (*resourceDescriptor)(nil)
    26  
    27  // resourceDescriptor is a handle for a bucket-backed resource used
    28  // internally in engine/pkg/externalresource.
    29  //
    30  // Note that this implementation caches a brStorage.ExternalStorage object,
    31  // so it is not thread-safe to use. But thread-safety does not seem
    32  // to be a necessary requirement.
    33  type resourceDescriptor struct {
    34  	Ident internal.ResourceIdent
    35  
    36  	creator Creator
    37  	storage brStorage.ExternalStorage
    38  }
    39  
    40  func newResourceDescriptor(
    41  	ident internal.ResourceIdent,
    42  	creator Creator,
    43  ) *resourceDescriptor {
    44  	return &resourceDescriptor{
    45  		Ident:   ident,
    46  		creator: creator,
    47  	}
    48  }
    49  
    50  // ExternalStorage creates the storage object if one has not been created yet, and returns the
    51  // created storage object.
    52  func (r *resourceDescriptor) ExternalStorage(ctx context.Context) (brStorage.ExternalStorage, error) {
    53  	if r.storage == nil {
    54  		storage, err := r.makeExternalStorage(ctx)
    55  		if err != nil {
    56  			return nil, err
    57  		}
    58  		r.storage = storage
    59  	}
    60  	return r.storage, nil
    61  }
    62  
    63  // makeExternalStorage actually creates the storage object.
    64  func (r *resourceDescriptor) makeExternalStorage(ctx context.Context) (brStorage.ExternalStorage, error) {
    65  	return r.creator.newBucketFromURI(ctx, r.URI())
    66  }
    67  
    68  func (r *resourceDescriptor) URI() string {
    69  	return fmt.Sprintf("%s/%s", r.creator.baseURI(), r.Ident.BuildResPath())
    70  }
    71  
    72  func (r *resourceDescriptor) ResourceIdent() internal.ResourceIdent {
    73  	return r.Ident
    74  }
    75  
    76  func (r *resourceDescriptor) ID() string {
    77  	return resModel.BuildResourceID(r.creator.resourceType(), r.Ident.Name)
    78  }