本文整理匯總了Golang中github.com/cockroachdb/cockroach/pkg/sql/sqlbase.GetTableDescriptor函數的典型用法代碼示例。如果您正苦於以下問題:Golang GetTableDescriptor函數的具體用法?Golang GetTableDescriptor怎麽用?Golang GetTableDescriptor使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了GetTableDescriptor函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestDropIndexInterleaved
func TestDropIndexInterleaved(t *testing.T) {
defer leaktest.AfterTest(t)()
const chunkSize = 200
params, _ := createTestServerParams()
params.Knobs = base.TestingKnobs{
SQLSchemaChanger: &sql.SchemaChangerTestingKnobs{
BackfillChunkSize: chunkSize,
},
}
s, sqlDB, kvDB := serverutils.StartServer(t, params)
defer s.Stopper().Stop()
numRows := 2*chunkSize + 1
createKVInterleavedTable(t, sqlDB, numRows)
tableDesc := sqlbase.GetTableDescriptor(kvDB, "t", "kv")
tablePrefix := roachpb.Key(keys.MakeTablePrefix(uint32(tableDesc.ID)))
checkKeyCount(t, kvDB, tablePrefix, 3*numRows)
if _, err := sqlDB.Exec(`DROP INDEX [email protected]_idx`); err != nil {
t.Fatal(err)
}
checkKeyCount(t, kvDB, tablePrefix, 2*numRows)
// Ensure that index is not active.
tableDesc = sqlbase.GetTableDescriptor(kvDB, "t", "intlv")
if _, _, err := tableDesc.FindIndexByName("intlv_idx"); err == nil {
t.Fatalf("table descriptor still contains index after index is dropped")
}
}
示例2: TestTableNameNotCaseSensitive
// Test that table names are not treated as case sensitive by the name cache.
func TestTableNameNotCaseSensitive(t *testing.T) {
defer leaktest.AfterTest(t)()
s, db, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop()
leaseManager := s.LeaseManager().(*LeaseManager)
if _, err := db.Exec(`
CREATE DATABASE t;
CREATE TABLE t.test (k CHAR PRIMARY KEY, v CHAR);
`); err != nil {
t.Fatal(err)
}
// Populate the name cache.
if _, err := db.Exec("SELECT * FROM t.test;"); err != nil {
t.Fatal(err)
}
tableDesc := sqlbase.GetTableDescriptor(kvDB, "t", "test")
// Check that we can get the table by a different name.
lease := leaseManager.tableNames.get(tableDesc.ParentID, "tEsT", s.Clock())
if lease == nil {
t.Fatalf("no name cache entry")
}
if err := leaseManager.Release(lease); err != nil {
t.Fatal(err)
}
}
示例3: TestDropTableInterleaved
// TestDropTableInterleaved tests dropping a table that is interleaved within
// another table.
func TestDropTableInterleaved(t *testing.T) {
defer leaktest.AfterTest(t)()
params, _ := createTestServerParams()
s, sqlDB, kvDB := serverutils.StartServer(t, params)
defer s.Stopper().Stop()
numRows := 2*sql.TableTruncateChunkSize + 1
createKVInterleavedTable(t, sqlDB, numRows)
tableDesc := sqlbase.GetTableDescriptor(kvDB, "t", "kv")
tablePrefix := roachpb.Key(keys.MakeTablePrefix(uint32(tableDesc.ID)))
checkKeyCount(t, kvDB, tablePrefix, 3*numRows)
if _, err := sqlDB.Exec(`DROP TABLE t.intlv`); err != nil {
t.Fatal(err)
}
checkKeyCount(t, kvDB, tablePrefix, numRows)
// Test that deleted table cannot be used. This prevents regressions where
// name -> descriptor ID caches might make this statement erronously work.
if _, err := sqlDB.Exec(`SELECT * FROM t.intlv`); !testutils.IsError(
err, `table "t.intlv" does not exist`,
) {
t.Fatalf("different error than expected: %v", err)
}
}
示例4: TestPGWireConnectionCloseReleasesLeases
// Test that abruptly closing a pgwire connection releases all leases held by
// that session.
func TestPGWireConnectionCloseReleasesLeases(t *testing.T) {
defer leaktest.AfterTest(t)()
s, _, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop()
url, cleanupConn := sqlutils.PGUrl(t, s.ServingAddr(), "SetupServer", url.User(security.RootUser))
defer cleanupConn()
conn, err := pq.Open(url.String())
if err != nil {
t.Fatal(err)
}
ex := conn.(driver.Execer)
if _, err := ex.Exec("CREATE DATABASE test", nil); err != nil {
t.Fatal(err)
}
if _, err := ex.Exec("CREATE TABLE test.t (i INT PRIMARY KEY)", nil); err != nil {
t.Fatal(err)
}
// Start a txn so leases are accumulated by queries.
if _, err := ex.Exec("BEGIN", nil); err != nil {
t.Fatal(err)
}
// Get a table lease.
if _, err := ex.Exec("SELECT * FROM test.t", nil); err != nil {
t.Fatal(err)
}
// Abruptly close the connection.
if err := conn.Close(); err != nil {
t.Fatal(err)
}
// Verify that there are no leases held.
tableDesc := sqlbase.GetTableDescriptor(kvDB, "test", "t")
lm := s.LeaseManager().(*LeaseManager)
// Looking for a table state validates that there used to be a lease on the
// table.
ts := lm.findTableState(tableDesc.ID, false /* create */)
if ts == nil {
t.Fatal("table state not found")
}
ts.mu.Lock()
leases := ts.active.data
ts.mu.Unlock()
if len(leases) != 1 {
t.Fatalf("expected one lease, found: %d", len(leases))
}
// Wait for the lease to be released.
util.SucceedsSoon(t, func() error {
ts.mu.Lock()
refcount := ts.active.data[0].refcount
ts.mu.Unlock()
if refcount != 0 {
return errors.Errorf(
"expected lease to be unused, found refcount: %d", refcount)
}
return nil
})
}
示例5: TestCantLeaseDeletedTable
// Test that we fail to lease a table that was marked for deletion.
func TestCantLeaseDeletedTable(testingT *testing.T) {
defer leaktest.AfterTest(testingT)()
var mu syncutil.Mutex
clearSchemaChangers := false
params, _ := createTestServerParams()
params.Knobs = base.TestingKnobs{
SQLSchemaChanger: &csql.SchemaChangerTestingKnobs{
SyncFilter: func(tscc csql.TestingSchemaChangerCollection) {
mu.Lock()
defer mu.Unlock()
if clearSchemaChangers {
tscc.ClearSchemaChangers()
}
},
AsyncExecNotification: asyncSchemaChangerDisabled,
},
}
t := newLeaseTest(testingT, params)
defer t.cleanup()
sql := `
CREATE DATABASE test;
CREATE TABLE test.t(a INT PRIMARY KEY);
`
_, err := t.db.Exec(sql)
if err != nil {
t.Fatal(err)
}
// Block schema changers so that the table we're about to DROP is not actually
// dropped; it will be left in a "deleted" state.
mu.Lock()
clearSchemaChangers = true
mu.Unlock()
// DROP the table
_, err = t.db.Exec(`DROP TABLE test.t`)
if err != nil {
t.Fatal(err)
}
// Make sure we can't get a lease on the descriptor.
tableDesc := sqlbase.GetTableDescriptor(t.kvDB, "test", "t")
// try to acquire at a bogus version to make sure we don't get back a lease we
// already had.
_, err = t.acquire(1, tableDesc.ID, tableDesc.Version+1)
if !testutils.IsError(err, "table is being dropped") {
t.Fatalf("got a different error than expected: %v", err)
}
}
示例6: TestDropIndex
func TestDropIndex(t *testing.T) {
defer leaktest.AfterTest(t)()
const chunkSize = 200
params, _ := createTestServerParams()
params.Knobs = base.TestingKnobs{
SQLSchemaChanger: &sql.SchemaChangerTestingKnobs{
BackfillChunkSize: chunkSize,
},
}
s, sqlDB, kvDB := serverutils.StartServer(t, params)
defer s.Stopper().Stop()
numRows := 2*chunkSize + 1
createKVTable(t, sqlDB, numRows)
tableDesc := sqlbase.GetTableDescriptor(kvDB, "t", "kv")
status, i, err := tableDesc.FindIndexByName("foo")
if err != nil {
t.Fatal(err)
}
if status != sqlbase.DescriptorActive {
t.Fatal("Index 'foo' is not active.")
}
indexPrefix := roachpb.Key(sqlbase.MakeIndexKeyPrefix(tableDesc, tableDesc.Indexes[i].ID))
checkKeyCount(t, kvDB, indexPrefix, numRows)
if _, err := sqlDB.Exec(`DROP INDEX [email protected]`); err != nil {
t.Fatal(err)
}
checkKeyCount(t, kvDB, indexPrefix, 0)
tableDesc = sqlbase.GetTableDescriptor(kvDB, "t", "kv")
if _, _, err := tableDesc.FindIndexByName("foo"); err == nil {
t.Fatalf("table descriptor still contains index after index is dropped")
}
}
示例7: setupRanges
func setupRanges(
db *gosql.DB, s *server.TestServer, cdb *client.DB, t *testing.T,
) ([]roachpb.RangeDescriptor, *sqlbase.TableDescriptor) {
if _, err := db.Exec(`CREATE DATABASE t`); err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`CREATE TABLE test (k INT PRIMARY KEY)`); err != nil {
t.Fatal(err)
}
values := []int{0, 10, 20}
for _, val := range values {
// Multiply by 10 to space out the values so we can easily construct keys
// that fall within the range.
if _, err := db.Exec("INSERT INTO test VALUES ($1)", val*10); err != nil {
t.Fatal(err)
}
}
tableDesc := sqlbase.GetTableDescriptor(cdb, "t", "test")
// Split every SQL row to its own range.
rowRanges := make([]roachpb.RangeDescriptor, len(values))
for i, val := range values {
var err error
var l roachpb.RangeDescriptor
l, rowRanges[i], err = splitRangeAtVal(s, tableDesc, val)
if err != nil {
t.Fatal(err)
}
if i > 0 {
rowRanges[i-1] = l
}
}
// TODO(andrei): The sleep below serves to remove the noise that the
// RangeCache might encounter, clobbering descriptors with old versions.
// Remove once all the causes of such clobbering, listed in #10751, have been
// fixed.
time.Sleep(300 * time.Millisecond)
// Run a select across the whole table to populate the caches with all the
// ranges.
if _, err := db.Exec(`SELECT COUNT(1) from test`); err != nil {
t.Fatal(err)
}
return rowRanges, tableDesc
}
示例8: TestAddingFKs
// TestAddingFKs checks the behavior of a table in the non-public `ADD` state.
// Being non-public, it should not be visible to clients, and is therefore
// assumed to be empty (e.g. by foreign key checks), since no one could have
// written to it yet.
func TestAddingFKs(t *testing.T) {
defer leaktest.AfterTest(t)()
params, _ := createTestServerParams()
s, sqlDB, kvDB := serverutils.StartServer(t, params)
defer s.Stopper().Stop()
if _, err := sqlDB.Exec(`
CREATE DATABASE t;
CREATE TABLE t.products (id INT PRIMARY KEY);
INSERT INTO t.products VALUES (1), (2);
CREATE TABLE t.orders (id INT PRIMARY KEY, product INT REFERENCES t.products, INDEX (product));
`); err != nil {
t.Fatal(err)
}
// Step the referencing table back to the ADD state.
ordersDesc := sqlbase.GetTableDescriptor(kvDB, "t", "orders")
ordersDesc.State = sqlbase.TableDescriptor_ADD
ordersDesc.Version++
if err := kvDB.Put(
context.TODO(),
sqlbase.MakeDescMetadataKey(ordersDesc.ID),
sqlbase.WrapDescriptor(ordersDesc),
); err != nil {
t.Fatal(err)
}
// Generally a referenced table needs to lookup referencing tables to check
// FKs during delete operations, but referencing tables in the ADD state are
// given special treatment.
if _, err := sqlDB.Exec(`DELETE FROM t.products`); err != nil {
t.Fatal(err)
}
// Client should not see the orders table.
if _, err := sqlDB.Exec(
`SELECT * FROM t.orders`,
); !testutils.IsError(err, "table is being added") {
t.Fatal(err)
}
}
示例9: verifyTables
// verifyTables ensures that the correct number of tables were created and that
// they all correspond to individual table descriptor IDs in the correct range
// of values.
func verifyTables(
t *testing.T,
tc *testcluster.TestCluster,
completed chan int,
expectedNumOfTables int,
descIDStart int64,
) {
descIDEnd := descIDStart + int64(expectedNumOfTables)
usedTableIDs := make(map[sqlbase.ID]string)
var count int
for id := range completed {
count++
tableName := fmt.Sprintf("table_%d", id)
kvDB := tc.Servers[count%tc.NumServers()].KVClient().(*client.DB)
tableDesc := sqlbase.GetTableDescriptor(kvDB, "test", tableName)
if int64(tableDesc.ID) < descIDStart || int64(tableDesc.ID) >= descIDEnd {
t.Fatalf(
"table %s's ID %d is not within the expected range of %d to %d",
tableName,
tableDesc.ID,
descIDStart,
descIDEnd,
)
}
usedTableIDs[tableDesc.ID] = tableName
}
if e, a := expectedNumOfTables, len(usedTableIDs); e != a {
t.Fatalf("expected %d tables created, only got %d", e, a)
}
kvDB := tc.Servers[count%tc.NumServers()].KVClient().(*client.DB)
if descID, err := kvDB.Get(context.Background(), keys.DescIDGenerator); err != nil {
t.Fatal(err)
} else {
if e, a := descIDEnd, descID.ValueInt(); e != a {
t.Fatalf("expected next descriptor ID to be %d, got %d", e, a)
}
}
}
示例10: TestNameCacheEntryDoesntReturnExpiredLease
// Tests that a name cache entry with by an expired lease is not returned.
func TestNameCacheEntryDoesntReturnExpiredLease(t *testing.T) {
defer leaktest.AfterTest(t)()
s, db, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop()
leaseManager := s.LeaseManager().(*LeaseManager)
const tableName = "test"
if _, err := db.Exec(fmt.Sprintf(`
CREATE DATABASE t;
CREATE TABLE t.%s (k CHAR PRIMARY KEY, v CHAR);
`, tableName)); err != nil {
t.Fatal(err)
}
// Populate the name cache.
if _, err := db.Exec("SELECT * FROM t.test;"); err != nil {
t.Fatal(err)
}
tableDesc := sqlbase.GetTableDescriptor(kvDB, "t", tableName)
// Check the assumptions this tests makes: that there is a cache entry
// (with a valid lease).
if lease := leaseManager.tableNames.get(tableDesc.ParentID, tableName, s.Clock()); lease == nil {
t.Fatalf("name cache has no unexpired entry for (%d, %s)", tableDesc.ParentID, tableName)
} else {
if err := leaseManager.Release(lease); err != nil {
t.Fatal(err)
}
}
leaseManager.ExpireLeases(s.Clock())
// Check the name no longer resolves.
if lease := leaseManager.tableNames.get(tableDesc.ParentID, tableName, s.Clock()); lease != nil {
t.Fatalf("name cache has unexpired entry for (%d, %s): %s", tableDesc.ParentID, tableName, lease)
}
}
示例11: TestAcquireFreshestFromStoreRaces
// TestAcquireFreshestFromStoreRaces runs
// LeaseManager.acquireFreshestFromStore() in parallel to test for races.
func TestAcquireFreshestFromStoreRaces(t *testing.T) {
defer leaktest.AfterTest(t)()
s, db, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop()
leaseManager := s.LeaseManager().(*LeaseManager)
if _, err := db.Exec(`
CREATE DATABASE t;
CREATE TABLE t.test (k CHAR PRIMARY KEY, v CHAR);
`); err != nil {
t.Fatal(err)
}
tableDesc := sqlbase.GetTableDescriptor(kvDB, "t", "test")
var wg sync.WaitGroup
numRoutines := 10
wg.Add(numRoutines)
for i := 0; i < numRoutines; i++ {
go func() {
defer wg.Done()
err := kvDB.Txn(context.TODO(), func(txn *client.Txn) error {
lease, err := leaseManager.acquireFreshestFromStore(txn, tableDesc.ID)
if err != nil {
return err
}
if err := leaseManager.Release(lease); err != nil {
return err
}
return nil
})
if err != nil {
t.Error(err)
}
}()
}
wg.Wait()
}
示例12: TestRaceWithBackfill
// Test schema change backfills are not affected by various operations
// that run simultaneously.
func TestRaceWithBackfill(t *testing.T) {
defer leaktest.AfterTest(t)()
var backfillNotification chan bool
params, _ := createTestServerParams()
// Disable asynchronous schema change execution to allow synchronous path
// to trigger start of backfill notification.
params.Knobs = base.TestingKnobs{
SQLSchemaChanger: &csql.SchemaChangerTestingKnobs{
RunBeforeBackfillChunk: func(sp roachpb.Span) error {
if backfillNotification != nil {
// Close channel to notify that the backfill has started.
close(backfillNotification)
backfillNotification = nil
}
return nil
},
AsyncExecNotification: asyncSchemaChangerDisabled,
},
}
server, sqlDB, kvDB := serverutils.StartServer(t, params)
defer server.Stopper().Stop()
if _, err := sqlDB.Exec(`
CREATE DATABASE t;
CREATE TABLE t.test (k INT PRIMARY KEY, v INT, pi DECIMAL DEFAULT (DECIMAL '3.14'));
CREATE UNIQUE INDEX vidx ON t.test (v);
`); err != nil {
t.Fatal(err)
}
// Bulk insert.
maxValue := 4000
if err := bulkInsertIntoTable(sqlDB, maxValue); err != nil {
t.Fatal(err)
}
// Read table descriptor for version.
tableDesc := sqlbase.GetTableDescriptor(kvDB, "t", "test")
tablePrefix := roachpb.Key(keys.MakeTablePrefix(uint32(tableDesc.ID)))
tableEnd := tablePrefix.PrefixEnd()
// number of keys == 3 * number of rows; 2 column families and 1 index entry
// for each row.
if kvs, err := kvDB.Scan(context.TODO(), tablePrefix, tableEnd, 0); err != nil {
t.Fatal(err)
} else if e := 3 * (maxValue + 1); len(kvs) != e {
t.Fatalf("expected %d key value pairs, but got %d", e, len(kvs))
}
// Run some schema changes with operations.
// Add column.
backfillNotification = make(chan bool)
runSchemaChangeWithOperations(
t,
sqlDB,
kvDB,
"ALTER TABLE t.test ADD COLUMN x DECIMAL DEFAULT (DECIMAL '1.4')",
maxValue,
4,
backfillNotification)
// Drop column.
backfillNotification = make(chan bool)
runSchemaChangeWithOperations(
t,
sqlDB,
kvDB,
"ALTER TABLE t.test DROP pi",
maxValue,
3,
backfillNotification)
// Add index.
backfillNotification = make(chan bool)
runSchemaChangeWithOperations(
t,
sqlDB,
kvDB,
"CREATE UNIQUE INDEX foo ON t.test (v)",
maxValue,
4,
backfillNotification)
// Drop index.
backfillNotification = make(chan bool)
runSchemaChangeWithOperations(
t,
sqlDB,
kvDB,
"DROP INDEX [email protected]",
maxValue,
3,
backfillNotification)
// Verify that the index foo over v is consistent, and that column x has
// been backfilled properly.
rows, err := sqlDB.Query(`SELECT v, x from [email protected]`)
if err != nil {
//.........這裏部分代碼省略.........
示例13: runSchemaChangeWithOperations
// Run a particular schema change and run some OLTP operations in parallel, as
// soon as the schema change starts executing its backfill.
func runSchemaChangeWithOperations(
t *testing.T,
sqlDB *gosql.DB,
kvDB *client.DB,
schemaChange string,
maxValue int,
keyMultiple int,
backfillNotification chan bool,
) {
tableDesc := sqlbase.GetTableDescriptor(kvDB, "t", "test")
// Run the schema change in a separate goroutine.
var wg sync.WaitGroup
wg.Add(1)
go func() {
start := timeutil.Now()
// Start schema change that eventually runs a backfill.
if _, err := sqlDB.Exec(schemaChange); err != nil {
t.Error(err)
}
t.Logf("schema change %s took %v", schemaChange, timeutil.Since(start))
wg.Done()
}()
// Wait until the schema change backfill starts.
<-backfillNotification
// Run a variety of operations during the backfill.
// Grabbing a schema change lease on the table will fail, disallowing
// another schema change from being simultaneously executed.
sc := csql.NewSchemaChangerForTesting(tableDesc.ID, 0, 0, *kvDB, nil)
if l, err := sc.AcquireLease(); err == nil {
t.Fatalf("schema change lease acquisition on table %d succeeded: %v", tableDesc.ID, l)
}
// Update some rows.
var updatedKeys []int
for i := 0; i < 10; i++ {
k := rand.Intn(maxValue)
v := maxValue + i + 1
if _, err := sqlDB.Exec(`UPDATE t.test SET v = $1 WHERE k = $2`, v, k); err != nil {
t.Error(err)
}
updatedKeys = append(updatedKeys, k)
}
// Reupdate updated values back to what they were before.
for _, k := range updatedKeys {
if _, err := sqlDB.Exec(`UPDATE t.test SET v = $1 WHERE k = $2`, maxValue-k, k); err != nil {
t.Error(err)
}
}
// Delete some rows.
deleteStartKey := rand.Intn(maxValue - 10)
for i := 0; i < 10; i++ {
if _, err := sqlDB.Exec(`DELETE FROM t.test WHERE k = $1`, deleteStartKey+i); err != nil {
t.Error(err)
}
}
// Reinsert deleted rows.
for i := 0; i < 10; i++ {
k := deleteStartKey + i
if _, err := sqlDB.Exec(`INSERT INTO t.test VALUES($1, $2)`, k, maxValue-k); err != nil {
t.Error(err)
}
}
// Insert some new rows.
numInserts := 10
for i := 0; i < numInserts; i++ {
k := maxValue + i + 1
if _, err := sqlDB.Exec(`INSERT INTO t.test VALUES($1, $1)`, k); err != nil {
t.Error(err)
}
}
wg.Wait() // for schema change to complete.
// Verify the number of keys left behind in the table to validate schema
// change operations.
tablePrefix := roachpb.Key(keys.MakeTablePrefix(uint32(tableDesc.ID)))
tableEnd := tablePrefix.PrefixEnd()
if kvs, err := kvDB.Scan(context.TODO(), tablePrefix, tableEnd, 0); err != nil {
t.Fatal(err)
} else if e := keyMultiple * (maxValue + numInserts + 1); len(kvs) != e {
for _, kv := range kvs {
t.Errorf("key %s, value %s", kv.Key, kv.Value)
}
t.Fatalf("expected %d key value pairs, but got %d", e, len(kvs))
}
// Delete the rows inserted.
for i := 0; i < numInserts; i++ {
if _, err := sqlDB.Exec(`DELETE FROM t.test WHERE k = $1`, maxValue+i+1); err != nil {
t.Error(err)
}
//.........這裏部分代碼省略.........
示例14: TestAsyncSchemaChanger
func TestAsyncSchemaChanger(t *testing.T) {
defer leaktest.AfterTest(t)()
// The descriptor changes made must have an immediate effect
// so disable leases on tables.
defer csql.TestDisableTableLeases()()
// Disable synchronous schema change execution so the asynchronous schema
// changer executes all schema changes.
params, _ := createTestServerParams()
params.Knobs = base.TestingKnobs{
SQLSchemaChanger: &csql.SchemaChangerTestingKnobs{
SyncFilter: func(tscc csql.TestingSchemaChangerCollection) {
tscc.ClearSchemaChangers()
},
AsyncExecQuickly: true,
},
}
s, sqlDB, kvDB := serverutils.StartServer(t, params)
defer s.Stopper().Stop()
if _, err := sqlDB.Exec(`
CREATE DATABASE t;
CREATE TABLE t.test (k CHAR PRIMARY KEY, v CHAR);
INSERT INTO t.test VALUES ('a', 'b'), ('c', 'd');
`); err != nil {
t.Fatal(err)
}
// Read table descriptor for version.
tableDesc := sqlbase.GetTableDescriptor(kvDB, "t", "test")
// A long running schema change operation runs through
// a state machine that increments the version by 3.
expectedVersion := tableDesc.Version + 3
// Run some schema change
if _, err := sqlDB.Exec(`
CREATE INDEX foo ON t.test (v)
`); err != nil {
t.Fatal(err)
}
retryOpts := retry.Options{
InitialBackoff: 20 * time.Millisecond,
MaxBackoff: 200 * time.Millisecond,
Multiplier: 2,
}
// Wait until index is created.
for r := retry.Start(retryOpts); r.Next(); {
tableDesc = sqlbase.GetTableDescriptor(kvDB, "t", "test")
if len(tableDesc.Indexes) == 1 {
break
}
}
// Ensure that the indexes have been created.
mTest := makeMutationTest(t, kvDB, sqlDB, tableDesc)
indexQuery := `SELECT v FROM [email protected]`
mTest.CheckQueryResults(indexQuery, [][]string{{"b"}, {"d"}})
// Ensure that the version has been incremented.
tableDesc = sqlbase.GetTableDescriptor(kvDB, "t", "test")
newVersion := tableDesc.Version
if newVersion != expectedVersion {
t.Fatalf("bad version; e = %d, v = %d", expectedVersion, newVersion)
}
// Apply a schema change that only sets the UpVersion bit.
expectedVersion = newVersion + 1
mTest.Exec(`ALTER INDEX [email protected] RENAME TO ufo`)
for r := retry.Start(retryOpts); r.Next(); {
// Ensure that the version gets incremented.
tableDesc = sqlbase.GetTableDescriptor(kvDB, "t", "test")
name := tableDesc.Indexes[0].Name
if name != "ufo" {
t.Fatalf("bad index name %s", name)
}
newVersion = tableDesc.Version
if newVersion == expectedVersion {
break
}
}
// Run many schema changes simultaneously and check
// that they all get executed.
count := 5
for i := 0; i < count; i++ {
mTest.Exec(fmt.Sprintf(`CREATE INDEX foo%d ON t.test (v)`, i))
}
// Wait until indexes are created.
for r := retry.Start(retryOpts); r.Next(); {
tableDesc = sqlbase.GetTableDescriptor(kvDB, "t", "test")
if len(tableDesc.Indexes) == count+1 {
break
}
}
for i := 0; i < count; i++ {
indexQuery := fmt.Sprintf(`SELECT v FROM [email protected]%d`, i)
//.........這裏部分代碼省略.........
示例15: TestSchemaChangeProcess
func TestSchemaChangeProcess(t *testing.T) {
defer leaktest.AfterTest(t)()
// The descriptor changes made must have an immediate effect
// so disable leases on tables.
defer csql.TestDisableTableLeases()()
params, _ := createTestServerParams()
// Disable external processing of mutations.
params.Knobs.SQLSchemaChanger = &csql.SchemaChangerTestingKnobs{
AsyncExecNotification: asyncSchemaChangerDisabled,
}
s, sqlDB, kvDB := serverutils.StartServer(t, params)
defer s.Stopper().Stop()
var id = sqlbase.ID(keys.MaxReservedDescID + 2)
var node = roachpb.NodeID(2)
stopper := stop.NewStopper()
leaseMgr := csql.NewLeaseManager(
&base.NodeIDContainer{},
*kvDB,
hlc.NewClock(hlc.UnixNano, time.Nanosecond),
csql.LeaseManagerTestingKnobs{},
stopper,
&csql.MemoryMetrics{},
)
defer stopper.Stop()
changer := csql.NewSchemaChangerForTesting(id, 0, node, *kvDB, leaseMgr)
if _, err := sqlDB.Exec(`
CREATE DATABASE t;
CREATE TABLE t.test (k CHAR PRIMARY KEY, v CHAR, INDEX foo(v));
INSERT INTO t.test VALUES ('a', 'b'), ('c', 'd');
`); err != nil {
t.Fatal(err)
}
// Read table descriptor for version.
tableDesc := sqlbase.GetTableDescriptor(kvDB, "t", "test")
expectedVersion := tableDesc.Version
desc, err := changer.MaybeIncrementVersion()
if err != nil {
t.Fatal(err)
}
tableDesc = desc.GetTable()
newVersion := tableDesc.Version
if newVersion != expectedVersion {
t.Fatalf("bad version; e = %d, v = %d", expectedVersion, newVersion)
}
isDone, err := changer.IsDone()
if err != nil {
t.Fatal(err)
}
if !isDone {
t.Fatalf("table expected to not have an outstanding schema change: %v", tableDesc)
}
// Check that MaybeIncrementVersion increments the version
// correctly.
expectedVersion++
tableDesc.UpVersion = true
if err := kvDB.Put(
context.TODO(),
sqlbase.MakeDescMetadataKey(tableDesc.ID),
sqlbase.WrapDescriptor(tableDesc),
); err != nil {
t.Fatal(err)
}
isDone, err = changer.IsDone()
if err != nil {
t.Fatal(err)
}
if isDone {
t.Fatalf("table expected to have an outstanding schema change: %v", desc.GetTable())
}
desc, err = changer.MaybeIncrementVersion()
if err != nil {
t.Fatal(err)
}
tableDesc = desc.GetTable()
savedTableDesc := sqlbase.GetTableDescriptor(kvDB, "t", "test")
newVersion = tableDesc.Version
if newVersion != expectedVersion {
t.Fatalf("bad version in returned desc; e = %d, v = %d", expectedVersion, newVersion)
}
newVersion = savedTableDesc.Version
if newVersion != expectedVersion {
t.Fatalf("bad version in saved desc; e = %d, v = %d", expectedVersion, newVersion)
}
isDone, err = changer.IsDone()
if err != nil {
t.Fatal(err)
}
if !isDone {
t.Fatalf("table expected to not have an outstanding schema change: %v", tableDesc)
}
// Check that RunStateMachineBeforeBackfill doesn't do anything
// if there are no mutations queued.
//.........這裏部分代碼省略.........