Пример #1
0
  /** Performs a deep copy on <i>other</i>. */
  public KsDef(KsDef other) {
    __isset_bitfield = other.__isset_bitfield;
    if (other.isSetName()) {
      this.name = other.name;
    }
    if (other.isSetStrategy_class()) {
      this.strategy_class = other.strategy_class;
    }
    if (other.isSetStrategy_options()) {
      Map<String, String> __this__strategy_options = new HashMap<String, String>();
      for (Map.Entry<String, String> other_element : other.strategy_options.entrySet()) {

        String other_element_key = other_element.getKey();
        String other_element_value = other_element.getValue();

        String __this__strategy_options_copy_key = other_element_key;

        String __this__strategy_options_copy_value = other_element_value;

        __this__strategy_options.put(
            __this__strategy_options_copy_key, __this__strategy_options_copy_value);
      }
      this.strategy_options = __this__strategy_options;
    }
    this.replication_factor = other.replication_factor;
    if (other.isSetCf_defs()) {
      List<CfDef> __this__cf_defs = new ArrayList<CfDef>();
      for (CfDef other_element : other.cf_defs) {
        __this__cf_defs.add(new CfDef(other_element));
      }
      this.cf_defs = __this__cf_defs;
    }
    this.durable_writes = other.durable_writes;
  }
 @Test
 // 50 times: 597 mills.
 public void listKeyspaces() throws Exception {
   int count = 50;
   //
   long beg = System.currentTimeMillis();
   for (int i = 0; i < count; i++) {
     CtSession ctSession = ctSessionFactory.openSession();
     System.out.println(ctSession);
     //
     List<KsDef> kds = ctSession.describe_keyspaces();
     for (KsDef kd : kds) {
       StringBuilder buffer = new StringBuilder();
       buffer.append("[");
       buffer.append(kd.getName());
       buffer.append("] ");
       List<CfDef> cds = kd.getCf_defs();
       int size = cds.size();
       for (int j = 0; j < size; j++) {
         buffer.append(cds.get(j).getName());
         if (j < size - 1) {
           buffer.append(", ");
         }
       }
       //
       System.out.println(buffer);
     }
     ctSessionFactory.closeSession();
   }
   long end = System.currentTimeMillis();
   System.out.println(count + " times: " + (end - beg) + " mills. ");
 }
Пример #3
0
    public void write(org.apache.thrift.protocol.TProtocol oprot, KsDef struct)
        throws org.apache.thrift.TException {
      struct.validate();

      oprot.writeStructBegin(STRUCT_DESC);
      if (struct.name != null) {
        oprot.writeFieldBegin(NAME_FIELD_DESC);
        oprot.writeString(struct.name);
        oprot.writeFieldEnd();
      }
      if (struct.strategy_class != null) {
        oprot.writeFieldBegin(STRATEGY_CLASS_FIELD_DESC);
        oprot.writeString(struct.strategy_class);
        oprot.writeFieldEnd();
      }
      if (struct.strategy_options != null) {
        if (struct.isSetStrategy_options()) {
          oprot.writeFieldBegin(STRATEGY_OPTIONS_FIELD_DESC);
          {
            oprot.writeMapBegin(
                new org.apache.thrift.protocol.TMap(
                    org.apache.thrift.protocol.TType.STRING,
                    org.apache.thrift.protocol.TType.STRING,
                    struct.strategy_options.size()));
            for (Map.Entry<String, String> _iter81 : struct.strategy_options.entrySet()) {
              oprot.writeString(_iter81.getKey());
              oprot.writeString(_iter81.getValue());
            }
            oprot.writeMapEnd();
          }
          oprot.writeFieldEnd();
        }
      }
      if (struct.isSetReplication_factor()) {
        oprot.writeFieldBegin(REPLICATION_FACTOR_FIELD_DESC);
        oprot.writeI32(struct.replication_factor);
        oprot.writeFieldEnd();
      }
      if (struct.cf_defs != null) {
        oprot.writeFieldBegin(CF_DEFS_FIELD_DESC);
        {
          oprot.writeListBegin(
              new org.apache.thrift.protocol.TList(
                  org.apache.thrift.protocol.TType.STRUCT, struct.cf_defs.size()));
          for (CfDef _iter82 : struct.cf_defs) {
            _iter82.write(oprot);
          }
          oprot.writeListEnd();
        }
        oprot.writeFieldEnd();
      }
      if (struct.isSetDurable_writes()) {
        oprot.writeFieldBegin(DURABLE_WRITES_FIELD_DESC);
        oprot.writeBool(struct.durable_writes);
        oprot.writeFieldEnd();
      }
      oprot.writeFieldStop();
      oprot.writeStructEnd();
    }
 @PooledConnection
 public void addKeyspace(String keyspace)
     throws InvalidRequestException, TException, SchemaDisagreementException {
   // TODO: Take key space in via JSON/XML. (Replace hard-coded values)
   List<CfDef> cfDefList = new ArrayList<CfDef>();
   KsDef ksDef = new KsDef(keyspace, "org.apache.cassandra.locator.SimpleStrategy", cfDefList);
   ksDef.putToStrategy_options("replication_factor", "1");
   getConnection(null).system_add_keyspace(ksDef);
 }
