Ejemplo n.º 1
0
// Locates the region in which the given row key for the given table is.
func (c *client) locateRegion(ctx context.Context,
	table, key []byte) (hrpc.RegionInfo, string, uint16, error) {

	metaKey := createRegionSearchKey(table, key)
	rpc, err := hrpc.NewGetBefore(ctx, metaTableName, metaKey, hrpc.Families(infoFamily))
	if err != nil {
		return nil, "", 0, err
	}
	rpc.SetRegion(c.metaRegionInfo)
	resp, err := c.sendRPC(rpc)

	if err != nil {
		ch := c.metaRegionInfo.AvailabilityChan()
		if ch != nil {
			select {
			case <-ch:
				return c.locateRegion(ctx, table, key)
			case <-rpc.Context().Done():
				return nil, "", 0, ErrDeadline
			}
		} else {
			return nil, "", 0, err
		}
	}

	metaRow := resp.(*pb.GetResponse)
	if metaRow.Result == nil {
		return nil, "", 0, TableNotFound
	}

	reg, host, port, err := region.ParseRegionInfo(metaRow)
	if err != nil {
		return nil, "", 0, err
	}
	if !bytes.Equal(table, reg.Table()) {
		// This would indicate a bug in HBase.
		return nil, "", 0, fmt.Errorf("WTF: Meta returned an entry for the wrong table!"+
			"  Looked up table=%q key=%q got region=%s", table, key, reg)
	} else if len(reg.StopKey()) != 0 &&
		bytes.Compare(key, reg.StopKey()) >= 0 {
		// This would indicate a hole in the meta table.
		return nil, "", 0, fmt.Errorf("WTF: Meta returned an entry for the wrong region!"+
			"  Looked up table=%q key=%q got region=%s", table, key, reg)
	}
	return reg, host, port, nil
}
Ejemplo n.º 2
0
func TestRegionDiscovery(t *testing.T) {
	client := newClient("~invalid.quorum~") // We shouldn't connect to ZK.

	reg := client.getRegionFromCache([]byte("test"), []byte("theKey"))
	if reg != nil {
		t.Errorf("Found region %#v even though the cache was empty?!", reg)
	}

	// Inject a "test" table with a single region that covers the entire key
	// space (both the start and stop keys are empty).
	family := []byte("info")
	metaRow := &pb.GetResponse{
		Result: &pb.Result{Cell: []*pb.Cell{
			&pb.Cell{
				Row:       []byte("test,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
				Family:    family,
				Qualifier: []byte("regioninfo"),
				Value: []byte("PBUF\b\xc4\xcd\xe9\x99\xe0)\x12\x0f\n\adefault\x12\x04test" +
					"\x1a\x00\"\x00(\x000\x008\x00"),
			},
			&pb.Cell{
				Row:       []byte("test,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
				Family:    family,
				Qualifier: []byte("seqnumDuringOpen"),
				Value:     []byte("\x00\x00\x00\x00\x00\x00\x00\x02"),
			},
			&pb.Cell{
				Row:       []byte("test,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
				Family:    family,
				Qualifier: []byte("server"),
				Value:     []byte("localhost:50966"),
			},
			&pb.Cell{
				Row:       []byte("test,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
				Family:    family,
				Qualifier: []byte("serverstartcode"),
				Value:     []byte("\x00\x00\x01N\x02\x92R\xb1"),
			},
		}}}

	reg, _, _, err := region.ParseRegionInfo(metaRow)
	if err != nil {
		t.Fatalf("Failed to discover region: %s", err)
	}
	client.regions.put(reg)

	reg = client.getRegionFromCache([]byte("test"), []byte("theKey"))
	if reg == nil {
		t.Fatal("Region not found even though we injected it in the cache.")
	}
	expected := region.NewInfo(
		[]byte("test"),
		[]byte("test,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
		nil,
		nil,
	)
	if !bytes.Equal(reg.Table(), expected.Table()) ||
		!bytes.Equal(reg.Name(), expected.Name()) ||
		!bytes.Equal(reg.StartKey(), expected.StartKey()) ||
		!bytes.Equal(reg.StopKey(), expected.StopKey()) {
		t.Errorf("Found region %#v \nbut expected %#v", reg, expected)
	}

	reg = client.getRegionFromCache([]byte("notfound"), []byte("theKey"))
	if reg != nil {
		t.Errorf("Found region %#v even though this table doesn't exist", reg)
	}
}