def __init__(self):
        super(AbstractCassandraRepository, self).__init__()

        self._cluster = connection_util.create_cluster()
        self._session = connection_util.create_session(self._cluster)
        self._retention = conf.cassandra.retention_policy * 24 * 3600
        self._cache_size = conf.cassandra.max_definition_cache_size
        self._max_batches = conf.cassandra.max_batches
Beispiel #2
0
    def __init__(self):
        super(AbstractCassandraRepository, self).__init__()

        self._cluster = connection_util.create_cluster()
        self._session = connection_util.create_session(self._cluster)
        self._retention = conf.cassandra.retention_policy * 24 * 3600
        self._cache_size = conf.cassandra.max_definition_cache_size
        self._max_batches = conf.cassandra.max_batches
 def _setup(cls, cql, result_handler):
     cls.cluster = connection_util.create_cluster()
     cls.session = connection_util.create_session(cls.cluster)
     cls.prepared = cls.session.prepare(cql)
     cls.result_handler = result_handler
 def _setup(cls, cql, result_handler):
     cls.cluster = connection_util.create_cluster()
     cls.session = connection_util.create_session(cls.cluster)
     cls.prepared = cls.session.prepare(cql)
     cls.result_handler = result_handler
def main():
    """persister recreate metric_id tool."""

    config.parse_args()
    conf = cfg.CONF

    try:
        LOG.info('Starting check and repair of metric_id consistency.')

        # Connection setup
        # rocky style - note that we don't deliver pike style
        _cluster = connection_util.create_cluster()
        _session = connection_util.create_session(_cluster)
        _retention = conf.cassandra.retention_policy * 24 * 3600

        metric_all_stmt = _session.prepare(METRIC_ALL_CQL)
        metric_repair_stmt = _session.prepare(METRICS_INSERT_CQL)

        rows = _session.execute(metric_all_stmt)

        # if rows:
        #     LOG.info('First - {}'.format(rows[0]))
        #     # LOG.info('First name {} and id {}'.format(
        #     #     rows[0].metric_name, rows[0].metric_id)) # metric_id can't be logged raw

        # Bit of a misnomer - "null" is not in the cassandra db
        missing_value_rows = []
        for row in rows:
            if row.metric_id is None:
                LOG.info('Row with missing metric_id - {}'.format(row))
                missing_value_rows.append(row)

                # check created_at
                fixed_created_at = row.created_at
                if row.created_at is None and row.updated_at is not None:
                    LOG.info("Metric created_at was also None, repairing.")
                    fixed_created_at = row.updated_at

                # TODO(joadavis) update the updated_at timestamp to now

                # recreate metric id
                # copied from metrics_repository.py
                hash_string = '%s\0%s\0%s\0%s' % (row.region, row.tenant_id,
                                                  row.metric_name,
                                                  '\0'.join(row.dimensions))
                metric_id = hashlib.sha1(hash_string.encode('utf8')).hexdigest()
                id_bytes = bytearray.fromhex(metric_id)

                LOG.info("Recreated hash for metric id: {}".format(hash_string))
                # LOG.info("new id_bytes {}".format(id_bytes)) # can't unicode decode for logging

                # execute cql
                metric_repair_bound_stmt = metric_repair_stmt.bind((_retention,
                                                                    id_bytes,
                                                                    fixed_created_at,
                                                                    row.updated_at,
                                                                    row.region,
                                                                    row.tenant_id,
                                                                    row.metric_name,
                                                                    row.dimensions,
                                                                    row.dimension_names))

                _session.execute(metric_repair_bound_stmt)

        # LOG.info("of {} rows there are {} missing metric_id".format(len(rows), len(null_rows)))
        if len(missing_value_rows) > 0:
            LOG.warning("--> There were {} rows missing metric_id.".format(
                len(missing_value_rows)))
            LOG.warning("    Those rows have been updated.")
        else:
            LOG.info("No missing metric_ids were found, no changes made.")

        LOG.info('Done with metric_id consistency check and repair.')

        return 0

    except Exception:
        LOG.exception('Error! Exiting.')
Beispiel #6
0
def main():
    """persister check for missing metric_id tool."""

    config.parse_args()

    try:
        LOG.info('Starting check of metric_id consistency.')

        # Connection setup
        # rocky style - note that we don't deliver pike style
        _cluster = connection_util.create_cluster()
        _session = connection_util.create_session(_cluster)

        metric_all_stmt = _session.prepare(METRIC_ALL_CQL)

        rows = _session.execute(metric_all_stmt)

        # if rows:
        #     LOG.info('First - {}'.format(rows[0]))
        #     # LOG.info('First name {} and id {}'.format(
        #     #     rows[0].metric_name, rows[0].metric_id)) # metric_id can't be logged raw

        # Bit of a misnomer - "null" is not in the cassandra db
        missing_value_rows = []
        for row in rows:
            if row.metric_id is None:
                LOG.info('Row with missing metric_id - {}'.format(row))
                missing_value_rows.append(row)

                # check created_at
                if row.created_at is None and row.updated_at is not None:
                    LOG.info("Metric created_at was also None.")

                # TODO(joadavis) update the updated_at timestamp to now

                # recreate metric id
                # copied from metrics_repository.py
                hash_string = '%s\0%s\0%s\0%s' % (row.region, row.tenant_id,
                                                  row.metric_name,
                                                  '\0'.join(row.dimensions))
                # metric_id = hashlib.sha1(hash_string.encode('utf8')).hexdigest()
                # id_bytes = bytearray.fromhex(metric_id)

                LOG.info("Recreated hash for metric id: {}".format(hash_string))
                # LOG.info("new id_bytes {}".format(id_bytes)) # can't unicode decode for logging

        # LOG.info("of {} rows there are {} missing metric_id".format(len(rows), len(null_rows)))
        if len(missing_value_rows) > 0:
            LOG.warning("--> There were {} rows missing metric_id.".format(
                len(missing_value_rows)))
            LOG.warning("    Those rows have NOT been updated.\n"
                        "    Please run the persister-recreate-metric-id "
                        "tool to repair the rows.")
        else:
            LOG.info("No missing metric_ids were found, no changes made.")

        LOG.info('Done with metric_id consistency check.')

        return 0

    except Exception:
        LOG.exception('Error! Exiting.')