FluidFramework icon indicating copy to clipboard operation
FluidFramework copied to clipboard

Support receiving data in utf8 from storage / driver

Open vladsud opened this issue 6 years ago • 17 comments

Today, all of our code base assumes base64:

export interface IChannelStorageService {
    /**
     * Reads the object contained at the given path. Returns a base64 string representation for the object.
     */
    read(path: string): Promise<string>;

This means that server (SPO) receives our utf8 data, encodes it in base64, and sends over in base64, and client decodes. This has 3 types of inefficiencies:

  1. server spends time encoding base64
  2. network traffic is larger
  3. clients spends time decoding base64.

All of that can be addressed, as IBlob we receive already has encoding, so we can support utf8, and server can start sending utf8 after that.

All places where we call fromBase64ToUtf8() would need to be examined.

vladsud avatar Aug 13 '20 04:08 vladsud

Relevant discussion from the past

KurtB: All data for us is always raw binary. Any place where we take UTF-8 is simply a convenience method because the person is giving us a string (likely JSON). The return as base64 is because on retrieval, since everything is binary, we no longer know the original intent.

This has caused enough confusion though that for all driver level interfaces we may want to swap to just using a raw Buffer to be very explicit.

vladsud avatar Aug 13 '20 04:08 vladsud

@wes-carlson , let me know if you could help with this item at some point.

vladsud avatar Aug 13 '20 04:08 vladsud

@heliocliu this might have some relationship with your work on removing dependency on Buffer.

curtisman avatar Aug 13 '20 05:08 curtisman

How about doing base64 end to end? Client will send and receive base64 and server does not need to do any conversion. I understand the storage amplification but its also the most generic one and gives us an opportunity to send binary blob as well. To tackle storage/wire amplification, We can always consider gzipping the payload.

tanviraumi avatar Aug 13 '20 07:08 tanviraumi

The part that I do not understand (and can't find any good answer quickly) is level of support from http stack. It used to be that only 7-bit traffic was allowed, and hence base64 was born. But we are sending utf-8, I do not see much difference from binary here (I assume that payload size is communicated in protocol, not terminating symbol '\0' used to denote end of stream). If that's the case, there is no need for base64.

Either way, I feel like the best course of action for protocol to allow roundtrip and never touch data. If utf-8 comes in, utf-8 should come out. Same for base64.

vladsud avatar Aug 13 '20 15:08 vladsud

Pasting my email here as well for keeping track of things.

Not all binary data is transferable as utf-8. Depending on the protocol, the range of 256 characters can be interpreted differently. base64 is safer because it only uses a well-established range of 64 characters, interpreted as same across every protocol out there.

Since we only deal with text today, we really don't care whether its utf-8 or base64. utf-8 will work great and the payload will be smaller. If we want to support binary blobs as well, we need to keep supporting base64.

The closest help from internet is this one (https://stackoverflow.com/questions/201479/what-is-base-64-encoding-used-for)

tanviraumi avatar Aug 13 '20 20:08 tanviraumi

This is great, but it does not answer if utf-8 we used today (upload) is safe - following same logic we can say it's not. We need clear spec that describes precisely what is allowed and what is not. From quick read, it sounds by default http uses ISO-8859-1 which is not safe, unless Content-Type is provided, then it's encoded based on that type.

vladsud avatar Aug 14 '20 06:08 vladsud

Agree. Uploading in utf-8 is only safe as long as we are only dealing with texts. In our case, non-textual data will probably be handled through blob api. So the spec should say that clearly.

On the HTTP side, r11s always (hopefully) uses application/json as a content type and I believe that maps to UTF-8.

tanviraumi avatar Aug 14 '20 06:08 tanviraumi

Ok, some more digging:

I believe Scriptor puts text into Sequence as is, i.e. no encoding. We have tested Chinese characters and a bunch of others. When summarizing, Merge Tree does not do anything special with text I see that summaries (via convertToSummaryTree) always put Unicode string (i.e. even if base64 payload was provided by DDS, we decode it). And looking at ODSP’s uploadSummaryWithContext() implementation, we just write it out as is, no encoding, nothing. "Content-Type" = "application/json" header is supplied there. And glancing through https://www.ietf.org/rfc/rfc4627.txt, it sounds like protocol guarantees correct handling of Unicode and auto-detection of encoding (utf-8 / 16 / 32) in such case.

Given that summaries have no encoding, it means that reading back (snapshot) can be symmetrical and not require returning base 64. Users that deal with (let's say) Uint8Array, would need to encode it in some other format (given json does not support that type or any binary type), and client can decode it as well. I.e. maybe it's base64, maybe it's something else, but none of protocols / APIs should care.

Blob API (readBlob, writeBlob) should be binary and they are already (Uint8Array).

The only other thing left here is crossing APIs, i.e. creating blobs via snapshot, and reading then not via read() but via readBlob(). This should be banned, as protocol does not describe how strings are encoded (utf-8 or utf-16 or utf-32), so getting binary data can give you different results depending on implementation.

Note that at storage level, we can have single API for reading blobs. But depending on what client is asking (i.e. calls read() or readBlob()), proper conversion to string (i.e. auto-detection of encoding per rfc4627) is required in the driver when creating a string.

@tanviraumi , @wes-carlson - can you please take a look?

vladsud avatar Oct 27 '20 07:10 vladsud

Here is plan:

  1. Change OdspDocumentStorageService.read() to respect encoding on IBlob. I.e. if it's base64, keep doing what it does, if it's utf-8, encode it using base64, to keep current contract of read() API the same - it always returns base64 string.
  2. Add IDocumentStorageService.readString() API: It should return decoded (from base64) string.
    • It can call IDocumentStorageService.read() for starters in all drivers and decode returned string from base64 using fromBase64ToUtf8().
    • But ODSP version (OdspDocumentStorageService) should be refactored such that it does not do any unneeded computation. I.e. if blob came in as base64, it need to decode it, but if it came as utf-8, it does nothing, i.e. pass-through.
  3. Wait at least one release. I.e. previous item should be available in release/0.x.y branch when next items can be completed.
  4. All code in runtime that calls IDocumentStorageService.read() and later into fromBase64ToUtf8() is changed to call into readString() API.
  5. Wait at least one release. I.e. previous item should be available in release/0.x.y branch when next items can be completed.
  6. Remove IDocumentStorageService.read()

Note - we should merge changes as they become available. I.e. # 1 & # 2 are independent and need different type of seasoning - first change needs to reach Bohemia and ship to prod before SPO can make changes on their side. Second needs to be released before we can actually start depending on it in runtime due to versioning - all clients would need to consume this change before we can ship changes in runtime (later points).

Once # 1 ships to production, we need to notify SPO folks about it such that they can stop returning base64 encoded streams.

vladsud avatar Oct 28 '20 05:10 vladsud

@chensixx - FYI - please take a look at this issue, ask questions, etc. - per @curtisman, this work is coming your way for November. Please assign it to yourself, github gives me troubles here.

vladsud avatar Oct 28 '20 06:10 vladsud

So the work item of changing r11s driver to return "plain string" is coupled with work item 2?

tanviraumi avatar Oct 28 '20 13:10 tanviraumi

Yes, clarified # 2 in place.

vladsud avatar Oct 29 '20 22:10 vladsud

I'm not sure if I fully followed the thread but some context...

A lot of the confusion comes from GitHub's REST API which our driver interfaces are based on. When posting data there is an encoding field which can be either "utf-8" or "base64" https://developer.github.com/v3/git/blobs/#create-a-blob. This is somewhat of a poorly named field though. The "base64" definition is correct but in the case of "utf-8" it really just means "when you write this to disk store it as utf-8." In memory I think JavaScript strings are UTF-16. And JSON can be 8/16/32... So the API really is just trying to differentiate between a string or a binary encoded representation. And if it's a string then the encoding specifies how to serialize it.

Once stored the assumption is everything is binary with no stored content types.

Although the return type of GitHub's REST API provides the encoding option as well it's always base64. This is because there's no content type so they don't know the format the data is in.

Given that it shows up as a property in a JSON object the binary data needs to be base64 (or other) encoded since it may have invalid character values for the sent encoding (most likely utf-8). If you were sending the data directly over HTTP you could not encode it at all and just send the raw binary data.

I always liked the idea of moving to a place where all driver APIs only took a Buffer or better yet a Stream. It gives the higher level layers more flexibility in what/how they're storing/retrieving things. And keeps what the service needs to send and do to a minimum. Also minimizes what the driver needs to implement. Helpers to return a value as JSON, a string, binary seem like they could be layered on top of this (the fetch api does this).

But having to pass around base64 strings through the higher level layers is less than ideal. That's mostly an outgrowth of directly passing the GitHub REST API return results around.

kurtb avatar Nov 26 '20 03:11 kurtb

Yes, encoding is really poorly named :)

The reason for this work is this two requirements:

  1. Base64 decoding is one of the most expensive parts of runtime impact on boot perf. We want to eliminate it completely.
  2. We have to have ability (driver) to batch requests together and return results in smaller number of roundtrips, to be able to hit perf goals. This goal does not directly related to this work, but something that needs to be kept in mind.

Currently, we have these APIs on driver:

  • readBlob() - returns buffer - binary representation of blob in storage
  • read() - returns base64 encoded binary blob.

readBlob() is already implemented as binary protocol in SPO. It uses same base64-encoded transfer in r11s. read() should be removed as obsolete method, but to simplify working with strings, we are adding readString() API. This is not strictly required (as binary -> string conversion can happen at each call site), but I think it's better to have it for simplicity of code, as well as for symmetry (summary creation can work with strings).

Initially I though about not putting any encoding information for strings (string in, string out, but driver controls how that maps to storage / encoding). This is similar to some other systems (for example, BSON encodes all strings as utf-16 and does not leave any option here). But looking at latest PR by Si, it's pretty clear that we have a bunch of adapter and prefertch layers that do not know anything about how blob was created, so they need to assume binary, and when readString() call comes in, they need to know encoding (or guess it, similar to how https://www.ietf.org/rfc/rfc4627.txt handles it). utf-8 is likely good enough for most data (as "structural" component today outweighs user content), but we should allow flexibility in the future. Thus encoding should be provided on reading & writing strings, but I think it would be great for it to be optional, and utf-8 to be used if omitted.

Writing should be similar. We already have writeBlob() API for binary data, and snapshot creation already works with strings. We should change base64 option (not really used today) to ability to provide Buffer for binary data, that would completely remove the need in runtime layer to work with base64 (driver is free to use whatever it wants).

vladsud avatar Dec 07 '20 04:12 vladsud

Actually, looking more at layers and wrappers, I think storage (IDocumentStorageService) should just expose readBlob(), but IChannelStorageService should expose both readBlob() and readString(). That way we keep driver layer (and all adapter layers) simple, but provide better interface to final consumers.

vladsud avatar Dec 07 '20 04:12 vladsud

All the work has been completed here. Waiting for it to land in prod and switch ODSP to using string payloads

vladsud avatar Mar 06 '21 00:03 vladsud