func SetServers(svcId string, servers []core.Server) error { // in case of failure oldService, err := database.GetService(svcId) if err != nil { return err } oldServers := oldService.Servers // implement in balancer err = balance.SetServers(svcId, servers) if err != nil { return err } if !database.CentralStore { // add to backend err = database.SetServers(svcId, servers) if err != nil { // undo balance action if uerr := balance.SetServers(svcId, oldServers); uerr != nil { err = fmt.Errorf("%v - %v", err.Error(), uerr.Error()) } return err } } return nil }
func (n None) SetServers(svcId string, servers []core.Server) error { err := common.SetServers(svcId, servers) if err != nil { return err } if database.CentralStore { return database.SetServers(svcId, servers) } return nil }
// SetServers tells all members to replace a service's servers with a new set. // rolls back on failure func (r *Redis) SetServers(svcId string, servers []core.Server) error { conn := pool.Get() defer conn.Close() service, err := common.GetService(svcId) if err != nil { return NoServiceError } oldServers := service.Servers // publishStringJson to others err = r.publishStringJson(conn, "set-servers", svcId, servers) if err != nil { return err } actionHash := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("set-servers %s %s", servers, svcId)))) // ensure all members applied action err = r.waitForMembers(conn, actionHash) if err != nil { uActionHash := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("set-servers %s %s", oldServers, svcId)))) // cleanup rollback cruft. clear actionHash ensures no mistakes on re-submit defer conn.Do("DEL", uActionHash, actionHash) // attempt rollback - no need to waitForMembers here uerr := r.publishStringJson(conn, "set-servers", svcId, oldServers) if uerr != nil { err = fmt.Errorf("%v - %v", err, uerr) } return err } if database.CentralStore { return database.SetServers(svcId, servers) } return nil }
func TestSetServers(t *testing.T) { servers := []core.Server{} servers = append(servers, testServer2) if err := database.SetServers(testService1.Id, servers); err != nil { t.Errorf("Failed to SET servers - %v", err) } service, err := ioutil.ReadFile("/tmp/scribbleTest/services/tcp-192_168_0_15-80.json") if err != nil { t.Error(err) } svc := testService1 svc.Servers = append(svc.Servers, testServer1) jService, err := toJson(svc) if err != nil { t.Error(err) } if string(service) == string(jService) { t.Errorf("Failed to clear old servers on PUT") } }