github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/externalresource/internal/local/resource_controller.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 local
    15  
    16  import (
    17  	"context"
    18  
    19  	"github.com/pingcap/tiflow/engine/model"
    20  	"github.com/pingcap/tiflow/engine/pkg/client"
    21  	"github.com/pingcap/tiflow/engine/pkg/externalresource/internal"
    22  	resModel "github.com/pingcap/tiflow/engine/pkg/externalresource/model"
    23  	"github.com/pingcap/tiflow/pkg/errors"
    24  )
    25  
    26  var _ internal.ResourceController = &resourceController{}
    27  
    28  // resourceController defines operations specific to the local file type.
    29  type resourceController struct {
    30  	// clientManager is used to communicate with executors.
    31  	clientGroup client.ExecutorGroup
    32  }
    33  
    34  // NewFileResourceController creates a new LocalFileResourceController.
    35  func NewFileResourceController(clientGroup client.ExecutorGroup) *resourceController {
    36  	return &resourceController{clientGroup: clientGroup}
    37  }
    38  
    39  // GCSingleResource remove a persisted resource on executor.
    40  func (r *resourceController) GCSingleResource(ctx context.Context, res *resModel.ResourceMeta) error {
    41  	return r.removeFilesOnExecutor(ctx, res)
    42  }
    43  
    44  func (r *resourceController) removeFilesOnExecutor(ctx context.Context, resource *resModel.ResourceMeta) error {
    45  	cli, err := r.clientGroup.GetExecutorClientB(ctx, resource.Executor)
    46  	if err != nil {
    47  		return errors.Annotate(err, "removeFilesOnExecutor")
    48  	}
    49  
    50  	return cli.RemoveResource(ctx, resource.Worker, resource.ID)
    51  }
    52  
    53  // GCExecutor removes all temporary resources created by the offlined executors.
    54  func (r *resourceController) GCExecutor(
    55  	_ context.Context, _ []*resModel.ResourceMeta, _ model.ExecutorID,
    56  ) error {
    57  	// There is no need to clean up temporary files, because no persistent volumes are
    58  	// mounted to executor.
    59  	return nil
    60  }