Skip to content

Commit

Permalink
Added logging
Browse files Browse the repository at this point in the history
  • Loading branch information
lowjiajin committed Jul 14, 2020
1 parent 2175d45 commit a92eba0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ A Snowfall GUID consists of:

As such, Snowfall returns unique GUIDs for as long as:
1. The generator id is within `[0, 4096)`.
2. No more than `2048` GUIDs are generated within one ms.
2. No more than `2048` GUIDs are generated within one ms per generator id.
3. The lifetime of the system is no more than `2^41ms` (~70 years) from the epoch time set.

## User Guide
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name="snowfall",
version="1.0.2",
version="1.0.3",
description="Bigint-based distributed GUID generator",
long_description=(HERE / "README.md").read_text(),
long_description_content_type="text/markdown",
Expand Down
10 changes: 10 additions & 0 deletions snowfall/generator_syncers/database_syncer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from sqlalchemy.orm.scoping import ScopedSession
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.engine.base import Engine
import logging

from snowfall.generator_syncers.abstracts import BaseSyncer
from snowfall.utils import get_current_timestamp_ms
Expand Down Expand Up @@ -143,6 +144,7 @@ def _generate_orm_classes(
"""
Defines the base classes that map to tables in our DBMS, and their columns.
"""
logging.info("Generating ORM classes")
manifest_table_name = f"snowfall_{schema_group_name}_manifest"
properties_table_name = f"snowfall_{schema_group_name}_properties"

Expand All @@ -168,6 +170,7 @@ def _create_sql_tables(
"""
One-time operation to create the manifest and properties tables if they don't already exist.
"""
logging.info("Creating manifest and properties tables")
if engine.dialect.has_table(engine, manifest_table_name):
raise RuntimeError(f"Manifest for schema group: {manifest_table_name} already exists in database.")
elif engine.dialect.has_table(engine, properties_table_name):
Expand Down Expand Up @@ -201,7 +204,9 @@ def _insert_initial_rows(
properties_class(key="min_ms_between_claim_retries", value=min_ms_between_claim_retries),
properties_class(key="max_ms_between_claim_retries", value=max_ms_between_claim_retries)
]
logging.info("Populating manifest table")
session.bulk_save_objects(manifest_rows)
logging.info("Populating properties table")
session.bulk_save_objects(properties)
session.commit()
session_factory.remove()
Expand All @@ -212,6 +217,7 @@ def _claim_generator_id(self) -> int:
checks, and then claims the first such generator id as reserved.
"""
def try_to_claim():
logging.info("Attempting to claim generator id")
current_timestamp_ms = get_current_timestamp_ms()
release_threshold_ms = current_timestamp_ms - self._ms_to_release_generator_id
released = session.query(self.manifest_row_class) \
Expand All @@ -223,6 +229,7 @@ def try_to_claim():
released_id = released.generator_id
released.last_updated_ms = current_timestamp_ms
session.commit()
logging.info(f"Claimed generator id {released_id}")
self._last_alive_ms = current_timestamp_ms
return released_id
else:
Expand Down Expand Up @@ -259,6 +266,7 @@ def _set_liveliness(
"""
Writes the latest timestamp at which the Snowfall instance is alive to the manifest.
"""
logging.debug("Attempting to update liveliness in manifest")
session = self.session_factory()
num_rows_updated = session.query(self.manifest_row_class) \
.filter_by(generator_id=generator_id) \
Expand All @@ -267,10 +275,12 @@ def _set_liveliness(
if num_rows_updated == 0:
raise RuntimeError("Generator id claimed by another Snowfall instance.")
session.commit()
logging.debug(f"Liveliness updated to timestamp: {current_timestamp_ms}")
self.session_factory.remove()
self._last_alive_ms = current_timestamp_ms

def get_properties(self) -> PropertiesTuple:
logging.info("Getting properties from database")
session = self.session_factory()
liveliness_probe_s = session.query(self.properties_class) \
.filter_by(key="liveliness_probe_s") \
Expand Down

0 comments on commit a92eba0

Please sign in to comment.