Commit 3aa6a040 authored by Patrick Bajao's avatar Patrick Bajao

Merge branch '220386-index-trial-groups' into 'master'

Index trial groups in Elasticsearch

Closes #225744

See merge request gitlab-org/gitlab!38541
parents 392091d1 25ee81c1
# Elasticsearch for paid tiers on GitLab.com
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/220246) in GitLab 13.2
> - It's deployed behind a feature flag, disabled by default.
> - It's disabled on GitLab.com.
> - It's not recommended for use in GitLab self-managed instances.
This document describes how to enable Elasticsearch with GitLab for all paid tiers on GitLab.com. Once enabled,
all paid tiers will have access to the [Advanced Global Search feature](../../integration/elasticsearch.md) on GitLab.com.
## Enable or disable Elasticsearch for all paid tiers on GitLab.com
Since we're still in the process of rolling this out and want to control the timing this is behind a feature flag
which defaults to off.
To enable it:
```ruby
# Instance-wide
Feature.enable(:elasticsearch_index_only_paid_groups)
```
To disable it:
```ruby
# Instance-wide
Feature.disable(:elasticsearch_index_only_paid_groups)
```
......@@ -36,8 +36,8 @@ class GitlabSubscription < ApplicationRecord
[0, max_seats_used - seats].max
end
def has_a_paid_hosted_plan?
!trial? &&
def has_a_paid_hosted_plan?(include_trials: false)
(include_trials || !trial?) &&
hosted? &&
seats > 0 &&
Plan::PAID_HOSTED_PLANS.include?(plan_name)
......@@ -88,13 +88,18 @@ class GitlabSubscription < ApplicationRecord
namespace_id.present?
end
def automatically_index_in_elasticsearch?
return false unless ::Gitlab.dev_env_or_com?
return false if expired?
has_a_paid_hosted_plan?(include_trials: true)
end
# Kick off Elasticsearch indexing for paid groups with new or upgraded paid, hosted subscriptions
# Uses safe_find_or_create_by to avoid ActiveRecord::RecordNotUnique exception when upgrading from
# one paid plan to another paid plan
def index_namespace
return unless ::Feature.enabled?(:elasticsearch_index_only_paid_groups) &&
has_a_paid_hosted_plan? &&
saved_changes.key?('hosted_plan_id')
return unless automatically_index_in_elasticsearch?
ElasticsearchIndexedNamespace.safe_find_or_create_by!(namespace_id: namespace_id)
end
......
---
title: Index trial groups in Elasticsearch
merge_request: 38541
author:
type: added
......@@ -254,39 +254,71 @@ RSpec.describe GitlabSubscription do
describe 'callbacks' do
context 'after_commit :index_namespace' do
where(:elasticsearch_index_only_paid_groups_setting, :operation, :initial_plan, :should_update_plan?, :should_index_namespace?) do
false | :create | :bronze | false | false
true | :create | :bronze | false | true
true | :create | :free | false | false
true | :create | { trial: true } | false | false
false | :update | :bronze | false | false
true | :update | :bronze | false | false
true | :update | :bronze | true | true
true | :update | :free | true | true
true | :update | { trial: true } | true | true
let(:gitlab_subscription) { build(:gitlab_subscription, plan) }
let(:dev_env_or_com) { true }
let(:expiration_date) { Date.today + 10 }
let(:plan) { :bronze }
before do
allow(::Gitlab).to receive(:dev_env_or_com?).and_return(dev_env_or_com)
gitlab_subscription.end_date = expiration_date
end
it 'indexes the namespace' do
expect(ElasticsearchIndexedNamespace).to receive(:safe_find_or_create_by!).with(namespace_id: gitlab_subscription.namespace_id)
gitlab_subscription.save!
end
context 'when it is a trial' do
let(:gitlab_subscription) { build(:gitlab_subscription, :active_trial) }
it 'indexes the namespace' do
expect(ElasticsearchIndexedNamespace).to receive(:safe_find_or_create_by!).with(namespace_id: gitlab_subscription.namespace_id)
gitlab_subscription.save!
end
end
context 'when not ::Gitlab.dev_env_or_com?' do
let(:dev_env_or_com) { false }
it 'does not index the namespace' do
expect(ElasticsearchIndexedNamespace).not_to receive(:safe_find_or_create_by!)
gitlab_subscription.save!
end
end
with_them do
let!(:gitlab_subscription) { operation == :create ? build(:gitlab_subscription, initial_plan) : create(:gitlab_subscription, initial_plan) }
context 'when the plan has expired' do
let(:expiration_date) { Date.today - 8.days }
it 'does not index the namespace' do
expect(ElasticsearchIndexedNamespace).not_to receive(:safe_find_or_create_by!)
gitlab_subscription.save!
end
end
context 'when it is not a hosted plan' do
before do
stub_feature_flags(elasticsearch_index_only_paid_groups: elasticsearch_index_only_paid_groups_setting)
gitlab_subscription.namespace_id = nil
end
it 'indexes namespace' do
if should_index_namespace?
expect(ElasticsearchIndexedNamespace).to receive(:safe_find_or_create_by!).with(namespace_id: gitlab_subscription.namespace_id)
else
expect(ElasticsearchIndexedNamespace).not_to receive(:safe_find_or_create_by!)
end
it 'does not index anything' do
expect(ElasticsearchIndexedNamespace).not_to receive(:safe_find_or_create_by!)
case operation
when :create
gitlab_subscription.save!
when :update
update_attributes = should_update_plan? ? { hosted_plan: create(:silver_plan), trial: false } : { max_seats_used: 32 }
gitlab_subscription.update!(update_attributes)
end
gitlab_subscription.save!
end
end
context 'when it is a free plan' do
let(:plan) { :free }
it 'does not index the namespace' do
expect(ElasticsearchIndexedNamespace).not_to receive(:safe_find_or_create_by!)
gitlab_subscription.save!
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment