Exemplo n.º 1
0
func TestSetServices(t *testing.T) {
	services := []core.Service{}
	services = append(services, testService2)

	if err := database.SetServices(services); err != nil {
		t.Errorf("Failed to SET services - %v", err)
	}

	if _, err := os.Stat("/tmp/scribbleTest/services/tcp-192_168_0_15-80.json"); !os.IsNotExist(err) {
		t.Errorf("Failed to clear old services on PUT - %v", err)
	}

	service, err := ioutil.ReadFile("/tmp/scribbleTest/services/tcp-192_168_0_16-80.json")
	if err != nil {
		t.Error(err)
	}

	jService, err := toJson(testService2)
	if err != nil {
		t.Error(err)
	}

	if string(service) != string(jService) {
		t.Errorf("Read service differs from written service")
	}
}
Exemplo n.º 2
0
func SetServices(services []core.Service) error {
	// in case of failure
	oldServices, err := database.GetServices()
	if err != nil {
		return err
	}

	// apply services to balancer
	err = balance.SetServices(services)
	if err != nil {
		return err
	}

	if !database.CentralStore {
		// save to backend
		err = database.SetServices(services)
		if err != nil {
			// undo balance action
			if uerr := balance.SetServices(oldServices); uerr != nil {
				err = fmt.Errorf("%v - %v", err.Error(), uerr.Error())
			}
			return err
		}
	}
	return nil
}
Exemplo n.º 3
0
func (n None) SetServices(services []core.Service) error {
	err := common.SetServices(services)
	if err != nil {
		return err
	}
	if database.CentralStore {
		return database.SetServices(services)
	}
	return nil
}
Exemplo n.º 4
0
// SetServices tells all members to replace the services in their database with a new set.
// rolls back on failure
func (r *Redis) SetServices(services []core.Service) error {
	conn := pool.Get()
	defer conn.Close()

	oldServices, err := common.GetServices()
	if err != nil {
		return err
	}

	// publishJson to others
	err = r.publishJson(conn, "set-services", services)
	if err != nil {
		// if i failed to publishJson, request should fail
		return err
	}

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

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

	if database.CentralStore {
		return database.SetServices(services)
	}

	return nil
}