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 static java.util.Objects.requireNonNull;
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;
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 ActorContext actorContext;
33 private final ActorRef shardActor;
34 private final String shardName;
36 ShardTransactionActorFactory(ShardDataTree dataTree, DatastoreContext datastoreContext,
37 String txnDispatcherPath, ActorRef shardActor, ActorContext actorContext, ShardStats shardMBean,
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);
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);
73 transaction = dataTree.newReadWriteTransaction(transactionID);
76 throw new IllegalArgumentException("Unsupported transaction type " + type);
79 return actorContext.actorOf(ShardTransaction.props(type, transaction, shardActor, datastoreContext, shardMBean)
80 .withDispatcher(txnDispatcherPath), actorNameFor(transactionID));