Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / cds-access-client / src / main / java / org / opendaylight / controller / cluster / access / client / SavingClientActorBehavior.java
1 /*
2  * Copyright (c) 2016 Cisco 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.access.client;
9
10 import static java.util.Objects.requireNonNull;
11
12 import akka.persistence.DeleteSnapshotsFailure;
13 import akka.persistence.DeleteSnapshotsSuccess;
14 import akka.persistence.SaveSnapshotFailure;
15 import akka.persistence.SaveSnapshotSuccess;
16 import akka.persistence.SnapshotSelectionCriteria;
17 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Transient behavior handling messages while the new generation is being persisted.
23  *
24  * @author Robert Varga
25  */
26 final class SavingClientActorBehavior extends RecoveredClientActorBehavior<InitialClientActorContext> {
27     private static final Logger LOG = LoggerFactory.getLogger(SavingClientActorBehavior.class);
28     private final ClientIdentifier myId;
29
30     SavingClientActorBehavior(final InitialClientActorContext context, final ClientIdentifier nextId) {
31         super(context);
32         myId = requireNonNull(nextId);
33     }
34
35     @Override
36     AbstractClientActorBehavior<?> onReceiveCommand(final Object command) {
37         if (command instanceof SaveSnapshotFailure saveFailure) {
38             LOG.error("{}: failed to persist state", persistenceId(), saveFailure.cause());
39             return null;
40         } else if (command instanceof SaveSnapshotSuccess saved) {
41             LOG.debug("{}: got command: {}", persistenceId(), saved);
42             context().deleteSnapshots(new SnapshotSelectionCriteria(scala.Long.MaxValue(),
43                     saved.metadata().timestamp() - 1, 0L, 0L));
44             return this;
45         } else if (command instanceof DeleteSnapshotsSuccess deleteSuccess) {
46             LOG.debug("{}: got command: {}", persistenceId(), deleteSuccess);
47         } else if (command instanceof DeleteSnapshotsFailure deleteFailure) {
48             // Not treating this as a fatal error.
49             LOG.warn("{}: failed to delete prior snapshots", persistenceId(), deleteFailure.cause());
50         } else {
51             LOG.debug("{}: stashing command {}", persistenceId(), command);
52             context().stash();
53             return this;
54         }
55
56         context().unstash();
57         return context().createBehavior(myId);
58     }
59 }