Return event to one of potentially many clustered cassandra nodes

    Requires that configuration be enabled via 'event_return'
    option in master config.

    Cassandra does not support an auto-increment feature due to the
    highly inefficient nature of creating a monotonically increasing
    number across all nodes in a distributed database. Each event
    will be assigned a uuid by the connecting client.

def event_return(events):
    '''
    Return event to one of potentially many clustered cassandra nodes

    Requires that configuration be enabled via 'event_return'
    option in master config.

    Cassandra does not support an auto-increment feature due to the
    highly inefficient nature of creating a monotonically increasing
    number across all nodes in a distributed database. Each event
    will be assigned a uuid by the connecting client.
    '''
    for event in events:
        tag = event.get('tag', '')
        data = event.get('data', '')
        query = '''INSERT INTO {keyspace}.salt_events (
                     id, alter_time, data, master_id, tag
                   ) VALUES (
                     ?, ?, ?, ?, ?)
                 '''.format(keyspace=_get_keyspace())
        statement_arguments = [six.text_type(uuid.uuid1()),
                               int(time.time() * 1000),
                               salt.utils.json.dumps(data).replace("'", "''"),
                               __opts__['id'],
                               tag]

        # cassandra_cql.cql_query may raise a CommandExecutionError
        try:
            __salt__['cassandra_cql.cql_query_with_prepare'](query, 'salt_events',
                                                             statement_arguments,
                                                             asynchronous=True)
        except CommandExecutionError:
            log.critical('Could not store events with Cassandra returner.')
            raise
        except Exception as e:
            log.critical(
                'Unexpected error while inserting into salt_events: %s', e)
            raise