2 * Copyright (c) 2015 Brocade Communications 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
8 package org.opendaylight.controller.cluster.datastore;
10 import akka.actor.ActorRef;
11 import akka.actor.UntypedActorContext;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
14 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
15 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
16 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
17 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
20 * A factory for creating ShardTransaction actors.
22 * @author Thomas Pantelis
24 class ShardTransactionActorFactory {
26 private final ShardDataTree dataTree;
27 private final DatastoreContext datastoreContext;
28 private final String txnDispatcherPath;
29 private final ShardStats shardMBean;
30 private final UntypedActorContext actorContext;
31 private final ActorRef shardActor;
33 ShardTransactionActorFactory(ShardDataTree dataTree, DatastoreContext datastoreContext,
34 String txnDispatcherPath, ActorRef shardActor, UntypedActorContext actorContext, ShardStats shardMBean) {
35 this.dataTree = Preconditions.checkNotNull(dataTree);
36 this.datastoreContext = datastoreContext;
37 this.txnDispatcherPath = txnDispatcherPath;
38 this.shardMBean = shardMBean;
39 this.actorContext = actorContext;
40 this.shardActor = shardActor;
43 private static String actorNameFor(final TransactionIdentifier txId) {
44 final LocalHistoryIdentifier historyId = txId.getHistoryId();
45 final ClientIdentifier clientId = historyId.getClientId();
46 final FrontendIdentifier frontendId = clientId.getFrontendId();
48 final StringBuilder sb = new StringBuilder("shard-");
49 sb.append(frontendId.getMemberName().getName()).append(':');
50 sb.append(frontendId.getClientType().getName()).append('@');
51 sb.append(clientId.getGeneration()).append(':');
52 if (historyId.getHistoryId() != 0) {
53 sb.append(historyId.getHistoryId()).append('-');
56 return sb.append(txId.getTransactionId()).toString();
59 ActorRef newShardTransaction(TransactionType type, TransactionIdentifier transactionID) {
60 final AbstractShardDataTreeTransaction<?> transaction;
63 transaction = dataTree.newReadOnlyTransaction(transactionID);
64 shardMBean.incrementReadOnlyTransactionCount();
67 transaction = dataTree.newReadWriteTransaction(transactionID);
68 shardMBean.incrementReadWriteTransactionCount();
71 transaction = dataTree.newReadWriteTransaction(transactionID);
72 shardMBean.incrementWriteOnlyTransactionCount();
75 throw new IllegalArgumentException("Unsupported transaction type " + type);
78 return actorContext.actorOf(ShardTransaction.props(type, transaction, shardActor, datastoreContext, shardMBean)
79 .withDispatcher(txnDispatcherPath), actorNameFor(transactionID));