Skip to content
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

Store the raw JSON payload from the getRelays API call #6767

Conversation

rablador
Copy link
Contributor

@rablador rablador commented Sep 9, 2024

Instead of storing the parsed relays to disk we should store the raw json response. This will help us avoid old structure being read when doing migration to new object properties in our relay related data.


This change is Reviewable

@rablador rablador added the iOS Issues related to iOS label Sep 9, 2024
@rablador rablador self-assigned this Sep 9, 2024
Copy link

linear bot commented Sep 9, 2024

@rablador rablador force-pushed the ios-814-store-the-raw-json-payload-from-the-getrelays-api-call branch from 059fad1 to 7017e92 Compare September 9, 2024 12:18
Copy link
Collaborator

@pinkisemils pinkisemils left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 13 files reviewed, 1 unresolved discussion (waiting on @rablador)


ios/MullvadREST/Relay/StoredRelays.swift line 25 at r1 (raw file):

    public var relays: REST.ServerRelaysResponse {
        get throws {
            try REST.Coding.makeJSONDecoder().decode(REST.ServerRelaysResponse.self, from: rawData)

How often do we get it? Could we make this lazy as to not decode the whole relay list all of the time?

Copy link
Collaborator

@pinkisemils pinkisemils left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 13 files reviewed, 2 unresolved discussions (waiting on @rablador)


ios/MullvadVPN/RelayCacheTracker/RelayCacheTracker.swift line 122 at r1 (raw file):

        )

        let now = Date()

As per the discussion earlier, I do not believe updatedAt should be changed to the current timestamp. Instead, we shouldn't change it at all or change it such that it forces a relay list update.

The reason we have the hotfix is because there will be cases where people cannot reliably reach our API - but we should do our best to fetch a new relay list if we know that the data on disk is incomplete.

Copy link
Collaborator

@pinkisemils pinkisemils left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 13 files reviewed, 2 unresolved discussions (waiting on @rablador)


ios/MullvadREST/Relay/StoredRelays.swift line 25 at r1 (raw file):

Previously, pinkisemils (Emīls Piņķis) wrote…

How often do we get it? Could we make this lazy as to not decode the whole relay list all of the time?

Also, could we not store the REST.ServerRelaysResponse as is here? The fact that this is a fallible getter gets on my nerves and inflates the diff size here.

Copy link
Collaborator

@pinkisemils pinkisemils left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 13 files reviewed, 2 unresolved discussions (waiting on @rablador)


ios/MullvadREST/Relay/StoredRelays.swift line 25 at r1 (raw file):

Previously, pinkisemils (Emīls Piņķis) wrote…

Also, could we not store the REST.ServerRelaysResponse as is here? The fact that this is a fallible getter gets on my nerves and inflates the diff size here.

The above could be achieved by deserializing the relay list from the raw data and never serializing it to disk in the first place.

Copy link
Contributor

@buggmagnet buggmagnet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3 of 13 files at r1, all commit messages.
Reviewable status: 3 of 13 files reviewed, 3 unresolved discussions (waiting on @rablador)


ios/MullvadREST/Relay/RelayCache.swift line 43 at r1 (raw file):

    public func read() throws -> StoredRelays {
        do {
            return try fileCache.read()

The first time we launch the app with this patch, reading from the cache will fail because a CachedRelays type was stored, but we're trying to decode a StoredRelays type.

In this case, we want to be able to create a StoredRelays directly from a CachedRelays.
We probably want something along those lines

    public init(cachedRelays: CachedRelays) throws {
        self.etag = cachedRelays.etag
        self.rawData = try JSONEncoder().encode(cachedRelays.relays)
        self.updatedAt = cachedRelays.updatedAt
    }

Copy link
Contributor Author

@rablador rablador left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 3 of 13 files reviewed, 3 unresolved discussions (waiting on @pinkisemils)


ios/MullvadREST/Relay/RelayCache.swift line 43 at r1 (raw file):

Previously, buggmagnet wrote…

The first time we launch the app with this patch, reading from the cache will fail because a CachedRelays type was stored, but we're trying to decode a StoredRelays type.

In this case, we want to be able to create a StoredRelays directly from a CachedRelays.
We probably want something along those lines

    public init(cachedRelays: CachedRelays) throws {
        self.etag = cachedRelays.etag
        self.rawData = try JSONEncoder().encode(cachedRelays.relays)
        self.updatedAt = cachedRelays.updatedAt
    }

Yes, with fallback on the bundled relays. As also discussed with @pinkisemils, we should do something like the above.


ios/MullvadREST/Relay/StoredRelays.swift line 25 at r1 (raw file):

Previously, pinkisemils (Emīls Piņķis) wrote…

The above could be achieved by deserializing the relay list from the raw data and never serializing it to disk in the first place.

I'm not sure I follow. So we shouldn't store the raw data after all...?


ios/MullvadVPN/RelayCacheTracker/RelayCacheTracker.swift line 122 at r1 (raw file):

Previously, pinkisemils (Emīls Piņķis) wrote…

As per the discussion earlier, I do not believe updatedAt should be changed to the current timestamp. Instead, we shouldn't change it at all or change it such that it forces a relay list update.

The reason we have the hotfix is because there will be cases where people cannot reliably reach our API - but we should do our best to fetch a new relay list if we know that the data on disk is incomplete.

If we force a new fetch here there would be no need for the hotfix either, since the idea behind the fix is to graft some new properties into old structure without the need for network calls. I think leaving it makes more sense.

Copy link
Collaborator

@pinkisemils pinkisemils left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 3 of 13 files reviewed, 3 unresolved discussions (waiting on @rablador)


ios/MullvadREST/Relay/StoredRelays.swift line 25 at r1 (raw file):

Previously, rablador (Jon Petersson) wrote…

I'm not sure I follow. So we shouldn't store the raw data after all...?

We should store rawData, but fail the deserialization if we cannot deserialize rawData into ServerRelyResponse during deserializaiton. We can chat about this in person.


ios/MullvadVPN/RelayCacheTracker/RelayCacheTracker.swift line 122 at r1 (raw file):

Previously, rablador (Jon Petersson) wrote…

If we force a new fetch here there would be no need for the hotfix either, since the idea behind the fix is to graft some new properties into old structure without the need for network calls. I think leaving it makes more sense.

We can force the fetch, but we can never rely on the fetch working. Even if we don't force a new fetch, we shouldn't update the timestamp every time the grafting takes place.

Copy link
Contributor Author

@rablador rablador left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 3 of 13 files reviewed, 3 unresolved discussions (waiting on @pinkisemils)


ios/MullvadREST/Relay/RelayCache.swift line 43 at r1 (raw file):

Previously, rablador (Jon Petersson) wrote…

Yes, with fallback on the bundled relays. As also discussed with @pinkisemils, we should do something like the above.

Done.


ios/MullvadVPN/RelayCacheTracker/RelayCacheTracker.swift line 122 at r1 (raw file):

Previously, pinkisemils (Emīls Piņķis) wrote…

We can force the fetch, but we can never rely on the fetch working. Even if we don't force a new fetch, we shouldn't update the timestamp every time the grafting takes place.

As discussed offline, let's keep the old date.

Copy link
Contributor Author

@rablador rablador left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 3 of 13 files reviewed, 3 unresolved discussions (waiting on @pinkisemils)


ios/MullvadVPN/RelayCacheTracker/RelayCacheTracker.swift line 122 at r1 (raw file):

Previously, rablador (Jon Petersson) wrote…

As discussed offline, let's keep the old date.

Done.

@rablador rablador force-pushed the ios-814-store-the-raw-json-payload-from-the-getrelays-api-call branch from 7017e92 to 8260467 Compare September 10, 2024 12:27
Copy link
Contributor

@buggmagnet buggmagnet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 7 of 13 files at r1, 4 of 4 files at r2, all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @pinkisemils)


ios/MullvadREST/Relay/StoredRelays.swift line 25 at r1 (raw file):

Previously, pinkisemils (Emīls Piņķis) wrote…

We should store rawData, but fail the deserialization if we cannot deserialize rawData into ServerRelyResponse during deserializaiton. We can chat about this in person.

We discussed this offline, and came to the conclusion taht we can make the init failable, create this as a let property and do the JSON parsing once when we instantiate this to avoid decoding from raw JSON every time.

@rablador rablador force-pushed the ios-814-store-the-raw-json-payload-from-the-getrelays-api-call branch from 8260467 to f620344 Compare September 10, 2024 14:26
Copy link
Contributor Author

@rablador rablador left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 9 of 13 files reviewed, 3 unresolved discussions (waiting on @buggmagnet and @pinkisemils)


ios/MullvadREST/Relay/StoredRelays.swift line 25 at r1 (raw file):

Previously, buggmagnet wrote…

We discussed this offline, and came to the conclusion taht we can make the init failable, create this as a let property and do the JSON parsing once when we instantiate this to avoid decoding from raw JSON every time.

Done.

@rablador rablador force-pushed the ios-814-store-the-raw-json-payload-from-the-getrelays-api-call branch from f620344 to 09c30d8 Compare September 10, 2024 14:35
Copy link
Collaborator

@pinkisemils pinkisemils left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 7 of 7 files at r3, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @buggmagnet)

@pinkisemils pinkisemils force-pushed the ios-814-store-the-raw-json-payload-from-the-getrelays-api-call branch from 09c30d8 to 747747e Compare September 10, 2024 20:06
Copy link
Collaborator

@pinkisemils pinkisemils left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 2 of 2 files at r4, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @buggmagnet)

Copy link
Collaborator

@pinkisemils pinkisemils left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @buggmagnet)

@pinkisemils pinkisemils merged commit 5fb205f into main Sep 10, 2024
8 of 9 checks passed
@pinkisemils pinkisemils deleted the ios-814-store-the-raw-json-payload-from-the-getrelays-api-call branch September 10, 2024 20:39
Copy link

🚨 End to end tests failed. Please check the failed workflow run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
iOS Issues related to iOS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants