Repository navigation
Description
Background
Why do we need to lock? As of now, there's no way to guarantee mutual exclusion of resources like
- Bento Bundles
- Deployment Operators
As certain operations (like deployment) take a long time to execute, there's no way to guarantee mutual exclusion on the resources that these operations touch. For example, if one user is trying to deploy a Bento and another is trying to delete it through the same YataiService, what happens when the deletion finishes before the deployment finishes?
As BentoML scales to larger companies, often just a single YataiService isn't enough to handle all of the deployment needs. However, in order to support multiple concurrent YataiServices, we need to be able to deploy multiple things concurrently and for that we need a locking module to make access concurrently safe.
User-facing API Proposal
Developers just need to wrap code in a with block. Lock will automatically release after everything inside it is done. If the user wishes to retry the operation with some back-off strategy, they can do that too.
from bentoml.locking import (Lock, LockType)
from bentoml.exceptions import FailedLockAcquisition
import backoff
class YataiServiceImpl(base):
def ApplyDeployment(self, request, context=None):
with Lock(request.name, request.namespace, type=LockType.READ, timeout_seconds=3): # aquire lock here, will throw BentoMLException if failed
try:
# apply default namespace if not set
request.deployment.namespace = (
request.deployment.namespace or self.default_namespace
)
def DeleteDeployment(self, request, context=None):
with Lock(request.name, request.namespace, type=LockType.WRITE): # acquire write lock here
try:
request.namespace = request.namespace or self.default_namespace
deployment_pb = self.deployment_store.get(
request.deployment_name, request.namespace
)Implementation
Abstract Store
The same underlying operations of bentoml/yatai/repository/metadata_store.py, bentoml/yatai/deployment/store.py, and bentoml/yatai/label_store.py are all the same. Could pull all common operations to bentoml/yatai/db.py (which all 3 are subclasses of). That way, creating a new store to keep track of lock state will be easy to implement.
Lock State Table
class RESOURCE_TYPE(enum.Enum):
deployment = 1
bento = 2
class LOCK_TYPE(enum.Enum):
mutex = 1
rw_mutex = 2
class LOCK_STATUS(enum.Enum):
unlocked = 1
read_lock = 2
write_lock = 3
class Lock(Base):
__tablename__ = 'locks'
__table_args__ = tuple(
UniqueConstraint(
'resource_type',
'resource_id',
name='_resource_type_resource_id_uc',
)
)
id = Column(Integer, primary_key=True)
resource_type = Column(Enum(RESOURCE_TYPE))
resource_id = Column(Integer, nullable=False)
lock_type = Column(Enum(LOCK_TYPE))
lock_status = Column(Enum(LOCK_STATUS))User-facing API
Implement with a context manager.
class LockType:
READ = 2
WRITE = 3
class Lock():
def __init__(self, lock_identifier, type=LockType.READ, timeout_seconds=None, retry_interval_seconds=None, max_retry_count=5):
self.type = type
# create proper database session
# maybe use the other context manager create_session(session_maker) here?
def __enter__(self):
# attempt to acquire lock based off of retry strategy
# when timeout is None and failed to acquire lock, raise an exception immediately
# when timeout_seconds is an integer number:
# retry acquiring the lock every `retry_interval_seconds` seconds
# until `timeout_seconds` seconds or `max_retry_count` has reached, raise an exception
if (self.type == LockType.READ):
# wait for col lock_status to be either unlocked or read_lock
else:
# wait for col lock_status to be unlocked
# set lock_status to be self.type on row resource_id = name_namespace
def __exit__(self, type, value, traceback):
# release lock, set lock_status to 1https://book.pythontips.com/en/latest/context_managers.html
Plan of Action
- YataiService Store Abstraction → abstract existing stores to reduce duplicate code
- label store
- metadata store
- deployment store
- Create new SQL table for locks
- Write schema
- Implement general locking module
Revisions
- Should also have a TTL (time-to-live)
- Should check lock holder identity before releasing
Activity
[-]Operation Level Locking[/-][+]Bundle/Deployment Locking Module[/+]parano commentedon Mar 29, 2021
Suggestion from our discussion earlier:
Let's put the retry logic inside the
Lockmodule, e.g.: