-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vtctld: flag to proxy vttablet URLs #6058
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
Copyright 2020 The Vitess Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package vtctld | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httputil" | ||
"net/url" | ||
"strings" | ||
"sync" | ||
|
||
"vitess.io/vitess/go/vt/log" | ||
) | ||
|
||
var ( | ||
proxyTablets = flag.Bool("proxy_tablets", false, "Setting this true will make vtctld proxy the tablet status instead of redirecting to them") | ||
|
||
proxyMu sync.Mutex | ||
remotes = make(map[string]*httputil.ReverseProxy) | ||
) | ||
|
||
func addRemote(tabletID, path string) { | ||
u, err := url.Parse(path) | ||
if err != nil { | ||
log.Errorf("Error parsing URL %v: %v", path, err) | ||
return | ||
} | ||
|
||
proxyMu.Lock() | ||
defer proxyMu.Unlock() | ||
if _, ok := remotes[tabletID]; ok { | ||
return | ||
} | ||
|
||
rp := &httputil.ReverseProxy{} | ||
rp.Director = func(req *http.Request) { | ||
splits := strings.SplitN(req.URL.Path, "/", 4) | ||
if len(splits) < 4 { | ||
return | ||
} | ||
req.URL.Scheme = u.Scheme | ||
req.URL.Host = u.Host | ||
req.URL.Path = "/" + splits[3] | ||
} | ||
|
||
prefixPath := fmt.Sprintf("/vttablet/%s/", tabletID) | ||
|
||
rp.ModifyResponse = func(r *http.Response) error { | ||
b, _ := ioutil.ReadAll(r.Body) | ||
b = bytes.ReplaceAll(b, []byte(`href="/`), []byte(fmt.Sprintf(`href="%s`, prefixPath))) | ||
b = bytes.ReplaceAll(b, []byte(`href=/`), []byte(fmt.Sprintf(`href=%s`, prefixPath))) | ||
r.Body = ioutil.NopCloser(bytes.NewBuffer(b)) | ||
r.Header["Content-Length"] = []string{fmt.Sprint(len(b))} | ||
// Don't forget redirects | ||
loc := r.Header["Location"] | ||
for i, v := range loc { | ||
loc[i] = strings.Replace(v, "/", prefixPath, 1) | ||
} | ||
return nil | ||
} | ||
http.Handle(prefixPath, rp) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be more natural to respond to tablets coming/going and changing addresses if there were only one handler, which looks at a cached lookup table of tablet aliases to addresses. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is what I started out with. The problem with the unified approach is that there is no easy way to associate a response to a request, which is needed for rewriting URLs to match the tablet id. I looked at making the vttablets send an extra header identifying the tablet id. But the one-proxy-per-tablet felt much simpler. What I'll instead do is replace the per-tablet proxy every time. I looked at the code. It's just metdata (no goroutines etc). So, it should be fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see. Couldn't you still have a single handler though? If reverse proxy objects are cheap to make, you can just make a new one for each request you handle and use it once. Then you don't have handlers slowly leaking in cases when tablet IDs go away. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, the page that renders the tablets is the one that causes the map entry to be created. Thinking about it again, there's no real need for that page to be controlling the redirects. We could directly |
||
remotes[tabletID] = rp | ||
} |
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean the proxy will stop working for a given tablet after its address changes?