Bug 5419: Add persistAsync method to DataPersistenceProvider
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / RaftActorDelegatingPersistentDataProvider.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.raft;
9
10 import akka.japi.Procedure;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.controller.cluster.DataPersistenceProvider;
13 import org.opendaylight.controller.cluster.DelegatingPersistentDataProvider;
14 import org.opendaylight.controller.cluster.PersistentDataProvider;
15 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
16 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.PersistentPayload;
17
18 /**
19  * The DelegatingPersistentDataProvider used by RaftActor to override the configured persistent provider to
20  * persist ReplicatedLogEntry's based on whether or not the payload is a PersistentPayload instance.
21  *
22  * @author Thomas Pantelis
23  */
24 class RaftActorDelegatingPersistentDataProvider extends DelegatingPersistentDataProvider {
25     private final PersistentDataProvider persistentProvider;
26
27     RaftActorDelegatingPersistentDataProvider(DataPersistenceProvider delegate,
28             PersistentDataProvider persistentProvider) {
29         super(delegate);
30         this.persistentProvider = Preconditions.checkNotNull(persistentProvider);
31     }
32
33     @Override
34     public <T> void persist(final T entry, final Procedure<T> procedure) {
35         doPersist(entry, procedure, false);
36     }
37
38     @Override
39     public <T> void persistAsync(T entry, Procedure<T> procedure) {
40         doPersist(entry, procedure, true);
41     }
42
43     private <T> void doPersist(final T entry, final Procedure<T> procedure, final boolean async) {
44         if (getDelegate().isRecoveryApplicable()) {
45             persistSuper(entry, procedure, async);
46         } else {
47             if (entry instanceof ReplicatedLogEntry) {
48                 Payload payload = ((ReplicatedLogEntry)entry).getData();
49                 if (payload instanceof PersistentPayload) {
50                     // We persist the Payload but not the ReplicatedLogEntry to avoid gaps in the journal indexes
51                     // on recovery if data persistence is later enabled.
52                     if (async) {
53                         persistentProvider.persistAsync(payload, p -> procedure.apply(entry));
54                     } else {
55                         persistentProvider.persist(payload, p -> procedure.apply(entry));
56                     }
57                 } else {
58                     persistSuper(entry, procedure, async);
59                 }
60             } else {
61                 persistSuper(entry, procedure, async);
62             }
63         }
64     }
65
66     private <T> void persistSuper(final T object, final Procedure<T> procedure, final boolean async) {
67         if (async) {
68             super.persistAsync(object, procedure);
69         } else {
70             super.persist(object, procedure);
71         }
72     }
73 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.