+ }
+
+ 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);
+ }
+ }
+
+ final ListenableFuture<Void> future = cohort.commit();
+ shardMBean.incrementCommittedTransactionCount();
+ final ActorRef self = getSelf();
+ future.addListener(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ future.get();
+
+ if(sender != null) {
+ sender
+ .tell(new CommitTransactionReply().toSerializable(),
+ self);
+ } else {
+ LOG.error("sender is null ???");
+ }
+ } catch (InterruptedException | ExecutionException e) {
+ // FIXME : Handle this properly
+ LOG.error(e, "An exception happened when committing");
+ }
+ }
+ }, getContext().dispatcher());
+ }
+
+ private void handleForwardedCommit(ForwardedCommitTransaction message) {
+ Object serializedModification = message.getModification().toSerializable();
+
+ modificationToCohort
+ .put(serializedModification , message.getCohort());
+
+ if(persistent) {
+ this.persistData(getSender(), "identifier", new CompositeModificationPayload(serializedModification));
+ } else {
+ this.commit(getSender(), serializedModification);
+ }
+ }
+
+ private void updateSchemaContext(UpdateSchemaContext message) {
+ this.schemaContext = message.getSchemaContext();
+ store.onGlobalContextUpdated(message.getSchemaContext());
+ }
+
+ private void registerChangeListener(
+ RegisterChangeListener registerChangeListener) {
+
+ LOG.debug("registerDataChangeListener for " + registerChangeListener.getPath());
+
+
+ ActorSelection dataChangeListenerPath = getContext()
+ .system().actorSelection(
+ registerChangeListener.getDataChangeListenerPath());
+
+
+ // Notify the listener if notifications should be enabled or not
+ // If this shard is the leader then it will enable notifications else
+ // it will not
+ dataChangeListenerPath.tell(new EnableNotification(isLeader()), getSelf());
+
+ // Now store a reference to the data change listener so it can be notified
+ // at a later point if notifications should be enabled or disabled
+ dataChangeListeners.add(dataChangeListenerPath);
+
+ AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>
+ listener = new DataChangeListenerProxy(schemaContext,dataChangeListenerPath);
+
+ org.opendaylight.yangtools.concepts.ListenerRegistration<AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>>
+ registration =
+ store.registerChangeListener(registerChangeListener.getPath(),
+ listener, registerChangeListener.getScope());
+ ActorRef listenerRegistration =
+ getContext().actorOf(
+ DataChangeListenerRegistration.props(registration));
+
+ LOG.debug("registerDataChangeListener sending reply, listenerRegistrationPath = " + listenerRegistration.path().toString());
+
+ getSender()
+ .tell(new RegisterChangeListenerReply(listenerRegistration.path()),
+ getSelf());
+ }
+
+ private void createTransactionChain() {
+ DOMStoreTransactionChain chain = store.createTransactionChain();
+ ActorRef transactionChain =
+ getContext().actorOf(
+ ShardTransactionChain.props(chain, schemaContext));
+ getSender()
+ .tell(new CreateTransactionChainReply(transactionChain.path())
+ .toSerializable(),
+ getSelf());
+ }
+
+ @Override protected void applyState(ActorRef clientActor, String identifier,
+ Object data) {
+
+ if(data instanceof CompositeModificationPayload){
+ Object modification =
+ ((CompositeModificationPayload) data).getModification();
+
+ if(modification != null){
+ commit(clientActor, modification);
+ } else {
+ LOG.error("modification is null - this is very unexpected");
+ }
+
+
+ } else {
+ LOG.error("Unknown state received {}", data);
+ }
+
+ }
+
+ @Override protected Object createSnapshot() {
+ throw new UnsupportedOperationException("createSnapshot");
+ }
+
+ @Override protected void applySnapshot(Object snapshot) {
+ throw new UnsupportedOperationException("applySnapshot");
+ }
+
+ @Override protected void onStateChanged() {
+ for(ActorSelection dataChangeListener : dataChangeListeners){
+ dataChangeListener.tell(new EnableNotification(isLeader()), getSelf());
+ }
+
+ if(getLeaderId() != null){
+ shardMBean.setLeader(getLeaderId());
+ }
+
+ shardMBean.setRaftState(getRaftState().name());
+ }
+
+ @Override public String persistenceId() {
+ return this.name;
+ }
+
+
+ private static class ShardConfigParams extends DefaultConfigParamsImpl {
+ public static final FiniteDuration HEART_BEAT_INTERVAL =
+ new FiniteDuration(500, TimeUnit.MILLISECONDS);