Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardTransactionActorFactory.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;
9
10 import static java.util.Objects.requireNonNull;
11
12 import akka.actor.AbstractActor.ActorContext;
13 import akka.actor.ActorRef;
14 import java.util.concurrent.atomic.AtomicLong;
15 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
16 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
17 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
18 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
19
20 /**
21  * A factory for creating ShardTransaction actors.
22  *
23  * @author Thomas Pantelis
24  */
25 class ShardTransactionActorFactory {
26     private static final AtomicLong ACTOR_NAME_COUNTER = new AtomicLong();
27
28     private final ShardDataTree dataTree;
29     private final DatastoreContext datastoreContext;
30     private final String txnDispatcherPath;
31     private final ShardStats shardMBean;
32     private final ActorContext actorContext;
33     private final ActorRef shardActor;
34     private final String shardName;
35
36     ShardTransactionActorFactory(ShardDataTree dataTree, DatastoreContext datastoreContext,
37             String txnDispatcherPath, ActorRef shardActor, ActorContext actorContext, ShardStats shardMBean,
38             String shardName) {
39         this.dataTree = requireNonNull(dataTree);
40         this.datastoreContext = requireNonNull(datastoreContext);
41         this.txnDispatcherPath = requireNonNull(txnDispatcherPath);
42         this.shardMBean = requireNonNull(shardMBean);
43         this.actorContext = requireNonNull(actorContext);
44         this.shardActor = requireNonNull(shardActor);
45         this.shardName = requireNonNull(shardName);
46     }
47
48     private String actorNameFor(final TransactionIdentifier txId) {
49         final LocalHistoryIdentifier historyId = txId.getHistoryId();
50         final ClientIdentifier clientId = historyId.getClientId();
51         final FrontendIdentifier frontendId = clientId.getFrontendId();
52
53         final StringBuilder sb = new StringBuilder("shard-");
54         sb.append(shardName).append('-')
55             .append(frontendId.getMemberName().getName()).append(':')
56             .append(frontendId.getClientType().getName()).append('@')
57             .append(clientId.getGeneration()).append(':');
58         if (historyId.getHistoryId() != 0) {
59             sb.append(historyId.getHistoryId()).append('-');
60         }
61
62         return sb.append(txId.getTransactionId()).append('_').append(ACTOR_NAME_COUNTER.incrementAndGet()).toString();
63     }
64
65     ActorRef newShardTransaction(TransactionType type, TransactionIdentifier transactionID) {
66         final AbstractShardDataTreeTransaction<?> transaction;
67         switch (type) {
68             case READ_ONLY:
69                 transaction = dataTree.newReadOnlyTransaction(transactionID);
70                 break;
71             case READ_WRITE:
72             case WRITE_ONLY:
73                 transaction = dataTree.newReadWriteTransaction(transactionID);
74                 break;
75             default:
76                 throw new IllegalArgumentException("Unsupported transaction type " + type);
77         }
78
79         return actorContext.actorOf(ShardTransaction.props(type, transaction, shardActor, datastoreContext, shardMBean)
80             .withDispatcher(txnDispatcherPath), actorNameFor(transactionID));
81     }
82 }