github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/db/gc/delete.go (about)

     1  // Copyright 2021 Matrix Origin
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package gc
    16  
    17  import (
    18  	"context"
    19  	"sync"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    22  	"github.com/matrixorigin/matrixone/pkg/logutil"
    23  	"github.com/matrixorigin/matrixone/pkg/objectio"
    24  )
    25  
    26  type GCWorker struct {
    27  	sync.RWMutex
    28  	// objects is list of files that can be GC
    29  	objects []string
    30  
    31  	// The status of GCWorker, only one delete worker can be running
    32  	state CleanerState
    33  
    34  	cleaner *checkpointCleaner
    35  	fs      *objectio.ObjectFS
    36  }
    37  
    38  func NewGCWorker(fs *objectio.ObjectFS, cleaner *checkpointCleaner) *GCWorker {
    39  	return &GCWorker{
    40  		state:   Idle,
    41  		fs:      fs,
    42  		cleaner: cleaner,
    43  	}
    44  }
    45  
    46  func (g *GCWorker) Start() bool {
    47  	g.Lock()
    48  	defer g.Unlock()
    49  	if g.state == Running {
    50  		return false
    51  	}
    52  	g.state = Running
    53  	return true
    54  }
    55  
    56  func (g *GCWorker) resetObjects() {
    57  	g.objects = make([]string, 0)
    58  }
    59  
    60  func (g *GCWorker) ExecDelete(ctx context.Context, names []string, disableGC bool) error {
    61  	g.Lock()
    62  	g.objects = append(g.objects, names...)
    63  	if len(g.objects) == 0 {
    64  		g.state = Idle
    65  		g.Unlock()
    66  		return nil
    67  	}
    68  	g.Unlock()
    69  
    70  	logutil.Infof("[DB GC] disableGC: %v, files to delete: %v", disableGC, g.objects)
    71  	var err error
    72  	if !disableGC {
    73  		err = g.fs.DelFiles(ctx, g.objects)
    74  	}
    75  	g.Lock()
    76  	defer g.Unlock()
    77  	if err != nil && !moerr.IsMoErrCode(err, moerr.ErrFileNotFound) {
    78  		g.state = Idle
    79  		return err
    80  	}
    81  	g.cleaner.updateOutputs(g.objects)
    82  	g.resetObjects()
    83  	g.state = Idle
    84  	return nil
    85  }