influxdb icon indicating copy to clipboard operation
influxdb copied to clipboard

Line Protocol V2

Open joelegasse opened this issue 10 years ago • 47 comments

The current line protocol has a few quirks and strange behaviors because of the way the parser was implemented. Unfortunately those quirks were just documented and developed around, rather than discussing whether or not the protocol should be better defined and made slightly more uniform. Here is my attempt at a slightly more formal definition...

Uniform Line Protocol

This would be a breaking change to current line protocol, where each component had different rules as to what had to be escaped (e.g. measurement names didn't need to escape equal signs, but tag keys did), but I think it would provide a simpler ruleset for library maintainers and people writing directly to the API.

Key:
    () -> Grouping
    {} -> Zero or more
    [] -> Optional
    "" -> Literal value (case insensitive)
    '' -> Literal character (using common escape sequences)
    |  -> Alternate values
    .  -> End Definition
    ** -> Plain-language description

Batch         = { Comment } [ ParameterLine ] { Line } .
Comment       = "#" *UTF-8 string except '\n'* '\n' .
ParameterLine = "=" { " " String "=" String } '\n' .
Line          = Comment | Point .

Point         = Measurement [ "," TagSet ] " " FieldSet [ " " Time ] '\n' .
Measurement   = String .
TagSet        = Tag { "," Tag } .
FieldSet      = Field { "," Field } .
Time          = Digits [ Unit ] .
Unit          = [ "n" | "u" | "m" ] "s" .
Tag           = String "=" String .
Field         = String "=" Value .

Value         = QuotedString | Boolean | Integer | FloatingPoint .
Boolean       = "t" | "true" | "f" | "false" .
Integer       = [ "-" ] Digits 'i' .
Digits        = Digit { Digit } .
Digit         = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' .
FloatingPoint = [ "-" ] [ [ Digits ] "." ] Digits [ Exponent ] .
Exponent      = ( "e" | "E" ) [ "+" | "-" ] Digits .

String        = QuotedString | SimpleString .
QuotedString  = '"' *non-empty UTF-8 string with limited escaped characters* '"' .
SimpleString  = *non-empty UTF-8 string with special characters escaped* .

Within Strings, a backslash must be followed by a valid escape character:

For all Strings:
- Backslash       (U+005C, '\')
- Quotation Mark  (U+0022, '"', double quote)
- Lowercase N     (U+006E, 'n') [represents Line Feed (U+000A, '\n')]

For SimpleString only:
- Space           (U+0020, ' ')
- Equal Sign      (U+003D, '=')
- Comma           (U+002C, ',')

It should be noted that as a special case, '\n' is only ever a control
character, and must never appear in a string. This means that for
QuotedString, a literal newline character would appear as "\n"

There are a few things I'd like to point out as "features" of this proposal:

  • Names, keys, and values aren't restricted by these definitions, that's up for discussion in another issue (#6011),
  • String field values must be quoted (as they are currently)
  • The timestamp now has an optional unit (removing the minutes/hours options)
  • The ParameterLine allows for key=value pairs to be sent with a batch, which would allow the database, retention policy, and default precision to be set in the batch, making UDP point writing more flexible than it currently is.
  • Strings can either be quoted or unquoted in measurement names, tags and field keys, allowing for flexibility in formatting. Users with spaces, equals, or commas in their tags can just use quoted strings and only escape backslashes, quotes, and newlines

There is one feature that was requested in the other issue that I think is worth discussing here: setting a per-batch custom escape character. This could be done via the ParameterLine, meaning the line

file_stats dir="C:\\windows\\uses\\backslash",file="Important document!.docx",size=18923i

could instead be written

= escape=!
file_stats dir="C:\windows\uses\backslash",file="Important document!!.docx",size=18923i

As for a possible roll-out strategy, this new format could be selected at the write endpoint by setting a custom HTTP Header, or enabling a new UDP listener in the config. That way, all current clients would still work and have the chance to migrate/upgrade before the new version becomes the only available version some number of releases in the future. At that point, the custom header would be ignored, and the old UDP listener would be removed.

I've probably written enough at this point, and have most likely missed something here or there. Please leave your feedback here. Keep in mind this is just an idea, and mostly a means to begin a discussion around a "better" and well-defined line protocol that helps every level of the TICK stack (and other tools) that read/write points in our line protocol.

joelegasse avatar Mar 16 '16 20:03 joelegasse

could you provide some more concrete examples like at https://docs.influxdata.com/influxdb/v0.10/write_protocols/line/?

sparrc avatar Mar 16 '16 21:03 sparrc

Time          = Digits [ Unit ] .
Unit          = [ "n" | "u" | "m" ] "s" .

does this mean that line protocol would default to seconds?

sparrc avatar Mar 16 '16 21:03 sparrc

Point         = Measurement TagSet " " FieldSet [ " " Time ] '\n' .

by your definitions, shouldn't this be:

Point         = Measurement [ "," TagSet ] " " FieldSet [ " " Time ] '\n' .

sparrc avatar Mar 16 '16 21:03 sparrc

I like this proposal overall. I agree with @sparrc that more examples would make this all easier to grok (especially examples around strings and what does or does not need to be escaped).

I dislike the inclusion of comments into the line protocol. While that may make life slightly easier for those creating points by hand, I think we should discourage including irrelevant data in line protocol batches.

I also think the feature of overriding the escape character is too costly to implement, for line protocol readers.

mark-rushakoff avatar Mar 16 '16 21:03 mark-rushakoff

I would like to propose that either strings in the line protocol are surrounded by single quotes or we modify InfluxQL to let strings be surrounded by double quotes. There's a weird lack of parity between these two currently.

> insert cpu name="foo",value=3
> select value from cpu where "name" = 'foo'

The single quotes are required and I think they confuse a lot of people (at the least, they confused me). Double quotes are for variable names and are mostly used when a variable name conflicts with a reserved keyword.

jsternberg avatar Mar 16 '16 21:03 jsternberg

I will definitely work on some examples, and add them to the original post. As far as a few points to be addressed:

  • Tags would be defined as they currently are: comma separated from the measurement name. It's a subtle difference in the definition of TagSet and FieldSet. The difference is that technically each tag is preceded by a comma, and you can have zero or more directly following the measurement name. The empty string is a valid TagSet, which is why is doesn't need to be optional when defined in Point.
  • The default precision would still be server-specific (probably still nanoseconds if there is no unit, and to precision parameter has not been set). The unit definition would change from n, u, ms and s to ns, us, ms, and s, always including the s.
  • I'll leave the comment discussion to the wider group, it's trivial to ignore the extra information, and would only incur the cost of actually transferring the bytes from the client to the server.
  • I don't like speculation on "cost" of implementing features unless we actually try them and do some benchmarks. But if we're going to speculate... As long as the parser is written to use a variable for the escape character rather than hardcoding \ in the source, changing the character to reduce the burden on heavily-backslashed strings shouldn't be too difficult or "costly", I would think.
  • I'm all for making sure that InfluxQL and the line protocol use the same quoting for strings.

joelegasse avatar Mar 16 '16 22:03 joelegasse

got it, thanks Joe,

but by the logic that an empty string is a valid TagSet, doesn't that also mean that an empty timestamp is a valid timestamp?

also-- how is an empty string a valid tag if this is true:

Tag           = String "=" String .

sparrc avatar Mar 16 '16 22:03 sparrc

A timestamp is defined as Digits, which is at least one digit. Although we should specify that strings have a minimum length of 1 On Mar 16, 2016 6:32 PM, "Cameron Sparr" [email protected] wrote:

got it, thanks Joe,

but by the logic that an empty string is a valid TagSet, doesn't that also mean that an empty timestamp is a valid timestamp?

also-- how is an empty string a valid tag if this is true:

Tag = String "=" String .

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/influxdata/influxdb/issues/6037#issuecomment-197586127

joelegasse avatar Mar 16 '16 22:03 joelegasse

Generally I like this. The escaping rules look inline with the version of the line-protocol parser I implemented in er-escape-backslash, so they get the :+1: from me.

Comment       = "#" *UTF-8 string except '\n'* '\n' .

I agree with Mark on the comments; I don't see the real advantage to accepting them.

Point         = Measurement TagSet " " FieldSet [ " " Time ] '\n' .

Does a Point needs to end in a newline?

e-dard avatar Mar 16 '16 22:03 e-dard

so this is valid?:

cpu_usage,= idle=100

or should this be Tag:

Tag           = String [ "=" ] String .

sparrc avatar Mar 16 '16 22:03 sparrc

I'm not a huge fan of requiring the s at the end of the precision units. It feels unnecessary and it's not something I've seen people get tripped up on.

How about changing the default precision to seconds? Most other tools use second precision and it's a common beginner mistake to use seconds (resulting in a bunch of 1970 points that trip people up). Compression is also much better when using seconds as the precision.

gunnaraasen avatar Mar 16 '16 22:03 gunnaraasen

Does a Point needs to end in a newline?

I think so. A newline indicates that the point is complete. Without it, you may not know if the line was unexpectedly truncated.

mark-rushakoff avatar Mar 16 '16 22:03 mark-rushakoff

@gunnaraasen the unit is optional: Time = Digits [ Unit ] .

sparrc avatar Mar 16 '16 22:03 sparrc

@sparrc @joelegasse maybe it should be something like the following?

Point         = Measurement [ , TagSet ] " " FieldSet [ " " Time ] '\n' .
TagSet        = Tag { "," Tag } .
Tag           = String "=" String .

e-dard avatar Mar 16 '16 22:03 e-dard

@sparrc Whoops, my bad. I read @joelegasse's comment, "always including the s", and didn't think to check the spec.

gunnaraasen avatar Mar 16 '16 22:03 gunnaraasen

@e-dard your proposed grammar is equivalent to what is currently there.

joelegasse avatar Mar 16 '16 22:03 joelegasse

@gunnaraasen The default precision could be set in the parameter line, and handled by the client library. We could try to do some guessing if no precision has been set anywhere, and pick the time that seems closest in terms of order of magnitude. Although that is a separate concern from the line protocol itself.

joelegasse avatar Mar 16 '16 23:03 joelegasse

I also don't think we need to have customized escape characters. I think most, if not all, people will just be fine with a backslash. It adds more difficulty to have a custom escape character and I don't know what benefit it offers.

I think we should just focus on getting escaping to be more consistent and it so single quoted strings are allowed (we can allow both double and single quotes for strings).

jsternberg avatar Mar 16 '16 23:03 jsternberg

@joelegasse you're right, and now I can't think what I was trying to get at, at the time...

e-dard avatar Mar 16 '16 23:03 e-dard

Personally I prefer @e-dard's grammar, feels more consistent to me, also with the current grammar this is technically valid (I think...):

cpu_usage,= idle=100

sparrc avatar Mar 16 '16 23:03 sparrc