// GetZoneConfig returns the zone config for the object with 'id'. func GetZoneConfig(cfg config.SystemConfig, id uint32) (config.ZoneConfig, bool, error) { // Look in the zones table. if zoneVal := cfg.GetValue(sqlbase.MakeZoneKey(sqlbase.ID(id))); zoneVal != nil { var zone config.ZoneConfig // We're done. return zone, true, zoneVal.GetProto(&zone) } // No zone config for this ID. We need to figure out if it's a database // or table. Lookup its descriptor. if descVal := cfg.GetValue(sqlbase.MakeDescMetadataKey(sqlbase.ID(id))); descVal != nil { // Determine whether this is a database or table. var desc sqlbase.Descriptor if err := descVal.GetProto(&desc); err != nil { return config.ZoneConfig{}, false, err } if tableDesc := desc.GetTable(); tableDesc != nil { // This is a table descriptor. Lookup its parent database zone config. return GetZoneConfig(cfg, uint32(tableDesc.ParentID)) } } // Retrieve the default zone config, but only as long as that wasn't the ID // we were trying to retrieve (avoid infinite recursion). if id != keys.RootNamespaceID { return GetZoneConfig(cfg, keys.RootNamespaceID) } // No descriptor or not a table. return config.ZoneConfig{}, false, nil }
// GetZoneConfig returns the zone config for the object with 'id'. func GetZoneConfig(cfg config.SystemConfig, id uint32) (*config.ZoneConfig, error) { // Look in the zones table. if zoneVal := cfg.GetValue(MakeZoneKey(ID(id))); zoneVal != nil { zone := &config.ZoneConfig{} if err := zoneVal.GetProto(zone); err != nil { return nil, err } // We're done. return zone, nil } // No zone config for this ID. We need to figure out if it's a database // or table. Lookup its descriptor. if descVal := cfg.GetValue(MakeDescMetadataKey(ID(id))); descVal != nil { // Determine whether this is a database or table. desc := &Descriptor{} if err := descVal.GetProto(desc); err != nil { return nil, err } if tableDesc := desc.GetTable(); tableDesc != nil { // This is a table descriptor. Lookup its parent database zone config. return GetZoneConfig(cfg, uint32(tableDesc.ParentID)) } } // No descriptor or not a table. This table/db could have been deleted, just // return the default config. return config.DefaultZoneConfig, nil }
// GetTableDesc returns the table descriptor for the table with 'id'. // Returns nil if the descriptor is not present, or is present but is not a // table. func GetTableDesc(cfg config.SystemConfig, id sqlbase.ID) (*sqlbase.TableDescriptor, error) { if descVal := cfg.GetValue(sqlbase.MakeDescMetadataKey(id)); descVal != nil { desc := &sqlbase.Descriptor{} if err := descVal.GetProto(desc); err != nil { return nil, err } return desc.GetTable(), nil } return nil, nil }
func isDeleted(tableID sqlbase.ID, cfg config.SystemConfig) bool { descKey := sqlbase.MakeDescMetadataKey(tableID) val := cfg.GetValue(descKey) if val == nil { return false } var descriptor sqlbase.Descriptor if err := val.GetProto(&descriptor); err != nil { panic("unable to unmarshal table descriptor") } table := descriptor.GetTable() return table.Deleted() }
func expectDescriptor(systemConfig config.SystemConfig, idKey roachpb.Key, desc *Descriptor) error { descValue := systemConfig.GetValue(idKey) if descValue == nil { return errStaleMetadata } var cachedDesc Descriptor if err := descValue.GetProto(&cachedDesc); err != nil { return err } if !proto.Equal(&cachedDesc, desc) { return errStaleMetadata } return nil }
func expectDescriptorID(systemConfig config.SystemConfig, idKey roachpb.Key, id ID) error { idValue := systemConfig.GetValue(idKey) if idValue == nil { return errStaleMetadata } cachedID, err := idValue.GetInt() if err != nil { return err } if ID(cachedID) != id { return errStaleMetadata } return nil }
func waitForConfigChange(t *testing.T, s *server.TestServer) (*config.SystemConfig, error) { var foundDesc sql.DatabaseDescriptor var cfg *config.SystemConfig return cfg, util.IsTrueWithin(func() bool { if cfg = s.Gossip().GetSystemConfig(); cfg != nil { if val := cfg.GetValue(configDescKey); val != nil { if err := val.GetProto(&foundDesc); err != nil { t.Fatal(err) } return foundDesc.ID == configID } } return false }, 10*time.Second) }
func TestGet(t *testing.T) { defer leaktest.AfterTest(t) emptyKeys := []proto.KeyValue{} someKeys := []proto.KeyValue{ plainKV("a", "vala"), plainKV("c", "valc"), plainKV("d", "vald"), } testCases := []struct { values []proto.KeyValue key string found bool value string }{ {emptyKeys, "a", false, ""}, {emptyKeys, "b", false, ""}, {emptyKeys, "c", false, ""}, {emptyKeys, "d", false, ""}, {emptyKeys, "e", false, ""}, {someKeys, "", false, ""}, {someKeys, "b", false, ""}, {someKeys, "e", false, ""}, {someKeys, "a0", false, ""}, {someKeys, "a", true, "vala"}, {someKeys, "c", true, "valc"}, {someKeys, "d", true, "vald"}, } cfg := config.SystemConfig{} for tcNum, tc := range testCases { cfg.Values = tc.values val, found := cfg.GetValue([]byte(tc.key)) if found != tc.found { t.Errorf("#%d: expected found=%t", tcNum, tc.found) continue } if string(val) != tc.value { t.Errorf("#%d: expected value=%s, found %s", tcNum, tc.value, string(val)) } } }
// isRenamed tests if a descriptor is updated by gossip to the specified name // and version. func isRenamed( tableID sqlbase.ID, expectedName string, expectedVersion sqlbase.DescriptorVersion, cfg config.SystemConfig, ) bool { descKey := sqlbase.MakeDescMetadataKey(tableID) val := cfg.GetValue(descKey) if val == nil { return false } var descriptor sqlbase.Descriptor if err := val.GetProto(&descriptor); err != nil { panic("unable to unmarshal table descriptor") } table := descriptor.GetTable() return table.Name == expectedName && table.Version == expectedVersion }
func TestGet(t *testing.T) { defer leaktest.AfterTest(t) emptyKeys := []roachpb.KeyValue{} someKeys := []roachpb.KeyValue{ plainKV("a", "vala"), plainKV("c", "valc"), plainKV("d", "vald"), } aVal := roachpb.MakeValueFromString("vala") bVal := roachpb.MakeValueFromString("valc") cVal := roachpb.MakeValueFromString("vald") testCases := []struct { values []roachpb.KeyValue key string value *roachpb.Value }{ {emptyKeys, "a", nil}, {emptyKeys, "b", nil}, {emptyKeys, "c", nil}, {emptyKeys, "d", nil}, {emptyKeys, "e", nil}, {someKeys, "", nil}, {someKeys, "b", nil}, {someKeys, "e", nil}, {someKeys, "a0", nil}, {someKeys, "a", &aVal}, {someKeys, "c", &bVal}, {someKeys, "d", &cVal}, } cfg := config.SystemConfig{} for tcNum, tc := range testCases { cfg.Values = tc.values if val := cfg.GetValue([]byte(tc.key)); !proto.Equal(val, tc.value) { t.Errorf("#%d: expected=%s, found=%s", tcNum, tc.value, val) } } }
func waitForConfigChange(t *testing.T, s *testServer) *config.SystemConfig { var foundDesc sql.Descriptor var cfg *config.SystemConfig util.SucceedsSoon(t, func() error { if cfg = s.Gossip().GetSystemConfig(); cfg != nil { if val := cfg.GetValue(configDescKey); val != nil { if err := val.GetProto(&foundDesc); err != nil { t.Fatal(err) } if id := foundDesc.GetDatabase().GetID(); id != configID { return util.Errorf("expected database id %d; got %d", configID, id) } return nil } } return util.Errorf("got nil system config") }) return cfg }
// GetZoneConfig returns the zone config for the object with 'id'. func GetZoneConfig(cfg *config.SystemConfig, id uint32) (*config.ZoneConfig, error) { // Look in the zones table. if val, ok := cfg.GetValue(MakeZoneKey(ID(id))); ok { zone := &config.ZoneConfig{} if err := proto.Unmarshal(val, zone); err != nil { return nil, err } // We're done. return zone, nil } // No zone config for this ID. We need to figure out if it's a database // or table. Lookup its descriptor. rawDesc, ok := cfg.GetValue(MakeDescMetadataKey(ID(id))) if !ok { // No descriptor. This table/db could have been deleted, // just return the default config. return config.DefaultZoneConfig, nil } // Determine whether this is a database or table. // TODO(marc): we need a better way of doing this. Options include: // - add a type field on the descriptor table // - separate descriptor tables for databases and tables // - prebuild list of databases and tables in the system config var dbDesc DatabaseDescriptor if err := proto.Unmarshal(rawDesc, &dbDesc); err == nil { // parses as a database: return default config. return config.DefaultZoneConfig, nil } var tableDesc TableDescriptor if err := proto.Unmarshal(rawDesc, &tableDesc); err != nil { // does not parse as a table either: this means an entry in the // descriptor table we're not familiar with. return nil, util.Errorf("descriptor for object ID %d is not a table or database", id) } // This is a table descriptor. Lookup its parent database zone config. return GetZoneConfig(cfg, uint32(tableDesc.ParentID)) }
func expectDeleted(systemConfig config.SystemConfig, key roachpb.Key) error { if systemConfig.GetValue(key) != nil { return errStaleMetadata } return nil }