Created
August 13, 2010 14:07
-
-
Save shino/522942 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% Sample for mnesia fragmentation with load balancing. | |
-module(mnesia_repl_and_frag2). | |
-compile([export_all]). | |
-record(store, {key, value}). | |
primary() -> | |
net_kernel:start([localnode(1), longnames]), | |
ok = mnesia:start(), | |
ExtraNodes = lists:map(fun launch_node/1, extra_nodes()), | |
Nodes = [node() | ExtraNodes], | |
mnesia:change_config(extra_db_nodes, ExtraNodes), | |
case mnesia:create_table(store, | |
[{record_name, store}, {type, set}, | |
{frag_properties, | |
[{node_pool, Nodes}, | |
{n_ram_copies, 2}, | |
{n_fragments, 16}]}, | |
{attributes, record_info(fields, store)}]) of | |
{atomic, ok} -> | |
add_data(1, 1000), | |
{ok, created}; | |
{aborted, {already_exists,store}} -> | |
{ok, already_exists}; | |
{aborted, _Reason} -> | |
{aborted, _Reason} | |
end. | |
cleanup() -> | |
mnesia:delete_table(store), | |
lists:map(fun stop_node/1, extra_nodes()), | |
mnesia:stop(). | |
extra_nodes() -> | |
lists:seq(2, 5). | |
launch_node(Index) -> | |
{ok, Node} = case slave:start_link('127.0.0.1', Index) of | |
{ok, Node2} -> {ok, Node2}; | |
{error, {already_running, Node2}} -> {ok, Node2} | |
end, | |
ok = rpc:call(Node, mnesia, start, []), | |
ok = rpc:call(Node, mnesia, wait_for_tables, [[schema], 1000]), | |
Node. | |
stop_node(Index) -> | |
slave:stop(localnode(Index)). | |
localnode(Index) -> | |
list_to_atom(lists:flatten(io_lib:format("~B@127.0.0.1", [Index]))). | |
table_info(Item) -> | |
mnesia:activity(async_dirty, fun mnesia:table_info/2, | |
[store, Item], | |
mnesia_frag). | |
add_data(Start, Stop) -> | |
F = fun(Key) -> | |
mnesia:activity(async_dirty, fun mnesia:write/3, | |
[store, | |
#store{key = Key, value = Key}, | |
dirty_write], | |
mnesia_frag) | |
end, | |
lists:foreach(F, lists:seq(Start, Stop)), | |
ok. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment