Skip to content

Commit f40f6be

Browse files
committed
Change PUT to fail on tracker DB errors
The original goal is to create a Dataset only on dataset intake; all other operations will require that a Dataset already exist and fail in some manner if not. This is the first step towards that, with a server PUT upload attempt failing (not storing the uploaded dataset) if we're unable to create a Dataset.
1 parent 292918c commit f40f6be

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

lib/pbench/server/api/resources/upload_api.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import tempfile
44
import hashlib
55
from pathlib import Path
6+
from http import HTTPStatus
7+
68
from flask_restful import Resource, abort
79
from flask import request, jsonify
810
from werkzeug.utils import secure_filename
@@ -114,8 +116,16 @@ def put(self, controller):
114116
# TODO: Need real user from PUT!
115117

116118
# Create a tracking dataset object; it'll begin in UPLOADING state
117-
dataset = Dataset.create(controller=controller, path=tar_full_path, md5=md5sum)
118-
self.logger.info("Uploading {}", dataset)
119+
try:
120+
dataset = Dataset(controller=controller, path=tar_full_path, md5=md5sum)
121+
dataset.add()
122+
except Exception:
123+
self.logger.exception("unable to create dataset for {}", filename)
124+
abort(
125+
HTTPStatus.INTERNAL_SERVER_ERROR, message="INTERNAL ERROR",
126+
)
127+
128+
self.logger.info("Uploading file {} to {}", filename, dataset)
119129

120130
with tempfile.NamedTemporaryFile(mode="wb", dir=path) as ofp:
121131
chunk_size = 4096
@@ -189,11 +199,11 @@ def put(self, controller):
189199
)
190200
raise
191201

192-
if dataset:
193-
try:
194-
dataset.advance(States.UPLOADED)
195-
except Exception:
196-
self.logger.exception("Unable to finalize {}", dataset)
202+
try:
203+
dataset.advance(States.UPLOADED)
204+
except Exception:
205+
self.logger.exception("Unable to finalize {}", dataset)
206+
abort(HTTPStatus.INTERNAL_SERVER_ERROR, message="INTERNAL ERROR")
197207
response = jsonify(dict(message="File successfully uploaded"))
198208
response.status_code = 201
199209
return response

lib/pbench/server/db/models/tracker.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,10 @@ class States(enum.Enum):
239239

240240
UPLOADING = ("Uploading", True)
241241
UPLOADED = ("Uploaded", False)
242-
ARCHIVING = ("Archiving", True)
243-
ARCHIVED = ("Archived", False)
244242
UNPACKING = ("Unpacking", True)
245243
UNPACKED = ("Unpacked", False)
246244
INDEXING = ("Indexing", True)
247245
INDEXED = ("Indexed", False)
248-
REINDEX = ("Re-index", False)
249246
EXPIRING = ("Expiring", True)
250247
EXPIRED = ("Expired", False)
251248
QUARANTINED = ("Quarantined", False)
@@ -302,7 +299,7 @@ class Dataset(Database.Base):
302299
# because they're terminal states that cannot be exited.
303300
}
304301

305-
DEFAULT_USERNAME = "public"
302+
ANTI_USER = "public" # The "user" that owns unowned datasets
306303

307304
id = Column(Integer, primary_key=True, autoincrement=True)
308305
user = Column(String(255), unique=False, nullable=False)
@@ -367,13 +364,13 @@ def __init__(self, **kwargs):
367364
if type(kwargs.get("state")) is not States:
368365
raise DatasetBadParameterType(kwargs.get("state"), States)
369366
super().__init__(**kwargs)
370-
self.user = kwargs.get("user") if "user" in kwargs else Dataset.DEFAULT_USERNAME
367+
self.user = kwargs.get("user", Dataset.ANTI_USER)
371368
self.controller = kwargs.get("controller")
372369
self.name = kwargs.get("name")
373370
time = datetime.datetime.now()
374-
self.created = kwargs.get("created") if "created" in kwargs else time
375-
self.md5 = kwargs.get("md5") if "md5" in kwargs else None
376-
self.state = kwargs.get("state") if "state" in kwargs else States.UPLOADING
371+
self.created = time
372+
self.md5 = kwargs.get("md5")
373+
self.state = kwargs.get("state", States.UPLOADING)
377374
self.transition = time
378375

379376
@staticmethod

lib/pbench/test/unit/server/test_state_tracker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class TestStateTracker:
1717
def test_state_enum(self):
1818
""" Test the States ENUM properties
1919
"""
20-
assert len(States.__members__) == 12
20+
assert len(States.__members__) == 9
2121
for n, s in States.__members__.items():
2222
assert str(s) == s.friendly
2323
assert s.mutating == (
@@ -66,7 +66,7 @@ def test_attach_none(self):
6666
does not exist.
6767
"""
6868
with pytest.raises(DatasetNotFound):
69-
Dataset.attach(controller="frodo", name="venus", state=States.ARCHIVING)
69+
Dataset.attach(controller="frodo", name="venus", state=States.UPLOADING)
7070

7171
def test_attach_controller_path(self):
7272
""" Test that we can attach using controller and name to a

0 commit comments

Comments
 (0)