Skip to content

Commit

Permalink
Cleanup for remote transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
JBorrow committed Feb 29, 2024
1 parent 5c6374b commit cd98870
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
25 changes: 22 additions & 3 deletions librarian_server/api/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from ..logger import log
from ..orm.file import File
from ..orm.instance import RemoteInstance
from ..orm.librarian import Librarian
from ..orm.storemetadata import StoreMetadata
from ..orm.transfer import IncomingTransfer, OutgoingTransfer, TransferStatus
from .auth import CallbackUserDependency, ReadappendUserDependency
Expand Down Expand Up @@ -342,7 +343,8 @@ def complete(
Possible response codes:
200 - OK. Transfer status updated.
404 - Not found. Could not find transfer.
400 - Not found. Could not find transfer.
400 - Bad request. Could not find librarian.
406 - Not acceptable. Transfer is not in STAGED status.
"""

Expand All @@ -361,7 +363,7 @@ def complete(
f"Could not find transfer with ID {request.source_transfer_id}. Returning error."
)

response.status_code = status.HTTP_404_NOT_FOUND
response.status_code = status.HTTP_400_BAD_REQUEST
return CloneFailedResponse(
reason="Could not find transfer.",
suggested_remedy=(
Expand Down Expand Up @@ -389,13 +391,30 @@ def complete(
destination_transfer_id=request.destination_transfer_id,
)

librarian = session.query(Librarian).filter_by(name=transfer.destination).first()

if librarian is None:
log.debug(f"Could not find librarian {transfer.destination}. Returning error.")

response.status_code = status.HTTP_400_BAD_REQUEST
return CloneFailedResponse(
reason=f"Could not find librarian {transfer.destination}.",
suggested_remedy=(
f"Check your librarian configuration. The librarian {transfer.destination} needs "
"to be an entry in this database. No remote instances will be created, and the "
"transfer status will not be updated, so you can try again afterwards."
),
source_transfer_id=request.source_transfer_id,
destination_transfer_id=request.destination_transfer_id,
)

transfer.status = TransferStatus.COMPLETED

# Create new remote instance for this file that was just completed.
remote_instance = RemoteInstance.new_instance(
file=transfer.file,
store_id=request.store_id,
librarian=transfer.destination,
librarian=librarian,
)

session.add(remote_instance)
Expand Down
4 changes: 2 additions & 2 deletions librarian_server/orm/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ def new_instance(
return RemoteInstance(
file=file,
store_id=store_id,
librarian_id=librarian.id,
librarian=librarian,
copy_time=datetime.utcnow(),
# TODO: This should somehow be our name? Not just the displayed site name.
sender=server_settings.displayed_site_name,
sender=server_settings.name,
)
1 change: 1 addition & 0 deletions librarian_server/orm/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def new_transfer(
destination=destination,
transfer_size=file.size,
transfer_checksum=file.checksum,
file_name=file.name,
instance_id=instance.id,
start_time=datetime.datetime.utcnow(),
)
Expand Down
17 changes: 13 additions & 4 deletions tests/server_unit_test/test_clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,16 @@ def test_incoming_transfer_endpoints(
session.add(transfer)
session.commit()

librarian = test_orm.Librarian.new_librarian(
name="test2", url="http://localhost", port=5000, check_connection=False
)
librarian.authenticator = "does_not_authenticate"
session.add(librarian)
session.commit()

transfer_id = transfer.id
instance_id = instance.id
store_id = store.id

# We will first test the failure case where we have not set the transfer to be ongoing

Expand All @@ -317,7 +325,7 @@ def test_incoming_transfer_endpoints(
request = CloneCompleteRequest(
source_transfer_id=transfer_id,
destination_transfer_id=transfer_id,
store_id=store.id,
store_id=store_id,
)

response = test_client.post_with_auth(
Expand Down Expand Up @@ -351,10 +359,11 @@ def test_incoming_transfer_endpoints(

assert transfer.status == test_orm.TransferStatus.COMPLETED

instance = session.get(test_orm.Instance, instance_id)
file = session.get(test_orm.File, str(garbage_filename))

session.delete(instance)
session.delete(*file.instances)
session.delete(*file.remote_instances)
session.delete(transfer)
session.delete(file)
session.commit()

Expand All @@ -374,7 +383,7 @@ def test_complete_no_transfer(test_client, test_server, test_orm):
"/api/v2/clone/complete", content=request.model_dump_json()
)

assert response.status_code == 404
assert response.status_code == 400

decoded_response = CloneFailedResponse.model_validate_json(response.content)

Expand Down

0 comments on commit cd98870

Please sign in to comment.