def migrate(db, migrations_dir): """ Returns: accumulated_migrations, all_migrations_ran_successfully accumulated_migrations is a list of migration names that ran successfully. all_migrations_ran_successfully is a bool indicating overall success """ ensure_schema_migrations_tables(db) accumulated_migrations = [] if obtain_migration_lock(db): # So two servers don't run migration at same time migrations = get_migrations_to_run(db, migrations_dir) for i, (migration_name, migration) in enumerate(migrations): logging.info('Running ' + migration_name) try: migration.run(db) except Exception as e: exc_info = sys.exc_info() handle_failed_migration(db, exc_info) return accumulated_migrations, False logging.info('Finished migration ' + migration_name) mark_migration_done(db, migration_name) accumulated_migrations += migration_name release_migration_lock(db) return accumulated_migrations, True
def migrate(db, migrations_dir): """ Returns: accumulated_migrations, all_migrations_ran_successfully accumulated_migrations is a list of migration names that ran successfully. all_migrations_ran_successfully is a bool indicating overall success """ ensure_schema_migrations_tables(db) accumulated_migrations = [] if obtain_migration_lock(db): # So two servers don't run migration at same time migrations = get_migrations_to_run(db, migrations_dir) for i, (migration_name, migration) in enumerate(migrations): logging.info('Running ' + migration_name ) try: migration.run(db) except Exception as e: exc_info = sys.exc_info() handle_failed_migration(db, exc_info) return accumulated_migrations, False logging.info('Finished migration ' + migration_name ) mark_migration_done(db, migration_name) accumulated_migrations += migration_name release_migration_lock(db) return accumulated_migrations, True
def _exponential_splay(self, retry): if IS_TEST: sleep_coeff = 0 else: sleep_coeff = 1 # random => ! herd # Max: 2 ** 4 = 16 seconds time_to_sleep = sleep_coeff * 2**retry * random.random() logging.info('sleeping for %f seconds' % time_to_sleep) time.sleep(time_to_sleep)
def _change_throughput(self, new_throughput, boto_table, table_name): try: logging.warn('Attempting to increase throughput of ' + table_name) self.connection.update_table(table_name, provisioned_throughput=new_throughput) except Exception as e: # TODO: Fail gracefully here on Validation Exception. # TODO: Don't refresh table info after getting throughput exceeded exc_info = sys.exc_info() logging.error( 'Could not increase table throughput will continue ' 'retrying. Error was: %s %s %s', exc_info[0], exc_info[1], exc_info[2]) else: logging.info('Successfully increased throughput of ' + table_name)
def obtain_migration_lock(db): # lock = db.get_item(table_name=META_TABLE_NAME, tenant_id=SUPER_TENANT_ID, # purpose='check status', name=MIGRATION_META_STATUS_KEY) try: db.put_item(table_name=META_TABLE_NAME, item={ META_KEY_NAME: MIGRATION_META_STATUS_KEY, META_VALUE_NAME: YES_STATUS }, tenant_id=SUPER_TENANT_ID, purpose='obtain migration lock', overwrite=True, condition=META_VALUE_NAME + " = :status", vars={':status': {'S': NO_STATUS}}) except ClariDynamo.ClariDynamoConditionCheckFailedException as e: logging.info('Could not get migration lock, other process running.') return False return True
def obtain_migration_lock(db): # lock = db.get_item(table_name=META_TABLE_NAME, tenant_id=SUPER_TENANT_ID, # purpose='check status', name=MIGRATION_META_STATUS_KEY) try: db.put_item(table_name=META_TABLE_NAME, item={ META_KEY_NAME: MIGRATION_META_STATUS_KEY, META_VALUE_NAME: YES_STATUS }, tenant_id=SUPER_TENANT_ID, purpose='obtain migration lock', overwrite=True, condition=META_VALUE_NAME + " = :status", vars={':status': { 'S': NO_STATUS }}) except ClariDynamo.ClariDynamoConditionCheckFailedException as e: logging.info('Could not get migration lock, other process running.') return False return True
def wait_for_table_to_become_active(self, boto_table, table_name): while self.get_table_status(boto_table) != 'ACTIVE': logging.info('Waiting for table to finish creating') sleep(1)