|
| 1 | +""" |
| 2 | +Bootstrap a table that does not have sufficient partitions to determine rates |
| 3 | +of change. |
| 4 | +""" |
| 5 | + |
| 6 | +from datetime import timedelta |
| 7 | +import logging |
| 8 | +import operator |
| 9 | +import yaml |
| 10 | + |
| 11 | +from partitionmanager.types import ( |
| 12 | + ChangePlannedPartition, |
| 13 | + MaxValuePartition, |
| 14 | + NewPlannedPartition, |
| 15 | +) |
| 16 | +from partitionmanager.table_append_partition import ( |
| 17 | + table_is_compatible, |
| 18 | + get_current_positions, |
| 19 | + get_partition_map, |
| 20 | + generate_sql_reorganize_partition_commands, |
| 21 | +) |
| 22 | +from .tools import iter_show_end |
| 23 | + |
| 24 | +RATE_UNIT = timedelta(hours=1) |
| 25 | +MINIMUM_FUTURE_DELTA = timedelta(hours=2) |
| 26 | + |
| 27 | + |
| 28 | +def write_state_info(conf, out_fp): |
| 29 | + """ |
| 30 | + Write the state info for tables defined in conf to the provided file-like |
| 31 | + object. |
| 32 | + """ |
| 33 | + log = logging.getLogger("write_state_info") |
| 34 | + |
| 35 | + log.info("Writing current state information") |
| 36 | + state_info = {"time": conf.curtime, "tables": dict()} |
| 37 | + for table in conf.tables: |
| 38 | + problem = table_is_compatible(conf.dbcmd, table) |
| 39 | + if problem: |
| 40 | + raise Exception(problem) |
| 41 | + |
| 42 | + map_data = get_partition_map(conf.dbcmd, table) |
| 43 | + positions = get_current_positions(conf.dbcmd, table, map_data["range_cols"]) |
| 44 | + |
| 45 | + log.info(f'(Table("{table.name}"): {positions}),') |
| 46 | + state_info["tables"][str(table.name)] = positions |
| 47 | + |
| 48 | + yaml.dump(state_info, out_fp) |
| 49 | + |
| 50 | + |
| 51 | +def _get_time_offsets(num_entries, first_delta, subseq_delta): |
| 52 | + """ |
| 53 | + Construct a list of timedeltas of size num_entries of the form |
| 54 | + [ first_delta, subseq_delta, [subseq_delta...] ] |
| 55 | + """ |
| 56 | + if num_entries < 1: |
| 57 | + raise ValueError("Must request at least one entry") |
| 58 | + |
| 59 | + time_units = [first_delta] |
| 60 | + while len(time_units) < num_entries: |
| 61 | + prev = time_units[-1] |
| 62 | + time_units.append(prev + subseq_delta) |
| 63 | + |
| 64 | + return time_units |
| 65 | + |
| 66 | + |
| 67 | +def _plan_partitions_for_time_offsets( |
| 68 | + now_time, time_offsets, rate_of_change, ordered_current_pos, max_val_part |
| 69 | +): |
| 70 | + """ |
| 71 | + Return a list of PlannedPartitions, starting from now, corresponding to |
| 72 | + each supplied offset that will represent the positions then from the |
| 73 | + supplied current positions and the rate of change. The first planned |
| 74 | + partition will be altered out of the supplied MaxValue partition. |
| 75 | + """ |
| 76 | + changes = list() |
| 77 | + for (i, offset), is_final in iter_show_end(enumerate(time_offsets)): |
| 78 | + increase = [x * offset / RATE_UNIT for x in rate_of_change] |
| 79 | + predicted_positions = [ |
| 80 | + int(p + i) for p, i in zip(ordered_current_pos, increase) |
| 81 | + ] |
| 82 | + predicted_time = now_time + offset |
| 83 | + |
| 84 | + part = None |
| 85 | + if i == 0: |
| 86 | + part = ( |
| 87 | + ChangePlannedPartition(max_val_part) |
| 88 | + .set_position(predicted_positions) |
| 89 | + .set_timestamp(predicted_time) |
| 90 | + ) |
| 91 | + |
| 92 | + else: |
| 93 | + part = NewPlannedPartition().set_timestamp(predicted_time) |
| 94 | + |
| 95 | + if is_final: |
| 96 | + part.set_columns(len(predicted_positions)) |
| 97 | + else: |
| 98 | + part.set_position(predicted_positions) |
| 99 | + |
| 100 | + changes.append(part) |
| 101 | + return changes |
| 102 | + |
| 103 | + |
| 104 | +def calculate_sql_alters_from_state_info(conf, in_fp): |
| 105 | + """ |
| 106 | + Using the config and the input yaml file-like object, return the SQL |
| 107 | + statements to bootstrap the tables in config that also have data in |
| 108 | + the input yaml as a dictionary of { Table -> list(SQL ALTER statements) } |
| 109 | + """ |
| 110 | + log = logging.getLogger("calculate_sql_alters") |
| 111 | + |
| 112 | + log.info("Reading prior state information") |
| 113 | + prior_data = yaml.safe_load(in_fp) |
| 114 | + |
| 115 | + time_delta = (conf.curtime - prior_data["time"]) / RATE_UNIT |
| 116 | + if time_delta <= 0: |
| 117 | + raise ValueError( |
| 118 | + f"Time delta is too small: {conf.curtime} - " |
| 119 | + f"{prior_data['time']} = {time_delta}" |
| 120 | + ) |
| 121 | + |
| 122 | + commands = dict() |
| 123 | + |
| 124 | + for table_name, prior_pos in prior_data["tables"].items(): |
| 125 | + table = None |
| 126 | + for t in conf.tables: |
| 127 | + if t.name == table_name: |
| 128 | + table = t |
| 129 | + if not table: |
| 130 | + log.info(f"Skipping {table_name} as it is not in the current config") |
| 131 | + continue |
| 132 | + |
| 133 | + problem = table_is_compatible(conf.dbcmd, table) |
| 134 | + if problem: |
| 135 | + raise Exception(problem) |
| 136 | + |
| 137 | + map_data = get_partition_map(conf.dbcmd, table) |
| 138 | + current_positions = get_current_positions( |
| 139 | + conf.dbcmd, table, map_data["range_cols"] |
| 140 | + ) |
| 141 | + |
| 142 | + ordered_current_pos = [ |
| 143 | + current_positions[name] for name in map_data["range_cols"] |
| 144 | + ] |
| 145 | + ordered_prior_pos = [prior_pos[name] for name in map_data["range_cols"]] |
| 146 | + |
| 147 | + delta_positions = list( |
| 148 | + map(operator.sub, ordered_current_pos, ordered_prior_pos) |
| 149 | + ) |
| 150 | + rate_of_change = list(map(lambda pos: pos / time_delta, delta_positions)) |
| 151 | + |
| 152 | + max_val_part = map_data["partitions"][-1] |
| 153 | + if not isinstance(max_val_part, MaxValuePartition): |
| 154 | + log.error(f"Expected a MaxValue partition, got {max_val_part}") |
| 155 | + raise Exception("Unexpected part?") |
| 156 | + |
| 157 | + log.info( |
| 158 | + f"{table}, {time_delta:0.1f} hours, {ordered_prior_pos} - {ordered_current_pos}, " |
| 159 | + f"{delta_positions} pos_change, {rate_of_change}/hour" |
| 160 | + ) |
| 161 | + |
| 162 | + part_duration = conf.partition_period |
| 163 | + if table.partition_period: |
| 164 | + part_duration = table.partition_period |
| 165 | + |
| 166 | + time_offsets = _get_time_offsets( |
| 167 | + 1 + conf.num_empty, MINIMUM_FUTURE_DELTA, part_duration |
| 168 | + ) |
| 169 | + |
| 170 | + changes = _plan_partitions_for_time_offsets( |
| 171 | + conf.curtime, |
| 172 | + time_offsets, |
| 173 | + rate_of_change, |
| 174 | + ordered_current_pos, |
| 175 | + max_val_part, |
| 176 | + ) |
| 177 | + |
| 178 | + commands[table.name] = list( |
| 179 | + generate_sql_reorganize_partition_commands(table, changes) |
| 180 | + ) |
| 181 | + |
| 182 | + return commands |
0 commit comments