-
Notifications
You must be signed in to change notification settings - Fork 392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#5200]Update the Column data type for python client #5354
Conversation
@mchades can you please help to review? |
@noidname01 , would you please also help to review this PR? Thanks a lot. |
When I double check the types folder (gravitino/tree/main/api/src/main/java/org/apache/gravitino/rel/types/) I find it has another file Decimal.java, so just update a new commit to convert decimal.java to decimal.py with unit test in python client folder. |
@classmethod | ||
def get(cls): | ||
if cls._instance is None: | ||
cls._instance = cls() | ||
return cls._instance |
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'm not sure if these codes can guarantee the singleton of _instance
under race conditions in Python, can you help confirm it? @xunliu @noidname01
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.
Thank you for catching that! You’re absolutely correct—this implementation does not currently handle race conditions, and under high concurrency, there’s a chance multiple threads could attempt to create the singleton instance simultaneously, breaking the singleton pattern.
Solution: Adding Thread Safety with a Lock
To handle this, we can use threading.Lock, which ensures that only one thread can enter the critical section where the instance is created.
Here is the example:
class template:
_instance = None
_lock = threading.Lock() # A lock to ensure thread safety
@classmethod
def get(cls):
with cls._lock: # Ensures only one thread can access this block at a time
if cls._instance is None:
cls._instance = cls()
return cls._instance
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.
Lock
makes things more complex, is there another simple common method?
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.
Another option is to use __new__()
:
class Types:
class NullType(Type):
_instance = None
@classmethod
def get(cls):
if cls._instance is None:
cls._instance = cls.__new__(cls)
return cls._instance
logic here:
- get() checks if _instance is None.
- If _instance is None, cls.new(cls) is called to create a new instance of NullType without directly calling the constructor init.
- get() then returns the instance. Subsequent calls to get() will return the existing _instance.
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.
Does Python support singleton?
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.
Yes, Python does support the Singleton pattern, which ensures that only one instance of a class is created. While Python does not enforce singletons natively, you can implement it in several ways.
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.
hi @SophieTech88
I help you improve this PR, please help me review my change parts.
- We can use
__new__()
implement singleton. - Added some return type and function param type.
- Added some class and function comments.
@xunliu Thank you so much. It is a great help. |
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.
what are the codes for?
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.
@mchades The clients/client-python/tests/unittests/test_gvfs_with_local.py
report waring C0302: Too many lines in module (1102/1100) (too-many-lines)
, So I fixed it.
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.
The type definition logic LGTM
For Singleton Implementation in Python, as I used in implementing Error Handlers before, there's another approach we can consider, we can initialize the instance in separate module, and import wherever needed. This ensure the instance is Singleton. |
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.
@SophieTech88 Thank you for your contributions.
LGTM
What changes were proposed in this pull request?
Why are the changes needed?
We need to support the column data type in python client
Fix: #5200
Does this PR introduce any user-facing change?
No
How was this patch tested?
Need to add the unit tests