// reestablishRegion will continually attempt to reestablish a connection to a // given region func (c *Client) reestablishRegion(reg *regioninfo.Info) { // The meta client is not kept in the region client cache. if reg != c.metaRegionInfo { // This region is inaccessible, and a new client will be created, so the // client will be removed from the region client cache. c.clients.del(reg) } for { log.WithFields(log.Fields{ "Table": reg.Table, "RegionName": reg.RegionName, "StartKey": reg.StartKey, "StopKey": reg.StopKey, }).Warn("Attempting to re-establish region.") // A new context is created here because this is not specific to any // request that the user of gohbase initiated, and is instead an // internal goroutine that may be servicing any number of requests // initiated by the user. ctx, _ := context.WithTimeout(context.Background(), regionLookupTimeout) var err error if reg == c.metaRegionInfo { // If we're looking for the meta region.. err = c.locateMeta(ctx) // .. look it up in ZooKeeper. } else { // Otherwise do a normal meta lookup. _, _, err = c.locateRegion(ctx, reg.Table, reg.StartKey) } if err == nil { reg.MarkAvailable() return } // TODO: Make this configurable, or verify that it's a sane number time.Sleep(time.Millisecond * 100) } }
func (c *Client) establishRegion(originalReg *regioninfo.Info, host string, port uint16) { var err error reg := originalReg backoff := backoffStart for { ctx, _ := context.WithTimeout(context.Background(), regionLookupTimeout) if port != 0 && err == nil { // If this isn't the admin or meta region, check if a client // for this host/port already exists if c.clientType != AdminClient && reg != c.metaRegionInfo { client := c.clients.checkForClient(host, port) if client != nil { // There's already a client, add it to the // cache and mark the new region as available. c.clients.put(reg, client) originalReg.MarkAvailable() return } } // Make this channel buffered so that if we time out we don't // block the newRegion goroutine forever. ch := make(chan newRegResult, 1) var clientType region.ClientType if c.clientType == StandardClient { clientType = region.RegionClient } else { clientType = region.MasterClient } go newRegion(ctx, ch, clientType, host, port, c.rpcQueueSize, c.flushInterval) select { case res := <-ch: if res.Err == nil { if c.clientType == AdminClient { c.adminClient = res.Client } else if reg == c.metaRegionInfo { c.metaClient = res.Client } else { c.clients.put(reg, res.Client) if reg != originalReg { // Here `reg' is guaranteed to be available, so we // must publish the region->client mapping first, // because as soon as we add it to the key->region // mapping here, concurrent readers are gonna want // to find the client. c.regions.put(reg.RegionName, reg) } } originalReg.MarkAvailable() return } else { err = res.Err } case <-ctx.Done(): err = ErrDeadline } } if err != nil { if err == TableNotFound { c.regions.del(originalReg.RegionName) originalReg.MarkAvailable() return } // This will be hit if either there was an error locating the // region, or the region was located but there was an error // connecting to it. backoff, err = sleepAndIncreaseBackoff(ctx, backoff) if err != nil { continue } } if c.clientType == AdminClient { host, port, err = c.zkLookup(ctx, zk.Master) } else if reg == c.metaRegionInfo { host, port, err = c.zkLookup(ctx, zk.Meta) } else { reg, host, port, err = c.locateRegion(ctx, originalReg.Table, originalReg.StartKey) } } }