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;
32 private final String shardName;
34 ShardTransactionActorFactory(ShardDataTree dataTree, DatastoreContext datastoreContext,
35 String txnDispatcherPath, ActorRef shardActor, UntypedActorContext actorContext, ShardStats shardMBean,
37 this.dataTree = Preconditions.checkNotNull(dataTree);
38 this.datastoreContext = Preconditions.checkNotNull(datastoreContext);
39 this.txnDispatcherPath = Preconditions.checkNotNull(txnDispatcherPath);
40 this.shardMBean = Preconditions.checkNotNull(shardMBean);
41 this.actorContext = Preconditions.checkNotNull(actorContext);
42 this.shardActor = Preconditions.checkNotNull(shardActor);
43 this.shardName = Preconditions.checkNotNull(shardName);
46 private String actorNameFor(final TransactionIdentifier txId) {
47 final LocalHistoryIdentifier historyId = txId.getHistoryId();
48 final ClientIdentifier clientId = historyId.getClientId();
49 final FrontendIdentifier frontendId = clientId.getFrontendId();
51 final StringBuilder sb = new StringBuilder("shard-");
52 sb.append(shardName).append('-')
53 .append(frontendId.getMemberName().getName()).append(':')
54 .append(frontendId.getClientType().getName()).append('@')
55 .append(clientId.getGeneration()).append(':');
56 if (historyId.getHistoryId() != 0) {
57 sb.append(historyId.getHistoryId()).append('-');
60 return sb.append(txId.getTransactionId()).toString();
63 ActorRef newShardTransaction(TransactionType type, TransactionIdentifier transactionID) {
64 final AbstractShardDataTreeTransaction<?> transaction;
67 transaction = dataTree.newReadOnlyTransaction(transactionID);
68 shardMBean.incrementReadOnlyTransactionCount();
71 transaction = dataTree.newReadWriteTransaction(transactionID);
72 shardMBean.incrementReadWriteTransactionCount();
75 transaction = dataTree.newReadWriteTransaction(transactionID);
76 shardMBean.incrementWriteOnlyTransactionCount();
79 throw new IllegalArgumentException("Unsupported transaction type " + type);
82 return actorContext.actorOf(ShardTransaction.props(type, transaction, shardActor, datastoreContext, shardMBean)
83 .withDispatcher(txnDispatcherPath), actorNameFor(transactionID));