Beispiel #1
0
func (this *Election) doSaveConfig(config *thispb.Configuration) {
	config.CommitteeList = append([]string{}, this.committee...)
	config.MajoritySize = proto.Int32(int32(this.MajoritySize()))
	if this.InCommittee() {
		config.InCommittee = proto.Bool(true)
	}
}
Beispiel #2
0
// Configure initializes the election instance with the committee members.
func (this *Election) Configure(agents []string) error {
	lock, errLock := this.ctlr.LockAll()
	if errLock != nil {
		return errLock
	}
	defer lock.Unlock()

	if this.MajoritySize() > 0 {
		this.Errorf("election instance is already configured")
		return errs.ErrInvalid
	}

	// TODO: Check for duplicates.

	committee := make([]string, len(agents))
	copy(committee, agents)
	sort.Sort(sort.StringSlice(committee))
	majoritySize := len(committee)/2 + 1

	self := this.msn.UID()

	inCommittee := false
	for _, member := range committee {
		if member == self {
			inCommittee = true
			break
		}
	}

	// Save configuration in the wal.
	config := thispb.Configuration{}
	config.CommitteeList = committee
	config.MajoritySize = proto.Int32(int32(majoritySize))
	config.InCommittee = proto.Bool(inCommittee)
	return this.doUpdateConfig(&config)
}