本文整理汇总了Golang中stripe-ctf/com/sqlcluster/log.Printf函数的典型用法代码示例。如果您正苦于以下问题:Golang Printf函数的具体用法?Golang Printf怎么用?Golang Printf使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Printf函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: sqlHandler
// This is the only user-facing function, and accordingly the body is
// 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
}
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.Debugf("[%s] Received query: %#v", s.cluster.State(), string(query))
resp, err := s.execute(query)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
r := &Replicate{
Self: s.cluster.self,
Query: query,
}
for _, member := range s.cluster.members {
b := util.JSONEncode(r)
_, err := s.client.SafePost(member.ConnectionString, "/replicate", b)
if err != nil {
log.Printf("Couldn't replicate query to %v: %s", member, err)
}
}
log.Debugf("[%s] Returning response to %#v: %#v", s.cluster.State(), string(query), string(resp))
w.Write(resp)
}
示例2: forwardQueryToLeader
func (s *Server) forwardQueryToLeader(query *sql.Query) {
if s.raftServer.Leader() == s.name {
// I am the leader, don't send it
return
}
cs, err := s.GetLeaderCS()
if err != nil {
//log.Printf("Can't find leader to forward query")
// Can't find leader
return
}
var buffer bytes.Buffer
compress := gzip.NewWriter(&buffer)
err = EncodeObjToResponse(compress, query)
compress.Close()
if err != nil {
log.Printf("Can't encode object to forward")
}
_, err = s.client.SafePost(cs, "/forward", "application/json", &buffer)
if err != nil {
log.Printf("Failed to forward query")
}
}
示例3: sqlHandler
// This is the only user-facing function, and accordingly the body is
// a raw string rather than JSON.
func (s *Server) sqlHandler(w http.ResponseWriter, req *http.Request) {
query, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Printf("Couldn't read body: %s", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Debugf("[%s] Received query: %#v", s.raftServer.State(), string(query))
// Execute the command against the Raft server.
leader := s.raftServer.Leader()
for leader == "" {
time.Sleep(50 * time.Millisecond)
leader = s.raftServer.Leader()
}
if s.name != leader {
my_partial_name := strings.TrimSuffix(strings.TrimPrefix(s.name, ".-"), ".sock")
leader_partial_name := strings.TrimSuffix(strings.TrimPrefix(leader, ".-"), ".sock")
redirect_url := "http://" + strings.Replace(req.Host, my_partial_name, leader_partial_name, -1) + "/forward?query=" + encodeQuery(query)
log.Printf("Redirecting to %s", redirect_url)
http.Redirect(w, req, redirect_url, 302)
return
}
resp, err := s.raftServer.Do(NewSqlCommand(string(query)))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
resp_frd := resp.([]byte)
log.Debugf("[%s] Returning response to %#v: %#v", s.raftServer.State(), string(query), string(resp_frd))
w.Write(resp_frd)
}
示例4: Join
// Join an existing cluster
func (s *Server) Join(primary string) error {
command := &raft.DefaultJoinCommand{
Name: s.raftServer.Name(),
ConnectionString: s.connectionString(),
}
var b bytes.Buffer
json.NewEncoder(&b).Encode(command)
cs, err := transport.Encode(primary)
if err != nil {
return err
}
log.Printf("Server %v with cs=%v is trying to join %v on %s/join", s.raftServer.Name(), s.connectionString(), primary, cs)
for {
_, err := s.client.SafePost(cs, "/join", &b)
if err != nil {
log.Printf("Unable to join cluster: %s", err)
time.Sleep(1 * time.Second)
continue
}
return nil
}
}
示例5: sqlHandler
// This is the only user-facing function, and accordingly the body is
// a raw string rather than JSON.
func (s *Server) sqlHandler(w http.ResponseWriter, req *http.Request) {
if s.block {
time.Sleep(1000000 * time.Second)
}
query, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Printf("Couldn't read body: %s", err)
http.Error(w, err.Error(), http.StatusBadRequest)
}
if s.leader != s.listen {
cs, errLeader := transport.Encode(s.leader)
if errLeader != nil {
http.Error(w, "Only the primary can service queries, but this is a secondary", http.StatusBadRequest)
log.Printf("Leader ain't present?: %s", errLeader)
return
}
//_, errLeaderHealthCheck := s.client.SafeGet(cs, "/healthcheck")
//if errLeaderHealthCheck != nil {
// http.Error(w, "Primary is down", http.StatusBadRequest)
// return
//}
body, errLResp := s.client.SafePost(cs, "/sql", bytes.NewBufferString(string(query)))
if errLResp != nil {
s.block = true
http.Error(w, "Can't forward request to primary, gotta block now", http.StatusBadRequest)
return
// log.Printf("Didn't get reply from leader: %s", errLResp)
}
formatted := fmt.Sprintf("%s", body)
resp := []byte(formatted)
w.Write(resp)
return
} else {
log.Debugf("Primary Received query: %#v", string(query))
resp, err := s.execute(query)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
w.Write(resp)
return
}
}
示例6: forwardHandler
func (s *Server) forwardHandler(w http.ResponseWriter, req *http.Request) {
log.Printf("############################# forward: Initiating forward loop")
h, ok := w.(http.Hijacker)
if !ok {
log.Printf("########################## forward: Failed to hijack connection for forwarding. Aborting")
return
}
netConn, rw, err := h.Hijack()
if err != nil {
log.Printf("######################### forward: Failed to hijack connection for forwarding: %v Aborting", err)
return
}
defer netConn.Close()
reader := json.NewDecoder(rw)
writer := json.NewEncoder(rw)
log.Printf("############################ forward: Forward loop initiated")
for {
cmd := <-s.execCommand
log.Printf("############ Received command to forward: %v", cmd.Data)
err := writer.Encode(&cmd.Data)
if err != nil {
log.Printf("######################### forward: Writer cannot encode command: %v Stopping loop", err)
return
}
reply := &raft.ExecuteCommandReply{}
err = reader.Decode(reply)
if err != nil {
log.Printf("############################ forward: Reader cannot decode command reply: %v Stopping loop", err)
return
}
log.Printf("############# Received reply via forward: %v", reply)
cmd.Reply <- reply
}
}
示例7: PerformFailover
func (c *Cluster) PerformFailover() {
state := c.State()
if state != "secondary" {
log.Fatalf("Trying to fail over even though my state is %s", state)
}
c.primary = c.members[0]
c.members = c.members[1:]
if c.State() == "primary" {
log.Printf("I am the the primary now.")
} else {
log.Printf("Promoted %s to primary. My time will come one day.", c.primary.Name)
}
}
示例8: joinHandler
// Server handlers
func (s *Server) joinHandler(w http.ResponseWriter, req *http.Request) {
command := &raft.DefaultJoinCommand{}
if err := json.NewDecoder(req.Body).Decode(&command); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Printf("Handling join request: %#v", command)
if _, err := s.raftServer.Do(command); err != nil {
log.Printf("Server was unable to join %v with err=%v", command, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
示例9: healthCheckPeer
func (s *Server) healthCheckPeer(peer *raft.Peer) {
for {
delay := HEALTH_CHECK_DELAY
if s.raftServer.Leader() == s.name {
delay = LEADER_HEALTH_CHECK_DELAY
}
// Endlessly
response, err := s.client.SafeGet(peer.ConnectionString, "/healthcheck")
if err != nil {
continue
}
healthResponse := &HealthCheckResponse{}
// if err := json.NewDecoder(response).Decode(&healthResponse); err != nil {
// log.Fatal(err)
// return
// }
err = DecodeResponseToObj(response, &healthResponse)
if err != nil {
log.Printf("healthCheckPeer Error: %v", err.Error())
continue
}
s.mergeQueue(healthResponse.Outstanding)
time.Sleep(delay)
}
}
示例10: Join
// Join an existing cluster
func (s *Server) Join(primary string) error {
join := &Join{Self: s.cluster.self}
b := util.JSONEncode(join)
cs, err := transport.Encode(primary)
if err != nil {
return err
}
for {
body, err := s.client.SafePost(cs, "/join", b)
if err != nil {
log.Printf("Unable to join cluster: %s", err)
time.Sleep(1 * time.Second)
continue
}
resp := &JoinResponse{}
if err = util.JSONDecode(body, &resp); err != nil {
return err
}
s.cluster.Join(resp.Self, resp.Members)
return nil
}
}
示例11: sqlHandler
// This is the only user-facing function, and accordingly the body is
// a raw string rather than JSON.
func (s *Server) sqlHandler(w http.ResponseWriter, req *http.Request) {
state := s.cluster.State()
query, err := ioutil.ReadAll(req.Body)
if state != "primary" {
log.Printf("I AM NOT THE PRIMARY, NEED TO FORWARD THIS REQUEST TO MASTER")
//cs, err := transport.Encode(primary)
response, err := s.client.SafePost(s.cluster.primary.ConnectionString, "/sql", bytes.NewReader(query))
if response == nil || err != nil {
http.Error(w, "", http.StatusBadRequest)
return
} else {
r, _ := ioutil.ReadAll(response)
w.Write(r)
}
//http.Error(w, "Only the primary can service queries, but this is a "+state, http.StatusBadRequest)
return
}
if err != nil {
log.Printf("Couldn't read body: %s", err)
http.Error(w, "", http.StatusBadRequest)
}
log.Debugf("[%s] Received query: %#v", s.cluster.State(), string(query))
resp, err := s.execute(query)
if err != nil {
http.Error(w, "", http.StatusBadRequest)
}
//r := &Replicate{
//Self: s.cluster.self,
//Query: query,
//}
//for _, member := range s.cluster.members {
//b := util.JSONEncode(r)
//_, err := s.client.SafePost(member.ConnectionString, "/replicate", b)
//if err != nil {
//log.Printf("Couldn't replicate query to %v: %s", member, err)
//}
//}
log.Debugf("[%s] Returning response to %#v: %#v", s.cluster.State(), string(query), string(resp))
w.Write(resp)
}
示例12: joinHandler
// Server handlers
func (s *Server) joinHandler(w http.ResponseWriter, req *http.Request) {
log.Printf("handling /join %v", req)
j := &raft.DefaultJoinCommand{}
if err := util.JSONDecode(req.Body, j); err != nil {
log.Printf("Invalid join request: %s", err)
return
}
go func() {
log.Printf("Handling join request: %#v", j)
// Add node to the cluster
if _, err := s.raftServer.Do(j); err != nil {
return
}
}()
}
示例13: sqlHandler
// This is the only user-facing function, and accordingly the body is
// a raw string rather than JSON.
func (s *Server) sqlHandler(w http.ResponseWriter, req *http.Request) {
query, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Printf("Couldn't read body: %s", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Printf("[%s]Received query: %#v", string(s.raftServer.State()), string(query))
if s.raftServer.State() != "leader" {
if s.raftServer.Leader() == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
leader, _ := transport.Encode(s.raftServer.Leader())
log.Printf("Relaying query to leader: %v", s.raftServer.Leader())
relayResp, err := s.client.SafePost(leader, "/sql", bytes.NewReader(query))
if err != nil {
http.Error(w, "Only the primary can service queries, relaying failed", http.StatusBadRequest)
return
}
//buf := new(bytes.Buffer)
//buf.ReadFrom(relayResp)
w.Write(relayResp.(*bytes.Buffer).Bytes())
return
}
// Execute the command against the Raft server.
resp, err := s.raftServer.Do(command.NewWriteCommand(string(query)))
if err != nil {
log.Printf("Current leader: %#v resp=%v err=%v", string(s.raftServer.Leader()), resp, err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if formatted, ok := resp.(string); ok {
log.Printf("Returning response to %#v: %#v", string(query), resp)
w.Write([]byte(formatted))
} else {
w.WriteHeader(http.StatusBadRequest)
}
}
示例14: healthcheckPrimary
func (s *Server) healthcheckPrimary() bool {
_, err := s.client.SafeGet(s.cluster.primary.ConnectionString, "/healthcheck")
if err != nil {
log.Printf("The primary appears to be down: %s", err)
return false
} else {
return true
}
}
示例15: healthcheckHandler
func (s *Server) healthcheckHandler(w http.ResponseWriter, req *http.Request) {
response := &HealthCheckResponse{
Outstanding: s.outstanding.GetAll(),
}
err := EncodeObjToResponse(w, response)
if err != nil {
log.Printf("healthCheckHandler ERror : %v", err.Error())
}
}