From 69a7de1ad862ce1e88706e3e7e9689ccea9c35ee Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 5 Feb 2014 10:49:05 -0800 Subject: [PATCH] optimize for submission from clustered master nodes --- go/weed/weed_server/master_server.go | 8 ++++++-- go/weed/weed_server/master_server_handlers.go | 6 +++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/go/weed/weed_server/master_server.go b/go/weed/weed_server/master_server.go index e0fa677b9..d1f7914f1 100644 --- a/go/weed/weed_server/master_server.go +++ b/go/weed/weed_server/master_server.go @@ -62,7 +62,7 @@ func NewMasterServer(r *mux.Router, version string, port int, metaFolder string, r.HandleFunc("/vol/grow", ms.proxyToLeader(secure(ms.whiteList, ms.volumeGrowHandler))) r.HandleFunc("/vol/status", ms.proxyToLeader(secure(ms.whiteList, ms.volumeStatusHandler))) r.HandleFunc("/vol/vacuum", ms.proxyToLeader(secure(ms.whiteList, ms.volumeVacuumHandler))) - r.HandleFunc("/submit", ms.proxyToLeader(secure(ms.whiteList, ms.submitFromMasterServerHandler))) + r.HandleFunc("/submit", secure(ms.whiteList, ms.submitFromMasterServerHandler)) r.HandleFunc("/", ms.redirectHandler) ms.topo.StartRefreshWritableVolumes(garbageThreshold) @@ -74,9 +74,13 @@ func (ms *MasterServer) SetRaftServer(raftServer *RaftServer) { ms.raftServer = raftServer } +func (ms *MasterServer) IsLeader() bool { + return ms.raftServer == nil || ms.raftServer.IsLeader() +} + func (ms *MasterServer) proxyToLeader(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - if ms.raftServer == nil || ms.raftServer.IsLeader() { + if ms.IsLeader() { f(w, r) } else { targetUrl, err := url.Parse("http://" + ms.raftServer.Leader()) diff --git a/go/weed/weed_server/master_server_handlers.go b/go/weed/weed_server/master_server_handlers.go index fda341f02..99e8fd1ef 100644 --- a/go/weed/weed_server/master_server_handlers.go +++ b/go/weed/weed_server/master_server_handlers.go @@ -164,5 +164,9 @@ func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) } func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) { - submitForClientHandler(w, r, "localhost:"+strconv.Itoa(ms.port)) + if ms.IsLeader() { + submitForClientHandler(w, r, "localhost:"+strconv.Itoa(ms.port)) + } else { + submitForClientHandler(w, r, ms.raftServer.Leader()) + } }