Example #1
0
func (c *Client) sendRPCToRegion(rpc hrpc.Call, reg *regioninfo.Info) (proto.Message, error) {
	// On the first sendRPC to the meta or admin regions, a goroutine must be
	// manually kicked off for the meta or admin region client
	if c.adminClient == nil && reg == c.adminRegionInfo && !c.adminRegionInfo.IsUnavailable() ||
		c.metaClient == nil && reg == c.metaRegionInfo && !c.metaRegionInfo.IsUnavailable() {
		c.regionsLock.Lock()
		if reg == c.metaRegionInfo && !c.metaRegionInfo.IsUnavailable() ||
			reg == c.adminRegionInfo && !c.adminRegionInfo.IsUnavailable() {
			reg.MarkUnavailable()
			go c.reestablishRegion(reg)
		}
		c.regionsLock.Unlock()
	}
	// The region was in the cache, check
	// if the region is marked as available
	if !reg.IsUnavailable() {
		// The region is available

		rpc.SetRegion(reg)

		// Queue the RPC to be sent to the region
		client := c.clientFor(reg)
		var err error
		if client == nil {
			err = errors.New("no client for this region")
		} else {
			err = client.QueueRPC(rpc)
		}

		if err != nil {
			// There was an error queueing the RPC.
			// Mark the region as unavailable.
			first := reg.MarkUnavailable()
			// If this was the first goroutine to mark the region as
			// unavailable, start a goroutine to reestablish a connection
			if first {
				go c.reestablishRegion(reg)
			}
			// Block until the region becomes available.
			return c.waitOnRegion(rpc, reg)
		}

		// Wait for the response
		var res hrpc.RPCResult
		select {
		case res = <-rpc.GetResultChan():
		case <-rpc.GetContext().Done():
			return nil, ErrDeadline
		}

		// Check for errors
		if _, ok := res.Error.(region.RetryableError); ok {
			// There's an error specific to this region, but
			// our region client is fine. Mark this region as
			// unavailable (as opposed to all regions sharing
			// the client), and start a goroutine to reestablish
			// it.
			first := reg.MarkUnavailable()
			if first {
				go c.reestablishRegion(reg)
			}
			if reg != c.metaRegionInfo && reg != c.adminRegionInfo {
				// The client won't be in the cache if this is the
				// meta or admin region
				c.clients.del(reg)
			}
			return c.waitOnRegion(rpc, reg)
		} else if _, ok := res.Error.(region.UnrecoverableError); ok {
			// If it was an unrecoverable error, the region client is
			// considered dead.
			if reg == c.metaRegionInfo || reg == c.adminRegionInfo {
				// If this is the admin client or the meta table, mark the
				// region as unavailable and start up a goroutine to
				// reconnect if it wasn't already marked as such.
				first := reg.MarkUnavailable()
				if first {
					go c.reestablishRegion(reg)
				}
			} else {
				// Else this is a normal region. Mark all the regions
				// sharing this region's client as unavailable, and start
				// a goroutine to reconnect for each of them.
				downregions := c.clients.clientDown(reg)
				for _, downreg := range downregions {
					go c.reestablishRegion(downreg)
				}
			}

			// Fall through to the case of the region being unavailable,
			// which will result in blocking until it's available again.
			return c.waitOnRegion(rpc, reg)
		} else {
			// RPC was successfully sent, or an unknown type of error
			// occurred. In either case, return the results.
			return res.Msg, res.Error
		}
	}
	return c.waitOnRegion(rpc, reg)
}
Example #2
0
// sendRPC takes an RPC call, and will send it to the correct region server. If
// the correct region server is offline or otherwise unavailable, sendRPC will
// continually retry until the deadline set on the RPC's context is exceeded.
func (c *Client) sendRPC(rpc hrpc.Call) (proto.Message, error) {
	log.WithFields(log.Fields{
		"Type":  rpc.GetName(),
		"Table": string(rpc.Table()),
		"Key":   string(rpc.Key()),
	}).Debug("Sending RPC")
	err := c.queueRPC(rpc)
	if err == ErrDeadline {
		return nil, err
	} else if err != nil {
		log.WithFields(log.Fields{
			"Type":  rpc.GetName(),
			"Table": string(rpc.Table()),
			"Key":   string(rpc.Key()),
		}).Debug("We hit an error queuing the RPC. Resending.")
		// There was an error locating the region for the RPC, or the client
		// for the region encountered an error and has shut down.
		return c.sendRPC(rpc)
	}
	if err == nil {
		var res hrpc.RPCResult
		resch := rpc.GetResultChan()

		select {
		case res = <-resch:
		case <-rpc.GetContext().Done():
			return nil, ErrDeadline
		}

		err := res.Error
		log.WithFields(log.Fields{
			"Type":   rpc.GetName(),
			"Table":  string(rpc.Table()),
			"Key":    string(rpc.Key()),
			"Result": res.Msg,
			"Error":  err,
		}).Debug("Successfully sent RPC. Returning.")

		if _, ok := err.(region.RetryableError); ok {
			return c.sendRPC(rpc)
		} else if _, ok := err.(region.UnrecoverableError); ok {
			// Prevents dropping into the else block below,
			// error handling happens a few lines down
		} else {
			return res.Msg, res.Error
		}
	}

	// There was an issue related to the network, so we're going to mark the
	// region as unavailable, and generate the channel used for announcing
	// when it's available again
	region := rpc.GetRegion()

	log.WithFields(log.Fields{
		"Type":  rpc.GetName(),
		"Table": string(rpc.Table()),
		"Key":   string(rpc.Key()),
	}).Debug("Encountered a network error. Region unavailable?")

	if region != nil {
		succ := region.MarkUnavailable()
		if succ {
			go c.reestablishRegion(region)
		}
	}
	log.WithFields(log.Fields{
		"Type":  rpc.GetName(),
		"Table": string(rpc.Table()),
		"Key":   string(rpc.Key()),
	}).Debug("Retrying sendRPC")
	return c.sendRPC(rpc)
}