this repo has no description
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Remove dead code from Utils

+4 -78
+2 -2
lib/servers/storage.ex
··· 404 404 tag = state.id 405 405 start_version = state.data_version + 1 406 406 407 - buddy_server = Map.get(state.cluster.servers, buddy_tlog_id(generation, state.id)) 407 + buddy_server = Map.get(state.cluster.servers, buddy_tlog_id(generation.tlog_ids, state.id)) 408 408 409 409 case state.buddy_tlog_failed or is_nil(buddy_server) do 410 410 true -> ··· 431 431 %TLogGeneration{} = generation = current_tlog_generation(state) 432 432 assert result.tlog_id in generation.tlog_ids 433 433 434 - case result.tlog_id == buddy_tlog_id(generation, state.id) do 434 + case result.tlog_id == buddy_tlog_id(generation.tlog_ids, state.id) do 435 435 true -> 436 436 apply_buddy_peek_result(state, result) 437 437
+2 -53
lib/utils.ex
··· 3 3 Hobbes utils. 4 4 """ 5 5 6 - alias Hobbes.Structs.{Cluster, TLogGeneration, Server} 6 + alias Hobbes.Structs.{Cluster, Server} 7 7 alias Hobbes.Construct.SimServer 8 8 9 9 @type mutation :: {:write, binary, binary} | {:clear, binary} | {:clear_range, binary, binary} ··· 119 119 end 120 120 end 121 121 122 - @doc """ 123 - Returns the preferred tlog index for `tag`. 124 - """ 125 - @spec preferred_tlog_i(pos_integer, integer) :: integer 126 - def preferred_tlog_i(num_tlogs, tag) when is_integer(num_tlogs) and num_tlogs > 0 and is_integer(tag) do 127 - rem(tag, num_tlogs) 128 - end 129 - 130 - @spec buddy_tlog_id(TLogGeneration.t | [non_neg_integer], non_neg_integer) :: non_neg_integer 131 - def buddy_tlog_id(%TLogGeneration{} = generation, server_id), do: buddy_tlog_id(generation.tlog_ids, server_id) 132 - 122 + @spec buddy_tlog_id([non_neg_integer], non_neg_integer) :: non_neg_integer 133 123 def buddy_tlog_id(tlog_ids, server_id) when is_list(tlog_ids) and is_integer(server_id) do 134 124 index = rem(server_id, length(tlog_ids)) 135 125 Enum.at(tlog_ids, index) 136 - end 137 - 138 - @spec tlogs_for_tags(pos_integer, pos_integer, [integer]) :: [non_neg_integer] 139 - def tlogs_for_tags(num_tlogs, num_replicas, tags) 140 - when is_integer(num_tlogs) and is_integer(num_replicas) and (num_tlogs >= num_replicas) and is_list(tags) do 141 - # TODO: this function is called for literally every mutation - can we optimize it? 142 - tlogs = Enum.map(tags, fn t -> rem(t, num_tlogs) end) |> Enum.uniq() 143 - missing = num_replicas - length(tlogs) 144 - case missing <= 0 do 145 - true -> 146 - tlogs 147 - false -> 148 - all_tlogs = Enum.to_list(0..(num_tlogs - 1)) 149 - Enum.reduce(1..missing, tlogs, fn _i, acc -> 150 - choice = SimServer.deterministic_random(all_tlogs -- acc) 151 - [choice | acc] 152 - end) 153 - end 154 - end 155 - 156 - defp tlog_ids_for_tags(tlog_ids, tags, replication_factor) do 157 - # TODO: refactor tlogs_for_tags to do this 158 - tlogs_for_tags(length(tlog_ids), replication_factor, tags) 159 - |> Enum.map(&Enum.at(tlog_ids, &1)) 160 - end 161 - 162 - def slice_mutations_for_tlogs(tagged_mutations, all_tlog_ids, replication_factor) 163 - when is_list(tagged_mutations) and is_list(all_tlog_ids) and is_integer(replication_factor) and replication_factor >= 1 do 164 - tlog_mutations = Map.new(all_tlog_ids, fn id -> {id, []} end) 165 - 166 - tagged_mutations 167 - |> Enum.reduce(tlog_mutations, fn {tags, _mut} = tm, acc -> 168 - case meta_tag() in tags do 169 - true -> all_tlog_ids 170 - false -> tlog_ids_for_tags(all_tlog_ids, tags, replication_factor) 171 - end 172 - |> Enum.reduce(acc, fn id, acc -> 173 - Map.update!(acc, id, &[tm | &1]) 174 - end) 175 - end) 176 - |> Map.new(fn {id, mutations} -> {id, Enum.reverse(mutations)} end) 177 126 end 178 127 179 128 @doc """
-23
test/utils_test.exs
··· 31 31 end 32 32 end 33 33 34 - describe "tlogs_for_tags/3" do 35 - test "returns tlogs" do 36 - assert [1, 2, 3] = Utils.tlogs_for_tags(10, 3, [101, 102, 103]) 37 - assert [_, 0, 1] = l1 = Utils.tlogs_for_tags(10, 3, [100, 200, 201]) 38 - assert length(Enum.uniq(l1)) == 3 39 - end 40 - 41 - test "handles large number of collisions" do 42 - # All 100 tags will have the same preferred tlog, so the function must 43 - # choose the other 99 tlogs randomly, one by one 44 - tags = Enum.map(1..100, fn i -> i * 100 end) 45 - tlogs = Utils.tlogs_for_tags(100, 100, tags) 46 - 47 - assert length(Enum.uniq(tlogs)) == 100 48 - end 49 - 50 - test "raises if there are not enough tlogs" do 51 - assert_raise FunctionClauseError, fn -> 52 - Utils.tlogs_for_tags(9, 10, [1, 2, 3]) 53 - end 54 - end 55 - end 56 - 57 34 describe "merge_pairs/2" do 58 35 test "merges" do 59 36 l1 = [