github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/petri/schema_checker.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 petri 15 16 import ( 17 "sync/atomic" 18 "time" 19 20 "github.com/whtcorpsinc/milevadb/metrics" 21 "github.com/whtcorpsinc/milevadb/causetstore/einsteindb" 22 ) 23 24 // SchemaChecker is used for checking schemaReplicant-validity. 25 type SchemaChecker struct { 26 SchemaValidator 27 schemaVer int64 28 relatedBlockIDs []int64 29 } 30 31 type intSchemaVer int64 32 33 func (i intSchemaVer) SchemaMetaVersion() int64 { 34 return int64(i) 35 } 36 37 var ( 38 // SchemaOutOfDateRetryInterval is the backoff time before retrying. 39 SchemaOutOfDateRetryInterval = int64(500 * time.Millisecond) 40 // SchemaOutOfDateRetryTimes is the max retry count when the schemaReplicant is out of date. 41 SchemaOutOfDateRetryTimes = int32(10) 42 ) 43 44 // NewSchemaChecker creates a new schemaReplicant checker. 45 func NewSchemaChecker(do *Petri, schemaVer int64, relatedBlockIDs []int64) *SchemaChecker { 46 return &SchemaChecker{ 47 SchemaValidator: do.SchemaValidator, 48 schemaVer: schemaVer, 49 relatedBlockIDs: relatedBlockIDs, 50 } 51 } 52 53 // Check checks the validity of the schemaReplicant version. 54 func (s *SchemaChecker) Check(txnTS uint64) (*einsteindb.RelatedSchemaChange, error) { 55 return s.CheckBySchemaVer(txnTS, intSchemaVer(s.schemaVer)) 56 } 57 58 // CheckBySchemaVer checks if the schemaReplicant version valid or not at txnTS. 59 func (s *SchemaChecker) CheckBySchemaVer(txnTS uint64, startSchemaVer einsteindb.SchemaVer) (*einsteindb.RelatedSchemaChange, error) { 60 schemaOutOfDateRetryInterval := atomic.LoadInt64(&SchemaOutOfDateRetryInterval) 61 schemaOutOfDateRetryTimes := int(atomic.LoadInt32(&SchemaOutOfDateRetryTimes)) 62 for i := 0; i < schemaOutOfDateRetryTimes; i++ { 63 relatedChange, CheckResult := s.SchemaValidator.Check(txnTS, startSchemaVer.SchemaMetaVersion(), s.relatedBlockIDs) 64 switch CheckResult { 65 case ResultSucc: 66 return nil, nil 67 case ResultFail: 68 metrics.SchemaLeaseErrorCounter.WithLabelValues("changed").Inc() 69 return relatedChange, ErrSchemaReplicantChanged 70 case ResultUnknown: 71 time.Sleep(time.Duration(schemaOutOfDateRetryInterval)) 72 } 73 74 } 75 metrics.SchemaLeaseErrorCounter.WithLabelValues("outdated").Inc() 76 return nil, ErrSchemaReplicantExpired 77 }