Search in sources :

Example 31 with UntypedResultSet

use of org.apache.cassandra.cql3.UntypedResultSet in project cassandra by apache.

the class SystemKeyspace method loadPreparedStatements.

public static List<Pair<String, String>> loadPreparedStatements() {
    String query = format("SELECT logged_keyspace, query_string FROM %s", PreparedStatements.toString());
    UntypedResultSet resultSet = executeOnceInternal(query);
    List<Pair<String, String>> r = new ArrayList<>();
    for (UntypedResultSet.Row row : resultSet) r.add(Pair.create(row.has("logged_keyspace") ? row.getString("logged_keyspace") : null, row.getString("query_string")));
    return r;
}
Also used : UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet)

Example 32 with UntypedResultSet

use of org.apache.cassandra.cql3.UntypedResultSet in project cassandra by apache.

the class SystemKeyspace method readTruncationRecords.

private static Map<TableId, Pair<CommitLogPosition, Long>> readTruncationRecords() {
    UntypedResultSet rows = executeInternal(format("SELECT truncated_at FROM system.%s WHERE key = '%s'", LOCAL, LOCAL));
    Map<TableId, Pair<CommitLogPosition, Long>> records = new HashMap<>();
    if (!rows.isEmpty() && rows.one().has("truncated_at")) {
        Map<UUID, ByteBuffer> map = rows.one().getMap("truncated_at", UUIDType.instance, BytesType.instance);
        for (Map.Entry<UUID, ByteBuffer> entry : map.entrySet()) records.put(TableId.fromUUID(entry.getKey()), truncationRecordFromBlob(entry.getValue()));
    }
    return records;
}
Also used : UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) ByteBuffer(java.nio.ByteBuffer) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 33 with UntypedResultSet

use of org.apache.cassandra.cql3.UntypedResultSet in project cassandra by apache.

the class SystemKeyspace method incrementAndGetGeneration.

public static int incrementAndGetGeneration() {
    String req = "SELECT gossip_generation FROM system.%s WHERE key='%s'";
    UntypedResultSet result = executeInternal(format(req, LOCAL, LOCAL));
    int generation;
    if (result.isEmpty() || !result.one().has("gossip_generation")) {
        // seconds-since-epoch isn't a foolproof new generation
        // (where foolproof is "guaranteed to be larger than the last one seen at this ip address"),
        // but it's as close as sanely possible
        generation = (int) (System.currentTimeMillis() / 1000);
    } else {
        // Other nodes will ignore gossip messages about a node that have a lower generation than previously seen.
        final int storedGeneration = result.one().getInt("gossip_generation") + 1;
        final int now = (int) (System.currentTimeMillis() / 1000);
        if (storedGeneration >= now) {
            logger.warn("Using stored Gossip Generation {} as it is greater than current system time {}.  See CASSANDRA-3654 if you experience problems", storedGeneration, now);
            generation = storedGeneration;
        } else {
            generation = now;
        }
    }
    req = "INSERT INTO system.%s (key, gossip_generation) VALUES ('%s', ?)";
    executeInternal(format(req, LOCAL, LOCAL), generation);
    forceBlockingFlush(LOCAL);
    return generation;
}
Also used : UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet)

Example 34 with UntypedResultSet

use of org.apache.cassandra.cql3.UntypedResultSet in project cassandra by apache.

the class SystemKeyspace method getViewBuildStatus.

public static Pair<Integer, Token> getViewBuildStatus(String ksname, String viewName) {
    String req = "SELECT generation_number, last_token FROM system.%s WHERE keyspace_name = ? AND view_name = ?";
    UntypedResultSet queryResultSet = executeInternal(format(req, VIEWS_BUILDS_IN_PROGRESS), ksname, viewName);
    if (queryResultSet == null || queryResultSet.isEmpty())
        return null;
    UntypedResultSet.Row row = queryResultSet.one();
    Integer generation = null;
    Token lastKey = null;
    if (row.has("generation_number"))
        generation = row.getInt("generation_number");
    if (row.has("last_key")) {
        Token.TokenFactory factory = ViewsBuildsInProgress.partitioner.getTokenFactory();
        lastKey = factory.fromString(row.getString("last_key"));
    }
    return Pair.create(generation, lastKey);
}
Also used : UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet)

Example 35 with UntypedResultSet

use of org.apache.cassandra.cql3.UntypedResultSet in project cassandra by apache.

the class SystemKeyspace method checkHealth.

/**
     * One of three things will happen if you try to read the system keyspace:
     * 1. files are present and you can read them: great
     * 2. no files are there: great (new node is assumed)
     * 3. files are present but you can't read them: bad
     * @throws ConfigurationException
     */
public static void checkHealth() throws ConfigurationException {
    Keyspace keyspace;
    try {
        keyspace = Keyspace.open(SchemaConstants.SYSTEM_KEYSPACE_NAME);
    } catch (AssertionError err) {
        // this happens when a user switches from OPP to RP.
        ConfigurationException ex = new ConfigurationException("Could not read system keyspace!");
        ex.initCause(err);
        throw ex;
    }
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(LOCAL);
    String req = "SELECT cluster_name FROM system.%s WHERE key='%s'";
    UntypedResultSet result = executeInternal(format(req, LOCAL, LOCAL));
    if (result.isEmpty() || !result.one().has("cluster_name")) {
        // this is a brand new node
        if (!cfs.getLiveSSTables().isEmpty())
            throw new ConfigurationException("Found system keyspace files, but they couldn't be loaded!");
        // no system files.  this is a new node.
        return;
    }
    String savedClusterName = result.one().getString("cluster_name");
    if (!DatabaseDescriptor.getClusterName().equals(savedClusterName))
        throw new ConfigurationException("Saved cluster name " + savedClusterName + " != configured name " + DatabaseDescriptor.getClusterName());
}
Also used : UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException)

Aggregations

UntypedResultSet (org.apache.cassandra.cql3.UntypedResultSet)71 Test (org.junit.Test)22 Mutation (org.apache.cassandra.db.Mutation)5 ByteBuffer (java.nio.ByteBuffer)4 UUID (java.util.UUID)4 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)4 TableMetadata (org.apache.cassandra.schema.TableMetadata)4 File (java.io.File)3 InetAddress (java.net.InetAddress)3 SchemaLoader.createKeyspace (org.apache.cassandra.SchemaLoader.createKeyspace)3 PartitionUpdate (org.apache.cassandra.db.partitions.PartitionUpdate)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 IOException (java.io.IOException)2 Collections.emptyMap (java.util.Collections.emptyMap)2 Collections.singletonMap (java.util.Collections.singletonMap)2 HashMap (java.util.HashMap)2 Row (org.apache.cassandra.cql3.UntypedResultSet.Row)2 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)2