userver: dist_lock::DistLockedTask Class Reference
Loading...
Searching...
No Matches
dist_lock::DistLockedTask Class Referencefinal

#include <userver/dist_lock/dist_locked_task.hpp>

Detailed Description

A task that tries to acquire a distributed lock and runs user callback once while the lock is held.

When dist lock starts, the lock worker tries to take a lock in the loop. If succeeded, a task is launched that executes the user code. In the background, dist lock tries to extend the lock. In case of loss of the lock, the user task is canceled.

Example with retrying

auto strategy = GetSomeDistLockStrategyForTheSample();
std::atomic<std::size_t> counter = 0;
"example",
[&]() {
counter++;
if (counter < 5) {
throw std::runtime_error("");
}
},
strategy);
UEXPECT_NO_THROW(locked_task.Get());
EXPECT_EQ(counter, 5);

Example without retrying

auto strategy = GetSomeDistLockStrategyForTheSample();
std::atomic<std::size_t> counter = 0;
"example",
[&]() {
counter++;
throw std::runtime_error("123");
},
strategy, /*default settings*/ {}, dist_lock::DistLockWaitingMode::kWait,
try {
locked_task.Get();
FAIL() << "Should have thrown";
} catch (const std::runtime_error& exception) {
EXPECT_EQ(exception.what(), std::string("123"));
}
EXPECT_EQ(counter, 1);
See also
Periodics and DistLocks
AlwaysBusyDistLockStrategy

Definition at line 48 of file dist_locked_task.hpp.

+ Inheritance diagram for dist_lock::DistLockedTask:

Public Types

using WorkerFunc = std::function<void()>
 
enum class  Importance {
  kNormal ,
  kCritical
}
 Task importance. More...
 
enum class  State {
  kInvalid ,
  kNew ,
  kQueued ,
  kRunning ,
  kSuspended ,
  kCancelled ,
  kCompleted
}
 Task state. More...
 
enum class  WaitMode {
  kSingleWaiter ,
  kMultipleWaiters
}
 Task wait mode. More...
 

Public Member Functions

 DistLockedTask ()=default
 
 DistLockedTask (DistLockedTask &&)=delete
 
DistLockedTaskoperator= (DistLockedTask &&)=delete
 
 DistLockedTask (const DistLockedTask &)=delete
 
DistLockedTaskoperator= (const DistLockedTask &&)=delete
 
 DistLockedTask (std::string name, WorkerFunc worker_func, std::shared_ptr< DistLockStrategyBase > strategy, const DistLockSettings &settings={}, DistLockWaitingMode mode=DistLockWaitingMode::kWait, DistLockRetryMode retry_mode=DistLockRetryMode::kRetry)
 
 DistLockedTask (engine::TaskProcessor &task_processor, std::string name, WorkerFunc worker_func, std::shared_ptr< DistLockStrategyBase > strategy, const DistLockSettings &settings={}, DistLockWaitingMode mode=DistLockWaitingMode::kWait, DistLockRetryMode retry_mode=DistLockRetryMode::kRetry)
 Creates a DistLockedTask to be run in a specific engine::TaskProcessor.
 
std::optional< std::chrono::steady_clock::duration > GetLockedDuration () const
 
void Get () noexcept(false)
 
bool IsValid () const
 Checks whether this object owns an actual task (not State::kInvalid)
 
State GetState () const
 Gets the task State.
 
bool IsFinished () const
 Returns whether the task finished execution.
 
void Wait () const noexcept(false)
 Suspends execution until the task finishes or caller is cancelled. Can be called from coroutine context only. For non-coroutine context use BlockingWait().
 
template<typename Rep , typename Period >
void WaitFor (const std::chrono::duration< Rep, Period > &) const noexcept(false)
 Suspends execution until the task finishes or after the specified timeout or until caller is cancelled.
 
template<typename Clock , typename Duration >
void WaitUntil (const std::chrono::time_point< Clock, Duration > &) const noexcept(false)
 Suspends execution until the task finishes or until the specified time point is reached or until caller is cancelled.
 
void WaitUntil (Deadline) const
 Suspends execution until the task finishes or until the specified deadline is reached or until caller is cancelled.
 
void RequestCancel ()
 Queues task cancellation request.
 
void SyncCancel () noexcept
 Cancels the task and suspends execution until it is finished. Can be called from coroutine context only. For non-coroutine context use RequestCancel() + BlockingWait().
 
TaskCancellationReason CancellationReason () const
 Gets task cancellation reason.
 
void BlockingWait () const
 

Static Public Member Functions

static const std::string & GetStateName (State state)
 

Member Typedef Documentation

◆ WorkerFunc

