|
| 1 | +/* |
| 2 | +Copyright 2023 The Vitess Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "os" |
| 23 | + "path" |
| 24 | + |
| 25 | + "vitess.io/vitess/go/vt/log" |
| 26 | + vschemapb "vitess.io/vitess/go/vt/proto/vschema" |
| 27 | + vttestpb "vitess.io/vitess/go/vt/proto/vttest" |
| 28 | + "vitess.io/vitess/go/vt/topo" |
| 29 | +) |
| 30 | + |
| 31 | +func startVschemaWatcher(vschemaPersistenceDir string, keyspaces []*vttestpb.Keyspace, ts *topo.Server) { |
| 32 | + // Create the directory if it doesn't exist. |
| 33 | + if err := createDirectoryIfNotExists(vschemaPersistenceDir); err != nil { |
| 34 | + log.Fatalf("Unable to create vschema persistence directory %v: %v", vschemaPersistenceDir, err) |
| 35 | + } |
| 36 | + |
| 37 | + // If there are keyspace files, load them. |
| 38 | + loadKeyspacesFromDir(vschemaPersistenceDir, keyspaces, ts) |
| 39 | + |
| 40 | + // Rebuild the SrvVSchema object in case we loaded vschema from file |
| 41 | + if err := ts.RebuildSrvVSchema(context.Background(), tpb.Cells); err != nil { |
| 42 | + log.Fatalf("RebuildSrvVSchema failed: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + // Now watch for changes in the SrvVSchema object and persist them to disk. |
| 46 | + go watchSrvVSchema(context.Background(), ts, tpb.Cells[0]) |
| 47 | +} |
| 48 | + |
| 49 | +func loadKeyspacesFromDir(dir string, keyspaces []*vttestpb.Keyspace, ts *topo.Server) { |
| 50 | + for _, ks := range tpb.Keyspaces { |
| 51 | + ksFile := path.Join(dir, ks.Name+".json") |
| 52 | + if _, err := os.Stat(ksFile); err == nil { |
| 53 | + jsonData, err := os.ReadFile(ksFile) |
| 54 | + if err != nil { |
| 55 | + log.Fatalf("Unable to read keyspace file %v: %v", ksFile, err) |
| 56 | + } |
| 57 | + |
| 58 | + keyspace := &vschemapb.Keyspace{} |
| 59 | + err = json.Unmarshal(jsonData, keyspace) |
| 60 | + if err != nil { |
| 61 | + log.Fatalf("Unable to parse keyspace file %v: %v", ksFile, err) |
| 62 | + } |
| 63 | + |
| 64 | + ts.SaveVSchema(context.Background(), ks.Name, keyspace) |
| 65 | + log.Infof("Loaded keyspace %v from %v\n", ks.Name, ksFile) |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func watchSrvVSchema(ctx context.Context, ts *topo.Server, cell string) { |
| 71 | + data, ch, err := ts.WatchSrvVSchema(context.Background(), tpb.Cells[0]) |
| 72 | + if err != nil { |
| 73 | + log.Fatalf("WatchSrvVSchema failed: %v", err) |
| 74 | + } |
| 75 | + |
| 76 | + if data.Err != nil { |
| 77 | + log.Fatalf("WatchSrvVSchema could not retrieve initial vschema: %v", data.Err) |
| 78 | + } |
| 79 | + persistNewSrvVSchema(data.Value) |
| 80 | + |
| 81 | + for update := range ch { |
| 82 | + if update.Err != nil { |
| 83 | + log.Errorf("WatchSrvVSchema returned an error: %v", update.Err) |
| 84 | + } else { |
| 85 | + persistNewSrvVSchema(update.Value) |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func persistNewSrvVSchema(srvVSchema *vschemapb.SrvVSchema) { |
| 91 | + for ksName, ks := range srvVSchema.Keyspaces { |
| 92 | + jsonBytes, err := json.MarshalIndent(ks, "", " ") |
| 93 | + if err != nil { |
| 94 | + log.Errorf("Error marshaling keyspace: %v", err) |
| 95 | + continue |
| 96 | + } |
| 97 | + |
| 98 | + err = os.WriteFile(path.Join(*vschemaPersistenceDir, ksName+".json"), jsonBytes, 0644) |
| 99 | + if err != nil { |
| 100 | + log.Errorf("Error writing keyspace file: %v", err) |
| 101 | + } |
| 102 | + log.Infof("Persisted keyspace %v to %v", ksName, *vschemaPersistenceDir) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func createDirectoryIfNotExists(dir string) error { |
| 107 | + if _, err := os.Stat(dir); os.IsNotExist(err) { |
| 108 | + return os.Mkdir(dir, 0755) |
| 109 | + } |
| 110 | + return nil |
| 111 | +} |
0 commit comments