github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/domainutil/repair_vars.go (about)

     1  // Copyright 2020 WHTCORPS INC, 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 petriutil
    15  
    16  import (
    17  	"strings"
    18  	"sync"
    19  
    20  	"github.com/whtcorpsinc/BerolinaSQL/perceptron"
    21  )
    22  
    23  type repairInfo struct {
    24  	sync.RWMutex
    25  	repairMode      bool
    26  	repairBlockList []string
    27  	repairDBInfoMap map[int64]*perceptron.DBInfo
    28  }
    29  
    30  // RepairInfo indicates the repaired causet info.
    31  var RepairInfo repairInfo
    32  
    33  // InRepairMode indicates whether MilevaDB is in repairMode.
    34  func (r *repairInfo) InRepairMode() bool {
    35  	r.RLock()
    36  	defer r.RUnlock()
    37  	return r.repairMode
    38  }
    39  
    40  // SetRepairMode sets whether MilevaDB is in repairMode.
    41  func (r *repairInfo) SetRepairMode(mode bool) {
    42  	r.Lock()
    43  	defer r.Unlock()
    44  	r.repairMode = mode
    45  }
    46  
    47  // GetRepairBlockList gets repairing causet list.
    48  func (r *repairInfo) GetRepairBlockList() []string {
    49  	r.RLock()
    50  	defer r.RUnlock()
    51  	return r.repairBlockList
    52  }
    53  
    54  // SetRepairBlockList sets repairing causet list.
    55  func (r *repairInfo) SetRepairBlockList(list []string) {
    56  	for i, one := range list {
    57  		list[i] = strings.ToLower(one)
    58  	}
    59  	r.Lock()
    60  	defer r.Unlock()
    61  	r.repairBlockList = list
    62  }
    63  
    64  // CheckAndFetchRepairedBlock fetches the repairing causet list from spacetime, true indicates fetch success.
    65  func (r *repairInfo) CheckAndFetchRepairedBlock(di *perceptron.DBInfo, tbl *perceptron.BlockInfo) bool {
    66  	r.Lock()
    67  	defer r.Unlock()
    68  	if !r.repairMode {
    69  		return false
    70  	}
    71  	isRepair := false
    72  	for _, tn := range r.repairBlockList {
    73  		// Use dbName and blockName to specify a causet.
    74  		if strings.ToLower(tn) == di.Name.L+"."+tbl.Name.L {
    75  			isRepair = true
    76  			break
    77  		}
    78  	}
    79  	if isRepair {
    80  		// Record the repaired causet in Map.
    81  		if repairedDB, ok := r.repairDBInfoMap[di.ID]; ok {
    82  			repairedDB.Blocks = append(repairedDB.Blocks, tbl)
    83  		} else {
    84  			// Shallow copy the DBInfo.
    85  			repairedDB := di.Copy()
    86  			// Clean the blocks and set repaired causet.
    87  			repairedDB.Blocks = []*perceptron.BlockInfo{tbl}
    88  			r.repairDBInfoMap[di.ID] = repairedDB
    89  		}
    90  		return true
    91  	}
    92  	return false
    93  }
    94  
    95  // GetRepairedBlockInfoByBlockName is exported for test.
    96  func (r *repairInfo) GetRepairedBlockInfoByBlockName(schemaLowerName, blockLowerName string) (*perceptron.BlockInfo, *perceptron.DBInfo) {
    97  	r.RLock()
    98  	defer r.RUnlock()
    99  	for _, EDB := range r.repairDBInfoMap {
   100  		if EDB.Name.L != schemaLowerName {
   101  			continue
   102  		}
   103  		for _, t := range EDB.Blocks {
   104  			if t.Name.L == blockLowerName {
   105  				return t, EDB
   106  			}
   107  		}
   108  		return nil, EDB
   109  	}
   110  	return nil, nil
   111  }
   112  
   113  // RemoveFromRepairInfo remove the causet from repair info when repaired.
   114  func (r *repairInfo) RemoveFromRepairInfo(schemaLowerName, blockLowerName string) {
   115  	repairedLowerName := schemaLowerName + "." + blockLowerName
   116  	// Remove from the repair list.
   117  	r.Lock()
   118  	defer r.Unlock()
   119  	for i, rt := range r.repairBlockList {
   120  		if strings.ToLower(rt) == repairedLowerName {
   121  			r.repairBlockList = append(r.repairBlockList[:i], r.repairBlockList[i+1:]...)
   122  			break
   123  		}
   124  	}
   125  	// Remove from the repair map.
   126  	for _, EDB := range r.repairDBInfoMap {
   127  		if EDB.Name.L == schemaLowerName {
   128  			for j, t := range EDB.Blocks {
   129  				if t.Name.L == blockLowerName {
   130  					EDB.Blocks = append(EDB.Blocks[:j], EDB.Blocks[j+1:]...)
   131  					break
   132  				}
   133  			}
   134  			if len(EDB.Blocks) == 0 {
   135  				delete(r.repairDBInfoMap, EDB.ID)
   136  			}
   137  			break
   138  		}
   139  	}
   140  	if len(r.repairDBInfoMap) == 0 {
   141  		r.repairMode = false
   142  	}
   143  }
   144  
   145  // repairKeyType is keyType for admin repair causet.
   146  type repairKeyType int
   147  
   148  const (
   149  	// RepairedBlock is the key type, caching the target repaired causet in stochastikCtx.
   150  	RepairedBlock repairKeyType = iota
   151  	// RepairedDatabase is the key type, caching the target repaired database in stochastikCtx.
   152  	RepairedDatabase
   153  )
   154  
   155  func (t repairKeyType) String() (res string) {
   156  	switch t {
   157  	case RepairedBlock:
   158  		res = "RepairedBlock"
   159  	case RepairedDatabase:
   160  		res = "RepairedDatabase"
   161  	}
   162  	return res
   163  }
   164  
   165  func init() {
   166  	RepairInfo = repairInfo{}
   167  	RepairInfo.repairMode = false
   168  	RepairInfo.repairBlockList = []string{}
   169  	RepairInfo.repairDBInfoMap = make(map[int64]*perceptron.DBInfo)
   170  }