def setup_databases(self, **kwargs): settings.KEYSPACE = settings.KEYSPACE_TESTS print "Patched value of KEYSPACE to '{0}'".format(settings.KEYSPACE) if "dont_drop_daedalus_tests" not in os.environ: sys_mgr = SystemManager() try: sys_mgr.drop_keyspace(settings.KEYSPACE) print "Keyspace {0} droped OK".format(settings.KEYSPACE) except: pass finally: sys_mgr.close() sys_mgr = SystemManager() # keyspace 'settings.KEYSPACE' shouldn't exists if settings.KEYSPACE in sys_mgr.list_keyspaces(): print "ERROR: keyspace {0} EXISTS after been droped".format( settings.KEYSPACE) print " - Keyspaces:", sys_mgr.list_keyspaces() assert False, "ERROR" sys_mgr.close() storage.get_service().create_keyspace_and_cfs() return super(CassandraDjangoTestSuiteRunner, self).setup_databases(**kwargs)
def get_connection(): """ Creates a connection to Cassandra. Returs: pool """ cassandra_host = os.environ.get('CASSANDRA_HOST', 'localhost') sys_mgr = SystemManager() try: sys_mgr.describe_ring(KEYSPACE) except: sys_mgr.create_keyspace(KEYSPACE, SIMPLE_STRATEGY, {'replication_factor': '1'}) pool = ConnectionPool(KEYSPACE, server_list=[cassandra_host]) for cf_name in [CF_LOGS, CF_LOGS_BY_APP, CF_LOGS_BY_HOST, CF_LOGS_BY_SEVERITY]: try: cf = ColumnFamily(pool, cf_name) except: sys_mgr.create_column_family(KEYSPACE, cf_name, comparator_type=TimeUUIDType()) cf = ColumnFamily(pool, cf_name) cf.get_count(str(uuid.uuid4())) sys_mgr.close() return pool
def setup_class(cls): sys = SystemManager() sys.create_column_family(TEST_KS, 'SuperLongSubLong', super=True, comparator_type=LongType(), subcomparator_type=LongType()) sys.create_column_family(TEST_KS, 'SuperLongSubInt', super=True, comparator_type=LongType(), subcomparator_type=IntegerType()) sys.create_column_family(TEST_KS, 'SuperLongSubBigInt', super=True, comparator_type=LongType(), subcomparator_type=IntegerType()) sys.create_column_family(TEST_KS, 'SuperLongSubTime', super=True, comparator_type=LongType(), subcomparator_type=TimeUUIDType()) sys.create_column_family(TEST_KS, 'SuperLongSubLex', super=True, comparator_type=LongType(), subcomparator_type=LexicalUUIDType()) sys.create_column_family(TEST_KS, 'SuperLongSubAscii', super=True, comparator_type=LongType(), subcomparator_type=AsciiType()) sys.create_column_family(TEST_KS, 'SuperLongSubUTF8', super=True, comparator_type=LongType(), subcomparator_type=UTF8Type()) sys.create_column_family(TEST_KS, 'SuperLongSubBytes', super=True, comparator_type=LongType(), subcomparator_type=BytesType()) sys.close() cls.cf_suplong_sublong = ColumnFamily(pool, 'SuperLongSubLong') cls.cf_suplong_subint = ColumnFamily(pool, 'SuperLongSubInt') cls.cf_suplong_subbigint = ColumnFamily(pool, 'SuperLongSubBigInt') cls.cf_suplong_subtime = ColumnFamily(pool, 'SuperLongSubTime') cls.cf_suplong_sublex = ColumnFamily(pool, 'SuperLongSubLex') cls.cf_suplong_subascii = ColumnFamily(pool, 'SuperLongSubAscii') cls.cf_suplong_subutf8 = ColumnFamily(pool, 'SuperLongSubUTF8') cls.cf_suplong_subbytes = ColumnFamily(pool, 'SuperLongSubBytes') cls.cfs = [cls.cf_suplong_subint, cls.cf_suplong_subint, cls.cf_suplong_subtime, cls.cf_suplong_sublex, cls.cf_suplong_subascii, cls.cf_suplong_subutf8, cls.cf_suplong_subbytes]
def CreateCF(connStr, keySpace, cfs, columesTypeList = None): sysmgt = SystemManager(connStr) list = sysmgt.list_keyspaces() if keySpace in list: #print('pyagent is existed') pass else: sysmgt.create_keyspace(keySpace, SIMPLE_STRATEGY, {'replication_factor': '3'}) cfDict = sysmgt.get_keyspace_column_families(keySpace) allCF = cfDict.keys() for i in range(len(cfs)): columnFamily = cfs[i] #print(columnFamily) if columesTypeList != None: columes = columesTypeList[i] else: columes = None if columnFamily in allCF: #print(columnFamily + ' is existed') pass else: #print(columnFamily + ' is creating') sysmgt.create_column_family(keySpace, columnFamily, super=False, comparator_type=UTF8_TYPE, key_validation_class=ASCII_TYPE, default_validation_class=UTF8_TYPE, column_validation_classes=columes, gc_grace_seconds=1000) sysmgt.close()
def create_cfs(self): """ Creates the Cassandra Column Families (if not exist) """ sys_mgr = None pool = None try: sys_mgr = SystemManager() pool = ConnectionPool(settings.KEYSPACE, server_list=settings.CASSANDRA_HOSTS) for cf_name in [ CF_LOGS, CF_LOGS_BY_APP, CF_LOGS_BY_HOST, CF_LOGS_BY_SEVERITY ]: try: cf = ColumnFamily(pool, cf_name) except: logger.info("create_cfs(): Creating column family %s", cf_name) sys_mgr.create_column_family( settings.KEYSPACE, cf_name, comparator_type=TimeUUIDType()) cf = ColumnFamily(pool, cf_name) cf.get_count(str(uuid.uuid4())) finally: if pool: pool.dispose() if sys_mgr: sys_mgr.close()
def setup_class(cls): sys = SystemManager() sys.create_column_family(TEST_KS, 'StdLong', comparator_type=LongType()) sys.create_column_family(TEST_KS, 'StdInteger', comparator_type=IntegerType()) sys.create_column_family(TEST_KS, 'StdBigInteger', comparator_type=IntegerType()) sys.create_column_family(TEST_KS, 'StdDecimal', comparator_type=DecimalType()) sys.create_column_family(TEST_KS, 'StdTimeUUID', comparator_type=TimeUUIDType()) sys.create_column_family(TEST_KS, 'StdLexicalUUID', comparator_type=LexicalUUIDType()) sys.create_column_family(TEST_KS, 'StdAscii', comparator_type=AsciiType()) sys.create_column_family(TEST_KS, 'StdUTF8', comparator_type=UTF8Type()) sys.create_column_family(TEST_KS, 'StdBytes', comparator_type=BytesType()) sys.create_column_family(TEST_KS, 'StdComposite', comparator_type=CompositeType(LongType(), BytesType())) sys.close() cls.cf_long = ColumnFamily(pool, 'StdLong') cls.cf_int = ColumnFamily(pool, 'StdInteger') cls.cf_big_int = ColumnFamily(pool, 'StdBigInteger') cls.cf_decimal = ColumnFamily(pool, 'StdDecimal') cls.cf_time = ColumnFamily(pool, 'StdTimeUUID') cls.cf_lex = ColumnFamily(pool, 'StdLexicalUUID') cls.cf_ascii = ColumnFamily(pool, 'StdAscii') cls.cf_utf8 = ColumnFamily(pool, 'StdUTF8') cls.cf_bytes = ColumnFamily(pool, 'StdBytes') cls.cf_composite = ColumnFamily(pool, 'StdComposite') cls.cfs = [cls.cf_long, cls.cf_int, cls.cf_time, cls.cf_lex, cls.cf_ascii, cls.cf_utf8, cls.cf_bytes, cls.cf_composite]
def create(): keyspace = configuration.cassandra_keyspace mgr = SystemManager() try: workaround_1779(mgr.create_column_family, keyspace, 'Indexes', comparator_type=UTF8_TYPE) workaround_1779(mgr.create_column_family, keyspace, 'Stacktrace', comparator_type=UTF8_TYPE) workaround_1779(mgr.create_column_family, keyspace, 'AwaitingRetrace', comparator_type=UTF8_TYPE) finally: mgr.close()
def _create_cf(self, cf_name): sys_mng = SystemManager('localhost:9160') validators = {'name': UTF8_TYPE, 'content': UTF8_TYPE, 'post_time': UTF8_TYPE} sys_mng.create_column_family( 'bbs', cf_name, super=False, comparator_type=UTF8_TYPE, key_validation_class=UTF8_TYPE, column_validation_classes=validators ) sys_mng.close()
def handle(self, **options): """ :param options: """ sys = SystemManager(server=CASSANDRA_HOSTS[0]) sys.create_column_family(CASSANDRA_SESSIONS_KEYSPACE, CASSANDRA_SESSIONS_COLUMN_FAMILY, key_validation_class=types.UTF8Type(), column_validation_classes={ 'session_data': types.UTF8Type() }) sys.close()
def test_big_batched_writes(): ## this is an m1.xlarge doing nothing but supporting this test server = 'localhost:9160' keyspace = 'testkeyspace_' + getpass.getuser().replace('-', '_') family = 'testcf' sm = SystemManager(server) try: sm.drop_keyspace(keyspace) except pycassa.InvalidRequestException: pass sm.create_keyspace(keyspace, SIMPLE_STRATEGY, {'replication_factor': '1'}) sm.create_column_family(keyspace, family, super=False, key_validation_class = LEXICAL_UUID_TYPE, default_validation_class = LEXICAL_UUID_TYPE, column_name_class = ASCII_TYPE) sm.alter_column(keyspace, family, 'test', ASCII_TYPE) sm.close() pool = ConnectionPool(keyspace, [server], max_retries=10, pool_timeout=0, pool_size=10, timeout=120) pool.fill() pool.add_listener( Listener() ) ## assert that we are using framed transport conn = pool._q.get() assert isinstance(conn.transport, thrift.transport.TTransport.TFramedTransport) pool._q.put(conn) try: for num_rows in range(14, 20): ## write some data to cassandra using increasing data sizes one_mb = ' ' * 2**20 rows = [] for i in xrange(num_rows): key = uuid.uuid4() rows.append((key, dict(test=one_mb))) testcf = pycassa.ColumnFamily(pool, family) with testcf.batch() as batch: for (key, data_dict) in rows: data_size = len(data_dict.values()[0]) logger.critical('adding %r with %.6f MB' % (key, float(data_size)/2**20)) batch.insert(key, data_dict) logger.critical('%d rows written' % num_rows) finally: sm = SystemManager(server) try: sm.drop_keyspace(keyspace) except pycassa.InvalidRequestException: pass sm.close() logger.critical('clearing test keyspace: %r' % keyspace)
def test_add_remove_counter(self): sys = SystemManager() sys.create_column_family(TEST_KS, 'KeyLongCounter', key_validation_class=LongType(), default_validation_class='CounterColumnType') sys.close() cf_long = ColumnFamily(pool, 'KeyLongCounter') key = 1111111111111111L cf_long.add(key, 'col') assert_equal(cf_long.get(key), {'col': 1}) cf_long.remove_counter(key, 'col') time.sleep(0.1) assert_raises(NotFoundException, cf_long.get, key)
def create_keyspace(self): """ Creates the Cassandra Keyspace (if not exist) """ sys_mgr = None try: sys_mgr = SystemManager() try: sys_mgr.describe_ring(settings.KEYSPACE) except: logger.info("create_keyspace(): Creating keyspace %s", settings.KEYSPACE) sys_mgr.create_keyspace(settings.KEYSPACE, SIMPLE_STRATEGY, {'replication_factor': '1'}) finally: if sys_mgr: sys_mgr.close()
def test_default_validated_columns(self): sys = SystemManager() sys.create_column_family(TEST_KS, 'DefaultValidator', default_validation_class=LongType()) sys.alter_column(TEST_KS, 'DefaultValidator', 'subcol', TimeUUIDType()) sys.close() cf = ColumnFamily(pool, 'DefaultValidator') key = 'key1' col_cf = {'aaaaaa': 1L} col_cm = {'subcol': TIME1} col_ncf = {'aaaaaa': TIME1} # Both of these inserts work, as cf allows # longs and cm for 'subcol' allows TIMEUUIDs. cf.insert(key, col_cf) cf.insert(key, col_cm) assert_equal(cf.get(key), {'aaaaaa': 1L, 'subcol': TIME1})
def _create_column_family(self, family, bytes_columns=[], key_validation_class=TIME_UUID_TYPE): ''' Creates a column family of the name 'family' and sets any of the names in the bytes_column list to have the BYTES_TYPE. key_validation_class defaults to TIME_UUID_TYPE and could also be ASCII_TYPE for md5 hash keys, like we use for 'inbound' ''' sm = SystemManager(random.choice(self.server_list)) # sys.create_column_family(self.namespace, family, super=False) sm.create_column_family(self.namespace, family, super=False, key_validation_class = key_validation_class, default_validation_class = TIME_UUID_TYPE, column_name_class = ASCII_TYPE) for column in bytes_columns: sm.alter_column(self.namespace, family, column, BYTES_TYPE) sm.close()
def test_validated_columns(self): sys = SystemManager() sys.create_column_family(TEST_KS, 'Validators',) sys.alter_column(TEST_KS, 'Validators', 'long', LongType()) sys.alter_column(TEST_KS, 'Validators', 'int', IntegerType()) sys.alter_column(TEST_KS, 'Validators', 'time', TimeUUIDType()) sys.alter_column(TEST_KS, 'Validators', 'lex', LexicalUUIDType()) sys.alter_column(TEST_KS, 'Validators', 'ascii', AsciiType()) sys.alter_column(TEST_KS, 'Validators', 'utf8', UTF8Type()) sys.alter_column(TEST_KS, 'Validators', 'bytes', BytesType()) sys.close() cf = ColumnFamily(pool, 'Validators') key = 'key1' col = {'long': 1L} cf.insert(key, col) assert_equal(cf.get(key)['long'], 1L) col = {'int': 1} cf.insert(key, col) assert_equal(cf.get(key)['int'], 1) col = {'time': TIME1} cf.insert(key, col) assert_equal(cf.get(key)['time'], TIME1) col = {'lex': uuid.UUID(bytes='aaa aaa aaa aaaa')} cf.insert(key, col) assert_equal(cf.get(key)['lex'], uuid.UUID(bytes='aaa aaa aaa aaaa')) col = {'ascii': 'aaa'} cf.insert(key, col) assert_equal(cf.get(key)['ascii'], 'aaa') col = {'utf8': u'a\u0020'} cf.insert(key, col) assert_equal(cf.get(key)['utf8'], u'a\u0020') col = {'bytes': 'aaa'} cf.insert(key, col) assert_equal(cf.get(key)['bytes'], 'aaa') cf.remove(key)
def create_cfs(self): """ Creates the Cassandra Column Families (if not exist) """ sys_mgr = None pool = None try: sys_mgr = SystemManager() pool = ConnectionPool(settings.KEYSPACE, server_list=settings.CASSANDRA_HOSTS) for cf_name in [CF_LOGS, CF_LOGS_BY_APP, CF_LOGS_BY_HOST, CF_LOGS_BY_SEVERITY]: try: cf = ColumnFamily(pool, cf_name) except: logger.info("create_cfs(): Creating column family %s", cf_name) sys_mgr.create_column_family(settings.KEYSPACE, cf_name, comparator_type=TimeUUIDType()) cf = ColumnFamily(pool, cf_name) cf.get_count(str(uuid.uuid4())) finally: if pool: pool.dispose() if sys_mgr: sys_mgr.close()
def setup_package(): sys = SystemManager() if TEST_KS in sys.list_keyspaces(): sys.drop_keyspace(TEST_KS) try: sys.create_keyspace(TEST_KS, 'SimpleStrategy', {'replication_factor': '1'}) sys.create_column_family(TEST_KS, 'Standard1') sys.create_column_family(TEST_KS, 'Super1', super=True) sys.create_column_family(TEST_KS, 'Indexed1') sys.create_index(TEST_KS, 'Indexed1', 'birthdate', 'LongType') sys.create_column_family(TEST_KS, 'Counter1', default_validation_class='CounterColumnType') sys.create_column_family(TEST_KS, 'SuperCounter1', super=True, default_validation_class='CounterColumnType') except Exception as e: print(e) try: sys.drop_keyspace(TEST_KS) except: pass raise e sys.close()
def test_validation_with_packed_names(self): """ Make sure that validated columns are packed correctly when the column names themselves must be packed """ sys = SystemManager() sys.create_column_family(TEST_KS, 'Validators2', comparator_type=LongType(), default_validation_class=LongType()) sys.alter_column(TEST_KS, 'Validators2', 1, TimeUUIDType()) sys.close() my_uuid = uuid.uuid1() cf = ColumnFamily(pool, 'Validators2') cf.insert('key', {0: 0}) assert_equal(cf.get('key'), {0: 0}) cf.insert('key', {1: my_uuid}) assert_equal(cf.get('key'), {0: 0, 1: my_uuid}) cf.insert('key', {0: 0, 1: my_uuid}) assert_equal(cf.get('key'), {0: 0, 1: my_uuid})
def setup_class(cls): sys = SystemManager() sys.create_column_family(TEST_KS, 'KeyLong', key_validation_class=LongType()) sys.create_column_family(TEST_KS, 'KeyInteger', key_validation_class=IntegerType()) sys.create_column_family(TEST_KS, 'KeyTimeUUID', key_validation_class=TimeUUIDType()) sys.create_column_family(TEST_KS, 'KeyLexicalUUID', key_validation_class=LexicalUUIDType()) sys.create_column_family(TEST_KS, 'KeyAscii', key_validation_class=AsciiType()) sys.create_column_family(TEST_KS, 'KeyUTF8', key_validation_class=UTF8Type()) sys.create_column_family(TEST_KS, 'KeyBytes', key_validation_class=BytesType()) sys.close() cls.cf_long = ColumnFamily(pool, 'KeyLong') cls.cf_int = ColumnFamily(pool, 'KeyInteger') cls.cf_time = ColumnFamily(pool, 'KeyTimeUUID') cls.cf_lex = ColumnFamily(pool, 'KeyLexicalUUID') cls.cf_ascii = ColumnFamily(pool, 'KeyAscii') cls.cf_utf8 = ColumnFamily(pool, 'KeyUTF8') cls.cf_bytes = ColumnFamily(pool, 'KeyBytes') cls.cfs = [cls.cf_long, cls.cf_int, cls.cf_time, cls.cf_lex, cls.cf_ascii, cls.cf_utf8, cls.cf_bytes]
def _create_column_family(self, family, bytes_columns=[], key_validation_class=TIME_UUID_TYPE): ''' Creates a column family of the name 'family' and sets any of the names in the bytes_column list to have the BYTES_TYPE. key_validation_class defaults to TIME_UUID_TYPE and could also be ASCII_TYPE for md5 hash keys, like we use for 'inbound' ''' sm = SystemManager(random.choice(self.server_list)) # sys.create_column_family(self.namespace, family, super=False) sm.create_column_family(self.namespace, family, super=False, key_validation_class=key_validation_class, default_validation_class=TIME_UUID_TYPE, column_name_class=ASCII_TYPE) for column in bytes_columns: sm.alter_column(self.namespace, family, column, BYTES_TYPE) sm.close()
def setup_databases(self, **kwargs): settings.KEYSPACE = settings.KEYSPACE_TESTS print "Patched value of KEYSPACE to '{0}'".format(settings.KEYSPACE) if "dont_drop_daedalus_tests" not in os.environ: sys_mgr = SystemManager() try: sys_mgr.drop_keyspace(settings.KEYSPACE) print "Keyspace {0} droped OK".format(settings.KEYSPACE) except: pass finally: sys_mgr.close() sys_mgr = SystemManager() # keyspace 'settings.KEYSPACE' shouldn't exists if settings.KEYSPACE in sys_mgr.list_keyspaces(): print "ERROR: keyspace {0} EXISTS after been droped".format(settings.KEYSPACE) print " - Keyspaces:", sys_mgr.list_keyspaces() assert False, "ERROR" sys_mgr.close() storage.get_service().create_keyspace_and_cfs() return super(CassandraDjangoTestSuiteRunner, self).setup_databases(**kwargs)
def setup_class(cls): sys = SystemManager() sys.create_column_family(TEST_KS, 'SuperLong', super=True, comparator_type=LongType()) sys.create_column_family(TEST_KS, 'SuperInt', super=True, comparator_type=IntegerType()) sys.create_column_family(TEST_KS, 'SuperBigInt', super=True, comparator_type=IntegerType()) sys.create_column_family(TEST_KS, 'SuperTime', super=True, comparator_type=TimeUUIDType()) sys.create_column_family(TEST_KS, 'SuperLex', super=True, comparator_type=LexicalUUIDType()) sys.create_column_family(TEST_KS, 'SuperAscii', super=True, comparator_type=AsciiType()) sys.create_column_family(TEST_KS, 'SuperUTF8', super=True, comparator_type=UTF8Type()) sys.create_column_family(TEST_KS, 'SuperBytes', super=True, comparator_type=BytesType()) sys.close() cls.cf_suplong = ColumnFamily(pool, 'SuperLong') cls.cf_supint = ColumnFamily(pool, 'SuperInt') cls.cf_supbigint = ColumnFamily(pool, 'SuperBigInt') cls.cf_suptime = ColumnFamily(pool, 'SuperTime') cls.cf_suplex = ColumnFamily(pool, 'SuperLex') cls.cf_supascii = ColumnFamily(pool, 'SuperAscii') cls.cf_suputf8 = ColumnFamily(pool, 'SuperUTF8') cls.cf_supbytes = ColumnFamily(pool, 'SuperBytes') cls.cfs = [cls.cf_suplong, cls.cf_supint, cls.cf_suptime, cls.cf_suplex, cls.cf_supascii, cls.cf_suputf8, cls.cf_supbytes]
def create_cfs(self): """ Creates the Cassandra Column Families (if not exist) """ sys_mgr = None pool = None try: sys_mgr = SystemManager() pool = ConnectionPool(settings.KEYSPACE, server_list=settings.CASSANDRA_HOSTS) try: cf = ColumnFamily(pool, CF_LOGS) except: logger.info("create_cfs(): Creating column family %s", CF_LOGS) #======================================== # Column key -> CompositeType #======================================== # 1. UUID + Timestamp # 2. Host / Origin # 3. Application # 4. Severiry comparator = CompositeType(TimeUUIDType(), UTF8Type(), UTF8Type(), UTF8Type()) sys_mgr.create_column_family(settings.KEYSPACE, CF_LOGS, comparator_type=comparator) cf = ColumnFamily(pool, CF_LOGS) # cf.get_count(str(uuid.uuid4())) try: cf = ColumnFamily(pool, CF_METADATA) except: logger.info("create_cfs(): Creating column family %s", CF_METADATA) sys_mgr.create_column_family(settings.KEYSPACE, CF_METADATA, comparator_type=UTF8Type()) cf = ColumnFamily(pool, CF_METADATA) cf.get_count(str(uuid.uuid4())) try: cf = ColumnFamily(pool, CF_TIMESTAMP_BITMAP) except: logger.info("create_cfs(): Creating column family %s", CF_TIMESTAMP_BITMAP) sys_mgr.create_column_family(settings.KEYSPACE, CF_TIMESTAMP_BITMAP, comparator_type=IntegerType()) cf = ColumnFamily(pool, CF_TIMESTAMP_BITMAP) try: cf = ColumnFamily(pool, CF_MULTI_MESSAGELOGS) except: logger.info("create_cfs(): Creating column family %s", CF_MULTI_MESSAGELOGS) sys_mgr.create_column_family(settings.KEYSPACE, CF_MULTI_MESSAGELOGS, comparator_type=UTF8Type()) cf = ColumnFamily(pool, CF_MULTI_MESSAGELOGS) sys_mgr.create_index(settings.KEYSPACE, CF_MULTI_MESSAGELOGS, 'meta:host', UTF8_TYPE, index_name='multimsg_host_index') sys_mgr.create_index(settings.KEYSPACE, CF_MULTI_MESSAGELOGS, 'meta:application', UTF8_TYPE, index_name='multimsg_application_index') sys_mgr.create_index(settings.KEYSPACE, CF_MULTI_MESSAGELOGS, 'meta:status', UTF8_TYPE, index_name='multimsg_finish_status_index') finally: if pool: pool.dispose() if sys_mgr: sys_mgr.close()
def test(host='localhost', keyspace_provided=None): from pycassa.types import BytesType, CompositeType from pycassa.system_manager import SystemManager, SIMPLE_STRATEGY # Create a fake keyspace, if not provided if not keyspace_provided: import random keyspace = '%s_%s' % ('json', random.randrange(1000000, 100000000)) else: keyspace = keyspace_provided # Connect to cluster and create keyspace system_manager = SystemManager(host) system_manager.create_keyspace(keyspace, SIMPLE_STRATEGY, {'replication_factor': '1'}) try: # Create CF with many CompositeFields comparator = CompositeType(BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType()) system_manager.create_column_family(keyspace, 'json', comparator_type=comparator) # Connect to the KS/CF and save samples db = CassandraJsonMapper.db(keyspace, 'json') tests = 0 # ---------------------------------------------------------------------- # Test a complicated structure sample_1 = { 'key1': { 'a': 1, 2: 'b', 'c': { 'd': 3, 'e': { 'f': True }, 'g': [ [ 'h', 'i', 'j', 4, 5 ], [ 'k', 'l', 'm', 'n', 'o' ], ], 'p': [ { 'id': 6, 'q': 'r' }, { 'id': 7, 's': 't' } ], 'u': [], 'v': None } } } db.save(sample_1) if db.get('key1') != sample_1['key1']: raise AssertionError('What was saved is not being equally returned.') tests += 1 # ---------------------------------------------------------------------- # Test improper format missing values sample_2 = { 'key1': 1 } try: db.save(sample_2) raise AssertionError('sample_2 should have thrown a KeyError.') except KeyError: pass tests += 1 # ---------------------------------------------------------------------- # Test saving multiple keys sample_3 = { 'key2': {2:2}, 'key3': {3:3}, 'key4': {4:4} } db.save(sample_3) if db.get('key2') != {2:2} or db.get('key3') != {3:3} or \ db.get('key4') != {4:4}: raise AssertionError('Not all keys in a json_payload were saved.') tests += 1 # ---------------------------------------------------------------------- # Test delete db.delete('key1') if db.get('key1'): raise AssertionError('Not all keys in were delted.') tests += 1 # ---------------------------------------------------------------------- # Test deletes db.delete(['key2', 'key3', 'key4']) if db.get('key2') or db.get('key3') or db.get('key4'): raise AssertionError('Not all keys in were delted.') tests += 1 # ---------------------------------------------------------------------- # Test reading from fake keys if db.get('fake_key') != {}: raise AssertionError('A get(fake_key) should return {}.') tests += 1 # ---------------------------------------------------------------------- # Test multi-threaded save db.mt_save(sample_1) db.mt_finish() if db.get('key1') != sample_1['key1']: raise AssertionError('What was saved is not being equally returned.') tests += 1 # ---------------------------------------------------------------------- # print json_format(db.get('key1')) print '{0}/{0} tests passed!'.format(tests) except: raise finally: # For debugging purposes # raw_input('Press ENTER to continue...') if not keyspace_provided: # Delete the temporary KS's system_manager.drop_keyspace(keyspace) system_manager.close()
def delete_namespace(self): sm = SystemManager(random.choice(self.server_list)) sm.drop_keyspace(self.namespace) sm.close()
def get_status(self): status = {} _logger = logging.getLogger(__name__ + '.get_status') try: pool = _get_connection() pool.dispose() status['get_connection'] = "ok" except: status['get_connection'] = "error" _logger.exception("_get_connection() failed") try: status['get_error_count'] = self.get_error_count() except: status['get_error_count'] = "error" _logger.exception("get_error_count() failed") try: status['get_warn_count'] = self.get_warn_count() except: status['get_warn_count'] = "error" _logger.exception("get_warn_count() failed") try: status['get_info_count'] = self.get_info_count() except: status['get_info_count'] = "error" _logger.exception("get_info_count() failed") try: status['get_debug_count'] = self.get_debug_count() except: status['get_debug_count'] = "error" _logger.exception("get_debug_count() failed") try: self.query() status['query'] = "ok" except: status['query'] = "error" _logger.exception("query() failed") try: apps = self.list_applications() status['list_applications'] = ", ".join(apps) for app in apps: try: status['query_by_application_' + app] = len( self.query_by_application(app)) except: status['query_by_application_' + app] = "error" _logger.exception( "query_by_application() failed for app {0}".format( app)) except: status['list_applications'] = "error" _logger.exception("list_applications() failed") try: hosts = self.list_hosts() status['list_hosts'] = ", ".join(hosts) for a_host in hosts: try: status['query_by_host_' + a_host] = len( self.query_by_host(a_host)) except: status['query_by_host_' + a_host] = "error" _logger.exception( "query_by_host() failed for host {0}".format(a_host)) except: status['list_hosts'] = "error" _logger.exception("list_hosts() failed") try: sys_mgr = SystemManager() sys_mgr.describe_ring(settings.KEYSPACE) status['SystemManager.describe_ring(%s)' % settings.KEYSPACE] = 'ok' except: status['SystemManager.describe_ring(%s)' % settings.KEYSPACE] = 'error' _logger.exception("SystemManager.describe_ring() failed") finally: sys_mgr.close() return status
""" Create the Cassandra database. """ import pycassa from pycassa.system_manager import SystemManager mgr = SystemManager() mgr.create_keyspace('drought', strategy_options={'replication_factor': '1'}) mgr.create_column_family('drought', 'cmip5') mgr.create_column_family('drought', 'zipcodes') mgr.alter_column('drought', 'zipcodes', 'ZIPCODE', pycassa.types.IntegerType()) mgr.create_index('drought', 'zipcodes', 'ZIPCODE', pycassa.types.IntegerType()) mgr.alter_column('drought', 'zipcodes', 'CENTER_LATITUDE', pycassa.types.FloatType()) mgr.alter_column('drought', 'zipcodes', 'CENTER_LONGITUDE', pycassa.types.FloatType()) mgr.create_column_family('drought', 'counties') mgr.close()
def _create_namespace(self, namespace): sm = SystemManager(random.choice(self.server_list)) sm.create_keyspace(namespace, SIMPLE_STRATEGY, {'replication_factor': '1'}) sm.close()
def create_cfs(self): """ Creates the Cassandra Column Families (if not exist) """ sys_mgr = None pool = None try: sys_mgr = SystemManager() pool = ConnectionPool(settings.KEYSPACE, server_list=settings.CASSANDRA_HOSTS) try: cf = ColumnFamily(pool, CF_LOGS) except: logger.info("create_cfs(): Creating column family %s", CF_LOGS) #======================================== # Column key -> CompositeType #======================================== # 1. UUID + Timestamp # 2. Host / Origin # 3. Application # 4. Severiry comparator = CompositeType( TimeUUIDType(), UTF8Type(), UTF8Type(), UTF8Type() ) sys_mgr.create_column_family(settings.KEYSPACE, CF_LOGS, comparator_type=comparator) cf = ColumnFamily(pool, CF_LOGS) # cf.get_count(str(uuid.uuid4())) try: cf = ColumnFamily(pool, CF_METADATA) except: logger.info("create_cfs(): Creating column family %s", CF_METADATA) sys_mgr.create_column_family(settings.KEYSPACE, CF_METADATA, comparator_type=UTF8Type()) cf = ColumnFamily(pool, CF_METADATA) cf.get_count(str(uuid.uuid4())) try: cf = ColumnFamily(pool, CF_TIMESTAMP_BITMAP) except: logger.info("create_cfs(): Creating column family %s", CF_TIMESTAMP_BITMAP) sys_mgr.create_column_family(settings.KEYSPACE, CF_TIMESTAMP_BITMAP, comparator_type=IntegerType()) cf = ColumnFamily(pool, CF_TIMESTAMP_BITMAP) try: cf = ColumnFamily(pool, CF_MULTI_MESSAGELOGS) except: logger.info("create_cfs(): Creating column family %s", CF_MULTI_MESSAGELOGS) sys_mgr.create_column_family(settings.KEYSPACE, CF_MULTI_MESSAGELOGS, comparator_type=UTF8Type()) cf = ColumnFamily(pool, CF_MULTI_MESSAGELOGS) sys_mgr.create_index(settings.KEYSPACE, CF_MULTI_MESSAGELOGS, 'meta:host', UTF8_TYPE, index_name='multimsg_host_index') sys_mgr.create_index(settings.KEYSPACE, CF_MULTI_MESSAGELOGS, 'meta:application', UTF8_TYPE, index_name='multimsg_application_index') sys_mgr.create_index(settings.KEYSPACE, CF_MULTI_MESSAGELOGS, 'meta:status', UTF8_TYPE, index_name='multimsg_finish_status_index') finally: if pool: pool.dispose() if sys_mgr: sys_mgr.close()
def get_status(self): status = {} _logger = logging.getLogger(__name__ + '.get_status') try: pool = _get_connection() pool.dispose() status['get_connection'] = "ok" except: status['get_connection'] = "error" _logger.exception("_get_connection() failed") try: status['get_error_count'] = self.get_error_count() except: status['get_error_count'] = "error" _logger.exception("get_error_count() failed") try: status['get_warn_count'] = self.get_warn_count() except: status['get_warn_count'] = "error" _logger.exception("get_warn_count() failed") try: status['get_info_count'] = self.get_info_count() except: status['get_info_count'] = "error" _logger.exception("get_info_count() failed") try: status['get_debug_count'] = self.get_debug_count() except: status['get_debug_count'] = "error" _logger.exception("get_debug_count() failed") try: self.query() status['query'] = "ok" except: status['query'] = "error" _logger.exception("query() failed") try: apps = self.list_applications() status['list_applications'] = ", ".join(apps) for app in apps: try: status['query_by_application_' + app] = len(self.query_by_application(app)) except: status['query_by_application_' + app] = "error" _logger.exception("query_by_application() failed for app {0}".format(app)) except: status['list_applications'] = "error" _logger.exception("list_applications() failed") try: hosts = self.list_hosts() status['list_hosts'] = ", ".join(hosts) for a_host in hosts: try: status['query_by_host_' + a_host] = len(self.query_by_host(a_host)) except: status['query_by_host_' + a_host] = "error" _logger.exception("query_by_host() failed for host {0}".format(a_host)) except: status['list_hosts'] = "error" _logger.exception("list_hosts() failed") try: sys_mgr = SystemManager() sys_mgr.describe_ring(settings.KEYSPACE) status['SystemManager.describe_ring(%s)' % settings.KEYSPACE] = 'ok' except: status['SystemManager.describe_ring(%s)' % settings.KEYSPACE] = 'error' _logger.exception("SystemManager.describe_ring() failed") finally: sys_mgr.close() return status
def test_composite_column_names(client): ''' examine the unique nature of cassandras "wide sorted rows"; Basically, C* is good at storing large OrderedDict objects and less good at the simpler kind of key=value storage that we were doing earlier. ''' config = client._config namespace = client._app_namespace chosen_server = client._chosen_server sm = SystemManager(chosen_server) sm.create_keyspace(namespace, SIMPLE_STRATEGY, {'replication_factor': '1'}) family = 'test' sm.create_column_family( namespace, family, super=False, key_validation_class = ASCII_TYPE, default_validation_class = BYTES_TYPE, comparator_type=CompositeType(UUIDType(), UUIDType()), #column_name_class = LEXICAL_UUID_TYPE ) logger.info( sm.describe_schema_versions() ) pool = ConnectionPool(namespace, config['storage_addresses'], max_retries=1000, pool_timeout=10, pool_size=2, timeout=120) cf = pycassa.ColumnFamily(pool, family) u1, u2, u3, u4 = uuid.uuid1(), uuid.uuid1(), uuid.uuid1(), uuid.uuid1() ## insert four cf.insert('inbound', {(u1, u2): b''}) cf.insert('inbound', {(u1, u3): b''}) cf.insert('inbound', {(u1, u4): b''}) cf.insert('inbound', {(u1, u1): b'45'}) ## insert three with duplicates cf.insert('inbound', {(u3, u3): b'42'}) cf.insert('inbound', {(u3, u3): b'43'}) cf.insert('inbound', {(u3, u2): b''}) cf.insert('inbound', {(u3, u4): b''}) ## insert two cf.insert('inbound', {(u2, u3): b''}) cf.insert('inbound', {(u2, u4): b''}) ## verify start/finish parameters work as expected assert 4 == len(cf.get('inbound', column_start=(u1,), column_finish=(u1,))) assert 2 == len(cf.get('inbound', column_start=(u2,), column_finish=(u2,))) assert 3 == len(cf.get('inbound', column_start=(u3,), column_finish=(u3,))) ## check delete of a single column cf.remove('inbound', columns=[(u2, u3)]) assert 1 == len(cf.get('inbound', column_start=(u2,), column_finish=(u2,))) ## check that the last inserted value appears at the expected ## location in the OrderedDict assert '43' == cf.get('inbound', column_start=(u3,), column_finish=(u3,)).items()[1][1] rec1 = cf.get('inbound', column_start=(u3,u3), column_finish=(u3,u3)).items() assert len(rec1) == 1 start = uuid.UUID(int=0) finish = uuid.UUID(int=2**128-1) assert start < u1 < u2 < u3 < u4 < finish assert 8 == len(cf.get('inbound', column_start=(start,), column_finish=(finish,)).items()) ## test range searching start = uuid.UUID(int=u3.int - 1) finish = uuid.UUID(int=u3.int + 1) assert start.int < u3.int < finish.int rec2 = cf.get('inbound', column_start=(start,), column_finish=(finish,)).items() assert rec2[1][0] == rec1[0][0] assert rec2[1][1] == rec1[0][1] == '43' cf.insert('inbound', {(u3,u3): b''.join(map(lambda u: u.bytes, (u1,u2,u3,u4)))}) data = cf.get('inbound', column_start=(u3,u3), column_finish=(u3,u3)).items()[0][1] assert [u1,u2,u3,u4] == map(lambda b: uuid.UUID(bytes=b), grouper(data, 16)) sm.close()
def test(host='localhost', keyspace_provided=None): from pycassa.types import BytesType, CompositeType from pycassa.system_manager import SystemManager, SIMPLE_STRATEGY # Create a fake keyspace, if not provided if not keyspace_provided: import random keyspace = '%s_%s' % ('json', random.randrange(1000000, 100000000)) else: keyspace = keyspace_provided # Connect to cluster and create keyspace system_manager = SystemManager(host) system_manager.create_keyspace(keyspace, SIMPLE_STRATEGY, {'replication_factor': '1'}) try: # Create CF with many CompositeFields comparator = CompositeType(BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType(), BytesType()) system_manager.create_column_family(keyspace, 'json', comparator_type=comparator) # Connect to the KS/CF and save samples db = CassandraJsonMapper.db(keyspace, 'json') tests = 0 # ---------------------------------------------------------------------- # Test a complicated structure sample_1 = { 'key1': { 'a': 1, 2: 'b', 'c': { 'd': 3, 'e': { 'f': True }, 'g': [ ['h', 'i', 'j', 4, 5], ['k', 'l', 'm', 'n', 'o'], ], 'p': [{ 'id': 6, 'q': 'r' }, { 'id': 7, 's': 't' }], 'u': [], 'v': None } } } db.save(sample_1) if db.get('key1') != sample_1['key1']: raise AssertionError( 'What was saved is not being equally returned.') tests += 1 # ---------------------------------------------------------------------- # Test improper format missing values sample_2 = {'key1': 1} try: db.save(sample_2) raise AssertionError('sample_2 should have thrown a KeyError.') except KeyError: pass tests += 1 # ---------------------------------------------------------------------- # Test saving multiple keys sample_3 = {'key2': {2: 2}, 'key3': {3: 3}, 'key4': {4: 4}} db.save(sample_3) if db.get('key2') != {2:2} or db.get('key3') != {3:3} or \ db.get('key4') != {4:4}: raise AssertionError('Not all keys in a json_payload were saved.') tests += 1 # ---------------------------------------------------------------------- # Test delete db.delete('key1') if db.get('key1'): raise AssertionError('Not all keys in were delted.') tests += 1 # ---------------------------------------------------------------------- # Test deletes db.delete(['key2', 'key3', 'key4']) if db.get('key2') or db.get('key3') or db.get('key4'): raise AssertionError('Not all keys in were delted.') tests += 1 # ---------------------------------------------------------------------- # Test reading from fake keys if db.get('fake_key') != {}: raise AssertionError('A get(fake_key) should return {}.') tests += 1 # ---------------------------------------------------------------------- # Test multi-threaded save db.mt_save(sample_1) db.mt_finish() if db.get('key1') != sample_1['key1']: raise AssertionError( 'What was saved is not being equally returned.') tests += 1 # ---------------------------------------------------------------------- # print json_format(db.get('key1')) print '{0}/{0} tests passed!'.format(tests) except: raise finally: # For debugging purposes # raw_input('Press ENTER to continue...') if not keyspace_provided: # Delete the temporary KS's system_manager.drop_keyspace(keyspace) system_manager.close()
def setup_class(cls): sys = SystemManager() sys.create_column_family(TEST_KS, 'TestTimeUUIDs', comparator_type=TimeUUIDType()) sys.close() cls.cf_time = ColumnFamily(pool, 'TestTimeUUIDs')
def create(): keyspace = config.cassandra_keyspace creds = {'username': config.cassandra_username, 'password': config.cassandra_password} mgr = SystemManager(config.cassandra_hosts[0], credentials=creds) cfs = mgr.get_keyspace_column_families(keyspace).keys() try: if 'Indexes' not in cfs: workaround_1779(mgr.create_column_family, keyspace, 'Indexes', comparator_type=UTF8_TYPE) if 'Stacktrace' not in cfs: workaround_1779(mgr.create_column_family, keyspace, 'Stacktrace', comparator_type=UTF8_TYPE, default_validation_class=UTF8_TYPE) if 'AwaitingRetrace' not in cfs: workaround_1779(mgr.create_column_family, keyspace, 'AwaitingRetrace', key_validation_class=UTF8_TYPE, comparator_type=UTF8_TYPE, default_validation_class=UTF8_TYPE) if 'RetraceStats' not in cfs: workaround_1779(mgr.create_column_family, keyspace, 'RetraceStats', comparator_type=UTF8_TYPE, default_validation_class=CounterColumnType()) if 'UniqueUsers90Days' not in cfs: workaround_1779(mgr.create_column_family, keyspace, 'UniqueUsers90Days', comparator_type=UTF8_TYPE, key_validation_class=UTF8_TYPE, default_validation_class=LONG_TYPE) if 'BadRequest' not in cfs: workaround_1779(mgr.create_column_family, keyspace, 'BadRequest', default_validation_class=CounterColumnType()) if 'UserBinaryPackages' not in cfs: workaround_1779(mgr.create_column_family, keyspace, 'UserBinaryPackages', # The key_validation_class is a launchpad team ID, which is # always ascii. # The comparator is a binary package name, which is always # ascii according to Debian policy. # default_validation_class is bytes as it's always NULL. key_validation_class=ASCII_TYPE, comparator_type=ASCII_TYPE) if 'BugToCrashSignatures' not in cfs: workaround_1779(mgr.create_column_family, keyspace, 'BugToCrashSignatures', key_validation_class=INT_TYPE, comparator_type=UTF8_TYPE) if 'CouldNotBucket' not in cfs: workaround_1779(mgr.create_column_family, keyspace, 'CouldNotBucket', comparator_type=TIME_UUID_TYPE) if 'TimeToRetrace' not in cfs: workaround_1779(mgr.create_column_family, keyspace, 'TimeToRetrace', default_validation_class=FLOAT_TYPE) if 'UniqueSystemsForErrorsByRelease' not in cfs: workaround_1779(mgr.create_column_family, keyspace, 'UniqueSystemsForErrorsByRelease', comparator_type=DateType(), default_validation_class=LONG_TYPE) if 'SystemImages' not in cfs: workaround_1779(mgr.create_column_family, keyspace, 'SystemImages', key_validation_class=UTF8_TYPE, comparator_type=UTF8_TYPE) finally: mgr.close()
def _delete_namespace(config): sm = SystemManager(config['storage_addresses'][0]) sm.drop_keyspace(config['namespace']) sm.close()
def test_composite_column_names_second_level_range_query_with_decomposited_keys(client): ''' check that we can execute range queries on the second part of a CompositeType column name after we unpack the composite key into a long string of concatenated hex forms of the UUIDs ''' config = client._config namespace = client._app_namespace chosen_server = client._chosen_server sm = SystemManager(chosen_server) sm.create_keyspace(namespace, SIMPLE_STRATEGY, {'replication_factor': '1'}) family = 'test' sm.create_column_family( namespace, family, super=False, key_validation_class = ASCII_TYPE, default_validation_class = BYTES_TYPE, comparator_type=UTF8Type(), ) pool = ConnectionPool(namespace, config['storage_addresses'], max_retries=1000, pool_timeout=10, pool_size=2, timeout=120) cf = pycassa.ColumnFamily(pool, family) u1, u2, u3, u4 = uuid.uuid1(), uuid.uuid1(), uuid.uuid1(), uuid.uuid1() cf.insert('inbound', {join_uuids(u1, u2): b''}) cf.insert('inbound', {join_uuids(u1, u3): b''}) cf.insert('inbound', {join_uuids(u1, u4): b''}) ## test range searching start = uuid.UUID(int=u3.int - 1) finish = uuid.UUID(int=u3.int + 1) assert start.int < u3.int < finish.int rec3 = cf.get('inbound', column_start =join_uuids(u1, start), column_finish=join_uuids(u1, finish)).items() assert len(rec3) == 1 assert split_uuids(rec3[0][0])[1] == u3 #### This assert above passes! #### This next part fails :-/ ## now insert many rows -- enough that some should fall in each ## subrange below for i in xrange(1000): cf.insert('inbound', {join_uuids(u1, uuid.uuid4()): b''}) ## do four ranges, and expect more than zero in each step_size = 2**(128 - 2) for i in range(2**2, 0, -1): start = uuid.UUID(int=(i-1) * step_size) finish = uuid.UUID(int=min(i * step_size, 2**128 - 1)) recs = cf.get('inbound', column_start =join_uuids(u1, start), column_finish=join_uuids(u1, finish)).items() for key, val in recs: key = split_uuids(key) assert val == b'' assert key[0] == u1 assert key[1] < finish assert start < key[1] ## this passes!! assert len(recs) > 0 logger.info( '%r for %r %r' % (len(recs), start, finish)) sm.close()
def teardown_package(): sys = SystemManager() if TEST_KS in sys.list_keyspaces(): sys.drop_keyspace(TEST_KS) sys.close()
def _clean_db(server): dbsys = SystemManager(server) dbsys.drop_keyspace(KEY_SPACE) dbsys.close()