-
Notifications
You must be signed in to change notification settings - Fork 10
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
Data recovery #24
Open
egorps
wants to merge
3
commits into
spilgames:master
Choose a base branch
from
unknown repository
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
Data recovery #24
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '1.6.1' | ||
__version__ = '1.6.2' |
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 |
---|---|---|
|
@@ -54,6 +54,15 @@ class Query(object): | |
:param exc_queue: | ||
An instance of class:`Queue.Queue` instance to put exceptions in. | ||
|
||
:param drain_to_file: | ||
A ``bolean``. This indicates whether or not data in the fifo should | ||
be saved to a file if an error occurs. If false, all data in the | ||
fifo when an exception occurs will bediscarded. If true, data will | ||
be saved to a random directory in /tmp, and recover_dir on the | ||
:py:class:`Query` instance with contain the full path to the | ||
directory. It is possible that the contents of the fifo will overload | ||
the disk. If that is the case, the data will be discarded. | ||
Default: ``False``. | ||
""" | ||
|
||
daemon = True | ||
|
@@ -62,13 +71,15 @@ class Query(object): | |
if only those are left. | ||
""" | ||
|
||
def __init__( | ||
self, cursor, sql_query_str, fifo_path, exc_queue): | ||
def __init__(self, cursor, sql_query_str, fifo_path, exc_queue, | ||
drain_to_file=False): | ||
super(Query, self).__init__() | ||
self.cursor = cursor | ||
self.sql_query_str = sql_query_str | ||
self.exc_queue = exc_queue | ||
self.fifo_path = fifo_path | ||
self.drain_to_file = drain_to_file | ||
self.recover_file = None | ||
|
||
def run_query(self): | ||
""" | ||
|
@@ -89,11 +100,39 @@ def run_query(self): | |
|
||
# we need to consume the fifo, to make sure it isn't blocking the | ||
# write (and thus hanging forever). | ||
for line in codecs.open(self.fifo_path, 'r', 'utf-8'): | ||
pass | ||
fifo = codecs.open(self.fifo_path, 'r', 'utf-8') | ||
if self.drain_to_file: | ||
self._drain_to_file(fifo) | ||
else: | ||
self._drain(fifo) | ||
|
||
logger.debug('Query done') | ||
|
||
def _drain_to_file(self, fifo_obj): | ||
""" | ||
Pulls all data out of the fifo and saves it to a tmp file. | ||
If an exception is thrown while saving data to the file, | ||
it simply discards the data and removes the file. | ||
""" | ||
self.recover_file = os.path.join(tempfile.mkdtemp(), 'saved_inserts') | ||
file = open(self.recover_file, 'w') | ||
try: | ||
for line in fifo_obj: | ||
file.write(line) | ||
logger.info("Insert data saved to file %s" % self.recover_file) | ||
except IOError, ex: | ||
logger.error("Error saving data to file: %s" % ex) | ||
self._drain(fifo_obj) | ||
os.remove(self.recover_file) | ||
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. Not sure that removing the file would actually remove it from disk if the handle is still openI would thus add before:file.close() |
||
self.recover_file = None | ||
|
||
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. This is probably safer to have here as well before returning file.close() |
||
def _drain(self, fifo_obj): | ||
""" | ||
Discards all data in the fifo. | ||
""" | ||
for line in fifo_obj: | ||
pass | ||
|
||
|
||
class VerticaBatch(object): | ||
""" | ||
|
@@ -194,6 +233,15 @@ class VerticaBatch(object): | |
closing all of its resources. | ||
Default: ``False``. *Optional*. | ||
|
||
:param save_data_on_error: | ||
A ``boolean``. If true, when an error occurs, uncommitted data will | ||
be saved to a file. The file path will be accessible via | ||
``VerticaBatch.recover_file`` if an error occurred | ||
after calling ``commit_batch``. | ||
Default: ``False``. If the disk fills up while saving data, | ||
the recovery will be marked as failed and the recovery | ||
file will be removed. | ||
|
||
""" | ||
copy_options_dict = { | ||
'DELIMITER': ';', | ||
|
@@ -223,7 +271,8 @@ def __init__( | |
column_list=[], | ||
copy_options={}, | ||
connection=None, | ||
multi_batch=False): | ||
multi_batch=False, | ||
save_data_on_error=False): | ||
|
||
if connection and odbc_kwargs: | ||
raise ValueError("May only specify one of " | ||
|
@@ -363,6 +412,7 @@ def close_batch(self): | |
logger.debug('Batch ended') | ||
|
||
if not self._query_exc_queue.empty(): | ||
self.recover_file = self._query.recover_file | ||
raise self._query_exc_queue.get() | ||
|
||
self._batch_initialized = False | ||
|
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.
make sure file name is unique across runs by suffixing it with pid + timestamp
suffix = '.' + str(os.getpid()) + '.' + datetime.datetime.utcnow().strftime('%Y%m%d_%H%M%S')
file = tempfile.NamedTemporaryFile(dir=NeedToGetFromParam, mode='w', delete=false, suffix=suffix)
self.recover_file = file.name