Пример #1
0
// getMongoSpace updates info with the space that Mongo servers should exist in.
func (w *pgWorker) getMongoSpace(addrs [][]network.Address) (network.SpaceName, error) {
	unset := network.SpaceName("")

	stateInfo, err := w.st.ControllerInfo()
	if err != nil {
		return unset, errors.Annotate(err, "cannot get state server info")
	}

	switch stateInfo.MongoSpaceState {
	case state.MongoSpaceUnknown:
		if !w.providerSupportsSpaces {
			err := w.st.SetMongoSpaceState(state.MongoSpaceUnsupported)
			if err != nil {
				return unset, errors.Annotate(err, "cannot set Mongo space state")
			}
			return unset, nil
		}

		// We want to find a space that contains all Mongo servers so we can
		// use it to look up the IP address of each Mongo server to be used
		// to set up the peer group.
		spaceStats := generateSpaceStats(addrs)
		if spaceStats.LargestSpaceContainsAll == false {
			err := w.st.SetMongoSpaceState(state.MongoSpaceInvalid)
			if err != nil {
				return unset, errors.Annotate(err, "cannot set Mongo space state")
			}
			logger.Warningf("couldn't find a space containing all peer group machines")
			return unset, nil
		} else {
			spaceName, err := w.st.SetOrGetMongoSpaceName(spaceStats.LargestSpace)
			if err != nil {
				return unset, errors.Annotate(err, "error setting/getting Mongo space")
			}
			return spaceName, nil
		}

	case state.MongoSpaceValid:
		space, err := w.st.Space(stateInfo.MongoSpaceName)
		if err != nil {
			return unset, errors.Annotate(err, "looking up space")
		}
		return network.SpaceName(space.Name()), nil
	}

	return unset, nil
}
Пример #2
0
// networkAddress is a convenience helper to return the state type
// as network type, here for Address.
func (addr *address) networkAddress() network.Address {
	return network.Address{
		Value:     addr.Value,
		Type:      network.AddressType(addr.AddressType),
		Scope:     network.Scope(addr.Scope),
		SpaceName: network.SpaceName(addr.SpaceName),
	}
}
Пример #3
0
func hostPortInSpace(address, spaceName string) network.HostPort {
	netAddress := network.Address{
		Value:       address,
		Type:        network.IPv4Address,
		NetworkName: "net",
		Scope:       network.ScopeUnknown,
		SpaceName:   network.SpaceName(spaceName),
	}
	return network.HostPort{netAddress, 4711}
}
Пример #4
0
func (st *fakeState) SetOrGetMongoSpaceName(mongoSpaceName network.SpaceName) (network.SpaceName, error) {
	inf, _ := st.ControllerInfo()
	strMongoSpaceName := string(mongoSpaceName)

	if inf.MongoSpaceState == state.MongoSpaceUnknown {
		inf.MongoSpaceName = strMongoSpaceName
		inf.MongoSpaceState = state.MongoSpaceValid
		st.controllers.Set(inf)
	}
	return network.SpaceName(inf.MongoSpaceName), nil
}
Пример #5
0
// networkHostPort is a convenience helper to return the state type
// as network type, here for HostPort.
func (hp *hostPort) networkHostPort() network.HostPort {
	return network.HostPort{
		Address: network.Address{
			Value:     hp.Value,
			Type:      network.AddressType(hp.AddressType),
			Scope:     network.Scope(hp.Scope),
			SpaceName: network.SpaceName(hp.SpaceName),
		},
		Port: hp.Port,
	}
}
Пример #6
0
// getMongoSpace updates info with the space that Mongo servers should exist in.
func (w *pgWorker) getMongoSpace(info *peerGroupInfo) error {
	stateInfo, err := w.st.ControllerInfo()
	if err != nil {
		return errors.Annotate(err, "cannot get state server info")
	}

	switch stateInfo.MongoSpaceState {
	case state.MongoSpaceUnknown:
		if !w.providerSupportsSpaces {
			err = w.st.SetMongoSpaceState(state.MongoSpaceUnsupported)
			if err != nil {
				return errors.Annotate(err, "cannot set Mongo space state")
			}
			return nil
		}

		// We want to find a space that contains all Mongo servers so we can
		// use it to look up the IP address of each Mongo server to be used
		// to set up the peer group.
		spaceStats := generateSpaceStats(mongoAddresses(info.machines))
		if spaceStats.LargestSpaceContainsAll == false {
			err = w.st.SetMongoSpaceState(state.MongoSpaceInvalid)
			if err != nil {
				return errors.Annotate(err, "cannot set Mongo space state")
			}

			logger.Warningf("couldn't find a space containing all peer group machines")
			return nil
		} else {
			info.mongoSpace, err = w.st.SetOrGetMongoSpaceName(spaceStats.LargestSpace)
			if err != nil {
				return fmt.Errorf("error setting/getting Mongo space: %v", err)
			}
			info.mongoSpaceValid = true
		}

	case state.MongoSpaceValid:
		space, err := w.st.Space(stateInfo.MongoSpaceName)
		if err != nil {
			return fmt.Errorf("error looking up space: %v", err)
		}
		info.mongoSpace = network.SpaceName(space.Name())
		info.mongoSpaceValid = true
	}

	return nil
}