Improve logging

This commit is contained in:
Dmitry Ivanov
2022-09-30 03:53:39 +03:00
parent a9c2124267
commit f602e21aaf

View File

@@ -11,29 +11,39 @@ from pathlib import Path
import testgres
def run_command(args):
print('> Cmd:', ' '.join(args))
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
ret = p.wait()
output = p.stdout.read().strip()
if output:
print(textwrap.indent(output, '>> '))
if ret != 0:
raise subprocess.CalledProcessError(ret, args)
def make_tarfile(output_filename, source_dir):
print("* Packing the backup into a tarball")
cmd = ["tar", r"--transform=s/\.\///", "-C", str(source_dir), "-cf", str(output_filename), "."]
print("Command: ", " ".join(cmd))
r = subprocess.check_output(cmd).decode()
print(textwrap.indent(r, "> "))
run_command(cmd)
def create_tenant(tenant_id):
cmd = f"neon_local tenant create --tenant-id {tenant_id}"
print("Run command:", cmd)
r = subprocess.check_output(cmd.split()).decode()
print(textwrap.indent(r, "> "))
print("* Creating a new tenant")
cmd = ["neon_local", "tenant", "create", f"--tenant-id={tenant_id}"]
run_command(cmd)
def from_backup_at(args, backup_dir: Path):
def import_backup(args, backup_dir: Path):
tar = Path('/tmp/base.tar')
make_tarfile(tar, backup_dir / 'data')
print("* Importing the timeline into the pageserver")
manifest = json.loads((backup_dir / "data" / "backup_manifest").read_text())
start_lsn = manifest["WAL-Ranges"][0]["Start-LSN"]
end_lsn = manifest["WAL-Ranges"][0]["End-LSN"]
print("LSNs:", start_lsn, end_lsn)
print("Make tarball")
tar = Path("/tmp/base.tar")
make_tarfile(tar, backup_dir / "data")
print("> LSNs:", start_lsn, end_lsn)
cmd = (
"neon_local timeline import "
@@ -45,50 +55,45 @@ def from_backup_at(args, backup_dir: Path):
f"--node-name {args.node}"
)
print("Run neon_local")
r = subprocess.check_output(cmd.split()).decode()
print(textwrap.indent(r, "> "))
run_command(cmd.split())
def debug_prints(node):
print("RELID:", node.execute("select 'foo'::regclass::oid")[0][0])
print("DBs:", node.execute("table pg_database"))
print("foo:", node.execute("table foo"))
tuples = node.execute("table foo")
oid = node.execute("select 'foo'::regclass::oid")[0][0]
print("> foo's tuples:", tuples, "&", "oid:", oid)
print("> DBs:", node.execute("select oid, datname from pg_database"))
def main(args):
print("Create a node")
print("* Creating a node")
node = testgres.get_new_node()
node.init(unix_sockets=False, allow_streaming=True).start()
node.execute(
"""
create table foo as select 1;
"""
)
node.execute("create table foo as select 1")
debug_prints(node)
# node.pgbench_init(scale=1)
print("Create a backup")
print("* Creating a backup")
backup = node.backup()
backup_dir = Path(backup.base_dir)
print("> Backup dir:", backup_dir)
# pr = backup.spawn_primary().start()
# debug_prints(pr)
# exit(1)
print("Import a backup")
create_tenant(args.tenant_id)
from_backup_at(args, backup_dir)
import_backup(args, backup_dir)
print("Backup dir:", backup_dir)
print("Tenant:", args.tenant_id)
print("Timeline:", args.timeline_id)
print("Node:", args.node)
print("> Tenant:", args.tenant_id)
print("> Timeline:", args.timeline_id)
print("> Node:", args.node)
cmd = f"neon_local pg start --tenant-id={args.tenant_id} --timeline-id={args.timeline_id} {args.node}".split()
r = subprocess.check_output(cmd).decode()
print(textwrap.indent(r, "> "))
print("* Starting postgres")
cmd = ["neon_local", "pg", "start", f"--tenant-id={args.tenant_id}", f"--timeline-id={args.timeline_id}", args.node]
run_command(cmd)
print("* Opening psql session...")
cmd = ["psql", "host=127.0.0.1 port=55433 user=cloud_admin dbname=postgres"]
subprocess.call(cmd)