[#3213] Misc. tweaks: proper upsert in Hashtag, better feature toggle management.

This commit is contained in:
Ivan Tashkinov 2021-02-23 13:52:28 +03:00
parent a98c4423f3
commit 77f3da0358
8 changed files with 30 additions and 31 deletions

View File

@ -657,6 +657,8 @@ config :pleroma, :oauth2,
config :pleroma, :database, rum_enabled: false config :pleroma, :database, rum_enabled: false
config :pleroma, :features, improved_hashtag_timeline: :auto
config :pleroma, :populate_hashtags_table, fault_rate_allowance: 0.01 config :pleroma, :populate_hashtags_table, fault_rate_allowance: 0.01
config :pleroma, :env, Mix.env() config :pleroma, :env, Mix.env()

View File

@ -461,15 +461,16 @@ config :pleroma, :config_description, [
}, },
%{ %{
group: :pleroma, group: :pleroma,
key: :database, key: :features,
type: :group, type: :group,
description: "Database-related settings", description: "Customizable features",
children: [ children: [
%{ %{
key: :improved_hashtag_timeline, key: :improved_hashtag_timeline,
type: :keyword, type: {:dropdown, :atom},
description: description:
"If `true`, hashtags will be fetched from `hashtags` table for hashtags timeline. When `false`, object-embedded hashtags will be used (slower). Is auto-set to `true` (unless overridden) when HashtagsTableMigrator completes." "Setting to force toggle / force disable improved hashtags timeline. `:enabled` forces hashtags to be fetched from `hashtags` table for hashtags timeline. `:disabled` forces object-embedded hashtags to be used (slower). Keep it `:auto` for automatic behaviour (it is auto-set to `:enabled` [unless overridden] when HashtagsTableMigrator completes).",
suggestions: [:auto, :enabled, :disabled]
} }
] ]
}, },

View File

@ -66,7 +66,7 @@ To add configuration to your config file, you can copy it from the base config.
* `password_reset_token_validity`: The time after which reset tokens aren't accepted anymore, in seconds (default: one day). * `password_reset_token_validity`: The time after which reset tokens aren't accepted anymore, in seconds (default: one day).
## :database ## :database
* `improved_hashtag_timeline`: If `true`, hashtags will be fetched from `hashtags` table for hashtags timeline. When `false`, object-embedded hashtags will be used (slower). Is auto-set to `true` (unless overridden) when `HashtagsTableMigrator` completes. * `improved_hashtag_timeline`: Setting to force toggle / force disable improved hashtags timeline. `:enabled` forces hashtags to be fetched from `hashtags` table for hashtags timeline. `:disabled` forces object-embedded hashtags to be used (slower). Keep it `:auto` for automatic behaviour (it is auto-set to `:enabled` [unless overridden] when HashtagsTableMigrator completes).
## Background migrations ## Background migrations
* `populate_hashtags_table/sleep_interval_ms`: Sleep interval between each chunk of processed records in order to decrease the load on the system (defaults to 0 and should be keep default on most instances). * `populate_hashtags_table/sleep_interval_ms`: Sleep interval between each chunk of processed records in order to decrease the load on the system (defaults to 0 and should be keep default on most instances).

View File

@ -111,4 +111,8 @@ defmodule Pleroma.Config do
end end
) )
end end
def feature_enabled?(feature_name) do
get([:features, feature_name]) not in [nil, false, :disabled, :auto]
end
end end

View File

@ -27,19 +27,15 @@ defmodule Pleroma.Hashtag do
|> String.trim() |> String.trim()
end end
def get_by_name(name) do def get_or_create_by_name(name) do
Repo.get_by(Hashtag, name: normalize_name(name)) changeset = changeset(%Hashtag{}, %{name: name})
end
def get_or_create_by_name(name) when is_bitstring(name) do Repo.insert(
with %Hashtag{} = hashtag <- get_by_name(name) do changeset,
{:ok, hashtag} on_conflict: [set: [name: get_field(changeset, :name)]],
else conflict_target: :name,
_ -> returning: true
%Hashtag{} )
|> changeset(%{name: name})
|> Repo.insert()
end
end end
def get_or_create_by_names(names) when is_list(names) do def get_or_create_by_names(names) when is_list(names) do

View File

@ -24,7 +24,7 @@ defmodule Pleroma.Migrators.HashtagsTableMigrator do
defdelegate put_stat(key, value), to: State, as: :put_data_key defdelegate put_stat(key, value), to: State, as: :put_data_key
defdelegate increment_stat(key, increment), to: State, as: :increment_data_key defdelegate increment_stat(key, increment), to: State, as: :increment_data_key
@feature_config_path [:database, :improved_hashtag_timeline] @feature_config_path [:features, :improved_hashtag_timeline]
@reg_name {:global, __MODULE__} @reg_name {:global, __MODULE__}
def whereis, do: GenServer.whereis(@reg_name) def whereis, do: GenServer.whereis(@reg_name)
@ -296,16 +296,12 @@ defmodule Pleroma.Migrators.HashtagsTableMigrator do
end end
defp on_complete(data_migration) do defp on_complete(data_migration) do
cond do if data_migration.feature_lock || feature_state() == :disabled do
data_migration.feature_lock -> Logger.warn("#{__MODULE__}: migration complete but feature is locked; consider enabling.")
:noop :noop
else
not is_nil(feature_state()) -> Config.put(@feature_config_path, :enabled)
:noop :ok
true ->
Config.put(@feature_config_path, true)
:ok
end end
end end

View File

@ -1273,7 +1273,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|> exclude_invisible_actors(opts) |> exclude_invisible_actors(opts)
|> exclude_visibility(opts) |> exclude_visibility(opts)
if Config.get([:database, :improved_hashtag_timeline]) do if Config.feature_enabled?(:improved_hashtag_timeline) do
query query
|> restrict_hashtag_any(opts) |> restrict_hashtag_any(opts)
|> restrict_hashtag_all(opts) |> restrict_hashtag_all(opts)

View File

@ -220,8 +220,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
{:ok, status_four} = CommonAPI.post(user, %{status: ". #Any1 #any2"}) {:ok, status_four} = CommonAPI.post(user, %{status: ". #Any1 #any2"})
{:ok, status_five} = CommonAPI.post(user, %{status: ". #Any2 #any1"}) {:ok, status_five} = CommonAPI.post(user, %{status: ". #Any2 #any1"})
for hashtag_timeline_strategy <- [true, false] do for hashtag_timeline_strategy <- [:eanbled, :disabled] do
clear_config([:database, :improved_hashtag_timeline], hashtag_timeline_strategy) clear_config([:features, :improved_hashtag_timeline], hashtag_timeline_strategy)
fetch_one = ActivityPub.fetch_activities([], %{type: "Create", tag: "test"}) fetch_one = ActivityPub.fetch_activities([], %{type: "Create", tag: "test"})