You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mastodon/db/migrate
aschmitz 468523f4ad Non-Serial ("Snowflake") IDs (#4801)
* Use non-serial IDs

This change makes a number of nontrivial tweaks to the data model in
Mastodon:

* All IDs are now 8 byte integers (rather than mixed 4- and 8-byte)
* IDs are now assigned as:
  * Top 6 bytes: millisecond-resolution time from epoch
  * Bottom 2 bytes: serial (within the millisecond) sequence number
  * See /lib/tasks/db.rake's `define_timestamp_id` for details, but
    note that the purpose of these changes is to make it difficult to
    determine the number of objects in a table from the ID of any
    object.
* The Redis sorted set used for the feed will have values used to look
  up toots, rather than scores. This is almost always the same as the
  existing behavior, except in the case of boosted toots. This change
  was made because Redis stores scores as double-precision floats,
  which cannot store the new ID format exactly. Note that this doesn't
  cause problems with sorting/pagination, because ZREVRANGEBYSCORE
  sorts lexicographically when scores are tied. (This will still cause
  sorting issues when the ID gains a new significant digit, but that's
  extraordinarily uncommon.)

Note a couple of tradeoffs have been made in this commit:

* lib/tasks/db.rake is used to enforce many/most column constraints,
  because this commit seems likely to take a while to bring upstream.
  Enforcing a post-migrate hook is an easier way to maintain the code
  in the interim.
* Boosted toots will appear in the timeline as many times as they have
  been boosted. This is a tradeoff due to the way the feed is saved in
  Redis at the moment, but will be handled by a future commit.

This would effectively close Mastodon's #1059, as it is a
snowflake-like system of generating IDs. However, given how involved
the changes were simply within Mastodon, it may have unexpected
interactions with some clients, if they store IDs as doubles
(or as 4-byte integers). This was a problem that Twitter ran into with
their "snowflake" transition, particularly in JavaScript clients that
treated IDs as JS integers, rather than strings. It therefore would be
useful to test these changes at least in the web interface and popular
clients before pushing them to all users.

* Fix JavaScript interface with long IDs

Somewhat predictably, the JS interface handled IDs as numbers, which in
JS are IEEE double-precision floats. This loses some precision when
working with numbers as large as those generated by the new ID scheme,
so we instead handle them here as strings. This is relatively simple,
and doesn't appear to have caused any problems, but should definitely
be tested more thoroughly than the built-in tests. Several days of use
appear to support this working properly.

BREAKING CHANGE:

The major(!) change here is that IDs are now returned as strings by the
REST endpoints, rather than as integers. In practice, relatively few
changes were required to make the existing JS UI work with this change,
but it will likely hit API clients pretty hard: it's an entirely
different type to consume. (The one API client I tested, Tusky, handles
this with no problems, however.)

Twitter ran into this issue when introducing Snowflake IDs, and decided
to instead introduce an `id_str` field in JSON responses. I have opted
to *not* do that, and instead force all IDs to 64-bit integers
represented by strings in one go. (I believe Twitter exacerbated their
problem by rolling out the changes three times: once for statuses, once
for DMs, and once for user IDs, as well as by leaving an integer ID
value in JSON. As they said, "If you’re using the `id` field with JSON
in a Javascript-related language, there is a very high likelihood that
the integers will be silently munged by Javascript interpreters. In most
cases, this will result in behavior such as being unable to load or
delete a specific direct message, because the ID you're sending to the
API is different than the actual identifier associated with the
message." [1]) However, given that this is a significant change for API
users, alternatives or a transition time may be appropriate.

1: https://blog.twitter.com/developer/en_us/a/2011/direct-messages-going-snowflake-on-sep-30-2011.html

* Restructure feed pushes/unpushes

This was necessary because the previous behavior used Redis zset scores
to identify statuses, but those are IEEE double-precision floats, so we
can't actually use them to identify all 64-bit IDs. However, it leaves
the code in a much better state for refactoring reblog handling /
coalescing.

Feed-management code has been consolidated in FeedManager, including:

* BatchedRemoveStatusService no longer directly manipulates feed zsets
* RemoveStatusService no longer directly manipulates feed zsets
* PrecomputeFeedService has moved its logic to FeedManager#populate_feed

(PrecomputeFeedService largely made lots of calls to FeedManager, but
didn't follow the normal adding-to-feed process.)

This has the effect of unifying all of the feed push/unpush logic in
FeedManager, making it much more tractable to update it in the future.

Due to some additional checks that must be made during, for example,
batch status removals, some Redis pipelining has been removed. It does
not appear that this should cause significantly increased load, but if
necessary, some optimizations are possible in batch cases. These were
omitted in the pursuit of simplicity, but a batch_push and batch_unpush
would be possible in the future.

Tests were added to verify that pushes happen under expected conditions,
and to verify reblog behavior (both on pushing and unpushing). In the
case of unpushing, this includes testing behavior that currently leads
to confusion such as Mastodon's #2817, but this codifies that the
behavior is currently expected.

* Rubocop fixes

I could swear I made these changes already, but I must have lost them
somewhere along the line.

* Address review comments

This addresses the first two comments from review of this feature:

https://github.com/tootsuite/mastodon/pull/4801#discussion_r139336735
https://github.com/tootsuite/mastodon/pull/4801#discussion_r139336931

This adds an optional argument to FeedManager#key, the subtype of feed
key to generate. It also tests to ensure that FeedManager's settings are
such that reblogs won't be tracked forever.

* Hardcode IdToBigints migration columns

This addresses a comment during review:
https://github.com/tootsuite/mastodon/pull/4801#discussion_r139337452

This means we'll need to make sure that all _id columns going forward
are bigints, but that should happen automatically in most cases.

* Additional fixes for stringified IDs in JSON

These should be the last two. These were identified using eslint to try
to identify any plain casts to JavaScript numbers. (Some such casts are
legitimate, but these were not.)

Adding the following to .eslintrc.yml will identify casts to numbers:

~~~
  no-restricted-syntax:
  - warn
  - selector: UnaryExpression[operator='+'] > :not(Literal)
    message: Avoid the use of unary +
  - selector: CallExpression[callee.name='Number']
    message: Casting with Number() may coerce string IDs to numbers
~~~

The remaining three casts appear legitimate: two casts to array indices,
one in a server to turn an environment variable into a number.

* Only implement timestamp IDs for Status IDs

Per discussion in #4801, this is only being merged in for Status IDs at
this point. We do this in a migration, as there is no longer use for
a post-migration hook. We keep the initialization of the timestamp_id
function as a Rake task, as it is also needed after db:schema:load (as
db/schema.rb doesn't store Postgres functions).

* Change internal streaming payloads to stringified IDs as well

This is equivalent to 591a9af356 from
#5019, with an extra change for the addition to FeedManager#unpush.

* Ensure we have a status_id_seq sequence

Apparently this is not a given when specifying a custom ID function,
so now we ensure it gets created. This uses the generic version of this
function to more easily support adding additional tables with timestamp
IDs in the future, although it would be possible to cut this down to a
less generic version if necessary. It is only run during db:schema:load
or the relevant migration, so the overhead is extraordinarily minimal.

* Transition reblogs to new Redis format

This provides a one-way migration to transition old Redis reblog entries
into the new format, with a separate tracking entry for reblogs.

It is not invertible because doing so could (if timestamp IDs are used)
require a database query for each status in each users' feed, which is
likely to be a significant toll on major instances.

* Address review comments from @akihikodaki

No functional changes.

* Additional review changes

* Heredoc cleanup

* Run db:schema:load hooks for test in development

This matches the behavior in Rails'
ActiveRecord::Tasks::DatabaseTasks.each_current_configuration, which
would otherwise break `rake db:setup` in development.

It also moves some functionality out to a library, which will be a good
place to put additional related functionality in the near future.
7 years ago
..
20160220174730_create_accounts.rb Add migration versions (#3574) 7 years ago
20160220211917_create_statuses.rb Add migration versions (#3574) 7 years ago
20160221003140_create_users.rb Add migration versions (#3574) 7 years ago
20160221003621_create_follows.rb Add migration versions (#3574) 7 years ago
20160222122600_create_stream_entries.rb Add migration versions (#3574) 7 years ago
20160222143943_add_profile_fields_to_accounts.rb Add migration versions (#3574) 7 years ago
20160223162837_add_metadata_to_statuses.rb Add migration versions (#3574) 7 years ago
20160223164502_make_uris_nullable_in_statuses.rb Add migration versions (#3574) 7 years ago
20160223165723_add_url_to_statuses.rb Add migration versions (#3574) 7 years ago
20160223165855_add_url_to_accounts.rb Add migration versions (#3574) 7 years ago
20160223171800_create_favourites.rb Add migration versions (#3574) 7 years ago
20160224223247_create_mentions.rb Add migration versions (#3574) 7 years ago
20160227230233_add_attachment_avatar_to_accounts.rb Add migration versions (#3574) 7 years ago
20160305115639_add_devise_to_users.rb Add migration versions (#3574) 7 years ago
20160306172223_create_doorkeeper_tables.rb Add migration versions (#3574) 7 years ago
20160312193225_add_attachment_header_to_accounts.rb Add migration versions (#3574) 7 years ago
20160314164231_add_owner_to_application.rb Add migration versions (#3574) 7 years ago
20160316103650_add_missing_indices.rb Add migration versions (#3574) 7 years ago
20160322193748_add_avatar_remote_url_to_accounts.rb Add migration versions (#3574) 7 years ago
20160325130944_add_admin_to_users.rb Add migration versions (#3574) 7 years ago
20160826155805_add_superapp_to_oauth_applications.rb
20160905150353_create_media_attachments.rb
20160919221059_add_subscription_expires_at_to_accounts.rb
20160920003904_remove_verify_token_from_accounts.rb
20160926213048_remove_owner_from_application.rb
20161003142332_add_confirmable_to_users.rb
20161003145426_create_blocks.rb
20161006213403_rails_settings_migration.rb Add migration versions (#3574) 7 years ago
20161009120834_create_domain_blocks.rb
20161027172456_add_silenced_to_accounts.rb
20161104173623_create_tags.rb
20161105130633_create_statuses_tags_join_table.rb Adding hashtags 8 years ago
20161116162355_add_locale_to_users.rb Add user locale setting 8 years ago
20161119211120_create_notifications.rb Adding notifications column 8 years ago
20161122163057_remove_unneeded_indexes.rb Remove unneeded indices, improve error handling in background workers, don't needlessly reload reblogged status, send Devise e-mails asynchronously 8 years ago
20161123093447_add_sensitive_to_statuses.rb Adding sensitive marker to statuses in API 8 years ago
20161128103007_create_subscriptions.rb Adding embedded PuSH server 8 years ago
20161130142058_add_last_successful_delivery_at_to_subscriptions.rb Track successful PuSH deliveries 8 years ago
20161130185319_add_visibility_to_statuses.rb Per-status control for unlisted mode, also federation for unlisted mode 8 years ago
20161202132159_add_in_reply_to_account_id_to_statuses.rb Make data migration more merciful 8 years ago
20161203164520_add_from_account_id_to_notifications.rb Keep timelines in the UI trimmed when possible 8 years ago
20161205214545_add_suspended_to_accounts.rb Add account suspension 8 years ago
20161221152630_add_hidden_to_stream_entries.rb Private visibility on statuses prevents non-followers from seeing those 8 years ago
20161222201034_add_locked_to_accounts.rb Add "locked" flag to accounts, prevent blocked users from following, force-unfollow blocked users 8 years ago
20161222204147_create_follow_requests.rb Follow call on locked account creates follow request instead 8 years ago
20170105224407_add_shortcode_to_media_attachments.rb Fix #416 - Generate random unique 14-byte (19 characters) shortcodes 8 years ago
20170109120109_create_web_settings.rb Persist UI settings, add missing localizations for German 8 years ago
20170112154826_migrate_settings.rb Add migration versions (#3574) 7 years ago
20170114194937_add_application_to_statuses.rb Add tracking of OAuth app that posted a status, extend OAuth apps to have optional website field, add application details to API, show application name and website on detailed status views. Resolves #11 8 years ago
20170114203041_add_website_to_oauth_application.rb Add tracking of OAuth app that posted a status, extend OAuth apps to have optional website field, add application details to API, show application name and website on detailed status views. Resolves #11 8 years ago
20170119214911_create_preview_cards.rb Fix #463 - Fetch and display previews of URLs using OpenGraph tags 8 years ago
20170123162658_add_severity_to_domain_blocks.rb Domain blocks now have varying severity - auto-suspend vs auto-silence 8 years ago
20170123203248_add_reject_media_to_domain_blocks.rb Optional domain block attribute that prevents media attachments from being downloaded 8 years ago
20170125145934_add_spoiler_text_to_statuses.rb Fix spoiler_text not having "not null" constraint 8 years ago
20170127165745_add_devise_two_factor_to_users.rb Added optional two-factor authentication 8 years ago
20170129000348_create_devices.rb API for apps to register for push notifications 8 years ago
20170205175257_remove_devices.rb Fix #61 - Add list of blocked users to the UI; clean up failed push notifications API 8 years ago
20170209184350_add_reply_to_statuses.rb Fix #614 - extra reply-boolean on statuses to account for cases when replied-to 8 years ago
20170214110202_create_reports.rb Adding POST /api/v1/reports API, and a UI for submitting reports 8 years ago
20170217012631_add_reblog_of_id_foreign_key_to_statuses.rb Add foreign key to prevent reblogs of non-existent (after race conditions) statuses from happening 8 years ago
20170301222600_create_mutes.rb Feature conversations muting (#3017) 8 years ago
20170303212857_add_last_emailed_at_to_users.rb Add digest e-mails 8 years ago
20170304202101_add_type_to_media_attachments.rb Fix #431 - convert gif to webm during upload. Web UI treats them like it did 8 years ago
20170317193015_add_search_index_to_accounts.rb Make account search blazing fast and rank followers/followees higher in the results 8 years ago
20170318214217_add_header_remote_url_to_accounts.rb Federate header images, fix open-uri http->https redirection error 8 years ago
20170322021028_add_lowercase_index_to_accounts.rb More efficient single account retrieval (0.9ms vs 50ms before) 8 years ago
20170322143850_change_primary_key_to_bigint_on_statuses.rb Upgrade status IDs to bigint 8 years ago
20170322162804_add_search_index_to_tags.rb Fix full-text search query quotation, improve tag search performance with an index, 8 years ago
20170330021336_add_counter_caches.rb Fix up null values on latest migration, add notes 8 years ago
20170330163835_create_imports.rb Import feature for following/blocking lists (addresses #62, #177, #201, #454) 8 years ago
20170330164118_add_attachment_data_to_imports.rb Add migration versions (#3574) 7 years ago
20170403172249_add_action_taken_by_account_id_to_reports.rb Add basic logging of who resolved report 8 years ago
20170405112956_add_index_on_mentions_status_id.rb Add index on mentions status_id 8 years ago
20170406215816_add_notifications_and_favourites_indices.rb Low-hanging fruit of query optimization, these indices were missing 8 years ago
20170409170753_add_last_webfingered_at_to_accounts.rb Refresh webfinger (#1323) 8 years ago
20170414080609_add_devise_two_factor_backupable_to_users.rb Add recovery code support for two-factor auth (#1773) 8 years ago
20170414132105_add_language_to_statuses.rb Add language detection (#1772) 8 years ago
20170418160728_add_indexes_to_reports_for_accounts.rb Admin reports with accounts (#2092) 8 years ago
20170423005413_add_allowed_languages_to_user.rb Filter on allowed user language preferences (#2361) 8 years ago
20170424003227_create_account_domain_blocks.rb Account domain blocks (#2381) 8 years ago
20170424112722_add_status_id_index_to_statuses_tags.rb Add index to statuses_tags#status_id (#2394) 8 years ago
20170425131920_add_media_attachment_meta.rb Add media dimensions (#2448) 8 years ago
20170425202925_add_oembed_to_preview_cards.rb OEmbed support for PreviewCard (#2337) 8 years ago
20170427011934_re_add_owner_to_application.rb Application prefs section (#2758) 7 years ago
20170506235850_create_conversations.rb Add conversation model, <ostatus:conversation /> (#3016) 8 years ago
20170507000211_add_conversation_id_to_statuses.rb Add conversation model, <ostatus:conversation /> (#3016) 8 years ago
20170507141759_optimize_index_subscriptions.rb Add effective index to subscriptions (#2885) 8 years ago
20170508230434_create_conversation_mutes.rb Feature conversations muting (#3017) 8 years ago
20170516072309_add_index_accounts_on_uri.rb Make faster ProcessFeedService (#3080) 8 years ago
20170520145338_change_language_filter_to_opt_out.rb Filter languages with opt out (#3175) 8 years ago
20170601210557_add_index_on_media_attachments_account_id.rb Add index to media_attachments.account_id (#3510) 7 years ago
20170604144747_add_foreign_keys_for_accounts.rb Fix #3550 - Add all missing foreign keys (#3562) 7 years ago
20170606113804_change_tag_search_index_to_btree.rb Fix tag search order and not to use tsvector (#3611) 7 years ago
20170609145826_remove_default_language_from_statuses.rb Language detection defaults to nil (#3666) 7 years ago
20170610000000_add_statuses_index_on_account_id_id.rb Add index statuses on account_id and id (#3895) 7 years ago
20170623152212_create_session_activations.rb Revocable sessions (#3616) 7 years ago
20170624134742_add_description_to_session_activations.rb Add overview of active sessions (#3929) 7 years ago
20170625140443_add_access_token_id_to_session_activations.rb Bind web UI access tokens to sessions (#3940) 7 years ago
20170711225116_fix_null_booleans.rb Fix boolean columns sometimes having a null value (#4162) 7 years ago
20170713112503_make_tag_search_case_insensitive.rb Make tag search case insensitive again (#4184) 7 years ago
20170713175513_create_web_push_subscriptions.rb Web Push Notifications (#3243) 7 years ago
20170713190709_add_web_push_subscription_to_session_activations.rb Web Push Notifications (#3243) 7 years ago
20170714184731_add_domain_to_subscriptions.rb Fix #2672 - Connect signed PuSH subscription requests to instance domain (#4205) 7 years ago
20170718211102_add_activitypub_to_accounts.rb Add ActivityPub attributes to accounts (#4273) 7 years ago
20170720000000_add_index_favourites_on_account_id_and_id.rb Add index favourites on account_id and id (#4360) 7 years ago
20170823162448_create_status_pins.rb Pinned statuses (#4675) 7 years ago
20170824103029_add_timestamps_to_status_pins.rb Allow multiple pinned statuses to be shown and make them be ordered b… (#4690) 7 years ago
20170829215220_remove_status_pins_account_index.rb Remove unneccesary indices (#4738) 7 years ago
20170901141119_truncate_preview_cards.rb Make PreviewCard records reuseable between statuses (#4642) 7 years ago
20170901142658_create_join_table_preview_cards_statuses.rb Make PreviewCard records reuseable between statuses (#4642) 7 years ago
20170905044538_add_index_id_account_id_activity_type_on_notifications.rb add index_notifications_on_id_and_account_id_and_activity_type on notifications table (#4750) 7 years ago
20170905165803_add_local_to_statuses.rb Fix locking migration on statuses table. Nullable column and NO default value (#4825) 7 years ago
20170913000752_create_site_uploads.rb Uploads for admin site settings (#4913) 7 years ago
20170917153509_create_custom_emojis.rb Custom emoji (#4988) 7 years ago
20170918125918_ids_to_bigints.rb Make IdsToBigints (mostly!) non-blocking (#5088) 7 years ago
20170920024819_status_ids_to_timestamp_ids.rb Non-Serial ("Snowflake") IDs (#4801) 7 years ago
20170920032311_fix_reblogs_in_feeds.rb Non-Serial ("Snowflake") IDs (#4801) 7 years ago
20170924022025_ids_to_bigints2.rb Forgotten in #5039, change statuses_tags tag_id to bigint (#5070) 7 years ago
20170927215609_add_description_to_media_attachments.rb Add ability to specify alternative text for media attachments (#5123) 7 years ago