from pathlib import Path from types import SimpleNamespace import sys,sqlite3,tempfile,threading,hashlib,time,io,json sys.path.insert(0,str(Path(__file__).parent)) from release_dataset import Job import parallel_transfer as pipeline class API: def __init__(self): self.remote={};self.lock=threading.Lock();self.uploads=0;self.maxuploads=0;self.commits=0;self.maxcommits=0;self.once=True def preupload_lfs_files(self,*,additions,**kw): with self.lock: fail=self.once and any(x.path_in_repo.endswith("part-000000.tar.zst") for x in additions) if fail:self.once=False self.uploads+=1;self.maxuploads=max(self.maxuploads,self.uploads) try: if fail:raise ConnectionError("simulated connection reset") time.sleep(0.15) finally: with self.lock:self.uploads-=1 def create_commit(self,*,operations,**kw): with self.lock:self.commits+=1;self.maxcommits=max(self.maxcommits,self.commits) try: time.sleep(0.03) for operation in operations: content=Path(operation.path_or_fileobj).read_bytes() self.remote[operation.path_in_repo]=(len(content),hashlib.sha256(content).hexdigest()) return SimpleNamespace(oid="fake-commit") finally: with self.lock:self.commits-=1 def get_paths_info(self,repo,paths,**kw): return [SimpleNamespace(size=self.remote[x][0],lfs={"sha256":self.remote[x][1]}) for x in paths if x in self.remote] def repo_info(self,*a,**kw):return SimpleNamespace(sha="fake-commit") def check(): with tempfile.TemporaryDirectory(prefix="bounded-pipeline-test-",dir=Path(__file__).parent.parent/"runtime") as directory: job=Job.__new__(Job);job.state=Path(directory);(job.state/"staging").mkdir() job.mutex=threading.RLock();job.api=API() job.c={"repo_id":"test/repo","bucket":"test","read_workers":2,"compression_threads":1, "parallel_pack_workers":2,"parallel_upload_workers":2,"max_staging_bytes":1024**3, "min_free_disk_bytes":0,"min_available_memory_bytes":0,"retry_base_seconds":0.1,"max_host_cpu_percent":101} job.components={"visual":{"prefix":"source/visual/"}} job.db=sqlite3.connect(":memory:",check_same_thread=False) job.db.executescript("""CREATE TABLE objects(key,bytes,etag,component,unit,retained); CREATE TABLE units(component,unit,shard_id); CREATE TABLE shards(id PRIMARY KEY,component,hf_path,source_bytes,local_bytes,sha256,git_blob,status,commit_oid);""") data={} for i in range(6): sid="visual-"+str(i);unit="obj"+str(i);total=0 for n in range(8): key="source/visual/"+unit+"/"+str(n)+".bin";content=(str(i)+str(n)).encode()*30000 data[key]=content;total+=len(content) job.db.execute("INSERT INTO objects VALUES(?,?,?,?,?,1)",(key,len(content),"etag","visual",unit)) job.db.execute("INSERT INTO units VALUES(?,?,?)",("visual",unit,sid)) job.db.execute("INSERT INTO shards VALUES(?,?,?,?,NULL,NULL,NULL,'planned',NULL)",(sid,"visual","assets/r1/visual/part-"+f"{i:06d}"+".tar.zst",total)) job.db.commit() def get(**kw): time.sleep(0.005);content=data[kw["Key"]] return {"ContentLength":len(content),"Body":io.BytesIO(content)} job.s3=SimpleNamespace(get_object=get) job.safe=lambda e:str(e);job.repo_info=lambda:{"private":False} events=[];job.event=lambda stage,**kw:events.append((stage,kw)) active=0;peak=0;original=job.package;lock=threading.Lock() def package(row): nonlocal active,peak with lock:active+=1;peak=max(peak,active) try:return original(row) finally: with lock:active-=1 job.package=package pipeline.transfer(job,["visual"]) assert job.db.execute("SELECT COUNT(*) FROM shards WHERE status='verified'").fetchone()[0]==6 assert job.db.execute("SELECT COUNT(*) FROM shard_failures").fetchone()[0]==0 assert len(job.api.remote)==12 assert not list((job.state/"staging").iterdir()) assert peak==2 and job.api.maxuploads==2 and job.api.maxcommits==1 assert any(stage=="shard_retry_scheduled" for stage,_ in events) print(json.dumps({"passed":True,"pack_concurrency":peak,"upload_concurrency":job.api.maxuploads, "commit_concurrency":job.api.maxcommits,"verified_shards":6, "simulated_transient_recovered":True,"verified_temporary_files_cleaned":True})) if __name__=="__main__":check()