This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
gravel: add etcd #403
Merged
Merged
gravel: add etcd #403
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
1347206
images/microos: add etcd, python aetcd3
jecluis aa2a0b1
gravel: nodes/mgr: spin-off token generation
jecluis 547a116
gravel: nodes/mgr: spawn etcd on start / bootstrap
jecluis 85a0d34
gravel: add etcd support on multi-node deployments
jecluis 857989f
gravel: nodes/mgr: make ceph.conf managed by cephadm
jecluis a5c7ca6
gravel: add kvstore controller
jecluis 61fa349
gravel: nodes/mgr: move node init earlier in the file
jecluis 7fe2d14
gravel: nodes/mgr: make start async, add shutdown
jecluis fd73020
gravel: nodes/mgr: make as async as possible
jecluis c086a16
gravel: nodes/mgr: make etcd spawn async
jecluis 21eaea5
gravel: nodes/mgr: add kvstore support
jecluis 61ad42b
gravel: nodes/mgr: keep token in kvstore
jecluis 2fefa66
images: config.sh: install etcd from custom location
jecluis ac8d0af
gravel: nodes/mgr: obtain state on start, watch
jecluis a0ad5cd
gravel: ctrl/svc: make it a ticker
jecluis bb22107
gravel: ctrl/svc: gate operations if not ready
jecluis 61021bb
gravel: kv: allow returning None on 'get'
jecluis 5a973ad
gravel: nodes/mgr: expose kvstore to the world
jecluis c737488
gravel: ctrl/svc: keep state in kvstore
jecluis ba50e74
gravel: gstate: cleanup tickers on shutdown
jecluis 44d0224
gravel: ctrl/svc: cleanup watcher on shutdown
jecluis 9cc8749
aquarium: log shutdown event
jecluis File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# project aquarium's backend | ||
# Copyright (C) 2021 SUSE, LLC. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
|
||
import asyncio | ||
from typing import Callable, Optional | ||
import aetcd3 | ||
import aetcd3.locks | ||
import aetcd3.events | ||
import grpclib.exceptions | ||
|
||
from logging import Logger | ||
from fastapi.logger import logger as fastapi_logger | ||
|
||
logger: Logger = fastapi_logger | ||
|
||
|
||
class Lock: | ||
_lock: aetcd3.locks.Lock | ||
_is_acquired: bool | ||
|
||
def __init__(self, lock: aetcd3.locks.Lock): | ||
self._lock = lock | ||
self._is_acquired = False | ||
|
||
async def acquire(self) -> None: | ||
await self._lock.acquire() | ||
self._is_acquired = True | ||
|
||
async def release(self) -> None: | ||
if not self._is_acquired: | ||
return | ||
await self._lock.release() | ||
self._is_acquired = False | ||
|
||
|
||
class KV: | ||
|
||
_client: Optional[aetcd3.Etcd3Client] | ||
_is_closing: bool | ||
|
||
def __init__(self): | ||
self._client = None | ||
self._is_open = False | ||
self._is_closing = False | ||
|
||
async def ensure_connection(self) -> None: | ||
""" Open k/v store connection """ | ||
# try getting the status, loop until we make it. | ||
opened = False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if we never make it? The KV store is unavailable forever? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah. This might warrant a timeout or something. But yeah, that's it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's probably fine for now. It'll also presumably benefit once we have a cache later (at least we'll be able to read cached K/Vs while we're waiting for etcd) |
||
while not self._is_closing: | ||
try: | ||
async with aetcd3.client() as client: | ||
await client.status() | ||
except Exception: | ||
logger.warn("etcd not up yet? sleep.") | ||
await asyncio.sleep(1.0) | ||
continue | ||
opened = True | ||
break | ||
if opened: | ||
self._client = aetcd3.client() | ||
logger.info("opened kvstore connection") | ||
|
||
async def close(self) -> None: | ||
""" Close k/v store connection """ | ||
self._is_closing = True | ||
if not self._client: | ||
return | ||
await self._client.close() | ||
self._client = None | ||
|
||
async def put(self, key: str, value: str) -> None: | ||
""" Put key/value pair """ | ||
assert self._client | ||
await self._client.put(key, value) | ||
|
||
async def get(self, key: str) -> Optional[str]: | ||
""" Get value for provided key """ | ||
assert self._client | ||
value, _ = await self._client.get(key) | ||
if not value: | ||
return None | ||
return value.decode("utf-8") | ||
|
||
async def rm(self, key: str) -> None: | ||
""" Remove key from store """ | ||
assert self._client | ||
await self._client.delete(key) | ||
|
||
async def lock(self, key: str) -> Lock: | ||
""" Lock a given key. Requires compliant consumers. """ | ||
assert self._client | ||
return Lock(self._client.lock(key)) | ||
|
||
async def watch( | ||
self, | ||
key: str, | ||
callback: Callable[[str, str], None] | ||
) -> int: | ||
""" Watch updates on a given key """ | ||
assert self._client | ||
|
||
async def _cb(what: aetcd3.events.Event) -> None: | ||
if not what or \ | ||
type(what) == grpclib.exceptions.StreamTerminatedError: | ||
return | ||
callback(what.key.decode("utf-8"), what.value.decode("utf-8")) | ||
|
||
return await self._client.add_watch_callback(key, _cb) | ||
|
||
async def cancel_watch(self, watch_id: int) -> None: | ||
""" Cancel a watch """ | ||
assert self._client | ||
await self._client.cancel_watch(watch_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this referring to the fix for martyanov/aetcd#4, which was since closed? In that case, do we still need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll update the initial comment, but no. Even though that PR was closed, there's a couple of additional patches on top of the original repo, one of them to fix an aiofiles's dependency version -- original repo requires <0.6, while uvicorn requires >= 0.6, and it's annoying to have to deal with those dependencies when pip installs. Another one is a missing
await
when shutting down the lib IIRC.