-
Notifications
You must be signed in to change notification settings - Fork 802
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
merge history db file into a common db file to avoid db file booming #518
Open
lee3164
wants to merge
3
commits into
prometheus:master
Choose a base branch
from
lee3164:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#!/usr/bin/env python | ||
# coding=utf-8 | ||
|
||
import os | ||
|
||
__all__ = ('LOCK_EX', 'LOCK_SH', 'LOCK_NB', 'lock', 'unlock') | ||
|
||
|
||
def _fd(f): | ||
return f.fileno() if hasattr(f, 'fileno') else f | ||
|
||
|
||
if os.name == 'nt': | ||
import msvcrt | ||
from ctypes import (sizeof, c_ulong, c_void_p, c_int64, | ||
Structure, Union, POINTER, windll, byref) | ||
from ctypes.wintypes import BOOL, DWORD, HANDLE | ||
|
||
LOCK_SH = 0 # the default | ||
LOCK_NB = 0x1 # LOCKFILE_FAIL_IMMEDIATELY | ||
LOCK_EX = 0x2 # LOCKFILE_EXCLUSIVE_LOCK | ||
|
||
if sizeof(c_ulong) != sizeof(c_void_p): | ||
ULONG_PTR = c_int64 | ||
else: | ||
ULONG_PTR = c_ulong | ||
PVOID = c_void_p | ||
|
||
|
||
class _OFFSET(Structure): | ||
_fields_ = [ | ||
('Offset', DWORD), | ||
('OffsetHigh', DWORD)] | ||
|
||
|
||
class _OFFSET_UNION(Union): | ||
_anonymous_ = ['_offset'] | ||
_fields_ = [ | ||
('_offset', _OFFSET), | ||
('Pointer', PVOID)] | ||
|
||
|
||
class OVERLAPPED(Structure): | ||
_anonymous_ = ['_offset_union'] | ||
_fields_ = [ | ||
('Internal', ULONG_PTR), | ||
('InternalHigh', ULONG_PTR), | ||
('_offset_union', _OFFSET_UNION), | ||
('hEvent', HANDLE)] | ||
|
||
|
||
LPOVERLAPPED = POINTER(OVERLAPPED) | ||
|
||
LockFileEx = windll.kernel32.LockFileEx | ||
LockFileEx.restype = BOOL | ||
LockFileEx.argtypes = [HANDLE, DWORD, DWORD, DWORD, DWORD, LPOVERLAPPED] | ||
UnlockFileEx = windll.kernel32.UnlockFileEx | ||
UnlockFileEx.restype = BOOL | ||
UnlockFileEx.argtypes = [HANDLE, DWORD, DWORD, DWORD, LPOVERLAPPED] | ||
|
||
|
||
def lock(f, flags): | ||
hfile = msvcrt.get_osfhandle(_fd(f)) | ||
overlapped = OVERLAPPED() | ||
ret = LockFileEx(hfile, flags, 0, 0, 0xFFFF0000, byref(overlapped)) | ||
return bool(ret) | ||
|
||
|
||
def unlock(f): | ||
hfile = msvcrt.get_osfhandle(_fd(f)) | ||
overlapped = OVERLAPPED() | ||
ret = UnlockFileEx(hfile, 0, 0, 0xFFFF0000, byref(overlapped)) | ||
return bool(ret) | ||
else: | ||
try: | ||
import fcntl | ||
|
||
LOCK_SH = fcntl.LOCK_SH # shared lock | ||
LOCK_NB = fcntl.LOCK_NB # non-blocking | ||
LOCK_EX = fcntl.LOCK_EX | ||
except (ImportError, AttributeError): | ||
LOCK_EX = LOCK_SH = LOCK_NB = 0 | ||
|
||
|
||
def lock(f, flags): | ||
return False | ||
|
||
|
||
def unlock(f): | ||
return True | ||
else: | ||
def lock(f, flags): | ||
ret = fcntl.flock(_fd(f), flags) | ||
return ret == 0 | ||
|
||
|
||
def unlock(f): | ||
ret = fcntl.flock(_fd(f), fcntl.LOCK_UN) | ||
return ret == 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
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.
@brian-brazil here I use the same method to merge metrics
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 believe this will cause deadlock:
mark_process_dead
is already holding lock and callingmerge
will try to acquire the same lock again.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 don't believe it will -- it looks like lock acquisition via
flock
is reentrant safe, but unlocking viaflock
isn't, so I think this will prematurely release the lock after themerge
call here, at least on POSIX systems. I don't know what the locking behaviour on NT will be.