MasterCount explain logic around reorder+wraparound

This commit is contained in:
jayunit100 2015-09-24 16:09:33 -04:00 committed by jayvyas
parent 9ae90d2149
commit a2f9957745

View File

@ -212,18 +212,22 @@ func (c *Controller) SetEndpoints(serviceName string, ip net.IP, port int) error
glog.Warningf("Resetting endpoints for master service %q to %v", serviceName, e) glog.Warningf("Resetting endpoints for master service %q to %v", serviceName, e)
return c.EndpointRegistry.UpdateEndpoints(ctx, e) return c.EndpointRegistry.UpdateEndpoints(ctx, e)
} else if !ipCorrect { } else if !ipCorrect {
// We *always* add our own IP address; if there are too many IP // We *always* add our own IP address.
// addresses, we remove the ones lexicographically after our e.Subsets[0].Addresses = append(e.Subsets[0].Addresses, api.EndpointAddress{IP: ip.String()})
// Lexicographic order is retained by this step.
e.Subsets = endpoints.RepackSubsets(e.Subsets)
// If too many IP addresses, remove the ones lexicographically after our
// own IP address. Given the requirements stated at the top of // own IP address. Given the requirements stated at the top of
// this function, this should cause the list of IP addresses to // this function, this should cause the list of IP addresses to
// become eventually correct. // become eventually correct.
e.Subsets[0].Addresses = append(e.Subsets[0].Addresses, api.EndpointAddress{IP: ip.String()})
e.Subsets = endpoints.RepackSubsets(e.Subsets)
if addrs := &e.Subsets[0].Addresses; len(*addrs) > c.MasterCount { if addrs := &e.Subsets[0].Addresses; len(*addrs) > c.MasterCount {
// addrs is a pointer because we're going to mutate it. // addrs is a pointer because we're going to mutate it.
for i, addr := range *addrs { for i, addr := range *addrs {
if addr.IP == ip.String() { if addr.IP == ip.String() {
for len(*addrs) > c.MasterCount { for len(*addrs) > c.MasterCount {
// wrap around if necessary.
remove := (i + 1) % len(*addrs) remove := (i + 1) % len(*addrs)
*addrs = append((*addrs)[:remove], (*addrs)[remove+1:]...) *addrs = append((*addrs)[:remove], (*addrs)[remove+1:]...)
} }