Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIRCSTORE-526] Add ILR support for publishing batch request update events #489

Merged
merged 14 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion ramls/request-queue-reordering.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
"type": "string",
"$ref": "raml-util/schemas/uuid.schema"
},
"itemId": {
"description": "Item ID of reordered requests",
"type": "string",
"$ref": "raml-util/schemas/uuid.schema"
},
"requestLevel": {
"description": "Level of the request - Item or Title",
"type": "string",
"enum": ["Item", "Title"]
},
"requestIds": {
"description": "Array of requests ids",
"type": "array",
Expand All @@ -18,5 +28,8 @@
}
}
},
"additionalProperties": false
"additionalProperties": false,
"required": [
"requestLevel"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import io.vertx.sqlclient.RowSet;

public class RequestBatchResourceService {
private static final Logger LOG = LoggerFactory.getLogger(RequestBatchResourceService.class);
private static final Logger log = LoggerFactory.getLogger(RequestBatchResourceService.class);
private static final String REMOVE_POSITIONS_SQL =
"UPDATE %s.%s SET jsonb = jsonb - 'position' WHERE id::text IN (%s)";

Expand Down Expand Up @@ -70,7 +70,7 @@ public RequestBatchResourceService(Context context, Map<String, String> okapiHea
public void executeRequestBatchUpdate(List<Request> requests,
Handler<AsyncResult<Void>> onFinishHandler) {

LOG.debug("Removing positions for all request to go through positions constraint");
log.debug("Removing positions for all request to go through positions constraint");
List<Function<SQLConnection, Future<RowSet<Row>>>> allDatabaseOperations =
new ArrayList<>();

Expand All @@ -85,24 +85,29 @@ public void executeRequestBatchUpdate(List<Request> requests,
allDatabaseOperations.add(removePositionBatch);
allDatabaseOperations.addAll(updateRequestsBatch);

LOG.info("Executing batch update, total records to update [{}] (including remove positions)",
log.info("Executing batch update, total records to update [{}] (including remove positions)",
allDatabaseOperations.size());

RequestQueueReordering payload = mapRequestsToPayload(requests);
LOG.info("executeRequestBatchUpdate:: instanceId: {}, requests: {}",
payload.getInstanceId(), payload.getRequestIds());
log.info("executeRequestBatchUpdate:: instanceId: {}, itemId: {}, requestLevel: {}, " +
"requests: {}", payload.getInstanceId(), payload.getItemId(), payload.getRequestLevel(),
payload.getRequestIds());

batchResourceService.executeBatchUpdate(allDatabaseOperations, onFinishHandler)
.compose(v -> eventPublisher.publishCreated(payload.getInstanceId(), payload));
}

private RequestQueueReordering mapRequestsToPayload(List<Request> requests) {
var firstRequest = requests.get(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var firstRequest = requests.get(0);
var firstRequest = requests.iterator().next();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we work with List in this method (not Collection), accessing an element by index will be more efficient for array-based implementation with constant time complexity O(1)


return new RequestQueueReordering()
.withRequestIds(requests.stream()
.map(Request::getId)
.toList())
.withInstanceId(requests.get(0).getInstanceId());
.withInstanceId(firstRequest.getInstanceId())
.withItemId(firstRequest.getItemId())
.withRequestLevel(RequestQueueReordering.RequestLevel.valueOf(
firstRequest.getRequestLevel().name()));
}

private Function<SQLConnection, Future<RowSet<Row>>> removePositionsForRequestsBatch(
Expand Down
12 changes: 8 additions & 4 deletions src/test/java/org/folio/rest/api/RequestBatchAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.folio.rest.api.RequestsApiTest.requestStorageUrl;
import static org.folio.rest.api.StorageTestSuite.TENANT_ID;
import static org.folio.rest.api.StorageTestSuite.storageUrl;
import static org.folio.rest.jaxrs.model.RequestQueueReordering.RequestLevel.TITLE;
import static org.folio.rest.support.kafka.FakeKafkaConsumer.removeAllEvents;
import static org.folio.rest.support.matchers.DomainEventAssertions.assertRequestQueueReorderingEvent;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -22,6 +23,7 @@

import org.folio.rest.impl.RequestsBatchAPI;
import org.folio.rest.jaxrs.model.Request;
import org.folio.rest.jaxrs.model.RequestQueueReordering;
import org.folio.rest.support.ApiTests;
import org.folio.rest.support.JsonResponse;
import org.folio.rest.support.Response;
Expand Down Expand Up @@ -245,13 +247,15 @@ public void shouldPublishKafkaEventWhenUpdateRequestPositionsInBatchForTheInstan
.map(JsonObject.class::cast)
.sorted(Comparator.comparingInt(obj -> obj.getInteger("position")))
.toList();
String firstRequestId = firstRequest.getString("id");
String secondRequestId = secondRequest.getString("id");
assertThat(requestsSorted.get(0).getInteger("position"), is(1));
assertThat(requestsSorted.get(1).getInteger("position"), is(2));
assertThat(requestsSorted.get(0).getString("id"), is(secondRequest.getString("id")));
assertThat(requestsSorted.get(1).getString("id"), is(firstRequest.getString("id")));
assertThat(requestsSorted.get(0).getString("id"), is(secondRequestId));
assertThat(requestsSorted.get(1).getString("id"), is(firstRequestId));

assertRequestQueueReorderingEvent(instanceId.toString(), List.of(
firstRequest.getString("id"), secondRequest.getString("id")));
assertRequestQueueReorderingEvent(instanceId.toString(),
requestsSorted.get(1).getString("itemId"), List.of(firstRequestId, secondRequestId), TITLE);
Comment on lines +257 to +258
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m.b. it's a good idea to extract constants ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertRequestQueueReorderingEvent is a universal method that is also applicable to queues with ITEM requestLevel requests

}

private JsonObject getAllRequestsForItem(UUID itemId) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import org.awaitility.Awaitility;
import org.awaitility.core.ConditionFactory;
import org.folio.rest.jaxrs.model.RequestQueueReordering;
import org.folio.service.event.DomainEventType;

import io.vertx.core.MultiMap;
Expand Down Expand Up @@ -111,13 +112,15 @@ public static void assertCreateEventForRequest(JsonObject request) {
assertCreateEvent(getLastRequestEvent(requestId), request);
}

public static void assertRequestQueueReorderingEvent(String instanceId,
List<String> requestIds) {
public static void assertRequestQueueReorderingEvent(String instanceId, String itemId,
List<String> requestIds, RequestQueueReordering.RequestLevel requestLevel) {

await().until(() -> getRequestQueueReorderingEvents().size(), greaterThan(0));

JsonObject payload = new JsonObject()
.put("instanceId", instanceId)
.put("itemId", itemId)
.put("requestLevel", requestLevel.value())
.put("requestIds", new JsonArray(requestIds));

assertCreateEvent(getFirstRequestQueueReorderingEvent(), payload);
Expand Down