// TODO(q): There should be a on modified callback on the RegionSet
352847
func (r *RegionSet) flush() {
	for i := 1; i < len(r.regions); i++ {
		ov := r.overlaps(r.regions[i], 0, i)
		if len(ov) == 0 {
			continue
		}
		r.merge(ov[0], append(ov[1:], i))
	}
}
60995
func IsValidRegionCode(s string) bool {
	_, err := RegionCodeInfo(s)
	if err != nil {
		return false
	}
	return true
}
1272175
func (h *Helper) GetRegionsInfo() (*RegionsInfo, error) {
	etcd, ok := h.Store.(tikv.EtcdBackend)
	if !ok {
		return nil, errors.WithStack(errors.New("not implemented"))
	}
	pdHosts := etcd.EtcdAddrs()
	if len(pdHosts) == 0 {
		return nil, errors.New("pd unavailable")
	}
	req, err := http.NewRequest("GET", protocol+pdHosts[0]+pdapi.Regions, nil)
	if err != nil {
		return nil, errors.Trace(err)
	}
	timeout, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	resp, err := http.DefaultClient.Do(req.WithContext(timeout))
	defer cancelFunc()
	if err != nil {
		return nil, errors.Trace(err)
	}
	defer func() {
		err = resp.Body.Close()
		if err != nil {
			logutil.Logger(context.Background()).Error("close body failed", zap.Error(err))
		}
	}()
	var regionsInfo RegionsInfo
	err = json.NewDecoder(resp.Body).Decode(&regionsInfo)
	if err != nil {
		return nil, errors.Trace(err)
	}
	return &regionsInfo, nil
}
229378
func (s *RoleUsageType) SetRegion(v string) *RoleUsageType {
	s.Region = &v
	return s
}
78006
func RegionReadTimeout(to time.Duration) Option {
	return func(c *client) {
		c.regionReadTimeout = to
	}
}
420596
func (s *Reservation) SetRegion(v string) *Reservation {
	s.Region = &v
	return s
}
202550
func (c *EC2Metadata) Region() (string, error) {
	resp, err := c.GetMetadata("placement/availability-zone")
	if err != nil {
		return "", err
	}

	if len(resp) == 0 {
		return "", awserr.New("EC2MetadataError", "invalid Region response", nil)
	}

	// returns region without the suffix. Eg: us-west-2a becomes us-west-2
	return resp[:len(resp)-1], nil
}
78442
func (s *Location) SetRegion(v string) *Location {
	s.Region = &v
	return s
}
20984
func (s *ProjectDetails) SetRegion(v string) *ProjectDetails {
	s.Region = &v
	return s
}
1247178
func (c *Client) RegionInfo(regionIdentity string) (*Region, error) {
	var region Region
	return &region, c.Get(&region, "/regions/"+regionIdentity)
}
419475
func (c *RegionsClient) Create(ctx context.Context, createRegion *CreateRegion) (*Region, error) {
	p := "/regions/"

	req, err := c.backend.NewRequest(http.MethodPost, p, nil, createRegion)
	if err != nil {
		return nil, err
	}

	var resp Region
	_, err = c.backend.Do(ctx, req, &resp, func(code int) error {
		switch code {
		case 400, 409, 500:
			return &Error{}
		default:
			return nil
		}
	})
	if err != nil {
		return nil, err
	}

	return &resp, nil
}
668409
func (o *Subnet) SetRegion(v *string) *Subnet {
	if o.Region = v; o.Region == nil {
		o.nullFields = append(o.nullFields, "Region")
	}
	return o
}
858740
func (c *RegionCache) DropRegion(id RegionVerID) {
	c.mu.Lock()
	defer c.mu.Unlock()
	c.dropRegionFromCache(id)
}
971006
func (s *Resource) SetRegion(v string) *Resource {
	s.Region = &v
	return s
}
629066
func (s *TaskScheduledEventDetails) SetRegion(v string) *TaskScheduledEventDetails {
	s.Region = &v
	return s
}
774385
func (c *RegionCache) OnRegionEpochNotMatch(bo *Backoffer, ctx *RPCContext, currentRegions []*metapb.Region) error {
	c.mu.Lock()
	defer c.mu.Unlock()

	c.dropRegionFromCache(ctx.Region)

	// Find whether the region epoch in `ctx` is ahead of TiKV's. If so, backoff.
	for _, meta := range currentRegions {
		if meta.GetId() == ctx.Region.id &&
			(meta.GetRegionEpoch().GetConfVer() < ctx.Region.confVer ||
				meta.GetRegionEpoch().GetVersion() < ctx.Region.ver) {
			err := errors.Errorf("region epoch is ahead of tikv. rpc ctx: %+v, currentRegions: %+v", ctx, currentRegions)
			logutil.Logger(context.Background()).Info("region epoch is ahead of tikv", zap.Error(err))
			return bo.Backoff(BoRegionMiss, err)
		}
	}

	// If the region epoch is not ahead of TiKV's, replace region meta in region cache.
	for _, meta := range currentRegions {
		if _, ok := c.pdClient.(*codecPDClient); ok {
			if err := decodeRegionMetaKey(meta); err != nil {
				return errors.Errorf("newRegion's range key is not encoded: %v, %v", meta, err)
			}
		}
		region := &Region{
			meta: meta,
			peer: meta.Peers[0],
		}
		region.SwitchPeer(ctx.Peer.GetStoreId())
		c.insertRegionToCache(region)
	}
	return nil
}
1129020
func (s *CloneStackInput) SetRegion(v string) *CloneStackInput {
	s.Region = &v
	return s
}