BUG-5280: add {Create,Close,Purge}LocalHistoryPayload
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / LeaderFrontendState.java
index 3c65b799d1831c498c6321df878305fb2c56a737..deca605a5b97ec18d041d050d5fe0c467feb436e 100644 (file)
@@ -75,7 +75,7 @@ final class LeaderFrontendState implements Identifiable<ClientIdentifier> {
         this.persistenceId = Preconditions.checkNotNull(persistenceId);
         this.clientId = Preconditions.checkNotNull(clientId);
         this.tree = Preconditions.checkNotNull(tree);
-        standaloneHistory = new StandaloneFrontendHistory(persistenceId, clientId, tree);
+        standaloneHistory = new StandaloneFrontendHistory(persistenceId, tree.ticker(), clientId, tree);
     }
 
     @Override
@@ -94,16 +94,16 @@ final class LeaderFrontendState implements Identifiable<ClientIdentifier> {
     }
 
     @Nullable LocalHistorySuccess handleLocalHistoryRequest(final LocalHistoryRequest<?> request,
-            final RequestEnvelope envelope) throws RequestException {
+            final RequestEnvelope envelope, final long now) throws RequestException {
         checkRequestSequence(envelope);
 
         try {
             if (request instanceof CreateLocalHistoryRequest) {
                 return handleCreateHistory((CreateLocalHistoryRequest) request);
             } else if (request instanceof DestroyLocalHistoryRequest) {
-                return handleDestroyHistory((DestroyLocalHistoryRequest) request);
+                return handleDestroyHistory((DestroyLocalHistoryRequest) request, envelope, now);
             } else if (request instanceof PurgeLocalHistoryRequest) {
-                return handlePurgeHistory((PurgeLocalHistoryRequest)request);
+                return handlePurgeHistory((PurgeLocalHistoryRequest)request, envelope, now);
             } else {
                 throw new UnsupportedRequestException(request);
             }
@@ -133,12 +133,14 @@ final class LeaderFrontendState implements Identifiable<ClientIdentifier> {
             lastSeenHistory = id.getHistoryId();
         }
 
-        localHistories.put(id, new LocalFrontendHistory(persistenceId, tree.ensureTransactionChain(id)));
+        localHistories.put(id, new LocalFrontendHistory(persistenceId, tree, tree.ensureTransactionChain(id)));
         LOG.debug("{}: created history {}", persistenceId, id);
         return new LocalHistorySuccess(id, request.getSequence());
     }
 
-    private LocalHistorySuccess handleDestroyHistory(final DestroyLocalHistoryRequest request) throws RequestException {
+    private LocalHistorySuccess handleDestroyHistory(final DestroyLocalHistoryRequest request,
+            final RequestEnvelope envelope, final long now)
+            throws RequestException {
         final LocalHistoryIdentifier id = request.getTarget();
         final LocalFrontendHistory existing = localHistories.get(id);
         if (existing == null) {
@@ -147,32 +149,27 @@ final class LeaderFrontendState implements Identifiable<ClientIdentifier> {
             return new LocalHistorySuccess(id, request.getSequence());
         }
 
-        return existing.destroy(request.getSequence());
+        existing.destroy(request.getSequence(), envelope, now);
+        return null;
     }
 
-    private LocalHistorySuccess handlePurgeHistory(final PurgeLocalHistoryRequest request) throws RequestException {
+    private LocalHistorySuccess handlePurgeHistory(final PurgeLocalHistoryRequest request,
+            final RequestEnvelope envelope, final long now) throws RequestException {
         final LocalHistoryIdentifier id = request.getTarget();
         final LocalFrontendHistory existing = localHistories.remove(id);
-        if (existing != null) {
-            purgedHistories.add(Range.singleton(UnsignedLong.fromLongBits(id.getHistoryId())));
-
-            if (!existing.isDestroyed()) {
-                LOG.warn("{}: purging undestroyed history {}", persistenceId, id);
-                existing.destroy(request.getSequence());
-            }
-
-            // FIXME: record a PURGE tombstone in the journal
-
-            LOG.debug("{}: purged history {}", persistenceId, id);
-        } else {
+        if (existing == null) {
             LOG.debug("{}: history {} has already been purged", persistenceId, id);
+            return new LocalHistorySuccess(id, request.getSequence());
         }
 
-        return new LocalHistorySuccess(id, request.getSequence());
+        LOG.debug("{}: purging history {}", persistenceId, id);
+        purgedHistories.add(Range.singleton(UnsignedLong.fromLongBits(id.getHistoryId())));
+        existing.purge(request.getSequence(), envelope, now);
+        return null;
     }
 
     @Nullable TransactionSuccess<?> handleTransactionRequest(final TransactionRequest<?> request,
-            final RequestEnvelope envelope) throws RequestException {
+            final RequestEnvelope envelope, final long now) throws RequestException {
         checkRequestSequence(envelope);
 
         try {
@@ -189,7 +186,7 @@ final class LeaderFrontendState implements Identifiable<ClientIdentifier> {
                 history = standaloneHistory;
             }
 
-            return history.handleTransactionRequest(request, envelope);
+            return history.handleTransactionRequest(request, envelope, now);
         } finally {
             expectNextRequest();
         }