/** * A generic helper function to create a table with default test options. */ public static KuduTable createDefaultTable(KuduClient client, String tableName) throws KuduException { return client.createTable(tableName, getBasicSchema(), getBasicCreateTableOptions()); }
@Before public void setUp() throws Exception { client = harness.getAsyncClient(); table = harness.getClient().createTable(TABLE_NAME, schema, getBasicCreateTableOptions()); }
@Test(timeout = 100000) public void testFormatRangePartitionsUnbounded() throws Exception { CreateTableOptions builder = getBasicCreateTableOptions(); client.createTable(tableName, basicSchema, builder); assertEquals( ImmutableList.of("UNBOUNDED"), client.openTable(tableName).getFormattedRangePartitions(10000)); }
@Before public void setUp() throws Exception { CreateTableOptions options = getBasicCreateTableOptions().setNumReplicas(1); table = harness.getClient().createTable(TABLE_NAME, getBasicSchema(), options); }
@Test(timeout = 100000) public void testGetRangePartitionsUnbounded() throws Exception { CreateTableOptions builder = getBasicCreateTableOptions(); KuduTable table = client.createTable(tableName, BASIC_SCHEMA, builder); List<Partition> rangePartitions = table.getRangePartitions(client.getDefaultOperationTimeoutMs()); assertEquals(rangePartitions.size(), 1); Partition partition = rangePartitions.get(0); assertEquals(0, partition.getRangeKeyStart().length); assertEquals(0, partition.getRangeKeyEnd().length); }
/** * Test creating a table with various invalid schema cases. */ @Test(timeout = 100000) public void testCreateTableTooManyColumns() throws Exception { List<ColumnSchema> cols = new ArrayList<>(); cols.add(new ColumnSchema.ColumnSchemaBuilder("key", Type.STRING) .key(true) .build()); for (int i = 0; i < 1000; i++) { // not null with default cols.add(new ColumnSchema.ColumnSchemaBuilder("c" + i, Type.STRING) .build()); } Schema schema = new Schema(cols); try { client.createTable(TABLE_NAME, schema, getBasicCreateTableOptions()); fail(); } catch (NonRecoverableException nre) { assertThat(nre.toString(), containsString( "number of columns 1001 is greater than the permitted maximum")); } }
/** * Test creating a table with out of order primary keys in the table schema . */ @Test(timeout = 100000) public void testCreateTableOutOfOrderPrimaryKeys() throws Exception { ArrayList<ColumnSchema> columns = new ArrayList<>(6); columns.add(new ColumnSchema.ColumnSchemaBuilder("key_1", Type.INT8).key(true).build()); columns.add(new ColumnSchema.ColumnSchemaBuilder("column1_i", Type.INT32).build()); columns.add(new ColumnSchema.ColumnSchemaBuilder("key_2", Type.INT16).key(true).build()); columns.add(new ColumnSchema.ColumnSchemaBuilder("column2_i", Type.INT32).build()); columns.add(new ColumnSchema.ColumnSchemaBuilder("column3_s", Type.STRING).build()); columns.add(new ColumnSchema.ColumnSchemaBuilder("column4_b", Type.BOOL).build()); Schema schema = new Schema(columns); try { asyncClient.createTable("testCreateTableOutOfOrderPrimaryKeys-" + System.currentTimeMillis(), schema, getBasicCreateTableOptions()).join(); fail(); } catch (NonRecoverableException nre) { assertTrue(nre.getMessage().startsWith("Got out-of-order key column")); } } }
@Before public void setUp() throws Exception { CreateTableOptions builder = getBasicCreateTableOptions(); harness.getClient().createTable(TABLE_NAME, getBasicSchema(), builder); table = harness.getClient().openTable(TABLE_NAME); }
private KuduTable createTableWithSplitsAndTest(String tableNamePrefix, int splitsCount) throws Exception { String newTableName = tableNamePrefix + "-" + splitsCount; CreateTableOptions builder = getBasicCreateTableOptions(); if (splitsCount != 0) { for (int i = 1; i <= splitsCount; i++) { PartialRow row = BASIC_SCHEMA.newPartialRow(); row.addInt(0, i); builder.addSplitRow(row); } } KuduTable table = client.createTable(newTableName, BASIC_SCHEMA, builder); List<LocatedTablet> tablets = table.getTabletsLocations(DEFAULT_SLEEP); assertEquals(splitsCount + 1, tablets.size()); assertEquals(splitsCount + 1, table.asyncGetTabletsLocations(DEFAULT_SLEEP).join().size()); for (LocatedTablet tablet : tablets) { assertEquals(3, tablet.getReplicas().size()); } return table; }
@Test(timeout = 100000) public void testNumReplicas() throws Exception { for (int i = 1; i <= 3; i++) { // Ignore even numbers. if (i % 2 != 0) { String tableName = "testNumReplicas" + "-" + i; CreateTableOptions options = getBasicCreateTableOptions(); options.setNumReplicas(i); client.createTable(tableName, basicSchema, options); KuduTable table = client.openTable(tableName); assertEquals(i, table.getNumReplicas()); } } } }
@Test(timeout = 10000) public void testUpsert() throws Exception { KuduTable table = client.createTable(tableName, basicSchema, getBasicCreateTableOptions()); KuduSession session = client.newSession(); // Test an Upsert that acts as an Insert. assertFalse(session.apply(createUpsert(table, 1, 1, false)).hasRowError()); List<String> rowStrings = scanTableToStrings(table); assertEquals(1, rowStrings.size()); assertEquals( "INT32 key=1, INT32 column1_i=1, INT32 column2_i=3, " + "STRING column3_s=a string, BOOL column4_b=true", rowStrings.get(0)); // Test an Upsert that acts as an Update. assertFalse(session.apply(createUpsert(table, 1, 2, false)).hasRowError()); rowStrings = scanTableToStrings(table); assertEquals( "INT32 key=1, INT32 column1_i=2, INT32 column2_i=3, " + "STRING column3_s=a string, BOOL column4_b=true", rowStrings.get(0)); }
@Override @SuppressWarnings("AssertionFailureIgnored") public void run() { final String tableName = "TestAuthnTokenReacquire-table-" + threadIdx; try { ListTabletServersResponse response = client.listTabletServers(); assertNotNull(response); dropConnectionsAndExpireToken(); ListTablesResponse tableList = client.getTablesList(tableName); assertNotNull(tableList); assertTrue(tableList.getTablesList().isEmpty()); dropConnectionsAndExpireToken(); client.createTable(tableName, basicSchema, getBasicCreateTableOptions()); dropConnectionsAndExpireToken(); KuduTable table = client.openTable(tableName); assertEquals(basicSchema.getColumnCount(), table.getSchema().getColumnCount()); dropConnectionsAndExpireToken(); client.deleteTable(tableName); assertFalse(client.tableExists(tableName)); } catch (Throwable e) { //noinspection ThrowableResultOfMethodCallIgnored exceptions.put(threadIdx, e); } } });
/** * Test to verify that we can write in and read back UTF8. */ @Test(timeout = 100000) public void testUTF8() throws Exception { Schema schema = createManyStringsSchema(); client.createTable(TABLE_NAME, schema, getBasicCreateTableOptions()); KuduSession session = client.newSession(); KuduTable table = client.openTable(TABLE_NAME); Insert insert = table.newInsert(); PartialRow row = insert.getRow(); row.addString("key", "กขฃคฅฆง"); // some thai row.addString("c1", "✁✂✃✄✆"); // some icons row.addString("c2", "hello"); // some normal chars row.addString("c4", "🐱"); // supplemental plane session.apply(insert); session.flush(); List<String> rowStrings = scanTableToStrings(table); assertEquals(1, rowStrings.size()); assertEquals( "STRING key=กขฃคฅฆง, STRING c1=✁✂✃✄✆, STRING c2=hello, STRING c3=NULL, STRING c4=🐱", rowStrings.get(0)); }
@Test(timeout = 100000) public void testCreateTableWithConcurrentAlter() throws Exception { // Kick off an asynchronous table creation. Deferred<KuduTable> d = asyncClient.createTable(TABLE_NAME, createManyStringsSchema(), getBasicCreateTableOptions()); // Rename the table that's being created to make sure it doesn't interfere // with the "wait for all tablets to be created" behavior of createTable(). // // We have to retry this in a loop because we might run before the table // actually exists. while (true) { try { client.alterTable(TABLE_NAME, new AlterTableOptions().renameTable("foo")); break; } catch (KuduException e) { if (!e.getStatus().isNotFound()) { throw e; } } } // If createTable() was disrupted by the alterTable(), this will throw. d.join(); }
@Test(timeout = 100000) public void testIgnoreAllDuplicateRows() throws Exception { KuduTable table = client.createTable(tableName, basicSchema, getBasicCreateTableOptions()); KuduSession session = client.newSession(); session.setIgnoreAllDuplicateRows(true); for (int i = 0; i < 10; i++) { session.apply(createInsert(table, i)); } for (SessionConfiguration.FlushMode mode : SessionConfiguration.FlushMode.values()) { session.setFlushMode(mode); for (int i = 0; i < 10; i++) { OperationResponse resp = session.apply(createInsert(table, i)); if (mode == SessionConfiguration.FlushMode.AUTO_FLUSH_SYNC) { assertFalse(resp.hasRowError()); } } if (mode == SessionConfiguration.FlushMode.MANUAL_FLUSH) { List<OperationResponse> responses = session.flush(); for (OperationResponse resp : responses) { assertFalse(resp.hasRowError()); } } else if (mode == SessionConfiguration.FlushMode.AUTO_FLUSH_BACKGROUND) { while (session.hasPendingOperations()) { Thread.sleep(100); } assertEquals(0, session.countPendingErrors()); } } }
/** * Creates a local aclient that we auto-close while buffering one row, then makes sure that after * closing that we can read the row. */ @Test(timeout = 100000) public void testAutoClose() throws Exception { try (KuduClient localClient = new KuduClient.KuduClientBuilder(harness.getMasterAddressesAsString()).build()) { localClient.createTable(TABLE_NAME, basicSchema, getBasicCreateTableOptions()); KuduTable table = localClient.openTable(TABLE_NAME); KuduSession session = localClient.newSession(); session.setFlushMode(SessionConfiguration.FlushMode.MANUAL_FLUSH); Insert insert = createBasicSchemaInsert(table, 0); session.apply(insert); } KuduTable table = client.openTable(TABLE_NAME); AsyncKuduScanner scanner = new AsyncKuduScanner.AsyncKuduScannerBuilder(asyncClient, table).build(); assertEquals(1, countRowsInScan(scanner)); }
@Test(timeout = 100000) public void testCreateTableWithConcurrentInsert() throws Exception { KuduTable table = client.createTable( TABLE_NAME, createManyStringsSchema(), getBasicCreateTableOptions().setWait(false)); // Insert a row. // // It's very likely that the tablets are still being created, but the client // should transparently retry the insert (and associated master lookup) // until the operation succeeds. Insert insert = table.newInsert(); insert.getRow().addString("key", "key_0"); insert.getRow().addString("c1", "c1_0"); insert.getRow().addString("c2", "c2_0"); KuduSession session = client.newSession(); OperationResponse resp = session.apply(insert); assertFalse(resp.hasRowError()); // This won't do anything useful (i.e. if the insert succeeds, we know the // table has been created), but it's here for additional code coverage. assertTrue(client.isCreateTableDone(TABLE_NAME)); }
@Test(timeout = 100000) public void testBasicOps() throws Exception { KuduTable table = client.createTable(tableName, basicSchema, getBasicCreateTableOptions()); KuduSession session = client.newSession(); for (int i = 0; i < 10; i++) { session.apply(createInsert(table, i)); } assertEquals(10, countRowsInScan(client.newScannerBuilder(table).build())); OperationResponse resp = session.apply(createInsert(table, 0)); assertTrue(resp.hasRowError()); session.setFlushMode(SessionConfiguration.FlushMode.MANUAL_FLUSH); for (int i = 10; i < 20; i++) { session.apply(createInsert(table, i)); } session.flush(); assertEquals(20, countRowsInScan(client.newScannerBuilder(table).build())); }
@Test public void testBasicWorkflow() throws Exception { KuduTable table = client.createTable(TABLE_NAME, basicSchema, getBasicCreateTableOptions()); dropConnectionsAndExpireToken(); KuduSession session = client.newSession(); session.setTimeoutMillis(OP_TIMEOUT_MS); session.apply(createBasicSchemaInsert(table, 1)); session.flush(); RowErrorsAndOverflowStatus errors = session.getPendingErrors(); assertFalse(errors.isOverflowed()); assertEquals(0, session.countPendingErrors()); dropConnectionsAndExpireToken(); KuduTable scanTable = client.openTable(TABLE_NAME); AsyncKuduScanner scanner = new AsyncKuduScanner.AsyncKuduScannerBuilder(asyncClient, scanTable) .scanRequestTimeout(OP_TIMEOUT_MS) .build(); assertEquals(1, countRowsInScan(scanner)); dropConnectionsAndExpireToken(); client.deleteTable(TABLE_NAME); assertFalse(client.tableExists(TABLE_NAME)); } }
/** * Test setting and reading the most recent propagated timestamp. */ @Test(timeout = 100000) public void testLastPropagatedTimestamps() throws Exception { // Scan a table to ensure a timestamp is propagated. KuduTable table = client.createTable(TABLE_NAME, basicSchema, getBasicCreateTableOptions()); client.newScannerBuilder(table).build().nextRows().getNumRows(); assertTrue(client.hasLastPropagatedTimestamp()); assertTrue(client.hasLastPropagatedTimestamp()); assertTrue(asyncClient.hasLastPropagatedTimestamp()); long initial_ts = client.getLastPropagatedTimestamp(); // Check that the initial timestamp is consistent with the asynchronous client. assertEquals(initial_ts, client.getLastPropagatedTimestamp()); assertEquals(initial_ts, asyncClient.getLastPropagatedTimestamp()); // Attempt to change the timestamp to a lower value. This should not change // the internal timestamp, as it must be monotonically increasing. client.updateLastPropagatedTimestamp(initial_ts - 1); assertEquals(initial_ts, client.getLastPropagatedTimestamp()); assertEquals(initial_ts, asyncClient.getLastPropagatedTimestamp()); // Use the synchronous client to update the last propagated timestamp and // check with both clients that the timestamp was updated. client.updateLastPropagatedTimestamp(initial_ts + 1); assertEquals(initial_ts + 1, client.getLastPropagatedTimestamp()); assertEquals(initial_ts + 1, asyncClient.getLastPropagatedTimestamp()); }