Ejemplo n.º 1
0
    public async Task InitializeAsync()
    {
        _harness = await new MiniKuduClusterBuilder().BuildHarnessAsync();
        _client  = _harness.CreateClient();
        await using var session = _client.NewSession();

        // Create a 4-tablets table for scanning.
        var builder = new TableBuilder(_tableName)
                      .AddColumn("key1", KuduType.String, opt => opt.Key(true))
                      .AddColumn("key2", KuduType.String, opt => opt.Key(true))
                      .AddColumn("val", KuduType.String)
                      .SetRangePartitionColumns("key1", "key2");

        for (int i = 1; i < 4; i++)
        {
            builder.AddSplitRow(splitRow =>
            {
                splitRow.SetString("key1", i.ToString());
                splitRow.SetString("key2", "");
            });
        }

        var table = await _client.CreateTableAsync(builder);

        // The data layout ends up like this:
        // tablet '', '1': no rows
        // tablet '1', '2': '111', '122', '133'
        // tablet '2', '3': '211', '222', '233'
        // tablet '3', '': '311', '322', '333'
        var keys = new[] { "1", "2", "3" };

        foreach (var key1 in keys)
        {
            foreach (var key2 in keys)
            {
                var insert = table.NewInsert();
                insert.SetString(0, key1);
                insert.SetString(1, key2);
                insert.SetString(2, key2);
                await session.EnqueueAsync(insert);

                await session.FlushAsync();
            }
        }

        _beforeWriteTimestamp = _client.LastPropagatedTimestamp;

        // Reset the client in order to clear the propagated timestamp.
        _newClient = _harness.CreateClient();

        // Reopen the table using the new client.
        _table = await _newClient.OpenTableAsync(_tableName);

        _schema = _table.Schema;
    }
Ejemplo n.º 2
0
    public async Task TestScanTokensConcurrentAlterTable()
    {
        var builder = new TableBuilder(_tableName)
                      .SetNumReplicas(1)
                      .AddColumn("key", KuduType.Int64, opt => opt.Key(true))
                      .AddColumn("a", KuduType.Int64);

        var table = await _client.CreateTableAsync(builder);

        var tokenBuilder = _client.NewScanTokenBuilder(table);
        var tokens       = await tokenBuilder.IncludeTableMetadata(false).BuildAsync();

        var tokensWithMetadata = await tokenBuilder.IncludeTableMetadata(true).BuildAsync();

        var token             = Assert.Single(tokens);
        var tokenWithMetadata = Assert.Single(tokensWithMetadata);

        // Drop a column
        await _client.AlterTableAsync(new AlterTableBuilder(table)
                                      .DropColumn("a"));

        await Assert.ThrowsAsync <KeyNotFoundException>(
            async() => await _client.NewScanBuilderFromTokenAsync(token));

        var exception = await Assert.ThrowsAsync <NonRecoverableException>(async() =>
        {
            var scanBuilder = await _client.NewScanBuilderFromTokenAsync(tokenWithMetadata);
            await scanBuilder.Build().CountAsync();
        });

        Assert.Contains("Some columns are not present in the current schema: a", exception.Message);

        // Add a column with the same name, type, and nullability. It will have a
        // different id-- it's a  different column-- so the scan token will fail.
        await _client.AlterTableAsync(new AlterTableBuilder(table)
                                      .AddColumn("a", KuduType.Int64, opt => opt.DefaultValue(0L)));

        table = await _client.OpenTableAsync(_tableName);

        await Assert.ThrowsAsync <KeyNotFoundException>(
            async() => await _client.NewScanBuilderFromTokenAsync(token));
    }
Ejemplo n.º 3
0
    public async Task TestAlterAddColumns()
    {
        KuduTable table = await CreateTableAsync();

        await InsertRowsAsync(table, 0, 100);

        Assert.Equal(100, await ClientTestUtil.CountRowsAsync(_client, table));

        await _client.AlterTableAsync(new AlterTableBuilder(table)
                                      .AddColumn("addNonNull", KuduType.Int32, opt => opt
                                                 .Nullable(false)
                                                 .DefaultValue(100))
                                      .AddColumn("addNullable", KuduType.Int32)
                                      .AddColumn("addNullableDef", KuduType.Int32, opt => opt
                                                 .DefaultValue(200)));

        // Reopen table for the new schema.
        table = await _client.OpenTableAsync(_tableName);

        Assert.Equal(5, table.Schema.Columns.Count);

        // Add a row with addNullableDef=null
        var insert = table.NewInsert();

        insert.SetInt32("c0", 101);
        insert.SetInt32("c1", 101);
        insert.SetInt32("addNonNull", 101);
        insert.SetInt32("addNullable", 101);
        insert.SetNull("addNullableDef");
        await _session.EnqueueAsync(insert);

        await _session.FlushAsync();

        // Check defaults applied, and that row key=101
        var results = await ClientTestUtil.ScanTableToStringsAsync(_client, table);

        var expected = new List <string>(101);

        for (int i = 0; i < 100; i++)
        {
            expected.Add($"INT32 c0={i}, INT32 c1={i}, INT32 addNonNull=100, " +
                         "INT32 addNullable=NULL, INT32 addNullableDef=200");
        }

        expected.Add("INT32 c0=101, INT32 c1=101, INT32 addNonNull=101, " +
                     "INT32 addNullable=101, INT32 addNullableDef=NULL");

        Assert.Equal(
            expected.OrderBy(r => r),
            results.OrderBy(r => r));
    }