+public class Shard extends RaftActor {
+
+ private static final ConfigParams configParams = new ShardConfigParams();
+
+ public static final String DEFAULT_NAME = "default";
+
+ private final ListeningExecutorService storeExecutor =
+ MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
+
+ private final InMemoryDOMDataStore store;
+
+ private final Map<Object, DOMStoreThreePhaseCommitCohort>
+ modificationToCohort = new HashMap<>();
+
+ private final LoggingAdapter LOG =
+ Logging.getLogger(getContext().system(), this);
+
+ // By default persistent will be true and can be turned off using the system
+ // property persistent
+ private final boolean persistent;
+
+ private final String name;
+
+ private volatile SchemaContext schemaContext;
+
+ private final ShardStats shardMBean;
+
+ private final List<ActorSelection> dataChangeListeners = new ArrayList<>();
+
+ private Shard(String name, Map<String, String> peerAddresses) {
+ super(name, peerAddresses, Optional.of(configParams));
+
+ this.name = name;
+
+ String setting = System.getProperty("shard.persistent");
+
+ this.persistent = !"false".equals(setting);
+
+ LOG.info("Creating shard : {} persistent : {}", name, persistent);
+
+ store = new InMemoryDOMDataStore(name, storeExecutor);
+
+ shardMBean = ShardMBeanFactory.getShardStatsMBean(name);
+
+ }
+
+ public static Props props(final String name,
+ final Map<String, String> peerAddresses) {
+ return Props.create(new Creator<Shard>() {
+
+ @Override
+ public Shard create() throws Exception {
+ return new Shard(name, peerAddresses);
+ }
+
+ });
+ }
+
+
+ @Override public void onReceiveCommand(Object message) {
+ LOG.debug("Received message {} from {}", message.getClass().toString(),
+ getSender());
+
+ if (message.getClass()
+ .equals(CreateTransactionChain.SERIALIZABLE_CLASS)) {
+ if (isLeader()) {
+ createTransactionChain();
+ } else if (getLeader() != null) {
+ getLeader().forward(message, getContext());
+ }
+ } else if (message instanceof RegisterChangeListener) {
+ registerChangeListener((RegisterChangeListener) message);
+ } else if (message instanceof UpdateSchemaContext) {
+ updateSchemaContext((UpdateSchemaContext) message);
+ } else if (message instanceof ForwardedCommitTransaction) {
+ handleForwardedCommit((ForwardedCommitTransaction) message);
+ } else if (message.getClass()
+ .equals(CreateTransaction.SERIALIZABLE_CLASS)) {
+ if (isLeader()) {
+ createTransaction(CreateTransaction.fromSerializable(message));
+ } else if (getLeader() != null) {
+ getLeader().forward(message, getContext());
+ }
+ } else if (message instanceof PeerAddressResolved) {
+ PeerAddressResolved resolved = (PeerAddressResolved) message;
+ setPeerAddress(resolved.getPeerId(), resolved.getPeerAddress());
+ } else {
+ super.onReceiveCommand(message);
+ }
+ }
+
+ private ActorRef createTypedTransactionActor(
+ CreateTransaction createTransaction, String transactionId) {
+ if (createTransaction.getTransactionType()
+ == TransactionProxy.TransactionType.READ_ONLY.ordinal()) {
+ shardMBean.incrementReadOnlyTransactionCount();
+ return getContext().actorOf(
+ ShardTransaction
+ .props(store.newReadOnlyTransaction(), getSelf(),
+ schemaContext), transactionId);
+
+ } else if (createTransaction.getTransactionType()
+ == TransactionProxy.TransactionType.READ_WRITE.ordinal()) {
+ shardMBean.incrementReadWriteTransactionCount();
+ return getContext().actorOf(
+ ShardTransaction
+ .props(store.newReadWriteTransaction(), getSelf(),
+ schemaContext), transactionId);
+
+
+ } else if (createTransaction.getTransactionType()
+ == TransactionProxy.TransactionType.WRITE_ONLY.ordinal()) {
+ shardMBean.incrementWriteOnlyTransactionCount();
+ return getContext().actorOf(
+ ShardTransaction
+ .props(store.newWriteOnlyTransaction(), getSelf(),
+ schemaContext), transactionId);
+ } else {
+ throw new IllegalArgumentException(
+ "CreateTransaction message has unidentified transaction type="
+ + createTransaction.getTransactionType());
+ }
+ }
+
+ private void createTransaction(CreateTransaction createTransaction) {
+
+ String transactionId = "shard-" + createTransaction.getTransactionId();
+ LOG.info("Creating transaction : {} ", transactionId);
+ ActorRef transactionActor =
+ createTypedTransactionActor(createTransaction, transactionId);
+
+ getSender()
+ .tell(new CreateTransactionReply(
+ Serialization.serializedActorPath(transactionActor),
+ createTransaction.getTransactionId()).toSerializable(),
+ getSelf()
+ );
+ }
+
+ private void commit(final ActorRef sender, Object serialized) {
+ Modification modification =
+ MutableCompositeModification.fromSerializable(
+ serialized, schemaContext);
+ DOMStoreThreePhaseCommitCohort cohort =
+ modificationToCohort.remove(serialized);
+ if (cohort == null) {
+ LOG.error(
+ "Could not find cohort for modification : {}", modification);
+ LOG.info("Writing modification using a new transaction");
+ DOMStoreReadWriteTransaction transaction =
+ store.newReadWriteTransaction();
+ modification.apply(transaction);
+ DOMStoreThreePhaseCommitCohort commitCohort = transaction.ready();
+ ListenableFuture<Void> future =
+ commitCohort.preCommit();
+ try {
+ future.get();
+ future = commitCohort.commit();
+ future.get();
+ } catch (InterruptedException e) {
+ LOG.error("Failed to commit", e);
+ } catch (ExecutionException e) {
+ LOG.error("Failed to commit", e);
+ }
+ }