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 java.util.concurrent.atomic.AtomicLong;
14 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
15 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
16 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
17 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
18 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
21 * A factory for creating ShardTransaction actors.
23 * @author Thomas Pantelis
25 class ShardTransactionActorFactory {
26 private static final AtomicLong ACTOR_NAME_COUNTER = new AtomicLong();
28 private final ShardDataTree dataTree;
29 private final DatastoreContext datastoreContext;
30 private final String txnDispatcherPath;
31 private final ShardStats shardMBean;
32 private final UntypedActorContext actorContext;
33 private final ActorRef shardActor;
34 private final String shardName;
36 ShardTransactionActorFactory(ShardDataTree dataTree, DatastoreContext datastoreContext,
37 String txnDispatcherPath, ActorRef shardActor, UntypedActorContext actorContext, ShardStats shardMBean,
39 this.dataTree = Preconditions.checkNotNull(dataTree);
40 this.datastoreContext = Preconditions.checkNotNull(datastoreContext);
41 this.txnDispatcherPath = Preconditions.checkNotNull(txnDispatcherPath);
42 this.shardMBean = Preconditions.checkNotNull(shardMBean);
43 this.actorContext = Preconditions.checkNotNull(actorContext);
44 this.shardActor = Preconditions.checkNotNull(shardActor);
45 this.shardName = Preconditions.checkNotNull(shardName);
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();
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('-');
62 return sb.append(txId.getTransactionId()).append('_').append(ACTOR_NAME_COUNTER.incrementAndGet()).toString();
65 ActorRef newShardTransaction(TransactionType type, TransactionIdentifier transactionID) {
66 final AbstractShardDataTreeTransaction<?> transaction;
69 transaction = dataTree.newReadOnlyTransaction(transactionID);
70 shardMBean.incrementReadOnlyTransactionCount();
73 transaction = dataTree.newReadWriteTransaction(transactionID);
74 shardMBean.incrementReadWriteTransactionCount();
77 transaction = dataTree.newReadWriteTransaction(transactionID);
78 shardMBean.incrementWriteOnlyTransactionCount();
81 throw new IllegalArgumentException("Unsupported transaction type " + type);
84 return actorContext.actorOf(ShardTransaction.props(type, transaction, shardActor, datastoreContext, shardMBean)
85 .withDispatcher(txnDispatcherPath), actorNameFor(transactionID));