<commit_before>%% @author Northscale <<EMAIL>>
%% @copyright 2009 NorthScale, Inc.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%      http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% Monitor and maintain the vbucket layout of each bucket.
%%
-module(ns_janitor).

-include("ns_common.hrl").

-export([cleanup/1, current_states/2, graphviz/1]).


-spec cleanup(string()) -> ok.
cleanup(Bucket) ->
    {Map, Servers} =
        case ns_bucket:config(Bucket) of
            {NumReplicas, NumVBuckets, _, []} ->
                S = ns_cluster_membership:active_nodes(),
                M = ns_rebalancer:generate_initial_map(NumReplicas, NumVBuckets,
                                                       S),
                ns_bucket:set_servers(Bucket, S),
                ns_bucket:set_map(Bucket, M),
                {M, S};
            {_, _, M, S} ->
                {M, S}
        end,
    case Servers of
        [] -> ok;
        _ ->
            case wait_for_memcached(Servers, Bucket, 5) of
                [] ->
                    case sanify(Bucket, Map, Servers) of
                        Map -> ok;
                        Map1 ->
                            ns_bucket:set_map(Bucket, Map1)
                    end,
                    Replicas = lists:keysort(1, ns_bucket:map_to_replicas(Map)),
                    ReplicaGroups = lists:ukeymerge(1, misc:keygroup(1, Replicas),
                                                    [{N, []} || N <- lists:sort(Servers)]),
                    NodesReplicas = lists:map(fun ({Src, R}) -> % R is the replicas for this node
                                                      {Src, [{V, Dst} || {_, Dst, V} <- R]}
                                              end, ReplicaGroups),
                    ns_vbm_sup:set_replicas(Bucket, NodesReplicas);
                Down ->
                    ?log_error("Bucket ~p not yet ready on ~p", [Bucket, Down])
            end
    end.


state_color(active) ->
    "color=green";
state_color(pending) ->
    "color=blue";
state_color(replica) ->
    "color=yellow";
state_color(dead) ->
    "color=red".


-spec node_vbuckets(non_neg_integer(), node(), [{node(), vbucket_id(),
                                                 vbucket_state()}], map()) ->
                           iolist().
node_vbuckets(I, Node, States, Map) ->
    GState = lists:keysort(1,<commit_msg>use new sanified vbucket map when setting up replicas. MB-3442 [refresh]<commit_after>%% @author Northscale <<EMAIL>>
%% @copyright 2009 NorthScale, Inc.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%      http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% Monitor and maintain the vbucket layout of each bucket.
%%
-module(ns_janitor).

-include("ns_common.hrl").

-export([cleanup/1, current_states/2, graphviz/1]).


-spec cleanup(string()) -> ok.
cleanup(Bucket) ->
    {Map, Servers} =
        case ns_bucket:config(Bucket) of
            {NumReplicas, NumVBuckets, _, []} ->
                S = ns_cluster_membership:active_nodes(),
                M = ns_rebalancer:generate_initial_map(NumReplicas, NumVBuckets,
                                                       S),
                ns_bucket:set_servers(Bucket, S),
                ns_bucket:set_map(Bucket, M),
                {M, S};
            {_, _, M, S} ->
                {M, S}
        end,
    case Servers of
        [] -> ok;
        _ ->
            case wait_for_memcached(Servers, Bucket, 5) of
                [] ->
                    Map1 = case sanify(Bucket, Map, Servers) of
                               Map -> Map;
                               MapNew ->
                                   ns_bucket:set_map(Bucket, MapNew),
                                   MapNew
                           end,
                    Replicas = lists:keysort(1, ns_bucket:map_to_replicas(Map1)),
                    ReplicaGroups = lists:ukeymerge(1, misc:keygroup(1, Replicas),
                                                    [{N, []} || N <- lists:sort(Servers)]),
                    NodesReplicas = lists:map(fun ({Src, R}) -> % R is the replicas for this node
                                                      {Src, [{V, Dst} || {_, Dst, V} <- R]}
                                              end, ReplicaGroups),
                    ns_vbm_sup:set_replicas(Bucket, NodesReplicas);
                Down ->
                    ?log_error("Bucket ~p not yet ready on ~p", [Bucket, Down])
            end
    end.


state_color(active) ->
    "color=green";
state_color(pending) ->
    "color=blue";
state_color(replica) ->
    "color=yellow";
state_color(dead) ->
    "color=red".


-spec node_vbuckets(non_neg_integer(), node(), [{node(), vbucket_id(),
                                                 vbucket_state()}], map()) ->
                           iolist().
node_vbuckets(I, Node, States, Map) ->
    GState = lists:keysort(1,
{'fraction_non_alphanumeric': 0.11811158798283262, 'fraction_numerical': 0.0068669527896995704, 'mean_word_length': 1.632625395390872, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '465706', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1576, 'n_words': 437}