import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
import org.opendaylight.controller.cluster.datastore.messages.ShardLeaderStateChanged;
import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
-import org.opendaylight.controller.cluster.datastore.persisted.DataTreeCandidateSupplier;
import org.opendaylight.controller.cluster.datastore.utils.Dispatchers;
import org.opendaylight.controller.cluster.notifications.LeaderStateChanged;
import org.opendaylight.controller.cluster.notifications.RegisterRoleChangeListener;
import scala.concurrent.duration.FiniteDuration;
/**
- * A Shard represents a portion of the logical data tree <br/>
- * <p>
+ * A Shard represents a portion of the logical data tree.
+ * <p/>
* Our Shard uses InMemoryDataTree as it's internal representation and delegates all requests it
- * </p>
*/
public class Shard extends RaftActor {
private final ShardTransactionMessageRetrySupport messageRetrySupport;
+ private final FrontendMetadata frontendMetadata = new FrontendMetadata();
+
protected Shard(final AbstractBuilder<?, ?> builder) {
super(builder.getId().toString(), builder.getPeerAddresses(),
Optional.of(builder.getDatastoreContext().getShardRaftConfig()), DataStoreVersions.CURRENT_VERSION);
new ShardDataTreeChangeListenerPublisherActorProxy(getContext(), name + "-DTCL-publisher");
ShardDataChangeListenerPublisherActorProxy dataChangeListenerPublisher =
new ShardDataChangeListenerPublisherActorProxy(getContext(), name + "-DCL-publisher");
- if(builder.getDataTree() != null) {
+ if (builder.getDataTree() != null) {
store = new ShardDataTree(this, builder.getSchemaContext(), builder.getDataTree(),
treeChangeListenerPublisher, dataChangeListenerPublisher, name);
} else {
commitCoordinator.checkForExpiredTransactions(transactionCommitTimeout, this);
} else if (message instanceof DatastoreContext) {
onDatastoreContext((DatastoreContext)message);
- } else if (message instanceof RegisterRoleChangeListener){
+ } else if (message instanceof RegisterRoleChangeListener) {
roleChangeNotifier.get().forward(message, context());
} else if (message instanceof FollowerInitialSyncUpStatus) {
shardMBean.setFollowerInitialSyncStatus(((FollowerInitialSyncUpStatus) message).isInitialSyncDone());
context().parent().tell(message, self());
- } else if (GET_SHARD_MBEAN_MESSAGE.equals(message)){
+ } else if (GET_SHARD_MBEAN_MESSAGE.equals(message)) {
sender().tell(getShardMBean(), self());
} else if (message instanceof GetShardDataTree) {
sender().tell(store.getDataTree(), self());
- } else if (message instanceof ServerRemoved){
+ } else if (message instanceof ServerRemoved) {
context().parent().forward(message, context());
} else if (ShardTransactionMessageRetrySupport.TIMER_MESSAGE_CLASS.isInstance(message)) {
messageRetrySupport.onTimerMessage(message);
}
@Override
- protected LeaderStateChanged newLeaderStateChanged(final String memberId, final String leaderId, final short leaderPayloadVersion) {
+ protected LeaderStateChanged newLeaderStateChanged(final String memberId, final String leaderId,
+ final short leaderPayloadVersion) {
return isLeader() ? new ShardLeaderStateChanged(memberId, leaderId, store.getDataTree(), leaderPayloadVersion)
: new ShardLeaderStateChanged(memberId, leaderId, leaderPayloadVersion);
}
setTransactionCommitTimeout();
- if (datastoreContext.isPersistent() && !persistence().isRecoveryApplicable()) {
- setPersistence(true);
- } else if (!datastoreContext.isPersistent() && persistence().isRecoveryApplicable()) {
- setPersistence(false);
- }
+ setPersistence(datastoreContext.isPersistent());
updateConfigParams(datastoreContext.getShardRaftConfig());
}
LOG.debug("{}: Can committing transaction {}", persistenceId(), canCommit.getTransactionID());
if (isLeader()) {
- commitCoordinator.handleCanCommit(canCommit.getTransactionID(), getSender(), this);
+ commitCoordinator.handleCanCommit(canCommit.getTransactionID(), getSender(), this);
} else {
ActorSelection leader = getLeader();
if (leader == null) {
}
}
+ @SuppressWarnings("checkstyle:IllegalCatch")
protected void handleBatchedModificationsLocal(final BatchedModifications batched, final ActorRef sender) {
try {
commitCoordinator.handleBatchedModifications(batched, sender, this);
// we need to reconstruct previous BatchedModifications from the transaction
// DataTreeModification, honoring the max batched modification count, and forward all the
// previous BatchedModifications to the new leader.
- Collection<BatchedModifications> newModifications = commitCoordinator.createForwardedBatchedModifications(
- batched, datastoreContext.getShardBatchedModificationCount());
+ Collection<BatchedModifications> newModifications = commitCoordinator
+ .createForwardedBatchedModifications(batched,
+ datastoreContext.getShardBatchedModificationCount());
LOG.debug("{}: Forwarding {} BatchedModifications to leader {}", persistenceId(),
newModifications.size(), leader);
private boolean failIfIsolatedLeader(final ActorRef sender) {
if (isIsolatedLeader()) {
sender.tell(new akka.actor.Status.Failure(new NoShardLeaderException(String.format(
- "Shard %s was the leader but has lost contact with all of its followers. Either all" +
- " other follower nodes are down or this node is isolated by a network partition.",
+ "Shard %s was the leader but has lost contact with all of its followers. Either all"
+ + " other follower nodes are down or this node is isolated by a network partition.",
persistenceId()))), getSelf());
return true;
}
return getRaftState() == RaftState.IsolatedLeader;
}
+ @SuppressWarnings("checkstyle:IllegalCatch")
private void handleReadyLocalTransaction(final ReadyLocalTransaction message) {
LOG.debug("{}: handleReadyLocalTransaction for {}", persistenceId(), message.getTransactionID());
store.closeTransactionChain(closeTransactionChain.getIdentifier());
}
+ @SuppressWarnings("checkstyle:IllegalCatch")
private void createTransaction(final CreateTransaction createTransaction) {
try {
- if (TransactionType.fromInt(createTransaction.getTransactionType()) != TransactionType.READ_ONLY &&
- failIfIsolatedLeader(getSender())) {
+ if (TransactionType.fromInt(createTransaction.getTransactionType()) != TransactionType.READ_ONLY
+ && failIfIsolatedLeader(getSender())) {
return;
}
@Override
protected void applyState(final ActorRef clientActor, final Identifier identifier, final Object data) {
- if (data instanceof DataTreeCandidateSupplier) {
- if (clientActor == null) {
- // No clientActor indicates a replica coming from the leader
- try {
- store.applyStateFromLeader(identifier, (DataTreeCandidateSupplier)data);
- } catch (DataValidationFailedException | IOException e) {
- LOG.error("{}: Error applying replica {}", persistenceId(), identifier, e);
- }
- } else {
- // Replication consensus reached, proceed to commit
- store.payloadReplicationComplete(identifier, (DataTreeCandidateSupplier)data);
+ if (data instanceof Payload) {
+ try {
+ store.applyReplicatedPayload(identifier, (Payload)data);
+ } catch (DataValidationFailedException | IOException e) {
+ LOG.error("{}: Error applying replica {}", persistenceId(), identifier, e);
}
} else {
- LOG.error("{}: Unknown state received {} ClassLoader {}", persistenceId(), data,
- data.getClass().getClassLoader());
+ LOG.error("{}: Unknown state for {} received {}", persistenceId(), identifier, data);
}
}
// them to transaction messages and send to the new leader.
ActorSelection leader = getLeader();
if (leader != null) {
- Collection<?> messagesToForward = commitCoordinator.convertPendingTransactionsToMessages(
- datastoreContext.getShardBatchedModificationCount());
+ Collection<?> messagesToForward = convertPendingTransactionsToMessages();
if (!messagesToForward.isEmpty()) {
LOG.debug("{}: Forwarding {} pending transaction messages to leader {}", persistenceId(),
}
}
} else {
- commitCoordinator.abortPendingTransactions(
- "The transacton was aborted due to inflight leadership change and the leader address isn't available.",
- this);
+ commitCoordinator.abortPendingTransactions("The transacton was aborted due to inflight leadership "
+ + "change and the leader address isn't available.", this);
}
}
}
}
+ /**
+ * Clears all pending transactions and converts them to messages to be forwarded to a new leader.
+ *
+ * @return the converted messages
+ */
+ public Collection<?> convertPendingTransactionsToMessages() {
+ return commitCoordinator.convertPendingTransactionsToMessages(
+ datastoreContext.getShardBatchedModificationCount());
+ }
+
@Override
protected void pauseLeader(final Runnable operation) {
LOG.debug("{}: In pauseLeader, operation: {}", persistenceId(), operation);
return new Builder();
}
- public static abstract class AbstractBuilder<T extends AbstractBuilder<T, S>, S extends Shard> {
+ public abstract static class AbstractBuilder<T extends AbstractBuilder<T, S>, S extends Shard> {
private final Class<S> shardClass;
private ShardIdentifier id;
private Map<String, String> peerAddresses = Collections.emptyMap();
return (T) this;
}
- public T id(final ShardIdentifier id) {
+ public T id(final ShardIdentifier newId) {
checkSealed();
- this.id = id;
+ this.id = newId;
return self();
}
- public T peerAddresses(final Map<String, String> peerAddresses) {
+ public T peerAddresses(final Map<String, String> newPeerAddresses) {
checkSealed();
- this.peerAddresses = peerAddresses;
+ this.peerAddresses = newPeerAddresses;
return self();
}
- public T datastoreContext(final DatastoreContext datastoreContext) {
+ public T datastoreContext(final DatastoreContext newDatastoreContext) {
checkSealed();
- this.datastoreContext = datastoreContext;
+ this.datastoreContext = newDatastoreContext;
return self();
}
- public T schemaContext(final SchemaContext schemaContext) {
+ public T schemaContext(final SchemaContext newSchemaContext) {
checkSealed();
- this.schemaContext = schemaContext;
+ this.schemaContext = newSchemaContext;
return self();
}
- public T restoreFromSnapshot(final DatastoreSnapshot.ShardSnapshot restoreFromSnapshot) {
+ public T restoreFromSnapshot(final DatastoreSnapshot.ShardSnapshot newRestoreFromSnapshot) {
checkSealed();
- this.restoreFromSnapshot = restoreFromSnapshot;
+ this.restoreFromSnapshot = newRestoreFromSnapshot;
return self();
}
- public T dataTree(final TipProducingDataTree dataTree) {
+ public T dataTree(final TipProducingDataTree newDataTree) {
checkSealed();
- this.dataTree = dataTree;
+ this.dataTree = newDataTree;
return self();
}
public TreeType getTreeType() {
switch (datastoreContext.getLogicalStoreType()) {
- case CONFIGURATION:
- return TreeType.CONFIGURATION;
- case OPERATIONAL:
- return TreeType.OPERATIONAL;
+ case CONFIGURATION:
+ return TreeType.CONFIGURATION;
+ case OPERATIONAL:
+ return TreeType.OPERATIONAL;
+ default:
+ throw new IllegalStateException("Unhandled logical store type "
+ + datastoreContext.getLogicalStoreType());
}
-
- throw new IllegalStateException("Unhandled logical store type " + datastoreContext.getLogicalStoreType());
}
protected void verify() {