using dist_lock::DistLockedTask::WorkerFunc = std::function<void()>

Definition at line 50 of file dist_locked_task.hpp.

Member Enumeration Documentation

◆ Importance

enum class engine::TaskBase::Importance
stronginherited

Task importance.

Enumerator
kNormal 

Normal task.

kCritical 

Critical task. The task will be started regardless of cancellations, e.g. due to user request, deadline or TaskProcessor overload. After the task starts, it may be cancelled. In particular, if it received any cancellation requests before starting, then it will start as cancelled.

Definition at line 40 of file task_base.hpp.

◆ State

enum class engine::TaskBase::State
stronginherited

Task state.

Enumerator
kInvalid 

Unusable.

kNew 

just created, not registered with task processor

kQueued 

awaits execution

kRunning 

executing user code

kSuspended 

suspended, e.g. waiting for blocking call to complete

kCancelled 

exited user code because of external request

kCompleted 

exited user code with return or throw

Definition at line 52 of file task_base.hpp.

◆ WaitMode

enum class engine::TaskBase::WaitMode
stronginherited

Task wait mode.

Enumerator
kSingleWaiter 

Can be awaited by at most one task at a time.

kMultipleWaiters 

Can be awaited by multiple tasks simultaneously.

Definition at line 63 of file task_base.hpp.

Constructor & Destructor Documentation

◆ DistLockedTask() [1/2]

dist_lock::DistLockedTask::DistLockedTask ( )
default

Default constructor. Creates an invalid task.

◆ DistLockedTask() [2/2]

dist_lock::DistLockedTask::DistLockedTask ( std::string name,
WorkerFunc worker_func,
std::shared_ptr< DistLockStrategyBase > strategy,
const DistLockSettings & settings = {},
DistLockWaitingMode mode = DistLockWaitingMode::kWait,
DistLockRetryMode retry_mode = DistLockRetryMode::kRetry )

Creates a DistLockedTask.

Parameters
namename of the task
worker_funca callback that is started once we've acquired the lock and is cancelled when the lock is lost.
settingsdistributed lock settings
strategydistributed locking strategy
modedistributed lock waiting mode
Note
worker_func must honour task cancellation and stop ASAP when it is cancelled, otherwise brain split is possible (IOW, two different users do work assuming both of them hold the lock, which is not true).

Member Function Documentation

◆ BlockingWait()

void engine::TaskBase::BlockingWait ( ) const
inherited

Waits for the task in non-coroutine context (e.g. non-TaskProcessor's std::thread).

◆ GetLockedDuration()

std::optional< std::chrono::steady_clock::duration > dist_lock::DistLockedTask::GetLockedDuration ( ) const

Returns for how long the lock is held (if held at all). Returned value may be less than the real duration.

◆ IsValid()

bool engine::TaskBase::IsValid ( ) const
inherited

Checks whether this object owns an actual task (not State::kInvalid)

An invalid task cannot be used. The task becomes invalid after each of the following calls:

  1. the default constructor
  2. Detach()
  3. Get() (see engine::TaskWithResult)

◆ Wait()

void engine::TaskBase::Wait ( ) const
inherited

Suspends execution until the task finishes or caller is cancelled. Can be called from coroutine context only. For non-coroutine context use BlockingWait().

Exceptions
WaitInterruptedExceptionwhen current_task::IsCancelRequested() and no TaskCancellationBlockers are present.

◆ WaitFor()

template<typename Rep , typename Period >
void engine::TaskBase::WaitFor ( const std::chrono::duration< Rep, Period > & duration) const
inherited

Suspends execution until the task finishes or after the specified timeout or until caller is cancelled.

Exceptions
WaitInterruptedExceptionwhen current_task::IsCancelRequested() and no TaskCancellationBlockers are present.

Definition at line 191 of file task_base.hpp.

◆ WaitUntil() [1/2]

template<typename Clock , typename Duration >
void engine::TaskBase::WaitUntil ( const std::chrono::time_point< Clock, Duration > & until) const
inherited

Suspends execution until the task finishes or until the specified time point is reached or until caller is cancelled.

Exceptions
WaitInterruptedExceptionwhen current_task::IsCancelRequested() and no TaskCancellationBlockers are present.

Definition at line 197 of file task_base.hpp.

◆ WaitUntil() [2/2]

void engine::TaskBase::WaitUntil ( Deadline ) const
inherited

Suspends execution until the task finishes or until the specified deadline is reached or until caller is cancelled.

Exceptions
WaitInterruptedExceptionwhen current_task::IsCancelRequested() and no TaskCancellationBlockers are present.

The documentation for this class was generated from the following file: