package com.atlassian.bitbucket.mesh.transaction;

import com.atlassian.bitbucket.mesh.client.ReplicateParameters;
import com.atlassian.bitbucket.mesh.network.MeshNode;

import javax.annotation.Nonnull;

/**
 * Callback used by a {@link ReplicaTransaction} to notify the transaction leader when the replica's token
 * is granted to the leader, or when a request to yield the token was received from the replica.
 *
 * @see ReplicateParameters
 */
public interface ReplicaTransactionCallback {

    /**
     * Called when the replica transaction encountered a unrecoverable error.
     *
     * @param node the replica that encountered the error
     */
    void onReplicaError(@Nonnull MeshNode node);

    /**
     * @param node           the node that granted its write token
     * @param replicaVersion the version that the replica was at when the token was granted
     * @return {@code true} if the transaction coordinator accepts the token and now holds it
     */
    boolean onTokenGranted(@Nonnull MeshNode node, @Nonnull ReplicaVersion replicaVersion);

    /**
     * @param node the node that wishes to revoke its token from the lock
     * @return {@code true} if the token was yielded by the lock
     */
    boolean yieldToken(@Nonnull MeshNode node);
}
