Exemplo n.º 1
0
def create_keyspaces(replication):
    """ 
  Creates keyspace which AppScale uses for storing application 
  and user data

  Args:
    replication: Replication factor for Cassandra
  Raises:
    AppScaleBadArg: When args are bad
  """
    if int(replication) <= 0:
        raise dbconstants.AppScaleBadArg(
            "Replication must be greater than zero")

    print "Creating Cassandra Key Spaces"

    # Set this to False to keep data from a previous deployment. Setting it
    # it to True will remove previous tables.
    _DROP_TABLES = True

    # TODO use shared library to get constants
    host = helper_functions.read_file('/etc/appscale/my_private_ip')

    sysman = system_manager.SystemManager(host + ":" +\
                str(cassandra_interface.CASS_DEFAULT_PORT))

    if _DROP_TABLES:
        try:
            sysman.drop_keyspace(cassandra_interface.KEYSPACE)
        except pycassa.cassandra.ttypes.InvalidRequestException, e:
            pass
def create_keyspaces(replication):
  """ 
  Creates keyspace which AppScale uses for storing application 
  and user data

  Args:
    replication: Replication factor for Cassandra
  Raises:
    AppScaleBadArg: When args are bad
  """
  if int(replication) <= 0: 
    raise dbconstants.AppScaleBadArg("Replication must be greater than zero")

  print "Creating Cassandra Key Spaces" 

  # TODO use shared library to get constants
  host = file_io.read('/etc/appscale/my_private_ip')

  sysman = system_manager.SystemManager(host + ":" +\
              str(cassandra_interface.CASS_DEFAULT_PORT))

  try:
    sysman.create_keyspace(cassandra_interface.KEYSPACE, 
                      pycassa.SIMPLE_STRATEGY, 
                      {'replication_factor':str(replication)})

    # This column family is for testing for functional testing
    sysman.create_column_family(cassandra_interface.KEYSPACE, 
                           cassandra_interface.STANDARD_COL_FAM, 
                           comparator_type=system_manager.UTF8_TYPE)

    for table_name in dbconstants.INITIAL_TABLES:
      sysman.create_column_family(cassandra_interface.KEYSPACE, 
                               table_name,
                               comparator_type=system_manager.UTF8_TYPE)
  
    sysman.close()
  # TODO: Figure out the exact exceptions we're trying to catch in the 
  # case where we are doing data persistance
  except Exception, e:
    sysman.close()
    # TODO: Figure out the exact exceptions we're trying to catch in the 
    print "Received an exception of type " + str(e.__class__) +\
          " with message: " + str(e)
Exemplo n.º 3
0
def prime_cassandra(replication):
    """ Create Cassandra keyspace and initial tables.

  Args:
    replication: An integer specifying the replication factor for the keyspace.
  Raises:
    AppScaleBadArg if replication factor is not greater than 0.
    TypeError if replication is not an integer.
  """
    if not isinstance(replication, int):
        raise TypeError('Replication must be an integer')

    if int(replication) <= 0:
        raise dbconstants.AppScaleBadArg(
            'Replication must be greater than zero')

    hosts = appscale_info.get_db_ips()

    session = None
    remaining_retries = INITIAL_CONNECT_RETRIES
    while True:
        try:
            # Cassandra 2.1 only supports up to Protocol Version 3.
            cluster = Cluster(hosts, protocol_version=3)
            session = cluster.connect()
            break
        except cassandra.cluster.NoHostAvailable as connection_error:
            remaining_retries -= 1
            if remaining_retries < 0:
                raise connection_error
            time.sleep(3)
    session.default_consistency_level = ConsistencyLevel.QUORUM

    create_keyspace = """
    CREATE KEYSPACE IF NOT EXISTS "{keyspace}"
    WITH REPLICATION = %(replication)s
  """.format(keyspace=KEYSPACE)
    keyspace_replication = {
        'class': 'SimpleStrategy',
        'replication_factor': replication
    }
    session.execute(create_keyspace, {'replication': keyspace_replication})
    session.set_keyspace(KEYSPACE)

    for table in dbconstants.INITIAL_TABLES:
        create_table = """
      CREATE TABLE IF NOT EXISTS "{table}" (
        {key} blob,
        {column} text,
        {value} blob,
        PRIMARY KEY ({key}, {column})
      ) WITH COMPACT STORAGE
    """.format(table=table,
               key=ThriftColumn.KEY,
               column=ThriftColumn.COLUMN_NAME,
               value=ThriftColumn.VALUE)
        logging.info('Trying to create {}'.format(table))
        session.execute(create_table)

    create_pull_queue_tables(session)

    first_entity = session.execute('SELECT * FROM "{}" LIMIT 1'.format(
        dbconstants.APP_ENTITY_TABLE))
    existing_entities = len(list(first_entity)) == 1

    define_ua_schema(session)

    metadata_insert = """
    INSERT INTO "{table}" ({key}, {column}, {value})
    VALUES (%(key)s, %(column)s, %(value)s)
  """.format(table=dbconstants.DATASTORE_METADATA_TABLE,
             key=ThriftColumn.KEY,
             column=ThriftColumn.COLUMN_NAME,
             value=ThriftColumn.VALUE)

    if not existing_entities:
        parameters = {
            'key': bytearray(cassandra_interface.VERSION_INFO_KEY),
            'column': cassandra_interface.VERSION_INFO_KEY,
            'value': bytearray(str(POST_JOURNAL_VERSION))
        }
        session.execute(metadata_insert, parameters)

    # Indicate that the database has been successfully primed.
    parameters = {
        'key': bytearray(cassandra_interface.PRIMED_KEY),
        'column': cassandra_interface.PRIMED_KEY,
        'value': bytearray('true')
    }
    session.execute(metadata_insert, parameters)
    logging.info('Cassandra is primed.')