- localHistories.put(id, new LocalFrontendHistory(persistenceId, tree.ensureTransactionChain(id)));
- LOG.debug("{}: created history {}", persistenceId, id);
- return new LocalHistorySuccess(id, request.getSequence());
- }
+ private LocalHistorySuccess handleCreateHistory(final CreateLocalHistoryRequest request,
+ final RequestEnvelope envelope, final long now) throws RequestException {
+ final LocalHistoryIdentifier historyId = request.getTarget();
+ final AbstractFrontendHistory existing = localHistories.get(historyId);
+ if (existing != null) {
+ // History already exists: report success
+ LOG.debug("{}: history {} already exists", persistenceId(), historyId);
+ return new LocalHistorySuccess(historyId, request.getSequence());
+ }
+
+ // We have not found the history. Before we create it we need to check history ID sequencing so that we do
+ // not end up resurrecting a purged history.
+ if (purgedHistories.contains(historyId.getHistoryId())) {
+ LOG.debug("{}: rejecting purged request {}", persistenceId(), request);
+ throw new DeadHistoryException(purgedHistories.toImmutable());
+ }
+
+ // Update last history we have seen
+ if (lastSeenHistory == null || Long.compareUnsigned(lastSeenHistory, historyId.getHistoryId()) < 0) {
+ lastSeenHistory = historyId.getHistoryId();
+ }
+
+ // We have to send the response only after persistence has completed
+ final ShardDataTreeTransactionChain chain = tree().ensureTransactionChain(historyId, () -> {
+ LOG.debug("{}: persisted history {}", persistenceId(), historyId);
+ envelope.sendSuccess(new LocalHistorySuccess(historyId, request.getSequence()),
+ tree().readTime() - now);
+ });