Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Allow setting the time of a point manually. #304

Merged
merged 1 commit into from
Mar 18, 2016

Conversation

grundleborg
Copy link
Contributor

Point can be specified as either a number of nanoseconds, a python
datetime object (with or without timezone) or a string in ISO datetime
format.

If a time is not specified, the Helper sets the time at the time of
assembling the point fields so that multiple unique points with the same
tags can be committed simultaneously without them failing to add due to
all being assigned the same automatic time by the InfluxDB server.

This fix is based upon the discussion in #130 but also includes the
outstanding items for it to be merged. I'm happy to receive suggestions
for further ways to add test coverage to this change.

This also fixes #264 and fixes #259.

Point can be specified as either a number of nanoseconds, a python
datetime object (with or without timezone) or a string in ISO datetime
format.

If a time is not specified, the Helper sets the time at the time of
assembling the point fields so that multiple unique points with the same
tags can be committed simultaneously without them failing to add due to
all being assigned the same automatic time by the InfluxDB server.

This fix is based upon the discussion in influxdata#130 but also includes the
outstanding items for it to be merged. I'm happy to receive suggestions
for further ways to add test coverage to this change.

This also fixes influxdata#264 and fixes influxdata#259.
@matt-snider
Copy link
Contributor

Hi @grundleborg!

I've just opened a PR for the exact same feature. I didn't see that yours already existed, and didn't think to check because I had already told @aviau that I would work on this feature a few weeks ago. Had I seen this PR I would have discussed it with you beforehand.

I've taken a slightly different approach as the one in your PR & #130 that avoids a few issues:

  • Assigning a default time in _json_body() will result in a race condition where the points will have a very similar and possibly even identical timestamp, meaning in some cases SeriesHelper inserting only last point from the set #264 can still occur
    • Instead, I've done this at point-creation which also has the benefit that the default timestamps are more accurate
  • By rolling your own timestamp conversion, you duplicate some logic in line_protocol.py for handling timestamps

Take a look and let me know what you think 😃. Maybe @aviau can also weigh in.

@grundleborg
Copy link
Contributor Author

@matt-snider no problem, and quite happy for yours to be merged instead if it handles things more robustly. I needed the feature right away so couldn't wait for someone else to implement it, and I thought I might as well send a PR in since I didn't know how long it would be before anyone else actually dealt with it (and the original PR where the discussion was happening didn't seem to be going anywhere fast).

@matt-snider
Copy link
Contributor

Yep, I understand, especially considering the original PR was opened nearly a year ago...

The important part is that we get this feature merged! 😄

@aviau
Copy link
Collaborator

aviau commented Mar 18, 2016

Thank your for your contribution!

aviau added a commit that referenced this pull request Mar 18, 2016
Allow setting the time of a point manually. (Thanks @grundleborg !)
@aviau aviau merged commit f97effe into influxdata:master Mar 18, 2016
@matt-snider
Copy link
Contributor

Hi @aviau, did you even take a look at my PR? As I mentioned, I don't think this accurately solves #264.

@aviau
Copy link
Collaborator

aviau commented Mar 18, 2016

It does. You are setting a time when the point is created. #264 was because we didn't set a default time.

@matt-snider
Copy link
Contributor

Correct me if I'm wrong but won't all points receive approximately the same timestamp, as they are generated only when _json_body() is called. So depending on how quickly the loop runs, the precision used, etc, they may end up being sent over the wire with an identical timestamp.

Edit: in #264 he also mentions that all points collapse down to one because the timestamps are equivalent. What I'm saying is that that can still occur here

@aviau
Copy link
Collaborator

aviau commented Mar 18, 2016

The timestamps were originally equivalent because InfluxDB was setting one when receiving the batch. I don't know how quickly _json_body() runs but I wouldn't expect timestamps to be the same. We can also move the timestamp generation to when the user creates the point from the serieshelper. This is still better than what we originally had.

@matt-snider
Copy link
Contributor

This is exactly the approach I took because it results in more accurate timestamps and rules out any chance of accidental loss of data (i.e. due to a race condition).

Look I'm not advocating my approach over @grundleborg 's just because I want to have my code merge or anything like that. But if we're implementing automatic timestamps, shouldn't they be accurate to some degree? What about the case where I'm using a batch size (e.g. 500) and those points now all end up with the timestamp generated immediately before being sent?

matt-snider added a commit to matt-snider/influxdb-python that referenced this pull request Apr 10, 2016
This reverts commit c25ec08,
which this commit is part of PR influxdata#304.
aviau pushed a commit that referenced this pull request Apr 21, 2016
* Revert Allow setting the time of a point manually

This reverts commit c25ec08,
which this commit is part of PR #304.

* Allow time to be specified in SeriesHelper.__init__()

* Extract SeriesHelper default timestamp into method for testability

* Use datetime.utcnow() as default timestamp in SeriesHelper

This is preferable to time.time() because _convert_timestamp() from
influxdb.line_protocol will do precision handling and conversion if a
datetime object is given.

* Get existing tests working by mocking SeriesHelper._current_timestamp()

* Add additional tests for SeriesHelper time field

* Move _reset_() calls in TestSeriesHelper to tearDown()

* Use mock.patch() instead of unittest.mock.patch() for py27

* Update SeriesHelper docstring
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SeriesHelper inserting only last point from the set How to set time using SeriesHelper?
3 participants