github.com/matrixorigin/matrixone@v0.7.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  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    20  	"github.com/matrixorigin/matrixone/pkg/objectio"
    21  	"sync"
    22  )
    23  
    24  type GCWorker struct {
    25  	sync.RWMutex
    26  	// objects is list of files that can be GC
    27  	objects []string
    28  
    29  	// The status of GCWorker, only one delete worker can be running
    30  	state CleanerState
    31  
    32  	cleaner *DiskCleaner
    33  	fs      *objectio.ObjectFS
    34  }
    35  
    36  func NewGCWorker(fs *objectio.ObjectFS, cleaner *DiskCleaner) *GCWorker {
    37  	return &GCWorker{
    38  		state:   Idle,
    39  		fs:      fs,
    40  		cleaner: cleaner,
    41  	}
    42  }
    43  
    44  func (g *GCWorker) Start() bool {
    45  	g.Lock()
    46  	defer g.Unlock()
    47  	if g.state == Running {
    48  		return false
    49  	}
    50  	g.state = Running
    51  	return true
    52  }
    53  
    54  func (g *GCWorker) resetObjects() {
    55  	g.objects = make([]string, 0)
    56  }
    57  
    58  func (g *GCWorker) ExecDelete(names []string) error {
    59  	g.Lock()
    60  	g.objects = append(g.objects, names...)
    61  	if len(g.objects) == 0 {
    62  		g.state = Idle
    63  		g.Unlock()
    64  		return nil
    65  	}
    66  	g.Unlock()
    67  
    68  	err := g.fs.DelFiles(context.Background(), g.objects)
    69  	g.Lock()
    70  	defer g.Unlock()
    71  	if err != nil && !moerr.IsMoErrCode(err, moerr.ErrFileNotFound) {
    72  		g.state = Idle
    73  		return err
    74  	}
    75  	g.cleaner.updateOutputs(g.objects)
    76  	g.resetObjects()
    77  	g.state = Idle
    78  	return nil
    79  }