Пример #5
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;
  }
Пример #6
0
  public String addKeyspace(final KsDef keyspaceDefinition) throws Exception {
    if (logger.isInfoEnabled()) logger.info("Adding keyspace '{}'", keyspaceDefinition.getName());
    IManagerOperation<String> operation =
        new IManagerOperation<String>() {
          @Override
          public String execute(Client conn) throws Exception {
            return conn.system_add_keyspace(keyspaceDefinition);
          }
        };
    String schemaVersion = tryOperation(operation);
    if (logger.isInfoEnabled())
      logger.info(
          "Added keyspace '{}', schema version is now '{}'",
          new Object[] {keyspaceDefinition.getName(), schemaVersion});

    return schemaVersion;
  }
Пример #7
0
 /*
  * This keyspace exists because we need a way to pull the datacenter information and they only
  * way to do it is if you have a valid keyspace set up.  We will pull the info from here
  * so we can accurately create the actually NetworkTopologyStrategy keyspace.
  */
 private static void ensureTestKeyspaceExists(Cassandra.Client client) {
   try {
     try {
       client.describe_keyspace(CassandraConstants.SIMPLE_RF_TEST_KEYSPACE);
       return;
     } catch (NotFoundException e) {
       // need to create key space
     }
     KsDef testKs =
         new KsDef(
             CassandraConstants.SIMPLE_RF_TEST_KEYSPACE,
             CassandraConstants.SIMPLE_STRATEGY,
             ImmutableList.<CfDef>of());
     testKs.setStrategy_options(
         ImmutableMap.of(CassandraConstants.REPLICATION_FACTOR_OPTION, "1"));
     client.system_add_keyspace(testKs);
   } catch (Exception e) {
     log.warn(e.getMessage(), e);
   }
 }
Пример #8
0
 /**
  * Builds out a KsDef, does not persist.
  *
  * @param ksName
  * @return
  * @throws Exception
  */
 protected KsDef setupOtherKeyspace(
     Configuration configuration, String ksName, boolean addMetaData) throws Exception {
   CfDef cf = new CfDef(ksName, "OtherCf1");
   cf.setKey_validation_class("UTF8Type");
   cf.setComparator_type("UTF8Type");
   if (addMetaData) {
     cf.addToColumn_metadata(
         new ColumnDef(ByteBufferUtil.bytes("col_name_utf8"), UTF8Type.class.getName()));
     cf.addToColumn_metadata(
         new ColumnDef(ByteBufferUtil.bytes("col_name_bytes"), BytesType.class.getName()));
     cf.addToColumn_metadata(
         new ColumnDef(ByteBufferUtil.bytes("col_name_int"), IntegerType.class.getName()));
     cf.addToColumn_metadata(
         new ColumnDef(ByteBufferUtil.bytes("col_name_long"), LongType.class.getName()));
     cf.addToColumn_metadata(
         new ColumnDef(ByteBufferUtil.bytes("col_name_timeuuid"), TimeUUIDType.class.getName()));
   }
   KsDef ks = new KsDef(ksName, "org.apache.cassandra.locator.SimpleStrategy", Arrays.asList(cf));
   ks.setStrategy_options(
       KSMetaData.optsWithRF(
           configuration.getInt(CassandraClientHolder.CONF_PARAM_REPLICATION_FACTOR, 1)));
   return ks;
 }
