def mount_table(): try: if yt.get("//sys/tablet_cells/@count") == 0: for i in range(5): yt.create("tablet_cell", attributes={ "replication_factor": 3, "read_quorum": 2, "write_quorum": 2 }) yt.remove(TABLE_PATH, force=True) schema = yson.loads("<strict=%true; unique_keys=%true>[{name=key;\ type=int64; sort_order=ascending}; {name=value; type=int64}]" ) yt.create_table(TABLE_PATH, attributes={ "dynamic": True, "schema": schema }) yt.reshard_table(TABLE_PATH, pivot_keys=[[], [20], [30], [40], [50]]) except Exception as e: eprint(e) while yt.get(TABLE_PATH + "/@tablet_state") != "mounted": try: yt.mount_table(TABLE_PATH) except Exception as e: eprint(e) init_table()
def mount_table(): if retry(lambda: yt.get("//sys/tablet_cells/@count")) == 0: for i in range(5): retry(lambda: yt.create("tablet_cell", attributes={ "replication_factor": 3, "read_quorum": 2, "write_quorum": 2 })) retry(lambda: yt.remove(TABLE_PATH, force=True)) schema = yson.loads("<strict=%true; unique_keys=%true>[{name=key;\ type=int64; sort_order=ascending}; {name=value; type=int64}]" ) retry(lambda: yt.create_table(TABLE_PATH, attributes={ "dynamic": True, "schema": schema })) retry(lambda: yt.reshard_table(TABLE_PATH, pivot_keys=YT_SHARDS)) while retry(lambda: yt.get(TABLE_PATH + "/@tablet_state")) != "mounted": try: yt.mount_table(TABLE_PATH) except Exception as e: print(e, file=sys.stderr) init_table()
def wait_for_yt(): toBreak = False while not toBreak: toBreak = True try: yt.get('/') except Exception as e: toBreak = False time.sleep(2)
def init(): u"""Подключить таблицы.""" for table in ('user_groups', 'campaign_groups', 'experiments'): full_path = '{}/{}'.format(WORK_PATH, table) yt.mount_table(full_path) while yt.get('{}/@tablets/0/state'.format(full_path)) != 'mounted': time.sleep(0.1)
def handlemessage(): message, op, op_val = consume_input() try: if op == edn.Keyword("wait-for-yt"): wait_for_yt() elif op == edn.Keyword("read"): val = int(yt.get(PATH)) message[edn.Keyword("value")] = val message[edn.Keyword("type")] = edn.Keyword("ok") elif op == edn.Keyword("write"): message[edn.Keyword("type")] = edn.Keyword("info") yt.set(PATH, message[edn.Keyword("value")]) message[edn.Keyword("type")] = edn.Keyword("ok") elif op == edn.Keyword("start-tx"): try: with yt.Transaction(type='tablet', sticky=True): for row in yt.lookup_rows(TABLE_PATH, dyntables_ks_from_value(op_val)): key = row['key'] value = row['value'] op_val[int(key)] = int(value) message[edn.Keyword("type")] = edn.Keyword("ok") answer(message) message, op, op_val = consume_input() assert (op == edn.Keyword("commit")) yt.insert_rows(TABLE_PATH, dyntables_kvs_from_value(op_val)) message[edn.Keyword("type")] = edn.Keyword("ok") except yt.YtResponseError as e: if e.contains_code(1700): #Transaction lock conflict message[edn.Keyword("type")] = edn.Keyword("fail") else: raise e elif op == edn.Keyword("mount-table"): mount_table() elif op == edn.Keyword("terminate"): answer(message) sys.exit(0) else: message[edn.Keyword("type")] = edn.Keyword("fail") except Exception as e: eprint(e) if op in [edn.Keyword("write"), edn.Keyword("commit")]: message[edn.Keyword("type")] = edn.Keyword("info") else: message[edn.Keyword("type")] = edn.Keyword("fail") answer(message)
def wait_for_yt(): retry(lambda: yt.get('/'))