github.com/bartle-stripe/trillian@v1.2.1/testonly/hammer/mapreplay/main.go (about) 1 // Copyright 2018 Google Inc. All Rights Reserved. 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 // mapreplay replays a log of Trillian Map requests. 16 package main 17 18 import ( 19 "context" 20 "flag" 21 "os" 22 "regexp" 23 "strconv" 24 "strings" 25 26 "github.com/golang/glog" 27 "github.com/google/trillian" 28 "github.com/google/trillian/testonly/hammer" 29 "google.golang.org/grpc" 30 ) 31 32 var ( 33 mapIDs = flag.String("map_ids", "", "Comma-separated list of mapID:mapID pairs to convert") 34 rpcServer = flag.String("rpc_server", "", "Server address:port; leave blank for dry-run mode") 35 replayFrom = flag.String("replay_from", "", "File to record operations in") 36 ) 37 38 func main() { 39 flag.Parse() 40 defer glog.Flush() 41 ctx := context.Background() 42 43 if *replayFrom == "" { 44 glog.Exit("Need --replay_from option") 45 } 46 f, err := os.Open(*replayFrom) 47 if err != nil { 48 glog.Exitf("Failed to open replay file: %v", err) 49 } 50 51 var cl trillian.TrillianMapClient 52 if *rpcServer != "" { 53 c, err := grpc.Dial(*rpcServer, grpc.WithInsecure()) 54 if err != nil { 55 glog.Exitf("Failed to create map client conn: %v", err) 56 } 57 cl = trillian.NewTrillianMapClient(c) 58 } 59 60 pairRE := regexp.MustCompile(`(\d+):(\d+)`) 61 mapmap := make(map[int64]int64) 62 mIDs := strings.Split(*mapIDs, ",") 63 for _, pair := range mIDs { 64 if pair == "" { 65 continue 66 } 67 results := pairRE.FindStringSubmatch(pair) 68 if len(results) < 3 { 69 glog.Exitf("Malformed mapID mapping in %q", *mapIDs) 70 } 71 from, err := strconv.ParseInt(results[1], 10, 64) 72 if err != nil { 73 glog.Exitf("Malformed mapID mapping in %q", *mapIDs) 74 } 75 to, err := strconv.ParseInt(results[2], 10, 64) 76 if err != nil { 77 glog.Exitf("Malformed mapID mapping in %q", *mapIDs) 78 } 79 mapmap[from] = to 80 } 81 hammer.ReplayFile(ctx, f, cl, mapmap) 82 }