...

Source file src/go.etcd.io/etcd/server/v3/mvcc/backend/verify.go

Documentation: go.etcd.io/etcd/server/v3/mvcc/backend

     1  // Copyright 2022 The etcd Authors
     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 backend
    16  
    17  import (
    18  	"os"
    19  	"runtime/debug"
    20  	"strings"
    21  
    22  	"go.uber.org/zap"
    23  )
    24  
    25  const (
    26  	ENV_VERIFY           = "ETCD_VERIFY"
    27  	ENV_VERIFY_ALL_VALUE = "all"
    28  	ENV_VERIFY_LOCK      = "lock"
    29  )
    30  
    31  func ValidateCalledInsideApply(lg *zap.Logger) {
    32  	if !verifyLockEnabled() {
    33  		return
    34  	}
    35  	if !insideApply() {
    36  		lg.Panic("Called outside of APPLY!", zap.Stack("stacktrace"))
    37  	}
    38  }
    39  
    40  func ValidateCalledOutSideApply(lg *zap.Logger) {
    41  	if !verifyLockEnabled() {
    42  		return
    43  	}
    44  	if insideApply() {
    45  		lg.Panic("Called inside of APPLY!", zap.Stack("stacktrace"))
    46  	}
    47  }
    48  
    49  func ValidateCalledInsideUnittest(lg *zap.Logger) {
    50  	if !verifyLockEnabled() {
    51  		return
    52  	}
    53  	if !insideUnittest() {
    54  		lg.Fatal("Lock called outside of unit test!", zap.Stack("stacktrace"))
    55  	}
    56  }
    57  
    58  func verifyLockEnabled() bool {
    59  	return os.Getenv(ENV_VERIFY) == ENV_VERIFY_ALL_VALUE || os.Getenv(ENV_VERIFY) == ENV_VERIFY_LOCK
    60  }
    61  
    62  func insideApply() bool {
    63  	stackTraceStr := string(debug.Stack())
    64  	return strings.Contains(stackTraceStr, ".applyEntries")
    65  }
    66  
    67  func insideUnittest() bool {
    68  	stackTraceStr := string(debug.Stack())
    69  	return strings.Contains(stackTraceStr, "_test.go") && !strings.Contains(stackTraceStr, "tests/")
    70  }
    71  

View as plain text