Skip to content

Commit

Permalink
MB-48562 Use macros instead of hard-coded bucket values
Browse files Browse the repository at this point in the history
Rather than hard-code values which could be present in more than one
file we'll use macros.

Change-Id: Ifdb13375db4b6819f9f9c810741878bf010e76d9
Reviewed-on: http://review.couchbase.org/c/ns_server/+/163503
Well-Formed: Build Bot <build@couchbase.com>
Tested-by: Steve Watanabe <steve.watanabe@couchbase.com>
Reviewed-by: Meni Hillel <meni.hillel@couchbase.com>
  • Loading branch information
stevewatanabe committed Oct 13, 2021
1 parent 4348680 commit 55f7b92
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 28 deletions.
27 changes: 27 additions & 0 deletions include/ns_bucket.hrl
@@ -0,0 +1,27 @@
%% @author Couchbase <info@couchbase.com>
%% @copyright 2021-Present Couchbase, Inc.
%%
%% Use of this software is governed by the Business Source License included
%% in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
%% in that file, in accordance with the Business Source License, use of this
%% software will be governed by the Apache License, Version 2.0, included in
%% the file licenses/APL2.txt.
%%
%% @doc Bucket related macros
%%

-define(MAGMA_FRAG_PERCENTAGE, 50).
-define(MIN_MAGMA_FRAG_PERCENTAGE, 10).
-define(MAX_MAGMA_FRAG_PERCENTAGE, 100).

-define(MAGMA_STORAGE_QUOTA_PERCENTAGE, 10).
-define(MIN_MAGMA_STORAGE_QUOTA_PERCENTAGE, 1).
-define(MAX_MAGMA_STORAGE_QUOTA_PERCENTAGE, 85).

-define(NUM_WORKER_THREADS, 3).
-define(MIN_NUM_WORKER_THREADS, 2).
-define(MAX_NUM_WORKER_THREADS, 8).

-define(MEMBASE_HT_LOCKS, 47).
-define(MAX_NUM_REPLICAS, 3).
-define(MIN_DRIFT_BEHIND_THRESHOLD, 100).
4 changes: 3 additions & 1 deletion src/compaction_daemon.erl
Expand Up @@ -12,6 +12,7 @@
-behaviour(gen_server).

-include("ns_common.hrl").
-include("ns_bucket.hrl").

%% API
-export([start_link/0,
Expand Down Expand Up @@ -120,7 +121,8 @@ global_magma_frag_percent() ->
GlobalSettings = get_autocompaction_settings(Config),
%% The default value is needed as changing a different autocompaction
%% setting zaps all the others (tracked by MB-48767).
proplists:get_value(magma_fragmentation_percentage, GlobalSettings, 50).
proplists:get_value(magma_fragmentation_percentage, GlobalSettings,
?MAGMA_FRAG_PERCENTAGE).

%% While Pid is alive prevents autocompaction of views for given
%% bucket and given node. Returned Ref can be used to uninhibit
Expand Down
9 changes: 6 additions & 3 deletions src/memcached_bucket_config.erl
Expand Up @@ -13,6 +13,7 @@
-module(memcached_bucket_config).

-include("ns_common.hrl").
-include("ns_bucket.hrl").
-include("cut.hrl").

-record(cfg, {type, name, config, snapshot, engine_config, params}).
Expand Down Expand Up @@ -43,7 +44,7 @@ params(membase, BucketName, BucketConfig, MemQuota, UUID) ->
{"alog_path", [], filename:join(DBSubDir, "access.log")},
{"data_traffic_enabled", [], false},
{"max_num_workers", maybe_restart(),
proplists:get_value(num_threads, BucketConfig, 3)},
proplists:get_value(num_threads, BucketConfig, ?NUM_WORKER_THREADS)},
{"uuid", [], UUID},
{"conflict_resolution_type", [],
ns_bucket:conflict_resolution_type(BucketConfig)},
Expand All @@ -61,7 +62,8 @@ params(membase, BucketName, BucketConfig, MemQuota, UUID) ->
compaction_daemon:global_magma_frag_percent())},
%% The internal name, known by memcached, is a ratio so do the conversion.
{"magma_mem_quota_ratio", [{reload, flush}],
proplists:get_value(storage_quota_percentage, BucketConfig, 10) / 100},
proplists:get_value(storage_quota_percentage, BucketConfig,
?MAGMA_STORAGE_QUOTA_PERCENTAGE) / 100},
{"hlc_drift_ahead_threshold_us", [no_param, {reload, vbucket}],
DriftAheadThreshold},
{"hlc_drift_behind_threshold_us", [no_param, {reload, vbucket}],
Expand All @@ -77,7 +79,8 @@ params(membase, BucketName, BucketConfig, MemQuota, UUID) ->
{"max_ttl", [{reload, flush}], proplists:get_value(max_ttl, BucketConfig)},
{"ht_locks", [], proplists:get_value(
ht_locks, BucketConfig,
misc:getenv_int("MEMBASE_HT_LOCKS", 47))},
misc:getenv_int("MEMBASE_HT_LOCKS",
?MEMBASE_HT_LOCKS))},
{"ht_size", [],
proplists:get_value(ht_size, BucketConfig,
misc:getenv_int("MEMBASE_HT_SIZE", undefined))},
Expand Down
73 changes: 49 additions & 24 deletions src/menelaus_web_buckets.erl
Expand Up @@ -17,6 +17,7 @@
-include("ns_common.hrl").
-include("couch_db.hrl").
-include("ns_stats.hrl").
-include("ns_bucket.hrl").
-include("cut.hrl").

