2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard;
11 import akka.pattern.Patterns;
12 import akka.util.Timeout;
13 import com.google.common.base.Stopwatch;
14 import com.google.common.cache.Cache;
15 import com.google.common.cache.CacheBuilder;
16 import java.text.SimpleDateFormat;
17 import java.util.Date;
18 import java.util.List;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.atomic.AtomicLong;
22 import org.opendaylight.controller.cluster.datastore.Shard;
23 import org.opendaylight.controller.cluster.raft.client.messages.FollowerInfo;
24 import org.opendaylight.controller.cluster.raft.client.messages.GetOnDemandRaftState;
25 import org.opendaylight.controller.cluster.raft.client.messages.OnDemandRaftState;
26 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
27 import scala.concurrent.Await;
30 * Maintains statistics for a shard.
32 * @author Basheeruddin syedbahm@cisco.com
34 public class ShardStats extends AbstractMXBean implements ShardStatsMXBean {
35 public static String JMX_CATEGORY_SHARD = "Shards";
37 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
39 private static final Cache<String, OnDemandRaftState> onDemandRaftStateCache =
40 CacheBuilder.newBuilder().expireAfterWrite(2, TimeUnit.SECONDS).build();
42 private long committedTransactionsCount;
44 private long readOnlyTransactionCount;
46 private long writeOnlyTransactionCount;
48 private long readWriteTransactionCount;
50 private long lastCommittedTransactionTime;
52 private long failedTransactionsCount;
54 private final AtomicLong failedReadTransactionsCount = new AtomicLong();
56 private long abortTransactionsCount;
58 private boolean followerInitialSyncStatus = false;
62 private String statRetrievalError;
64 private String statRetrievalTime;
66 private long leadershipChangeCount;
68 private long lastLeadershipChangeTime;
70 public ShardStats(final String shardName, final String mxBeanType) {
71 super(shardName, mxBeanType, JMX_CATEGORY_SHARD);
74 public void setShard(Shard shard) {
78 private OnDemandRaftState getOnDemandRaftState() {
79 String name = getShardName();
80 OnDemandRaftState state = onDemandRaftStateCache.getIfPresent(name);
82 statRetrievalError = null;
83 statRetrievalTime = null;
86 Timeout timeout = new Timeout(10, TimeUnit.SECONDS);
88 Stopwatch timer = Stopwatch.createStarted();
90 state = (OnDemandRaftState) Await.result(Patterns.ask(shard.getSelf(),
91 GetOnDemandRaftState.INSTANCE, timeout), timeout.duration());
93 statRetrievalTime = timer.stop().toString();
94 onDemandRaftStateCache.put(name, state);
95 } catch (Exception e) {
96 statRetrievalError = e.toString();
100 state = state != null ? state : OnDemandRaftState.builder().build();
107 public String getShardName() {
108 return getMBeanName();
112 public long getCommittedTransactionsCount() {
113 return committedTransactionsCount;
117 public String getLeader() {
118 return getOnDemandRaftState().getLeader();
122 public String getRaftState() {
123 return getOnDemandRaftState().getRaftState();
127 public long getReadOnlyTransactionCount() {
128 return readOnlyTransactionCount;
132 public long getWriteOnlyTransactionCount() {
133 return writeOnlyTransactionCount;
137 public long getReadWriteTransactionCount() {
138 return readWriteTransactionCount;
142 public long getLastLogIndex() {
143 return getOnDemandRaftState().getLastLogIndex();
147 public long getLastLogTerm() {
148 return getOnDemandRaftState().getLastLogTerm();
152 public long getCurrentTerm() {
153 return getOnDemandRaftState().getCurrentTerm();
157 public long getCommitIndex() {
158 return getOnDemandRaftState().getCommitIndex();
162 public long getLastApplied() {
163 return getOnDemandRaftState().getLastApplied();
167 public long getLastIndex() {
168 return getOnDemandRaftState().getLastIndex();
172 public long getLastTerm() {
173 return getOnDemandRaftState().getLastTerm();
177 public long getSnapshotIndex() {
178 return getOnDemandRaftState().getSnapshotIndex();
182 public long getSnapshotTerm() {
183 return getOnDemandRaftState().getSnapshotTerm();
187 public long getReplicatedToAllIndex() {
188 return getOnDemandRaftState().getReplicatedToAllIndex();
192 public String getVotedFor() {
193 return getOnDemandRaftState().getVotedFor();
197 public boolean isSnapshotCaptureInitiated() {
198 return getOnDemandRaftState().isSnapshotCaptureInitiated();
202 public String getLastCommittedTransactionTime() {
203 return DATE_FORMAT.format(new Date(lastCommittedTransactionTime));
207 public long getFailedTransactionsCount() {
208 return failedTransactionsCount;
212 public long getFailedReadTransactionsCount() {
213 return failedReadTransactionsCount.get();
217 public long getAbortTransactionsCount() {
218 return abortTransactionsCount;
221 public long incrementCommittedTransactionCount() {
222 return ++committedTransactionsCount;
225 public long incrementReadOnlyTransactionCount() {
226 return ++readOnlyTransactionCount;
229 public long incrementWriteOnlyTransactionCount() {
230 return ++writeOnlyTransactionCount;
233 public long incrementReadWriteTransactionCount() {
234 return ++readWriteTransactionCount;
237 public long incrementFailedTransactionsCount() {
238 return ++failedTransactionsCount;
241 public long incrementFailedReadTransactionsCount() {
242 return failedReadTransactionsCount.incrementAndGet();
245 public long incrementAbortTransactionsCount ()
247 return ++abortTransactionsCount;
250 public void setLastCommittedTransactionTime(final long lastCommittedTransactionTime) {
251 this.lastCommittedTransactionTime = lastCommittedTransactionTime;
255 public long getInMemoryJournalDataSize(){
256 return getOnDemandRaftState().getInMemoryJournalDataSize();
260 public long getInMemoryJournalLogSize() {
261 return getOnDemandRaftState().getInMemoryJournalLogSize();
265 * resets the counters related to transactions
268 public void resetTransactionCounters(){
269 committedTransactionsCount = 0;
271 readOnlyTransactionCount = 0;
273 writeOnlyTransactionCount = 0;
275 readWriteTransactionCount = 0;
277 lastCommittedTransactionTime = 0;
279 failedTransactionsCount = 0;
281 failedReadTransactionsCount.set(0);
283 abortTransactionsCount = 0;
287 public void setFollowerInitialSyncStatus(boolean followerInitialSyncStatus) {
288 this.followerInitialSyncStatus = followerInitialSyncStatus;
292 public boolean getFollowerInitialSyncStatus() {
293 return followerInitialSyncStatus;
297 public List<FollowerInfo> getFollowerInfo() {
298 return getOnDemandRaftState().getFollowerInfoList();
302 public String getPeerAddresses() {
303 StringBuilder builder = new StringBuilder();
305 for(Map.Entry<String, String> e: getOnDemandRaftState().getPeerAddresses().entrySet()) {
307 builder.append(", ");
310 builder.append(e.getKey()).append(": ").append(e.getValue());
313 return builder.toString();
317 public String getStatRetrievalTime() {
318 getOnDemandRaftState();
319 return statRetrievalTime;
323 public String getStatRetrievalError() {
324 getOnDemandRaftState();
325 return statRetrievalError;
329 public long getLeadershipChangeCount() {
330 return leadershipChangeCount;
333 public void incrementLeadershipChangeCount() {
334 leadershipChangeCount++;
335 lastLeadershipChangeTime = System.currentTimeMillis();
339 public String getLastLeadershipChangeTime() {
340 return DATE_FORMAT.format(new Date(lastLeadershipChangeTime));
344 public int getPendingTxCommitQueueSize() {
345 return shard.getPendingTxCommitQueueSize();