Beispiel #1
0
func (d *Discoverd) SetState(state *state.DiscoverdState) error {
	data, err := json.Marshal(state.State)
	if err != nil {
		return err
	}
	meta := &discoverd.ServiceMeta{Index: state.Index, Data: data}
	if state.State.Primary != nil {
		meta.LeaderID = state.State.Primary.ID
	}
	if err := d.service.SetMeta(meta); err != nil {
		return err
	}
	state.Index = meta.Index
	return nil
}
Beispiel #2
0
func (d *discoverdSimulator) SetClusterState(s *state.DiscoverdState, async bool) {
	d.Lock()
	defer d.Unlock()

	if d.state.Index != s.Index {
		panic(fmt.Sprintf("incorrect state index, have %d, want %d", s.Index, d.state.Index))
	}
	d.state.Index++
	d.state.State = s.State.Clone()
	s.Index = d.state.Index
	for _, c := range d.clients {
		if async {
			go c.notifyStateChanged(d._clusterState())
		} else {
			c.notifyStateChanged(d._clusterState())
		}
	}
}
Beispiel #3
0
func (d *Discoverd) SetState(state *state.DiscoverdState) error {
	data, err := json.Marshal(state.State)
	if err != nil {
		return err
	}
	meta := &discoverd.ServiceMeta{Index: state.Index, Data: data}
	if err := d.service.SetMeta(meta); err != nil {
		return err
	}
	state.Index = meta.Index
	if state.State.Primary != nil {
		if err := d.service.SetLeader(state.State.Primary.ID); err != nil {
			d.log.Error("error setting discoverd leader", "id", state.State.Primary.ID, "err", err)
			// TODO(titanous): can we do anything about this?
		}
	}
	return nil
}