-define(MAGMA_MIN_RAMQUOTA, 256).
Expand Down Expand Up @@ -290,7 +291,8 @@ build_dynamic_bucket_info(streaming, _Id, _BucketConfig, _) ->
[];
build_dynamic_bucket_info(InfoLevel, Id, BucketConfig, Ctx) ->
[[{replicaNumber, ns_bucket:num_replicas(BucketConfig)},
{threadsNumber, proplists:get_value(num_threads, BucketConfig, 3)},
{threadsNumber, proplists:get_value(num_threads, BucketConfig,
?NUM_WORKER_THREADS)},
{quota, {[{ram, ns_bucket:ram_quota(BucketConfig)},
{rawRAM, ns_bucket:raw_ram_quota(BucketConfig)}]}},
{basicStats, {build_bucket_stats(InfoLevel, Id, Ctx)}},
Expand Down Expand Up @@ -336,7 +338,8 @@ build_magma_bucket_info(BucketConfig) ->
BucketConfig,
Default)},
{storageQuotaPercentage,
proplists:get_value(storage_quota_percentage, BucketConfig, 10)}];
proplists:get_value(storage_quota_percentage, BucketConfig,
?MAGMA_STORAGE_QUOTA_PERCENTAGE)}];
_ ->
[]
end.
Expand Down Expand Up @@ -1448,13 +1451,16 @@ validate_with_missing(GivenValue, DefaultValue, IsNew, Fn) ->
end.

parse_validate_replicas_number(NumReplicas) ->
case menelaus_util:parse_validate_number(NumReplicas, 0, 3) of
case menelaus_util:parse_validate_number(NumReplicas, 0,
?MAX_NUM_REPLICAS) of
invalid ->
{error, replicaNumber, <<"The replica number must be specified and must be a non-negative integer.">>};
too_small ->
{error, replicaNumber, <<"The replica number cannot be negative.">>};
too_large ->
{error, replicaNumber, <<"Replica number larger than 3 is not supported.">>};
Msg = io_lib:format("Replica number larger than ~p is not "
"supported.", [?MAX_NUM_REPLICAS]),
{error, replicaNumber, iolist_to_binary(Msg)};
{ok, X} -> {ok, num_replicas, X}
end.

Expand Down Expand Up @@ -1581,13 +1587,16 @@ parse_validate_frag_percent_inner(true = _IsEnterprise, true = _IsCompat,
fun do_parse_validate_frag_percent/1).

do_parse_validate_frag_percent(Val) ->
case menelaus_util:parse_validate_number(Val, 10, 100) of
case menelaus_util:parse_validate_number(Val, ?MIN_MAGMA_FRAG_PERCENTAGE,
?MAX_MAGMA_FRAG_PERCENTAGE) of
{ok, X} ->
{ok, frag_percent, X};
_Error ->
{error, fragmentationPercentage,
<<"Fragmentation percentage must be between 10 and 100, "
"inclusive">>}
Msg = io_lib:format("Fragmentation percentage must be between ~p "
"and ~p, inclusive",
[?MIN_MAGMA_FRAG_PERCENTAGE,
?MAX_MAGMA_FRAG_PERCENTAGE]),
{error, fragmentationPercentage, iolist_to_binary(Msg)}
end.

