private void flushKuduSession(final KuduSession kuduSession, boolean close, final List<RowError> rowErrors) throws KuduException { final List<OperationResponse> responses = close ? kuduSession.close() : kuduSession.flush(); if (kuduSession.getFlushMode() == SessionConfiguration.FlushMode.AUTO_FLUSH_BACKGROUND) { rowErrors.addAll(Arrays.asList(kuduSession.getPendingErrors().getRowErrors())); } else { responses.stream() .filter(OperationResponse::hasRowError) .map(OperationResponse::getRowError) .forEach(rowErrors::add); } }
@Override public void applyRandomMutations(List<Row> planned) throws Exception { KuduConnection connection = getConnection(); KuduSession session = connection.getSession(); KuduTable table = connection.getTable(config.getString(TABLE_CONFIG_NAME)); List<Operation> operations = extractOperations(planned, table); for (Operation operation : operations) { session.apply(operation); } // Wait until all operations have completed before checking for errors. while (session.hasPendingOperations()) { Thread.sleep(1); } // Fail fast on any error applying mutations if (session.countPendingErrors() > 0) { RowError firstError = session.getPendingErrors().getRowErrors()[0]; String errorMessage = String.format("Kudu output error '%s' during operation '%s' at tablet server '%s'", firstError.getErrorStatus(), firstError.getOperation(), firstError.getTsUUID()); throw new RuntimeException(errorMessage); } }
/** * Insert rows into the provided table. The table's columns must be ints, and * must have a primary key in the first column. * @param table the table * @param start the inclusive start key * @param end the exclusive end key */ private void insertRows(KuduTable table, int start, int end) throws KuduException { KuduSession session = client.newSession(); session.setFlushMode(SessionConfiguration.FlushMode.AUTO_FLUSH_BACKGROUND); for (int i = start; i < end; i++) { Insert insert = table.newInsert(); for (int idx = 0; idx < table.getSchema().getColumnCount(); idx++) { insert.getRow().addInt(idx, i); } session.apply(insert); } session.flush(); RowError[] rowErrors = session.getPendingErrors().getRowErrors(); assertEquals(String.format("row errors: %s", Arrays.toString(rowErrors)), 0, rowErrors.length); }
session.apply(insert); session.flush(); RowError[] rowErrors = session.getPendingErrors().getRowErrors(); assertEquals(String.format("row errors: %s", Arrays.toString(rowErrors)), 0, rowErrors.length);
/** * Test collecting errors from multiple tablets. * @throws Exception */ @Test(timeout = 100000) public void multiTabletTest() throws Exception { String tableName = TestRowErrors.class.getName() + "-" + System.currentTimeMillis(); createFourTabletsTableWithNineRows(harness.getAsyncClient(), tableName, DEFAULT_SLEEP); table = harness.getClient().openTable(tableName); KuduSession session = harness.getClient().newSession(); session.setFlushMode(KuduSession.FlushMode.AUTO_FLUSH_BACKGROUND); int dupRows = 3; session.apply(createInsert(12)); session.apply(createInsert(22)); session.apply(createInsert(32)); session.flush(); RowErrorsAndOverflowStatus reos = session.getPendingErrors(); assertEquals(dupRows, reos.getRowErrors().length); assertEquals(0, session.countPendingErrors()); }
session.apply(insert); session.flush(); RowError[] rowErrors = session.getPendingErrors().getRowErrors(); assertEquals(String.format("row errors: %s", Arrays.toString(rowErrors)), 0, rowErrors.length);
@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)); } }