Skip to content

Commit 4c9f7f8

Browse files
Merge branch 'master' into submit_collection_endpoint
2 parents d962055 + 5496189 commit 4c9f7f8

File tree

11 files changed

+107
-80
lines changed

11 files changed

+107
-80
lines changed

migrations/mysql_3.2.0.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE `DOWNLOAD` ADD `PRIORITY` TINYINT UNSIGNED DEFAULT 0 NOT NULL;

migrations/oracle_3.2.0.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE DOWNLOAD ADD PRIORITY NUMBER(2, 0) DEFAULT 0 NOT NULL;

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>org.icatproject</groupId>
66
<artifactId>datagateway-download-api</artifactId>
77
<packaging>war</packaging>
8-
<version>3.1.1-SNAPSHOT</version>
8+
<version>3.2.0-SNAPSHOT</version>
99
<name>DataGateway Download API</name>
1010
<description>Download backend for DataGateway</description>
1111

src/main/java/org/icatproject/topcat/DownloadBuilder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class DownloadBuilder {
3131
private String email;
3232
private String userName;
3333
private String fullName;
34+
private int priority = 0;
3435
private IcatClient icatClient;
3536

3637
public String transport;
@@ -54,6 +55,7 @@ public DownloadBuilder(String sessionId, String email, String fileName, String t
5455

5556
userName = icatClient.getUserName();
5657
fullName = icatClient.getFullName();
58+
priority = icatClient.getQueuePriority(userName);
5759
icatClient.checkQueueAllowed(userName);
5860
}
5961

@@ -246,6 +248,7 @@ public DatafilesResponse extractLocations(List<String> files) throws TopcatExcep
246248
}
247249
download.setDownloadItems(downloadItems);
248250
download.setSize(response.totalSize);
251+
download.setPriority(priority);
249252
downloads.add(download);
250253

251254
return response;