parse_validate_storage_quota_percentage(Params, BucketConfig, IsNew, Version,
Expand Down Expand Up @@ -1645,13 +1654,17 @@ parse_validate_storage_quota_percentage_inner(true = _IsEnterprise,
fun do_parse_validate_storage_quota_percentage/1).

do_parse_validate_storage_quota_percentage(Val) ->
case menelaus_util:parse_validate_number(Val, 1, 85) of
case menelaus_util:parse_validate_number(Val,
?MIN_MAGMA_STORAGE_QUOTA_PERCENTAGE,
?MAX_MAGMA_STORAGE_QUOTA_PERCENTAGE) of
{ok, X} ->
{ok, storage_quota_percentage, X};
_Error ->
{error, storageQuotaPercentage,
<<"Storage Quota Percentage must be between 1 and 85, "
"inclusive">>}
Msg = io_lib:format("Storage Quota Percentage must be between ~p "
"and ~p, inclusive",
[?MIN_MAGMA_STORAGE_QUOTA_PERCENTAGE,
?MAX_MAGMA_STORAGE_QUOTA_PERCENTAGE]),
{error, storageQuotaPercentage, iolist_to_binary(Msg)}
end.

parse_validate_threads_number(Params, IsNew) ->
Expand All @@ -1663,16 +1676,23 @@ parse_validate_flush_enabled("1") -> {ok, flush_enabled, true};
parse_validate_flush_enabled(_ReplicaValue) -> {error, flushEnabled, <<"flushEnabled can only be 1 or 0">>}.

parse_validate_threads_number(NumThreads) ->
case menelaus_util:parse_validate_number(NumThreads, 2, 8) of
case menelaus_util:parse_validate_number(NumThreads,
?MIN_NUM_WORKER_THREADS,
?MAX_NUM_WORKER_THREADS) of
invalid ->
{error, threadsNumber,
<<"The number of threads must be an integer between 2 and 8">>};
Msg = io_lib:format("The number of threads must be an integer "
"between ~p and ~p, inclusive",
[?MIN_NUM_WORKER_THREADS,
?MAX_NUM_WORKER_THREADS]),
{error, threadsNumber, iolist_to_binary(Msg)};
too_small ->
{error, threadsNumber,
<<"The number of threads can't be less than 2">>};
Msg = io_lib:format("The number of threads can't be less than ~p",
[?MIN_NUM_WORKER_THREADS]),
{error, threadsNumber, iolist_to_binary(Msg)};
too_large ->
{error, threadsNumber,
<<"The number of threads can't be greater than 8">>};
Msg = io_lib:format("The number of threads can't be greater "
"than ~p", [?MAX_NUM_WORKER_THREADS]),
{error, threadsNumber, iolist_to_binary(Msg)};
{ok, X} ->
{ok, num_threads, X}
end.
Expand Down Expand Up @@ -1734,13 +1754,18 @@ parse_validate_drift_ahead_threshold(Threshold) ->
end.

parse_validate_drift_behind_threshold(Threshold) ->
case menelaus_util:parse_validate_number(Threshold, 100, undefined) of
case menelaus_util:parse_validate_number(Threshold,
?MIN_DRIFT_BEHIND_THRESHOLD,
undefined) of
invalid ->
{error, driftBehindThresholdMs,
<<"The drift behind threshold must be an integer not less than 100ms">>};
Msg = io_lib:format("The drift behind threshold must be an "
"integer not less than ~pms",
[?MIN_DRIFT_BEHIND_THRESHOLD]),
{error, driftBehindThresholdMs, iolist_to_binary(Msg)};
too_small ->
{error, driftBehindThresholdMs,
<<"The drift behind threshold can't be less than 100ms">>};
Msg = io_lib:format("The drift behind threshold can't be less "
"than ~pms", [?MIN_DRIFT_BEHIND_THRESHOLD]),
{error, driftBehindThresholdMs, iolist_to_binary(Msg)};
{ok, X} ->
{ok, drift_behind_threshold_ms, X}
end.
Expand Down

0 comments on commit 55f7b92

Please sign in to comment.