github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/db/gc/diskcleaner.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/logutil"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/logstore/sm"
    23  )
    24  
    25  const (
    26  	MessgeReplay = iota
    27  	MessgeNormal
    28  )
    29  
    30  const MinMergeCount = 20
    31  
    32  // DiskCleaner is the main structure of gc operation,
    33  // and provides "JobFactory" to let tae notify itself
    34  // to perform a gc
    35  type DiskCleaner struct {
    36  	cleaner Cleaner
    37  
    38  	processQueue sm.Queue
    39  
    40  	onceStart sync.Once
    41  	onceStop  sync.Once
    42  }
    43  
    44  func NewDiskCleaner(
    45  	diskCleaner Cleaner,
    46  ) *DiskCleaner {
    47  	cleaner := &DiskCleaner{
    48  		cleaner: diskCleaner,
    49  	}
    50  	cleaner.processQueue = sm.NewSafeQueue(10000, 1000, cleaner.process)
    51  	return cleaner
    52  }
    53  
    54  func (cleaner *DiskCleaner) GC(ctx context.Context) (err error) {
    55  	logutil.Info("JobFactory is start")
    56  	return cleaner.tryClean(ctx)
    57  }
    58  
    59  func (cleaner *DiskCleaner) GetCleaner() Cleaner {
    60  	return cleaner.cleaner
    61  }
    62  
    63  func (cleaner *DiskCleaner) tryReplay() {
    64  	if _, err := cleaner.processQueue.Enqueue(MessgeReplay); err != nil {
    65  		panic(err)
    66  	}
    67  }
    68  
    69  func (cleaner *DiskCleaner) tryClean(ctx context.Context) (err error) {
    70  	_, err = cleaner.processQueue.Enqueue(MessgeNormal)
    71  	return
    72  }
    73  
    74  func (cleaner *DiskCleaner) replay() error {
    75  	return cleaner.cleaner.Replay()
    76  }
    77  
    78  func (cleaner *DiskCleaner) process(items ...any) {
    79  	if items[0].(int) == MessgeReplay {
    80  		err := cleaner.replay()
    81  		if err != nil {
    82  			panic(err)
    83  		}
    84  		cleaner.cleaner.TryGC()
    85  		if len(items) == 1 {
    86  			return
    87  		}
    88  	}
    89  
    90  	cleaner.cleaner.Process()
    91  
    92  }
    93  
    94  func (cleaner *DiskCleaner) Start() {
    95  	cleaner.onceStart.Do(func() {
    96  		cleaner.processQueue.Start()
    97  		cleaner.tryReplay()
    98  	})
    99  }
   100  
   101  func (cleaner *DiskCleaner) Stop() {
   102  	cleaner.onceStop.Do(func() {
   103  		cleaner.processQueue.Stop()
   104  		cleaner.cleaner.Stop()
   105  	})
   106  }