src/main/java/org/icatproject/topcat/IcatClient.java

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,40 @@ public IcatClient(String url, String sessionId) {
7070
* @param password ICAT password
7171
* @return json with sessionId of the form
7272
* <samp>{"sessionId","0d9a3706-80d4-4d29-9ff3-4d65d4308a24"}</samp>
73-
* @throws BadRequestException
73+
* @throws TopcatException
7474
*/
75-
public String login(String plugin, String username, String password) throws BadRequestException {
75+
public String login(String plugin, String username, String password) throws TopcatException {
76+
JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
77+
JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
78+
JsonObjectBuilder usernameBuilder = Json.createObjectBuilder();
79+
JsonObjectBuilder passwordBuilder = Json.createObjectBuilder();
80+
usernameBuilder.add("username", username);
81+
passwordBuilder.add("password", password);
82+
arrayBuilder.add(usernameBuilder);
83+
arrayBuilder.add(passwordBuilder);
84+
objectBuilder.add("plugin", plugin);
85+
objectBuilder.add("credentials", arrayBuilder);
86+
String jsonString = "json=" + objectBuilder.build().toString();
87+
Response response;
7688
try {
77-
JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
78-
JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
79-
JsonObjectBuilder usernameBuilder = Json.createObjectBuilder();
80-
JsonObjectBuilder passwordBuilder = Json.createObjectBuilder();
81-
usernameBuilder.add("username", username);
82-
passwordBuilder.add("password", password);
83-
arrayBuilder.add(usernameBuilder);
84-
arrayBuilder.add(passwordBuilder);
85-
objectBuilder.add("plugin", plugin);
86-
objectBuilder.add("credentials", arrayBuilder);
87-
String jsonString = "json=" + objectBuilder.build().toString();
88-
Response response = httpClient.post("session", new HashMap<String, String>(), jsonString);
89-
return response.toString();
89+
response = httpClient.post("session", new HashMap<String, String>(), jsonString);
9090
} catch (Exception e) {
9191
throw new BadRequestException(e.getMessage());
9292
}
93+
switch (response.getCode()) {
94+
case 200:
95+
return response.toString();
96+
case 400:
97+
throw new BadRequestException(response.toString());
98+
case 401:
99+
throw new AuthenticationException(response.toString());
100+
case 403:
101+
throw new ForbiddenException(response.toString());
102+
case 404:
103+
throw new NotFoundException(response.toString());
104+
default:
105+
throw new InternalException(response.toString());
106+
}
93107
}
94108

95109
public String getUserName() throws TopcatException {
@@ -461,7 +475,15 @@ public List<JsonObject> getEntities(String entityType, List<Long> entityIds) thr
461475
* another internal error is triggered)
462476
*/
463477
public void checkQueueAllowed(String userName) throws TopcatException {
464-
if (getQueuePriority(userName) < 1) {
478+
checkQueueAllowed(getQueuePriority(userName));
479+
}
480+
481+
/**
482+
* @param priority int priority to check
483+
* @throws TopcatException if priority < 1
484+
*/
485+
public void checkQueueAllowed(int priority) throws TopcatException {
486+
if (priority < 1) {
465487
throw new ForbiddenException("Queuing Downloads forbidden");
466488
}
467489
}

src/main/java/org/icatproject/topcat/StatusCheck.java

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,8 @@ public void startQueuedDownload(int maxActiveDownloads) throws Exception {
397397
availableDownloads -= activeDownloadsSize;
398398
}
399399

400-
String queuedQueryString = selectString + " and " + queuedCondition;
401-
queuedQueryString += " order by download.createdAt";
400+
String queuedQueryString = selectString + " and " + queuedCondition + " and download.priority > 0";
401+
queuedQueryString += " order by download.priority asc NULLS FIRST, download.createdAt asc";
402402
TypedQuery<Download> queuedDownloadsQuery = em.createQuery(queuedQueryString, Download.class);
403403
List<Download> queuedDownloads = queuedDownloadsQuery.getResultList();
404404
int queueSize = queuedDownloads.size();
@@ -410,42 +410,12 @@ public void startQueuedDownload(int maxActiveDownloads) throws Exception {
410410
if (maxActiveDownloads <= 0) {
411411
// No limits on how many to submit
412412
logger.info("Preparing 1 out of {} queued downloads", queueSize);
413-
Download queuedDownload = queuedDownloads.get(0);
414-
queuedDownload.setStatus(DownloadStatus.PREPARING);
415-
prepareDownload(queuedDownload, null, getQueueSessionId(sessionIds, queuedDownload.getFacilityName()));
416413
} else {
417414
logger.info("Preparing 1 out of {} queued downloads as {} spaces available", queueSize, availableDownloads);
418-
HashMap<Integer, List<Download>> mapping = new HashMap<>();
419-
for (Download queuedDownload : queuedDownloads) {
420-
String sessionId = getQueueSessionId(sessionIds, queuedDownload.getFacilityName());
421-
String icatUrl = FacilityMap.getInstance().getIcatUrl(queuedDownload.getFacilityName());
422-
IcatClient icatClient = new IcatClient(icatUrl, sessionId);
423-
int priority = icatClient.getQueuePriority(queuedDownload.getUserName());
424-
if (priority == 1) {
425-
// Highest priority, prepare now
426-
queuedDownload.setStatus(DownloadStatus.PREPARING);
427-
prepareDownload(queuedDownload, null, sessionId);
428-
return;
429-
} else {
430-
// Lower priority, add to mapping
431-
mapping.putIfAbsent(priority, new ArrayList<>());
432-
mapping.get(priority).add(queuedDownload);
433-
}
434-
}
435-
436-
// Get the highest priority encountered
437-
List<Integer> keyList = new ArrayList<>();
438-
for (Object key : mapping.keySet().toArray()) {
439-
keyList.add((Integer) key);
440-
}
441-
int priority = Collections.min(keyList);
442-
443-
// Prepare the first Download at this priority level
444-
List<Download> downloadList = mapping.get(priority);
445-
Download download = downloadList.get(0);
446-
download.setStatus(DownloadStatus.PREPARING);
447-
prepareDownload(download, null, getQueueSessionId(sessionIds, download.getFacilityName()));
448415
}
416+
Download queuedDownload = queuedDownloads.get(0);
417+
queuedDownload.setStatus(DownloadStatus.PREPARING);
418+
prepareDownload(queuedDownload, null, getQueueSessionId(sessionIds, queuedDownload.getFacilityName()));
449419
}
450420

451421
private void handleException( Download download, String reason, boolean doExpire ) {

src/main/java/org/icatproject/topcat/domain/Download.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public class Download implements Serializable {
7373
@Enumerated(EnumType.STRING)
7474
private DownloadStatus status;
7575

76+
@Column(name = "PRIORITY")
77+
private Integer priority;
78+
7679
@Column(name = "THE_SIZE")
7780
private long size;
7881

@@ -188,6 +191,14 @@ public void setStatus(DownloadStatus status) {
188191
this.status = status;
189192
}
190193

194+
public int getPriority() {
195+
return priority;
196+
}
197+
198+
public void setPriority(int priority) {
199+
this.priority = priority;
200+
}
201+
191202
public long getSize() {
192203
return size;
193204
}

src/main/java/org/icatproject/topcat/web/rest/UserResource.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,12 @@ private String getCartUserName(String userName, String sessionId) {
9898
* @param plugin ICAT authentication plugin. If null, a default value will be used.
9999
* @return json with sessionId of the form
100100
* <samp>{"sessionId","0d9a3706-80d4-4d29-9ff3-4d65d4308a24"}</samp>
101-
* @throws BadRequestException
101+
* @throws TopcatException
102102
*/
103103
@POST
104104
@Path("/session")
105-
public String login(@QueryParam("facilityName") String facilityName, @FormParam("username") String username, @FormParam("password") String password, @FormParam("plugin") String plugin) throws BadRequestException {
105+
public String login(@QueryParam("facilityName") String facilityName, @FormParam("username") String username,
106+
@FormParam("password") String password, @FormParam("plugin") String plugin) throws TopcatException {
106107
if (plugin == null) {
107108
plugin = defaultPlugin;
108109
}
@@ -759,6 +760,7 @@ public Response submitCart(@PathParam("facilityName") String facilityName,
759760
downloadItems.add(downloadItem);
760761
}
761762
download.setDownloadItems(downloadItems);
763+
download.setPriority(1);
762764
downloadId = submitDownload(idsClient, download, DownloadStatus.PREPARING);
763765
try {
764766
em.remove(cart);
@@ -849,6 +851,7 @@ private JsonArray submitDownloads(IdsClient idsClient, List<Download> downloads,
849851
*/
850852
private long submitDownload(IdsClient idsClient, Download download, DownloadStatus downloadStatus)
851853
throws TopcatException {
854+
852855
Boolean isTwoLevel = idsClient.isTwoLevel();
853856
download.setIsTwoLevel(isTwoLevel);
854857

src/test/java/org/icatproject/topcat/StatusCheckTest.java

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ public void testExpiredDownloadsIgnored() throws Exception {
562562

563563
// Create a single-tier download; initial status should be COMPLETE
564564
Download dummyDownload = TestHelpers.createDummyDownload("DummyUserName", preparedId, transport, false,
565-
status, false, downloadRepository);
565+
status, 0, false, downloadRepository);
566566
downloadId = dummyDownload.getId();
567567

568568
// Not testing delays, so set to zero
@@ -764,28 +764,44 @@ public void testStartQueuedDownloadNegative() throws Exception {
764764
System.out.println("DEBUG testStartQueuedDownloadNegative");
765765
Long downloadId1 = null;
766766
Long downloadId2 = null;
767+
Long downloadId3 = null;
768+
Long downloadId4 = null;
767769
try {
768770
String transport = "http";
769771
Download dummyDownload1 = TestHelpers.createDummyDownload("DummyUserName", null, transport, true,
770-
DownloadStatus.QUEUED, false, downloadRepository);
772+
DownloadStatus.QUEUED, 2, false, downloadRepository);
771773
Download dummyDownload2 = TestHelpers.createDummyDownload("DummyUserName", null, transport, true,
772-
DownloadStatus.QUEUED, false, downloadRepository);
774+
DownloadStatus.QUEUED, 1, false, downloadRepository);
775+
Download dummyDownload3 = TestHelpers.createDummyDownload("DummyUserName", null, transport, true,
776+
DownloadStatus.QUEUED, 2, false, downloadRepository);
777+
Download dummyDownload4 = TestHelpers.createDummyDownload("DummyUserName", null, transport, true,
778+
DownloadStatus.QUEUED, 1, false, downloadRepository);
773779
downloadId1 = dummyDownload1.getId();
774780
downloadId2 = dummyDownload2.getId();
781+
downloadId3 = dummyDownload3.getId();
782+
downloadId4 = dummyDownload4.getId();
775783

776784
statusCheck.startQueuedDownload(-1);
777785

778786
Download postDownload1 = TestHelpers.getDummyDownload(downloadId1, downloadRepository);
779787
Download postDownload2 = TestHelpers.getDummyDownload(downloadId2, downloadRepository);
780-
781-
assertEquals(DownloadStatus.RESTORING, postDownload1.getStatus());
782-
assertNotNull(postDownload1.getPreparedId());
783-
assertEquals(DownloadStatus.QUEUED, postDownload2.getStatus());
784-
assertNull(postDownload2.getPreparedId());
788+
Download postDownload3 = TestHelpers.getDummyDownload(downloadId3, downloadRepository);
789+
Download postDownload4 = TestHelpers.getDummyDownload(downloadId4, downloadRepository);
790+
791+
assertEquals(DownloadStatus.QUEUED, postDownload1.getStatus());
792+
assertEquals(DownloadStatus.RESTORING, postDownload2.getStatus());
793+
assertEquals(DownloadStatus.QUEUED, postDownload3.getStatus());
794+
assertEquals(DownloadStatus.QUEUED, postDownload4.getStatus());
795+
assertNull(postDownload1.getPreparedId());
796+
assertNotNull(postDownload2.getPreparedId());
797+
assertNull(postDownload3.getPreparedId());
798+
assertNull(postDownload4.getPreparedId());
785799
} finally {
786800
// clean up
787801
TestHelpers.deleteDummyDownload(downloadId1, downloadRepository);
788802
TestHelpers.deleteDummyDownload(downloadId2, downloadRepository);
803+
TestHelpers.deleteDummyDownload(downloadId3, downloadRepository);
804+
TestHelpers.deleteDummyDownload(downloadId4, downloadRepository);
789805
}
790806
}
791807

@@ -796,7 +812,7 @@ public void testStartQueuedDownloadZero() throws Exception {
796812
try {
797813
String transport = "http";
798814
Download dummyDownload = TestHelpers.createDummyDownload("DummyUserName", null, transport, true,
799-
DownloadStatus.QUEUED, false, downloadRepository);
815+
DownloadStatus.QUEUED, 1, false, downloadRepository);
800816
downloadId = dummyDownload.getId();
801817

802818
statusCheck.startQueuedDownload(0);
@@ -822,9 +838,9 @@ public void testStartQueuedDownloadNonZero() throws Exception {
822838
try {
823839
String transport = "http";
824840
Download dummyDownload1 = TestHelpers.createDummyDownload("DummyUserName", null, transport, true,
825-
DownloadStatus.QUEUED, false, downloadRepository);
841+
DownloadStatus.QUEUED, 1, false, downloadRepository);
826842
Download dummyDownload2 = TestHelpers.createDummyDownload("DummyUserName", null, transport, true,
827-
DownloadStatus.QUEUED, false, downloadRepository);
843+
DownloadStatus.QUEUED, 1, false, downloadRepository);
828844
downloadId1 = dummyDownload1.getId();
829845
downloadId2 = dummyDownload2.getId();
830846

@@ -852,9 +868,9 @@ public void testStartQueuedDownloadNonZeroRestoringDownload() throws Exception {
852868
try {
853869
String transport = "http";
854870
Download dummyDownload1 = TestHelpers.createDummyDownload("DummyUserName", "preparedId", transport, true,
855-
DownloadStatus.RESTORING, false, downloadRepository);
871+
DownloadStatus.RESTORING, 0, false, downloadRepository);
856872
Download dummyDownload2 = TestHelpers.createDummyDownload("DummyUserName", null, transport, true,
857-
DownloadStatus.QUEUED, false, downloadRepository);
873+
DownloadStatus.QUEUED, 1, false, downloadRepository);
858874
downloadId1 = dummyDownload1.getId();
859875
downloadId2 = dummyDownload2.getId();
860876

@@ -880,10 +896,10 @@ public void testStartQueuedDownloadNonZeroRestoringDownload() throws Exception {
880896
private Download createDummyDownload(String preparedId, String transport, Boolean isTwoLevel, Boolean isDeleted) {
881897
if (isTwoLevel) {
882898
return TestHelpers.createDummyDownload("DummyUserName", preparedId, transport, isTwoLevel,
883-
DownloadStatus.PREPARING, isDeleted, downloadRepository);
899+
DownloadStatus.PREPARING, 0, isDeleted, downloadRepository);
884900
} else {
885901
return TestHelpers.createDummyDownload("DummyUserName", preparedId, transport, isTwoLevel,
886-
DownloadStatus.COMPLETE, isDeleted, downloadRepository);
902+
DownloadStatus.COMPLETE, 0, isDeleted, downloadRepository);
887903
}
888904
}
889905
}

src/test/java/org/icatproject/topcat/TestHelpers.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public boolean verify(String hostname, SSLSession session) {
5959
}
6060

6161
public static Download createDummyDownload(String userName, String preparedId, String transport, Boolean isTwoLevel,
62-
DownloadStatus downloadStatus, Boolean isDeleted, DownloadRepository downloadRepository) {
62+
DownloadStatus downloadStatus, int priority, Boolean isDeleted, DownloadRepository downloadRepository) {
6363

6464
// This mocks what UserResource.submitCart() might do.
6565

@@ -86,14 +86,9 @@ public static Download createDummyDownload(String userName, String preparedId, S
8686

8787
List<DownloadItem> downloadItems = new ArrayList<DownloadItem>();
8888
download.setDownloadItems(downloadItems);
89-
9089
download.setIsTwoLevel(isTwoLevel);
91-
92-
if (isTwoLevel) {
93-
download.setStatus(downloadStatus);
94-
} else {
95-
download.setStatus(downloadStatus);
96-
}
90+
download.setStatus(downloadStatus);
91+
download.setPriority(priority);
9792

9893
return downloadRepository.save(download);
9994
}

0 commit comments

Comments
 (0)