<commit_before>/*
 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.controller.cluster.datastore;

import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import com.google.common.base.Preconditions;
import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;

/**
 * A factory for creating ShardTransaction actors.
 *
 * @author <NAME>
 */
class ShardTransactionActorFactory {

    private final ShardDataTree dataTree;
    private final DatastoreContext datastoreContext;
    private final String txnDispatcherPath;
    private final ShardStats shardMBean;
    private final UntypedActorContext actorContext;
    private final ActorRef shardActor;
    private final String shardName;

    ShardTransactionActorFactory(ShardDataTree dataTree, DatastoreContext datastoreContext,
            String txnDispatcherPath, ActorRef shardActor, UntypedActorContext actorContext, ShardStats shardMBean,
            String shardName) {
        this.dataTree = Preconditions.checkNotNull(dataTree);
        this.datastoreContext = Preconditions.checkNotNull(datastoreContext);
        this.txnDispatcherPath = Preconditions.checkNotNull(txnDispatcherPath);
        this.shardMBean = Preconditions.checkNotNull(shardMBean);
        this.actorContext = Preconditions.checkNotNull(actorContext);
        this.shardActor = Preconditions.checkNotNull(shardActor);
        this.shardName = Preconditions.checkNotNull(shardName);
    }

    private String actorNameFor(final TransactionIdentifier txId) {
        final LocalHistoryIdentifier historyId = txId.getHistoryId();
        final ClientIdentifier clientId = historyId.getClientId();
        final FrontendIdentifier frontendId = clientId.getFrontendId();

        final StringBuilder sb = new StringBuilder("shard-");
        sb.append(shardName).append('-')
            .append(frontendId.getMemberName().getName()).append(':')
            .append(frontendId.getClientType().getName()).append('@')
            .append(clientId.getGeneration()).append(':');
        if (historyId.getHistoryId() != 0) {
            sb.append(historyId.getHistoryId()).append('-');
        }

        return sb.append(txId.getTransactionId()).toString();
    }

    ActorRef newShardTransaction(TransactionType type, TransactionIdentifier transactionID) {
        final AbstractShardDataTreeTransaction<?> transaction;
        switch (type) {
            case READ_ONLY:
                transaction = dataTree.newReadOnlyTransaction(transactionID);
                shardMBean.incrementReadOnlyTransactionCount();
                break;
            case READ_WRITE:
                transaction = dataTree.newReadWriteTransaction(transactionID);
                shardMBean.incrementReadWriteTransactionCount();
                break;
            case WRITE_ONLY:
                transaction = dataTree.newReadWriteTransaction(transactionID);
                shardMBean.incrementWriteOnlyTransactionCount();
                break;
            default:
                throw new IllegalArgumentException("Unsupported transaction type " + type);
        }

        return actorContext.actorOf(ShardTransaction.props(type, transaction, shardActor, datastoreContext, shardMBean)
            .withDispatcher(txnDispatcherPath), actorNameFor(transactionID));
    }
}
<commit_msg>Bug 7814: Add counter to make tx actor names unique<commit_after>/*
 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.controller.cluster.datastore;

import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import com.google.common.base.Preconditions;
import java.util.concurrent.atomic.AtomicLong;
import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;

/**
 * A factory for creating ShardTransaction actors.
 *
 * @author <NAME>
 */
class ShardTransactionActorFactory {
    private static final AtomicLong ACTOR_NAME_COUNTER = new AtomicLong();

    private final ShardDataTree dataTree;
    private final DatastoreContext datastoreContext;
    private final String txnDispatcherPath;
    private final ShardStats shardMBean;
    private final UntypedActorContext actorContext;
    private final ActorRef shardActor;
    private final String shardName;

    ShardTransactionActorFactory(ShardDataTree dataTree, DatastoreContext datastoreContext,
            String txnDispatcherPath, ActorRef shardActor, UntypedActorContext actorContext, ShardStats shardMBean,
            String shardName) {
        this.dataTree = Preconditions.checkNotNull(dataTree);
        this.datastoreContext = Preconditions.checkNotNull(datastoreContext);
        this.txnDispatcherPath = Preconditions.checkNotNull(txnDispatcherPath);
        this.shardMBean = Preconditions.checkNotNull(shardMBean);
        this.actorContext = Preconditions.checkNotNull(actorContext);
        this.shardActor = Preconditions.checkNotNull(shardActor);
        this.shardName = Preconditions.checkNotNull(shardName);
    }

    private String actorNameFor(final TransactionIdentifier txId) {
        final LocalHistoryIdentifier historyId = txId.getHistoryId();
        final ClientIdentifier clientId = historyId.getClientId();
        final FrontendIdentifier frontendId = clientId.getFrontendId();

        final StringBuilder sb = new StringBuilder("shard-");
        sb.append(shardName).append('-')
            .append(frontendId.getMemberName().getName()).append(':')
            .append(frontendId.getClientType().getName()).append('@')
            .append(clientId.getGeneration()).append(':');
        if (historyId.getHistoryId() != 0) {
            sb.append(historyId.getHistoryId()).append('-');
        }

        return sb.append(txId.getTransactionId()).append('_').append(ACTOR_NAME_COUNTER.incrementAndGet()).toString();
    }

    ActorRef newShardTransaction(TransactionType type, TransactionIdentifier transactionID) {
        final AbstractShardDataTreeTransaction<?> transaction;
        switch (type) {
            case READ_ONLY:
                transaction = dataTree.newReadOnlyTransaction(transactionID);
                shardMBean.incrementReadOnlyTransactionCount();
                break;
            case READ_WRITE:
                transaction = dataTree.newReadWriteTransaction(transactionID);
                shardMBean.incrementReadWriteTransactionCount();
                break;
            case WRITE_ONLY:
                transaction = dataTree.newReadWriteTransaction(transactionID);
                shardMBean.incrementWriteOnlyTransactionCount();
                break;
            default:
                throw new IllegalArgumentException("Unsupported transaction type " + type);
        }

        return actorContext.actorOf(ShardTransaction.props(type, transaction, shardActor, datastoreContext, shardMBean)
            .withDispatcher(txnDispatcherPath), actorNameFor(transactionID));
    }
}

{'fraction_non_alphanumeric': 0.08523498211422227, 'fraction_numerical': 0.0027137042062415195, 'mean_word_length': 3.6042021578648495, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6726931', 'n_tokens_mistral': 2146, 'n_tokens_neox': 2065, 'n_words': 416}