Пример #9
0
  public boolean equals(KsDef that) {
    if (that == null) return false;

    boolean this_present_name = true && this.isSetName();
    boolean that_present_name = true && that.isSetName();
    if (this_present_name || that_present_name) {
      if (!(this_present_name && that_present_name)) return false;
      if (!this.name.equals(that.name)) return false;
    }

    boolean this_present_strategy_class = true && this.isSetStrategy_class();
    boolean that_present_strategy_class = true && that.isSetStrategy_class();
    if (this_present_strategy_class || that_present_strategy_class) {
      if (!(this_present_strategy_class && that_present_strategy_class)) return false;
      if (!this.strategy_class.equals(that.strategy_class)) return false;
    }

    boolean this_present_strategy_options = true && this.isSetStrategy_options();
    boolean that_present_strategy_options = true && that.isSetStrategy_options();
    if (this_present_strategy_options || that_present_strategy_options) {
      if (!(this_present_strategy_options && that_present_strategy_options)) return false;
      if (!this.strategy_options.equals(that.strategy_options)) return false;
    }

    boolean this_present_replication_factor = true && this.isSetReplication_factor();
    boolean that_present_replication_factor = true && that.isSetReplication_factor();
    if (this_present_replication_factor || that_present_replication_factor) {
      if (!(this_present_replication_factor && that_present_replication_factor)) return false;
      if (this.replication_factor != that.replication_factor) return false;
    }

    boolean this_present_cf_defs = true && this.isSetCf_defs();
    boolean that_present_cf_defs = true && that.isSetCf_defs();
    if (this_present_cf_defs || that_present_cf_defs) {
      if (!(this_present_cf_defs && that_present_cf_defs)) return false;
      if (!this.cf_defs.equals(that.cf_defs)) return false;
    }

    boolean this_present_durable_writes = true && this.isSetDurable_writes();
    boolean that_present_durable_writes = true && that.isSetDurable_writes();
    if (this_present_durable_writes || that_present_durable_writes) {
      if (!(this_present_durable_writes && that_present_durable_writes)) return false;
      if (this.durable_writes != that.durable_writes) return false;
    }

    return true;
  }
Пример #10
0
 @Override
 public void write(org.apache.thrift.protocol.TProtocol prot, KsDef struct)
     throws org.apache.thrift.TException {
   TTupleProtocol oprot = (TTupleProtocol) prot;
   oprot.writeString(struct.name);
   oprot.writeString(struct.strategy_class);
   {
     oprot.writeI32(struct.cf_defs.size());
     for (CfDef _iter83 : struct.cf_defs) {
       _iter83.write(oprot);
     }
   }
   BitSet optionals = new BitSet();
   if (struct.isSetStrategy_options()) {
     optionals.set(0);
   }
   if (struct.isSetReplication_factor()) {
     optionals.set(1);
   }
   if (struct.isSetDurable_writes()) {
     optionals.set(2);
   }
   oprot.writeBitSet(optionals, 3);
   if (struct.isSetStrategy_options()) {
     {
       oprot.writeI32(struct.strategy_options.size());
       for (Map.Entry<String, String> _iter84 : struct.strategy_options.entrySet()) {
         oprot.writeString(_iter84.getKey());
         oprot.writeString(_iter84.getValue());
       }
     }
   }
   if (struct.isSetReplication_factor()) {
     oprot.writeI32(struct.replication_factor);
   }
   if (struct.isSetDurable_writes()) {
     oprot.writeBool(struct.durable_writes);
   }
 }
