github.com/XiaoMi/Gaea@v1.2.5/cc/proxy/proxy.go (about) 1 // Copyright 2019 The Gaea Authors. 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 package proxy 16 17 import ( 18 "fmt" 19 "time" 20 21 "github.com/XiaoMi/Gaea/log" 22 "github.com/XiaoMi/Gaea/models" 23 ) 24 25 // Stats proxy stats 26 type Stats struct { 27 Host string `json:"host"` 28 Closed bool `json:"closed"` 29 Error string `json:"error"` 30 UnixTime int64 `json:"unixtime"` 31 Timeout bool `json:"timeout"` 32 } 33 34 // SQLFingerprint sql指纹 35 type SQLFingerprint struct { 36 SlowSQL map[string]string `json:"slow_sql"` 37 ErrorSQL map[string]string `json:"error_sql"` 38 } 39 40 // GetStats return proxy status 41 func GetStats(p *models.ProxyMonitorMetric, cfg *models.CCConfig, timeout time.Duration) *Stats { 42 fmt.Println(string(p.Encode())) 43 var ch = make(chan struct{}) 44 var host = p.IP + ":" + p.AdminPort 45 fmt.Println(host) 46 stats := &Stats{} 47 48 go func(host string) { 49 defer close(ch) 50 stats.Host = host 51 _, err := newProxyClient(host, cfg.ProxyUserName, cfg.ProxyPassword) 52 if err != nil { 53 stats.Error = err.Error() 54 stats.Closed = true 55 } else { 56 stats.Closed = false 57 } 58 }(host) 59 60 select { 61 case <-ch: 62 return stats 63 case <-time.After(timeout): 64 return &Stats{Host: host, Timeout: true} 65 } 66 } 67 68 func newProxyClient(host, user, password string) (*APIClient, error) { 69 log.Debug("call rpc xping to proxy %s", host) 70 c := NewAPIClient(host, user, password) 71 if err := c.Ping(); err != nil { 72 log.Fatal("call rpc xping to proxy failed") 73 return c, err 74 } 75 log.Debug("call rpc xping OK") 76 77 return c, nil 78 } 79 80 // PrepareConfig prepare phase of config change 81 func PrepareConfig(host, name string, cfg *models.CCConfig) error { 82 c, err := newProxyClient(host, cfg.ProxyUserName, cfg.ProxyPassword) 83 if err != nil { 84 log.Fatal("create proxy client failed, %v", err) 85 return err 86 } 87 88 err = c.PrepareConfig(name) 89 if err != nil { 90 log.Fatal("prepare proxy config failed, %v", err) 91 return err 92 } 93 return nil 94 } 95 96 // CommitConfig commit phase of config change 97 func CommitConfig(host, name string, cfg *models.CCConfig) error { 98 c, err := newProxyClient(host, cfg.ProxyUserName, cfg.ProxyPassword) 99 if err != nil { 100 log.Fatal("create proxy client failed, %v", err) 101 return err 102 } 103 err = c.CommitConfig(name) 104 if err != nil { 105 log.Fatal("commit proxy config failed, %v", err) 106 return err 107 } 108 return nil 109 } 110 111 // DelNamespace delete namespace 112 func DelNamespace(host, name string, cfg *models.CCConfig) error { 113 c, err := newProxyClient(host, cfg.ProxyUserName, cfg.ProxyPassword) 114 if err != nil { 115 log.Fatal("create proxy client failed, %v", err) 116 return err 117 } 118 err = c.DelNamespace(name) 119 if err != nil { 120 log.Warn("delete schema %s in proxy %s failed, %s", name, host, err.Error()) 121 return err 122 } 123 return nil 124 } 125 126 // QueryNamespaceSQLFingerprint return sql fingerprint 127 func QueryNamespaceSQLFingerprint(host, name string, cfg *models.CCConfig) (*SQLFingerprint, error) { 128 c, err := newProxyClient(host, cfg.ProxyUserName, cfg.ProxyPassword) 129 if err != nil { 130 log.Fatal("create proxy client failed, %v", err) 131 return nil, err 132 } 133 134 ret, err := c.GetNamespaceSQLFingerprint(name) 135 return ret, err 136 } 137 138 // QueryProxyConfigFingerprint return config fingerprint of proxy 139 func QueryProxyConfigFingerprint(host string, cfg *models.CCConfig) (string, error) { 140 c, err := newProxyClient(host, cfg.ProxyUserName, cfg.ProxyPassword) 141 if err != nil { 142 return "", err 143 } 144 configFingerprint, err := c.proxyConfigFingerprint() 145 return configFingerprint, err 146 }