github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/v1workermeta/db.go (about)

     1  // Copyright 2020 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 v1workermeta
    15  
    16  import (
    17  	"github.com/pingcap/tiflow/dm/pkg/terror"
    18  	"github.com/syndtr/goleveldb/leveldb"
    19  	"github.com/syndtr/goleveldb/leveldb/opt"
    20  )
    21  
    22  // kvConfig is the configuration of goleveldb.
    23  type kvConfig struct {
    24  	BlockCacheCapacity            int     `toml:"block-cache-capacity" json:"block-cache-capacity"`
    25  	BlockRestartInterval          int     `toml:"block-restart-interval" json:"block-restart-interval"`
    26  	BlockSize                     int     `toml:"block-size" json:"block-size"`
    27  	CompactionL0Trigger           int     `toml:"compaction-L0-trigger" json:"compaction-L0-trigger"`
    28  	CompactionTableSize           int     `toml:"compaction-table-size" json:"compaction-table-size"`
    29  	CompactionTotalSize           int     `toml:"compaction-total-size" json:"compaction-total-size"`
    30  	CompactionTotalSizeMultiplier float64 `toml:"compaction-total-size-multiplier" json:"compaction-total-size-multiplier"`
    31  	WriteBuffer                   int     `toml:"write-buffer" json:"write-buffer"`
    32  	WriteL0PauseTrigger           int     `toml:"write-L0-pause-trigger" json:"write-L0-pause-trigger"`
    33  	WriteL0SlowdownTrigger        int     `toml:"write-L0-slowdown-trigger" json:"write-L0-slowdown-trigger"`
    34  }
    35  
    36  // default leveldb config.
    37  var defaultKVConfig = &kvConfig{
    38  	BlockCacheCapacity:            8388608,
    39  	BlockRestartInterval:          16,
    40  	BlockSize:                     4096,
    41  	CompactionL0Trigger:           8,
    42  	CompactionTableSize:           67108864,
    43  	CompactionTotalSize:           536870912,
    44  	CompactionTotalSizeMultiplier: 8,
    45  	WriteBuffer:                   67108864,
    46  	WriteL0PauseTrigger:           24,
    47  	WriteL0SlowdownTrigger:        17,
    48  }
    49  
    50  // openDB opens a levelDB.
    51  func openDB(kvDir string, config *kvConfig) (*leveldb.DB, error) {
    52  	var opts opt.Options
    53  	opts.BlockCacheCapacity = config.BlockCacheCapacity
    54  	opts.BlockRestartInterval = config.BlockRestartInterval
    55  	opts.BlockSize = config.BlockSize
    56  	opts.CompactionL0Trigger = config.CompactionL0Trigger
    57  	opts.CompactionTableSize = config.CompactionTableSize
    58  	opts.CompactionTotalSize = config.CompactionTotalSize
    59  	opts.CompactionTotalSizeMultiplier = config.CompactionTotalSizeMultiplier
    60  	opts.WriteBuffer = config.WriteBuffer
    61  	opts.WriteL0PauseTrigger = config.WriteL0PauseTrigger
    62  	opts.WriteL0SlowdownTrigger = config.WriteL0SlowdownTrigger
    63  
    64  	db, err := leveldb.OpenFile(kvDir, &opts)
    65  	return db, terror.ErrWorkerOpenKVDBFile.Delegate(err)
    66  }