Beispiel #1
0
  public KsDef toThrift() {
    List<CfDef> cfDefs = new ArrayList<CfDef>();
    for (CFMetaData cfm : cfMetaData().values()) cfDefs.add(cfm.toThrift());
    KsDef ksdef = new KsDef(name, strategyClass.getName(), cfDefs);
    ksdef.setStrategy_options(strategyOptions);
    ksdef.setDurable_writes(durableWrites);

    return ksdef;
  }
Beispiel #2
0
  public RowMutation toSchema(long timestamp) {
    RowMutation rm = new RowMutation(Table.SYSTEM_TABLE, SystemTable.getSchemaKSKey(name));
    ColumnFamily cf = rm.addOrGet(SystemTable.SCHEMA_KEYSPACES_CF);

    cf.addColumn(Column.create(name, timestamp, "name"));
    cf.addColumn(Column.create(durableWrites, timestamp, "durable_writes"));
    cf.addColumn(Column.create(strategyClass.getName(), timestamp, "strategy_class"));
    cf.addColumn(Column.create(json(strategyOptions), timestamp, "strategy_options"));

    for (CFMetaData cfm : cfMetaData.values()) cfm.toSchema(rm, timestamp);

    return rm;
  }
Beispiel #3
0
  public KSMetaData validate() throws ConfigurationException {
    if (!CFMetaData.isNameValid(name))
      throw new ConfigurationException(
          String.format(
              "Invalid keyspace name: shouldn't be empty nor more than %s characters long (got \"%s\")",
              Schema.NAME_LENGTH, name));

    // Attempt to instantiate the ARS, which will throw a ConfigException if the strategy_options
    // aren't fully formed
    TokenMetadata tmd = StorageService.instance.getTokenMetadata();
    IEndpointSnitch eps = DatabaseDescriptor.getEndpointSnitch();
    AbstractReplicationStrategy.createReplicationStrategy(
        name, strategyClass, tmd, eps, strategyOptions);

    for (CFMetaData cfm : cfMetaData.values()) cfm.validate();

    return this;
  }
Beispiel #4
0
  /**
   * Deserialize ColumnFamilies from low-level schema representation, all of them belong to the same
   * keyspace
   *
   * @param row
   * @return map containing name of the ColumnFamily and it's metadata for faster lookup
   */
  public static Map<String, CFMetaData> deserializeColumnFamilies(Row row) {
    if (row.cf == null) return Collections.emptyMap();

    Map<String, CFMetaData> cfms = new HashMap<String, CFMetaData>();
    UntypedResultSet results =
        QueryProcessor.resultify("SELECT * FROM system.schema_columnfamilies", row);
    for (UntypedResultSet.Row result : results) {
      CFMetaData cfm = CFMetaData.fromSchema(result);
      cfms.put(cfm.cfName, cfm);
    }

    for (CFMetaData cfm : cfms.values()) {
      Row columnRow = ColumnDefinition.readSchema(cfm.ksName, cfm.cfName);
      for (ColumnDefinition cd : ColumnDefinition.fromSchema(columnRow, cfm))
        cfm.column_metadata.put(cd.name, cd);
    }

    return cfms;
  }
    @Override
    public void init(String keyspace) {
      Iterator<InetAddress> hostiter = hosts.iterator();
      while (hostiter.hasNext()) {
        try {
          // Query endpoint to ranges map and schemas from thrift
          InetAddress host = hostiter.next();
          Cassandra.Client client =
              createThriftClient(
                  host.getHostAddress(), rpcPort, this.user, this.passwd, this.transportFactory);

          setPartitioner(client.describe_partitioner());
          Token.TokenFactory tkFactory = getPartitioner().getTokenFactory();

          for (TokenRange tr : client.describe_ring(keyspace)) {
            Range<Token> range =
                new Range<>(
                    tkFactory.fromString(tr.start_token),
                    tkFactory.fromString(tr.end_token),
                    getPartitioner());
            for (String ep : tr.endpoints) {
              addRangeForEndpoint(range, InetAddress.getByName(ep));
            }
          }

          String query =
              String.format(
                  "SELECT * FROM %s.%s WHERE keyspace_name = '%s'",
                  Keyspace.SYSTEM_KS, SystemKeyspace.SCHEMA_COLUMNFAMILIES_CF, keyspace);
          CqlResult result =
              client.execute_cql3_query(
                  ByteBufferUtil.bytes(query), Compression.NONE, ConsistencyLevel.ONE);
          for (CqlRow row : result.rows) {
            CFMetaData metadata = CFMetaData.fromThriftCqlRow(row);
            knownCfs.put(metadata.cfName, metadata);
          }
          break;
        } catch (Exception e) {
          if (!hostiter.hasNext())
            throw new RuntimeException("Could not retrieve endpoint ranges: ", e);
        }
      }
    }