Example #1
0
// SetServer tells all members to add the server to their database.
// rolls back on failure
func (r *Redis) SetServer(svcId string, server *core.Server) error {
	conn := pool.Get()
	defer conn.Close()

	// publishStringJson to others
	err := r.publishStringJson(conn, "set-server", svcId, server)
	if err != nil {
		return err
	}

	actionHash := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("set-server %s %s", *server, svcId))))

	// ensure all members applied action
	err = r.waitForMembers(conn, actionHash)
	if err != nil {
		uActionHash := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("delete-server %s %s", server.Id, 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, "delete-server", server.Id, svcId)
		if uerr != nil {
			err = fmt.Errorf("%v - %v", err, uerr)
		}
		return err
	}

	if database.CentralStore {
		return database.SetServer(svcId, server)
	}

	return nil
}
Example #2
0
func SetServer(svcId string, server *core.Server) error {
	// // idempotent additions (don't update server on post)
	// if srv, _ := database.GetServer(svcId, server.Id); srv != nil {
	//  return nil
	// }

	// apply to balancer
	err := balance.SetServer(svcId, server)
	if err != nil {
		return err
	}

	if !database.CentralStore {
		// save to backend
		err = database.SetServer(svcId, server)
		if err != nil {
			// undo balance action
			if uerr := balance.DeleteServer(svcId, server.Id); uerr != nil {
				err = fmt.Errorf("%v - %v", err.Error(), uerr.Error())
			}
			return err
		}
	}
	return nil
}
Example #3
0
func (n None) SetServer(svcId string, server *core.Server) error {
	err := common.SetServer(svcId, server)
	if err != nil {
		return err
	}
	if database.CentralStore {
		return database.SetServer(svcId, server)
	}
	return nil
}
Example #4
0
func TestSetServer(t *testing.T) {
	database.SetService(&testService1)
	if err := database.SetServer(testService1.Id, &testServer1); err != nil {
		t.Errorf("Failed to SET server - %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("Read service differs from written service")
	}
}