/** * Gets the stored data center for the local node, or null if none have been set yet. */ public static String getDatacenter() { String req = "SELECT data_center FROM system.%s WHERE key='%s'"; UntypedResultSet result = executeInternal(String.format(req, LOCAL, LOCAL)); // Look up the Data center (return it if found) if (!result.isEmpty() && result.one().has("data_center")) return result.one().getString("data_center"); return null; }
public boolean isEmpty() { return size() == 0; }
private static Collection<Keyspace> readSchema() { String query = format("SELECT keyspace_name FROM %s.%s", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.LEGACY_KEYSPACES); Collection<String> keyspaceNames = new ArrayList<>(); query(query).forEach(row -> keyspaceNames.add(row.getString("keyspace_name"))); keyspaceNames.removeAll(SchemaConstants.LOCAL_SYSTEM_KEYSPACE_NAMES); Collection<Keyspace> keyspaces = new ArrayList<>(); keyspaceNames.forEach(name -> keyspaces.add(readKeyspace(name))); return keyspaces; }
private static List<ColumnDefinition> fetchColumns(String keyspace, String table, Types types) { String query = format("SELECT * FROM %s.%s WHERE keyspace_name = ? AND table_name = ?", SchemaConstants.SCHEMA_KEYSPACE_NAME, COLUMNS); UntypedResultSet columnRows = query(query, keyspace, table); if (columnRows.isEmpty()) throw new MissingColumns("Columns not found in schema table for " + keyspace + "." + table); List<ColumnDefinition> columns = new ArrayList<>(); columnRows.forEach(row -> columns.add(createColumnFromRow(row, types))); return columns; }
public static UntypedResultSet resultify(String query, List<Row> rows) { try { SelectStatement ss = (SelectStatement) getStatement(query, null).statement; ResultSet cqlRows = ss.process(rows); return UntypedResultSet.create(cqlRows); } catch (RequestValidationException e) { throw new AssertionError(e); } }
private Role getRoleFromTable(String name, SelectStatement statement, Function<UntypedResultSet.Row, Role> function) throws RequestExecutionException, RequestValidationException { ResultMessage.Rows rows = statement.execute(QueryState.forInternalCalls(), QueryOptions.forInternalCalls(consistencyForRole(name), Collections.singletonList(ByteBufferUtil.bytes(name))), System.nanoTime()); if (rows.result.isEmpty()) return NULL_ROLE; return function.apply(UntypedResultSet.create(rows.result).one()); }
private static long readTableTimestamp(String keyspaceName, String tableName) { String query = format("SELECT writeTime(type) AS timestamp FROM %s.%s WHERE keyspace_name = ? AND columnfamily_name = ?", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.LEGACY_COLUMNFAMILIES); return query(query, keyspaceName, tableName).one().getLong("timestamp"); }
private void replayAllFailedBatches() throws ExecutionException, InterruptedException { logger.debug("Started replayAllFailedBatches"); // rate limit is in bytes per second. Uses Double.MAX_VALUE if disabled (set to 0 in cassandra.yaml). // max rate is scaled by the number of nodes in the cluster (same as for HHOM - see CASSANDRA-5272). int throttleInKB = DatabaseDescriptor.getBatchlogReplayThrottleInKB() / StorageService.instance.getTokenMetadata().getAllEndpoints().size(); RateLimiter rateLimiter = RateLimiter.create(throttleInKB == 0 ? Double.MAX_VALUE : throttleInKB * 1024); UntypedResultSet page = executeInternal(String.format("SELECT id, data, written_at, version FROM %s.%s LIMIT %d", Keyspace.SYSTEM_KS, SystemKeyspace.BATCHLOG_CF, PAGE_SIZE)); while (!page.isEmpty()) { UUID id = processBatchlogPage(page, rateLimiter); if (page.size() < PAGE_SIZE) break; // we've exhausted the batchlog, next query would be empty. page = executeInternal(String.format("SELECT id, data, written_at, version FROM %s.%s WHERE token(id) > token(?) LIMIT %d", Keyspace.SYSTEM_KS, SystemKeyspace.BATCHLOG_CF, PAGE_SIZE), id); } cleanup(); logger.debug("Finished replayAllFailedBatches"); }
private static boolean hasExistingRoles() throws RequestExecutionException { // Try looking up the 'cassandra' default role first, to avoid the range query if possible. String defaultSUQuery = String.format("SELECT * FROM %s.%s WHERE role = '%s'", SchemaConstants.AUTH_KEYSPACE_NAME, AuthKeyspace.ROLES, DEFAULT_SUPERUSER_NAME); String allUsersQuery = String.format("SELECT * FROM %s.%s LIMIT 1", SchemaConstants.AUTH_KEYSPACE_NAME, AuthKeyspace.ROLES); return !QueryProcessor.process(defaultSUQuery, ConsistencyLevel.ONE).isEmpty() || !QueryProcessor.process(defaultSUQuery, ConsistencyLevel.QUORUM).isEmpty() || !QueryProcessor.process(allUsersQuery, ConsistencyLevel.QUORUM).isEmpty(); }
private void migrateLegacyHints(UUID hostId, Iterator<UntypedResultSet.Row> iterator, ByteBuffer buffer) { do { migrateLegacyHintsInternal(hostId, iterator, buffer); // if there are hints that didn't fit in the previous file, keep calling the method to write to a new // file until we get everything written. } while (iterator.hasNext()); }
public static List<String> getBuiltIndexes(String keyspaceName, Set<String> indexNames) { List<String> names = new ArrayList<>(indexNames); String req = "SELECT index_name from %s.\"%s\" WHERE table_name=? AND index_name IN ?"; UntypedResultSet results = executeInternal(String.format(req, SchemaConstants.SYSTEM_KEYSPACE_NAME, BUILT_INDEXES), keyspaceName, names); return StreamSupport.stream(results.spliterator(), false) .map(r -> r.getString("index_name")) .collect(Collectors.toList()); }
public static UntypedResultSet process(String query, ConsistencyLevel cl, List<ByteBuffer> values) throws RequestExecutionException { ResultMessage result = instance.process(query, QueryState.forInternalCalls(), QueryOptions.forInternalCalls(cl, values), System.nanoTime()); if (result instanceof ResultMessage.Rows) return UntypedResultSet.create(((ResultMessage.Rows)result).result); else return null; }
private Role getRoleFromTable(String name, SelectStatement statement, Function<UntypedResultSet.Row, Role> function) throws RequestExecutionException, RequestValidationException { ResultMessage.Rows rows = statement.execute(QueryState.forInternalCalls(), QueryOptions.forInternalCalls(consistencyForRole(name), Collections.singletonList(ByteBufferUtil.bytes(name))), System.nanoTime()); if (rows.result.isEmpty()) return NULL_ROLE; return function.apply(UntypedResultSet.create(rows.result).one()); }
private static long readFunctionTimestamp(String keyspaceName, String functionName, List<String> signature) { String query = format("SELECT writeTime(return_type) AS timestamp " + "FROM %s.%s " + "WHERE keyspace_name = ? AND function_name = ? AND signature = ?", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.LEGACY_FUNCTIONS); return query(query, keyspaceName, functionName, signature).one().getLong("timestamp"); }
private static List<ColumnDefinition> fetchColumns(String keyspace, String table, Types types) { String query = format("SELECT * FROM %s.%s WHERE keyspace_name = ? AND table_name = ?", SchemaConstants.SCHEMA_KEYSPACE_NAME, COLUMNS); UntypedResultSet columnRows = query(query, keyspace, table); if (columnRows.isEmpty()) throw new MissingColumns("Columns not found in schema table for " + keyspace + "." + table); List<ColumnDefinition> columns = new ArrayList<>(); columnRows.forEach(row -> columns.add(createColumnFromRow(row, types))); if (columns.stream().noneMatch(ColumnDefinition::isPartitionKey)) throw new MissingColumns("No partition key columns found in schema table for " + keyspace + "." + table); return columns; }
public static boolean isViewBuilt(String keyspaceName, String viewName) { String req = "SELECT view_name FROM %s.\"%s\" WHERE keyspace_name=? AND view_name=?"; UntypedResultSet result = executeInternal(String.format(req, SchemaConstants.SYSTEM_KEYSPACE_NAME, BUILT_VIEWS), keyspaceName, viewName); return !result.isEmpty(); }
private void migrateLegacyHints(UUID hostId, Iterator<UntypedResultSet.Row> iterator, ByteBuffer buffer) { do { migrateLegacyHintsInternal(hostId, iterator, buffer); // if there are hints that didn't fit in the previous file, keep calling the method to write to a new // file until we get everything written. } while (iterator.hasNext()); }
public static List<String> getBuiltIndexes(String keyspaceName, Set<String> indexNames) { List<String> names = new ArrayList<>(indexNames); String req = "SELECT index_name from %s.\"%s\" WHERE table_name=? AND index_name IN ?"; UntypedResultSet results = executeInternal(String.format(req, SchemaConstants.SYSTEM_KEYSPACE_NAME, BUILT_INDEXES), keyspaceName, names); return StreamSupport.stream(results.spliterator(), false) .map(r -> r.getString("index_name")) .collect(Collectors.toList()); }
@VisibleForTesting public int countAllBatches() { String query = String.format("SELECT count(*) FROM %s.%s", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.BATCHES); UntypedResultSet results = executeInternal(query); if (results == null || results.isEmpty()) return 0; return (int) results.one().getLong("count"); }
public static UntypedResultSet process(String query, ConsistencyLevel cl, List<ByteBuffer> values) throws RequestExecutionException { ResultMessage result = instance.process(query, QueryState.forInternalCalls(), QueryOptions.forInternalCalls(cl, values), System.nanoTime()); if (result instanceof ResultMessage.Rows) return UntypedResultSet.create(((ResultMessage.Rows)result).result); else return null; }