Пример #11
0
  static void checkAndSetReplicationFactor(
      Cassandra.Client client,
      KsDef ks,
      boolean freshInstance,
      int desiredRf,
      boolean safetyDisabled)
      throws InvalidRequestException, SchemaDisagreementException, TException {
    if (freshInstance) {
      Set<String> dcs = CassandraVerifier.sanityCheckDatacenters(client, desiredRf, safetyDisabled);
      // If RF exceeds # hosts, then Cassandra will reject writes
      ks.setStrategy_options(Maps2.createConstantValueMap(dcs, String.valueOf(desiredRf)));
      return;
    }

    final Set<String> dcs;
    if (CassandraConstants.SIMPLE_STRATEGY.equals(ks.getStrategy_class())) {
      int currentRF =
          Integer.parseInt(
              ks.getStrategy_options().get(CassandraConstants.REPLICATION_FACTOR_OPTION));
      String errorMessage =
          "This cassandra cluster is running using the simple partitioning stragegy.  "
              + "This partitioner is not rack aware and is not intended for use on prod.  "
              + "This will have to be fixed by manually configuring to the network partitioner "
              + "and running the appropriate repairs.  "
              + "Contact the AtlasDB team to perform these steps.";
      if (currentRF != 1) {
        logErrorOrThrow(errorMessage, safetyDisabled);
      }
      // Automatically convert RF=1 to look like network partitioner.
      dcs = CassandraVerifier.sanityCheckDatacenters(client, desiredRf, safetyDisabled);
      if (dcs.size() > 1) {
        logErrorOrThrow(errorMessage, safetyDisabled);
      }
      if (!safetyDisabled) {
        ks.setStrategy_class(CassandraConstants.NETWORK_STRATEGY);
        ks.setStrategy_options(ImmutableMap.of(dcs.iterator().next(), "1"));
      }
    } else {
      dcs = CassandraVerifier.sanityCheckDatacenters(client, desiredRf, safetyDisabled);
    }

    Map<String, String> strategyOptions = Maps.newHashMap(ks.getStrategy_options());
    for (String dc : dcs) {
      if (strategyOptions.get(dc) == null) {
        logErrorOrThrow(
            "The datacenter for this cassandra cluster is invalid. "
                + " failed dc: "
                + dc
                + "  strategyOptions: "
                + strategyOptions,
            safetyDisabled);
      }
    }

    String dc = dcs.iterator().next();

    int currentRF = Integer.parseInt(strategyOptions.get(dc));

    // We need to worry about user not running repair and user skipping replication levels.
    if (currentRF == 1 && desiredRf == 2) {
      log.error(
          "Upping AtlasDB replication factor from 1 to 2. User should run "
              + "`nodetool repair` on cluster if they have not already!");
      strategyOptions.put(dc, String.valueOf(desiredRf));
      ks.setStrategy_options(strategyOptions);
    } else if (currentRF == 1 && desiredRf == CassandraConstants.DEFAULT_REPLICATION_FACTOR) {
      log.error(
          "Upping AtlasDB replication factor from 1 "
              + "to 3 is NOT allowed directly.\n"
              + "Increase replication factor to 2 first, then run `nodetool repair`. If it succeeds, increase replication factor to 3, and run `nodetool repair`");
    } else if (currentRF == 2 && desiredRf == CassandraConstants.DEFAULT_REPLICATION_FACTOR) {
      strategyOptions.put(dc, String.valueOf(desiredRf));
      ks.setStrategy_options(strategyOptions);
      ks.setCf_defs(ImmutableList.<CfDef>of());
      client.system_update_keyspace(ks);
      log.warn(
          "Updating AtlasDB replication factor from "
              + currentRF
              + " to "
              + desiredRf
              + " process are NOT completed!"
              + " User may want to run `nodetool repair` to make all replicas consistent.");
    } else if (currentRF
        > desiredRf) { // We are moving to a lower RF, this should be always safe from a consistency
                       // rule standpoint
      log.error(
          "Reducing AtlasDB replication factor from "
              + currentRF
              + " to "
              + desiredRf
              + ". User may want to run `nodetool cleanup` to remove excess replication.");
      strategyOptions.put(dc, String.valueOf(desiredRf));
      ks.setStrategy_options(strategyOptions);
      ks.setCf_defs(ImmutableList.<CfDef>of());
      client.system_update_keyspace(ks);
    } else if (currentRF == desiredRf) {
      log.info("Did not change AtlasDB replication factor.");
    } else {
      logErrorOrThrow(
          "We only support replication up to 3.  Attempted to go from "
              + currentRF
              + " to "
              + desiredRf
              + ".",
          safetyDisabled);
    }
  }
