Permalink
Browse files

Try without failover...

with secondary requests proxied to primary
  • Loading branch information...
danielfone committed Jan 26, 2014
1 parent a3d560c commit 10be54c6915d1492791bb147a0c9daeb3c27003d
Showing with 21 additions and 6 deletions.
  1. +21 −6 server/server.go
View
@@ -1,6 +1,7 @@
package server
import (
+ "bytes"
"errors"
"fmt"
"github.com/gorilla/mux"
@@ -84,7 +85,7 @@ func (s *Server) ListenAndServe(primary string) error {
continue
}
- s.cluster.PerformFailover()
+ //s.cluster.PerformFailover()
if s.cluster.State() == "primary" {
break
@@ -176,17 +177,31 @@ func (s *Server) joinHandler(w http.ResponseWriter, req *http.Request) {
// a raw string rather than JSON.
func (s *Server) sqlHandler(w http.ResponseWriter, req *http.Request) {
state := s.cluster.State()
- if state != "primary" {
- http.Error(w, "Only the primary can service queries, but this is a "+state, http.StatusBadRequest)
- return
- }
+ primary := s.cluster.primary
+ members := s.cluster.members
- query, err := ioutil.ReadAll(req.Body)
+ query, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Printf("Couldn't read body: %s", err)
http.Error(w, err.Error(), http.StatusBadRequest)
}
+ log.Printf("Cluster state: primary %v, members %v", primary, members)
+
+ if state != "primary" {
+ log.Printf("Attempting to proxy to primary: %v", primary)
+ resp, err := s.client.SafePost(primary.ConnectionString, "/sql", bytes.NewReader(query))
+ if err != nil {
+ http.Error(w, "Couldn't proxy response to primary: " + err.Error(), http.StatusServiceUnavailable)
+ }
+ bytes, err := ioutil.ReadAll(resp)
+ if err != nil {
+ http.Error(w, "Couldn't proxy response to primary: " + err.Error(), http.StatusServiceUnavailable)
+ }
+ w.Write(bytes)
+ return
+ }
+
log.Debugf("[%s] Received query: %#v", s.cluster.State(), string(query))
resp, err := s.execute(query)
if err != nil {

0 comments on commit 10be54c

Please sign in to comment.