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;
19 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
22 * A factory for creating ShardTransaction actors.
24 * @author Thomas Pantelis
26 class ShardTransactionActorFactory {
27 private static final AtomicLong ACTOR_NAME_COUNTER = new AtomicLong();
29 private final ShardDataTree dataTree;
30 private final DatastoreContext datastoreContext;
31 private final String txnDispatcherPath;
32 private final ShardStats shardMBean;
33 private final ActorContext actorContext;
34 private final ActorRef shardActor;
35 private final String shardName;
37 ShardTransactionActorFactory(ShardDataTree dataTree, DatastoreContext datastoreContext,
38 String txnDispatcherPath, ActorRef shardActor, ActorContext actorContext, ShardStats shardMBean,
40 this.dataTree = requireNonNull(dataTree);
41 this.datastoreContext = requireNonNull(datastoreContext);
42 this.txnDispatcherPath = requireNonNull(txnDispatcherPath);
43 this.shardMBean = requireNonNull(shardMBean);
44 this.actorContext = requireNonNull(actorContext);
45 this.shardActor = requireNonNull(shardActor);
46 this.shardName = requireNonNull(shardName);
49 private String actorNameFor(final TransactionIdentifier txId) {
50 final LocalHistoryIdentifier historyId = txId.getHistoryId();
51 final ClientIdentifier clientId = historyId.getClientId();
52 final FrontendIdentifier frontendId = clientId.getFrontendId();
54 final StringBuilder sb = new StringBuilder("shard-");
55 sb.append(shardName).append('-')
56 .append(frontendId.getMemberName().getName()).append(':')
57 .append(frontendId.getClientType().getName()).append('@')
58 .append(clientId.getGeneration()).append(':');
59 if (historyId.getHistoryId() != 0) {
60 sb.append(historyId.getHistoryId()).append('-');
63 return sb.append(txId.getTransactionId()).append('_').append(ACTOR_NAME_COUNTER.incrementAndGet()).toString();
66 ActorRef newShardTransaction(TransactionType type, TransactionIdentifier transactionID) {
67 final AbstractShardDataTreeTransaction<?> transaction;
70 transaction = dataTree.newReadOnlyTransaction(transactionID);
74 transaction = dataTree.newReadWriteTransaction(transactionID);
77 throw new IllegalArgumentException("Unsupported transaction type " + type);
80 return actorContext.actorOf(ShardTransaction.props(type, transaction, shardActor, datastoreContext, shardMBean)
81 .withDispatcher(txnDispatcherPath), actorNameFor(transactionID));