Пример #12
0
    public void read(org.apache.thrift.protocol.TProtocol iprot, KsDef struct)
        throws org.apache.thrift.TException {
      org.apache.thrift.protocol.TField schemeField;
      iprot.readStructBegin();
      while (true) {
        schemeField = iprot.readFieldBegin();
        if (schemeField.type == org.apache.thrift.protocol.TType.STOP) {
          break;
        }
        switch (schemeField.id) {
          case 1: // NAME
            if (schemeField.type == org.apache.thrift.protocol.TType.STRING) {
              struct.name = iprot.readString();
              struct.setNameIsSet(true);
            } else {
              org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
            }
            break;
          case 2: // STRATEGY_CLASS
            if (schemeField.type == org.apache.thrift.protocol.TType.STRING) {
              struct.strategy_class = iprot.readString();
              struct.setStrategy_classIsSet(true);
            } else {
              org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
            }
            break;
          case 3: // STRATEGY_OPTIONS
            if (schemeField.type == org.apache.thrift.protocol.TType.MAP) {
              {
                org.apache.thrift.protocol.TMap _map74 = iprot.readMapBegin();
                struct.strategy_options = new HashMap<String, String>(2 * _map74.size);
                for (int _i75 = 0; _i75 < _map74.size; ++_i75) {
                  String _key76; // required
                  String _val77; // required
                  _key76 = iprot.readString();
                  _val77 = iprot.readString();
                  struct.strategy_options.put(_key76, _val77);
                }
                iprot.readMapEnd();
              }
              struct.setStrategy_optionsIsSet(true);
            } else {
              org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
            }
            break;
          case 4: // REPLICATION_FACTOR
            if (schemeField.type == org.apache.thrift.protocol.TType.I32) {
              struct.replication_factor = iprot.readI32();
              struct.setReplication_factorIsSet(true);
            } else {
              org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
            }
            break;
          case 5: // CF_DEFS
            if (schemeField.type == org.apache.thrift.protocol.TType.LIST) {
              {
                org.apache.thrift.protocol.TList _list78 = iprot.readListBegin();
                struct.cf_defs = new ArrayList<CfDef>(_list78.size);
                for (int _i79 = 0; _i79 < _list78.size; ++_i79) {
                  CfDef _elem80; // required
                  _elem80 = new CfDef();
                  _elem80.read(iprot);
                  struct.cf_defs.add(_elem80);
                }
                iprot.readListEnd();
              }
              struct.setCf_defsIsSet(true);
            } else {
              org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
            }
            break;
          case 6: // DURABLE_WRITES
            if (schemeField.type == org.apache.thrift.protocol.TType.BOOL) {
              struct.durable_writes = iprot.readBool();
              struct.setDurable_writesIsSet(true);
            } else {
              org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
            }
            break;
          default:
            org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
        }
        iprot.readFieldEnd();
      }
      iprot.readStructEnd();

      // check for required fields of primitive type, which can't be checked in the validate method
      struct.validate();
    }
