Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / BatchedModifications.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.datastore.messages;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import java.util.Optional;
17 import java.util.SortedSet;
18 import java.util.TreeSet;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
21 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
22 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
23
24 /**
25  * Message used to batch write, merge, delete modification operations to the  ShardTransaction actor.
26  *
27  * @author Thomas Pantelis
28  */
29 @Deprecated(since = "9.0.0", forRemoval = true)
30 public final class BatchedModifications extends MutableCompositeModification {
31     @java.io.Serial
32     private static final long serialVersionUID = 1L;
33
34     private boolean ready;
35     private boolean doCommitOnReady;
36     private int totalMessagesSent;
37     private TransactionIdentifier transactionId;
38
39     private @Nullable SortedSet<String> participatingShardNames;
40
41     public BatchedModifications() {
42     }
43
44     public BatchedModifications(final TransactionIdentifier transactionId, final short version) {
45         super(version);
46         this.transactionId = requireNonNull(transactionId, "transactionID can't be null");
47     }
48
49     public boolean isReady() {
50         return ready;
51     }
52
53     public void setReady(final Optional<SortedSet<String>> possibleParticipatingShardNames) {
54         ready = true;
55         participatingShardNames = requireNonNull(possibleParticipatingShardNames).orElse(null);
56         Preconditions.checkArgument(participatingShardNames == null || participatingShardNames.size() > 1);
57     }
58
59     public void setReady() {
60         setReady(Optional.empty());
61     }
62
63     public Optional<SortedSet<String>> getParticipatingShardNames() {
64         return Optional.ofNullable(participatingShardNames);
65     }
66
67     public boolean isDoCommitOnReady() {
68         return doCommitOnReady;
69     }
70
71     public void setDoCommitOnReady(final boolean doCommitOnReady) {
72         this.doCommitOnReady = doCommitOnReady;
73     }
74
75     public int getTotalMessagesSent() {
76         return totalMessagesSent;
77     }
78
79     public void setTotalMessagesSent(final int totalMessagesSent) {
80         this.totalMessagesSent = totalMessagesSent;
81     }
82
83     public TransactionIdentifier getTransactionId() {
84         return transactionId;
85     }
86
87     @Override
88     public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
89         super.readExternal(in);
90         transactionId = TransactionIdentifier.readFrom(in);
91         ready = in.readBoolean();
92         totalMessagesSent = in.readInt();
93         doCommitOnReady = in.readBoolean();
94
95         if (getVersion() >= DataStoreVersions.FLUORINE_VERSION) {
96             final int count = in.readInt();
97             if (count != 0) {
98                 SortedSet<String> shardNames = new TreeSet<>();
99                 for (int i = 0; i < count; i++) {
100                     shardNames.add((String) in.readObject());
101                 }
102
103                 participatingShardNames = shardNames;
104             }
105         }
106     }
107
108     @Override
109     public void writeExternal(final ObjectOutput out) throws IOException {
110         super.writeExternal(out);
111         transactionId.writeTo(out);
112         out.writeBoolean(ready);
113         out.writeInt(totalMessagesSent);
114         out.writeBoolean(doCommitOnReady);
115
116         if (getVersion() >= DataStoreVersions.FLUORINE_VERSION) {
117             if (participatingShardNames != null) {
118                 out.writeInt(participatingShardNames.size());
119                 for (String shardName : participatingShardNames) {
120                     out.writeObject(shardName);
121                 }
122             } else {
123                 out.writeInt(0);
124             }
125         }
126     }
127
128     @Override
129     public String toString() {
130         return "BatchedModifications [transactionId=" + transactionId
131                 + ", ready=" + isReady()
132                 + ", participatingShardNames=" + participatingShardNames
133                 + ", totalMessagesSent=" + totalMessagesSent
134                 + ", modifications size=" + getModifications().size() + "]";
135     }
136 }