-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from lowjiajin/jj/proof-of-concept
Snowfall GUID generator
- Loading branch information
Showing
12 changed files
with
639 additions
and
37 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
include README.md | ||
include requirements.txt |
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,3 @@ | ||
APScheduler==3.6.3 | ||
SQLAlchemy==1.3.18 | ||
numpy==1.19.0 |
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,31 @@ | ||
import pathlib | ||
from setuptools import setup, find_packages | ||
|
||
|
||
HERE = pathlib.Path(__file__).parent | ||
with open('requirements.txt') as f: | ||
REQUIREMENTS = f.read().strip().split('\n') | ||
|
||
setup( | ||
name="snowfall", | ||
version="1.0.0", | ||
description="Bigint-based distributed GUID generator", | ||
long_description=(HERE / "README.md").read_text(), | ||
long_description_content_type="text/markdown", | ||
url="https://github.com/lowjiajin/snowfall", | ||
author="Low Jia Jin", | ||
author_email="pixelrife@hotmail.com", | ||
license="MIT", | ||
classifiers=[ | ||
"License :: OSI Approved :: MIT License", | ||
"Programming Language :: Python :: 3.7", | ||
], | ||
packages=find_packages(), | ||
include_package_data=True, | ||
install_requires=REQUIREMENTS, | ||
entry_points={ | ||
"console_scripts": [ | ||
"create_db_schema_group=src.generator_syncers.database_syncers:create_schema_group", | ||
] | ||
}, | ||
) |
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 @@ | ||
from snowfall.main import Snowfall |
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,3 @@ | ||
from snowfall.generator_syncers.abstracts import BaseSyncer | ||
from snowfall.generator_syncers.database_syncer import DatabaseSyncer | ||
from snowfall.generator_syncers.simple_syncer import SimpleSyncer |
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,88 @@ | ||
from abc import ABC, abstractmethod | ||
from apscheduler.schedulers.background import BackgroundScheduler | ||
|
||
from snowfall.utils import get_current_timestamp_ms | ||
|
||
|
||
class BaseSyncer(ABC): | ||
|
||
PROBE_MISSES_TO_RELEASE = 2 | ||
MAX_GENERATOR_ID = 2 ** 12 - 1 | ||
|
||
def __init__(self): | ||
""" | ||
All syncers have a background task which updates the liveliness of its Snowfall instance in the manifest | ||
at periodic intervals | ||
""" | ||
self.scheduler = BackgroundScheduler() | ||
self.scheduler.add_job( | ||
func=self.update_liveliness_job, | ||
trigger="interval", | ||
seconds=self.liveliness_probe_s, | ||
) | ||
self.scheduler.start() | ||
|
||
self._last_alive_ms = 0 | ||
self._generator_id = self._claim_generator_id() | ||
|
||
def is_alive( | ||
self, | ||
current_timestamp_ms: int | ||
): | ||
""" | ||
The syncer, and by extension its Snowfall instance, is alive iff its generator id is still reserved. | ||
""" | ||
ms_since_last_updated = current_timestamp_ms - self._last_alive_ms | ||
if ms_since_last_updated <= self.ms_to_release_generator_id: | ||
return True | ||
else: | ||
return False | ||
|
||
def update_liveliness_job(self): | ||
self._set_liveliness( | ||
current_timestamp_ms=get_current_timestamp_ms(), | ||
generator_id=self._generator_id | ||
) | ||
|
||
@property | ||
@abstractmethod | ||
def liveliness_probe_s(self) -> int: | ||
raise NotImplementedError | ||
|
||
@property | ||
@abstractmethod | ||
def ms_to_release_generator_id(self) -> int: | ||
raise NotImplementedError | ||
|
||
@property | ||
@abstractmethod | ||
def generator_id(self) -> int: | ||
raise NotImplementedError | ||
|
||
@property | ||
@abstractmethod | ||
def last_alive_ms(self) -> int: | ||
raise NotImplementedError | ||
|
||
@property | ||
@abstractmethod | ||
def epoch_start_ms(self) -> int: | ||
raise NotImplementedError | ||
|
||
@classmethod | ||
@abstractmethod | ||
def create_schema_group(cls) -> None: | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def _claim_generator_id(self) -> int: | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def _set_liveliness( | ||
self, | ||
current_timestamp_ms: int, | ||
generator_id: int | ||
|
||
) -> None: | ||
raise NotImplementedError |
Oops, something went wrong.