github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/storage/gc/manager.go (about)

     1  // Copyright 2022 zGraph Authors. All rights reserved.
     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  	"sync/atomic"
    21  	"time"
    22  
    23  	"github.com/cockroachdb/pebble"
    24  	"github.com/sourcegraph/conc"
    25  	"github.com/vescale/zgraph/storage/resolver"
    26  )
    27  
    28  // Manager represents the GC manager which is used to scheduler GC tasks to GC worker.
    29  type Manager struct {
    30  	running  atomic.Bool
    31  	size     int
    32  	mu       sync.RWMutex
    33  	db       *pebble.DB
    34  	resolver *resolver.Scheduler
    35  	workers  []*worker
    36  	wg       conc.WaitGroup
    37  	cancelFn context.CancelFunc
    38  	pending  chan Task
    39  }
    40  
    41  func NewManager(size int) *Manager {
    42  	return &Manager{
    43  		size:    size,
    44  		pending: make(chan Task, 32),
    45  	}
    46  }
    47  
    48  func (m *Manager) SetDB(db *pebble.DB) {
    49  	m.mu.Lock()
    50  	defer m.mu.Unlock()
    51  	m.db = db
    52  }
    53  
    54  func (m *Manager) SetResolver(resolver *resolver.Scheduler) {
    55  	m.mu.Lock()
    56  	defer m.mu.Unlock()
    57  	m.resolver = resolver
    58  }
    59  
    60  func (m *Manager) Run() {
    61  	if m.running.Swap(true) {
    62  		return
    63  	}
    64  
    65  	ctx, cancelFn := context.WithCancel(context.Background())
    66  	for i := 0; i < m.size; i++ {
    67  		worker := newWorker(m.db, m.resolver)
    68  		m.workers = append(m.workers, worker)
    69  		m.wg.Go(func() { worker.run(ctx, m.pending) })
    70  	}
    71  	m.cancelFn = cancelFn
    72  
    73  	// Schedule tasks
    74  	m.wg.Go(func() { m.scheduler(ctx) })
    75  }
    76  
    77  func (m *Manager) scheduler(ctx context.Context) {
    78  	const interval = time.Second * 5
    79  	timer := time.NewTimer(interval)
    80  	for {
    81  		select {
    82  		case <-timer.C:
    83  			if len(m.pending) < cap(m.pending) {
    84  				// TODO: schedule some new tasks.
    85  			}
    86  
    87  		case <-ctx.Done():
    88  			return
    89  		}
    90  	}
    91  }
    92  
    93  func (m *Manager) Close() {
    94  	m.cancelFn()
    95  	m.wg.Wait()
    96  }