github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/tests/br_z_gc_safepoint/gc.go (about) 1 // Copyright 2019 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 // Test backup with exceeding GC safe point. 15 16 package main 17 18 import ( 19 "context" 20 "flag" 21 "time" 22 23 "github.com/pingcap/log" 24 "github.com/tikv/client-go/v2/oracle" 25 pd "github.com/tikv/pd/client" 26 "go.uber.org/zap" 27 ) 28 29 var ( 30 ca = flag.String("ca", "", "CA certificate path for TLS connection") 31 cert = flag.String("cert", "", "certificate path for TLS connection") 32 key = flag.String("key", "", "private key path for TLS connection") 33 pdAddr = flag.String("pd", "", "PD address") 34 gcOffset = flag.Duration("gc-offset", time.Second*10, 35 "Set GC safe point to current time - gc-offset, default: 10s") 36 updateService = flag.Bool("update-service", false, "use new service to update min SafePoint") 37 ) 38 39 func main() { 40 flag.Parse() 41 if *pdAddr == "" { 42 log.Panic("pd address is empty") 43 } 44 if *gcOffset == time.Duration(0) { 45 log.Panic("zero gc-offset is not allowed") 46 } 47 48 timeout := time.Second * 10 49 ctx, cancel := context.WithTimeout(context.Background(), timeout) 50 defer cancel() 51 pdclient, err := pd.NewClientWithContext(ctx, []string{*pdAddr}, pd.SecurityOption{ 52 CAPath: *ca, 53 CertPath: *cert, 54 KeyPath: *key, 55 }) 56 if err != nil { 57 log.Panic("create pd client failed", zap.Error(err)) 58 } 59 p, l, err := pdclient.GetTS(ctx) 60 if err != nil { 61 log.Panic("get ts failed", zap.Error(err)) 62 } 63 now := oracle.ComposeTS(p, l) 64 nowMinusOffset := oracle.GetTimeFromTS(now).Add(-*gcOffset) 65 newSP := oracle.ComposeTS(oracle.GetPhysical(nowMinusOffset), 0) 66 if *updateService { 67 _, err = pdclient.UpdateServiceGCSafePoint(ctx, "br", 300, newSP) 68 if err != nil { 69 log.Panic("update service safe point failed", zap.Error(err)) 70 } 71 log.Info("update service GC safe point", zap.Uint64("SP", newSP), zap.Uint64("now", now)) 72 } else { 73 _, err = pdclient.UpdateGCSafePoint(ctx, newSP) 74 if err != nil { 75 log.Panic("update safe point failed", zap.Error(err)) 76 } 77 log.Info("update GC safe point", zap.Uint64("SP", newSP), zap.Uint64("now", now)) 78 } 79 }