package com.atlassian.bitbucket.mesh.repository;

import javax.annotation.Nonnull;
import java.util.concurrent.CompletionStage;

/**
 * Component that repository access from RPC calls, and supports draining on a node, partition or hierarchy
 * level. Note that draining does NOT block new requests from being serviced; it only provides a mechanism for
 * detecting when the active request count reaches 0.
 *
 * @since 1.3
 */
public interface RpcRepositoryAccessTracker extends RepositoryAccessListener {

    /**
     * Drains regular client requests (excluding requests from other nodes and the control plane) that access any
     * repository on the node.
     *
     * @return a {@link CompletionStage} that completes when there are no active requests that access any repository
     */
    @Nonnull
    CompletionStage<Void> drainAll();

    /**
     * Drains regular client requests (excluding other nodes and control plane) that access any repository in the
     * provided hierarchy.
     *
     * @param partitionId the partition ID
     * @param hierarchyId the hierarchy ID
     * @return a {@link CompletionStage} that completes when there are no active requests for the hierarchy
     */
    @Nonnull
    CompletionStage<Void> drainHierarchy(@Nonnull String partitionId, @Nonnull String hierarchyId);

    /**
     * Drains regular client requests (excluding other nodes and control plane) that access any repository in the
     * provided partition.
     *
     * @param partitionId the partition ID
     * @return a {@link CompletionStage} that completes when there are no active requests for the hierarchy
     */
    @Nonnull
    CompletionStage<Void> drainPartition(@Nonnull String partitionId);

    /**
     * The number of currently active requests from regular clients (excluding requests from other nodes and the
     * control plane) that have accessed any repository.
     *
     * @return the number of currently active requests that have accessed any repository
     */
    int getActiveRequestCount();

    /**
     * The number of currently active requests from regular clients (excluding requests from other nodes and the
     * control plane) that have accessed a repository from the provided hierarchy.
     *
     * @param partitionId the partition ID
     * @param hierarchyId the hierarchy ID
     * @return the number of currently active requests that have accessed a repository from the provided hierarchy
     */
    int getActiveRequestCountByHierarchy(@Nonnull String partitionId, @Nonnull String hierarchyId);

    /**
     * The number of currently active requests from regular clients (excluding requests from other nodes and the
     * control plane) that have accessed a repository from the provided partition.
     *
     * @param partitionId the partition ID
     * @return the number of currently active requests that have accessed a repository from the provided partition
     */
    int getActiveRequestCountByPartition(@Nonnull String partitionId);
}