Пример #13
0
  public int compareTo(KsDef other) {
    if (!getClass().equals(other.getClass())) {
      return getClass().getName().compareTo(other.getClass().getName());
    }

    int lastComparison = 0;
    KsDef typedOther = (KsDef) other;

    lastComparison = Boolean.valueOf(isSetName()).compareTo(typedOther.isSetName());
    if (lastComparison != 0) {
      return lastComparison;
    }
    if (isSetName()) {
      lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.name, typedOther.name);
      if (lastComparison != 0) {
        return lastComparison;
      }
    }
    lastComparison =
        Boolean.valueOf(isSetStrategy_class()).compareTo(typedOther.isSetStrategy_class());
    if (lastComparison != 0) {
      return lastComparison;
    }
    if (isSetStrategy_class()) {
      lastComparison =
          org.apache.thrift.TBaseHelper.compareTo(this.strategy_class, typedOther.strategy_class);
      if (lastComparison != 0) {
        return lastComparison;
      }
    }
    lastComparison =
        Boolean.valueOf(isSetStrategy_options()).compareTo(typedOther.isSetStrategy_options());
    if (lastComparison != 0) {
      return lastComparison;
    }
    if (isSetStrategy_options()) {
      lastComparison =
          org.apache.thrift.TBaseHelper.compareTo(
              this.strategy_options, typedOther.strategy_options);
      if (lastComparison != 0) {
        return lastComparison;
      }
    }
    lastComparison =
        Boolean.valueOf(isSetReplication_factor()).compareTo(typedOther.isSetReplication_factor());
    if (lastComparison != 0) {
      return lastComparison;
    }
    if (isSetReplication_factor()) {
      lastComparison =
          org.apache.thrift.TBaseHelper.compareTo(
              this.replication_factor, typedOther.replication_factor);
      if (lastComparison != 0) {
        return lastComparison;
      }
    }
    lastComparison = Boolean.valueOf(isSetCf_defs()).compareTo(typedOther.isSetCf_defs());
    if (lastComparison != 0) {
      return lastComparison;
    }
    if (isSetCf_defs()) {
      lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.cf_defs, typedOther.cf_defs);
      if (lastComparison != 0) {
        return lastComparison;
      }
    }
    lastComparison =
        Boolean.valueOf(isSetDurable_writes()).compareTo(typedOther.isSetDurable_writes());
    if (lastComparison != 0) {
      return lastComparison;
    }
    if (isSetDurable_writes()) {
      lastComparison =
          org.apache.thrift.TBaseHelper.compareTo(this.durable_writes, typedOther.durable_writes);
      if (lastComparison != 0) {
        return lastComparison;
      }
    }
    return 0;
  }
Пример #14
0
 @Override
 public void read(org.apache.thrift.protocol.TProtocol prot, KsDef struct)
     throws org.apache.thrift.TException {
   TTupleProtocol iprot = (TTupleProtocol) prot;
   struct.name = iprot.readString();
   struct.setNameIsSet(true);
   struct.strategy_class = iprot.readString();
   struct.setStrategy_classIsSet(true);
   {
     org.apache.thrift.protocol.TList _list85 =
         new org.apache.thrift.protocol.TList(
             org.apache.thrift.protocol.TType.STRUCT, iprot.readI32());
     struct.cf_defs = new ArrayList<CfDef>(_list85.size);
     for (int _i86 = 0; _i86 < _list85.size; ++_i86) {
       CfDef _elem87; // required
       _elem87 = new CfDef();
       _elem87.read(iprot);
       struct.cf_defs.add(_elem87);
     }
   }
   struct.setCf_defsIsSet(true);
   BitSet incoming = iprot.readBitSet(3);
   if (incoming.get(0)) {
     {
       org.apache.thrift.protocol.TMap _map88 =
           new org.apache.thrift.protocol.TMap(
               org.apache.thrift.protocol.TType.STRING,
               org.apache.thrift.protocol.TType.STRING,
               iprot.readI32());
       struct.strategy_options = new HashMap<String, String>(2 * _map88.size);
       for (int _i89 = 0; _i89 < _map88.size; ++_i89) {
         String _key90; // required
         String _val91; // required
         _key90 = iprot.readString();
         _val91 = iprot.readString();
         struct.strategy_options.put(_key90, _val91);
       }
     }
     struct.setStrategy_optionsIsSet(true);
   }
   if (incoming.get(1)) {
     struct.replication_factor = iprot.readI32();
     struct.setReplication_factorIsSet(true);
   }
   if (incoming.get(2)) {
     struct.durable_writes = iprot.readBool();
     struct.setDurable_writesIsSet(true);
   }
 }