Erlang数据库服务器nodedown错误,无法通过数据库逻辑获取mnesia数据库数据

jdgnovmf  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(211)

我正在使用聊天服务器和数据库逻辑实现Erlang/OTP基本聊天应用程序(对于Mnesia)。我需要一个数据库服务器作为中间人。这个聊天服务器数据库都是分布式。这样,我就可以产生新的聊天用户我可以为每个可用的用户节点初始化数据库,db表将被复制到每个节点。**我可以通过数据库逻辑代码访问数据。**但不能使用数据库服务器进行任何数据库操作。我启动了db服务器,输出:

(john@DESKTOP-RD414DV)7> database_server:start_link([node()|nodes()]).
{local,database_server} (<0.129.0>) starting...

这是数据库逻辑代码

-export([install/1, get_db/1, get_all_dbe/1, delete_db/1, store_db/4]).
-include_lib("stdlib/include/qlc.hrl").
-record(userDetails, {node,username, location, gender}).
%%initialize database
install(Nodes) ->
  ok = mnesia:create_schema(Nodes),
  rpc:multicall(Nodes, application, start, [mnesia]),
  try
      mnesia:table_info(type,userDetails)
  catch
      exit:_  ->
        mnesia:create_table(userDetails, [{attributes, record_info(fields, userDetails)},
          {type, bag},
          {disc_copies, Nodes}])
  end.

store_db(Node, Username, Location, Gender) ->
  F = fun() ->
    mnesia:write(#userDetails{node =Node, username = Username, location = Location, gender = Gender})
      end,
  mnesia:transaction(F).

get_db(Node) ->
  F = fun() ->
    Query = qlc:q([X || X <- mnesia:table(userDetails),
      X#userDetails.node =:= Node]),
    Results = qlc:e(Query),
    lists:map(fun(Item) -> Item#userDetails.username end, Results)
      end,
  mnesia:transaction(F).

get_all_dbe(Node) ->
  F = fun() ->
    Query = qlc:q([X || X <- mnesia:table(userDetails),
      X#userDetails.node =:= Node]),
    Results = qlc:e(Query),
    lists:map(fun(Item) -> {Item#userDetails.username, Item#userDetails.location, Item#userDetails.gender} end, Results)
      end,
  mnesia:transaction(F).

delete_db(Node) ->
  F = fun() ->
    Query = qlc:q([X || X <- mnesia:table(userDetails),
      X#userDetails.node =:= Node]),
    Results = qlc:e(Query),

    FF = fun() ->
      lists:foreach(fun(Result) -> mnesia:delete_object(Result) end, Results)
         end,
    mnesia:transaction(FF)
      end,
  mnesia:transaction(F).

这是数据库服务器代码:

-behaviour(gen_server).

-export([start_link/1, store/4, getalldb/1, delete/1]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
  code_change/3]).

-define(SERVER, ?MODULE).

-record(database_server_state, {}).
%%%===================================================================
%%% Spawning and gen_server implementation
%%%===================================================================

start_link(Nodes) ->
  gen_server:start_link({local, ?SERVER}, ?MODULE, Nodes, []).

init(Nodes) ->
  process_flag(trap_exit, true),
  io:format("~p (~p) starting...~n", [{local, ?MODULE}, self()]),
  database_logic:install(Nodes),
  {ok, #database_server_state{}}.

store(Node, Username, Location, Gender) ->
  gen_server:call({local, ?MODULE}, {store_db, Node, Username, Location, Gender}).
%%getdb(Node) ->
%%  gen_server:call({local, ?MODULE}, {get_db, Node}).

getalldb(Node) ->
  gen_server:call({local, ?MODULE}, {get_all_db, Node}).
delete(Node) ->
  gen_server:call({local, ?MODULE}, {delete, Node}).

handle_call({store_db, Node, Username, Location, Gender}, _From, State = #database_server_state{}) ->
  database_logic:store_db(Node, Username, Location, Gender),
  io:format("userdetails are saved!"),
  {reply, ok, State};

%%handle_call({get_db, Node}, _From, State = #database_server_state{}) ->
%%  X = database_logic:get_db(Node),
%%  {_, [Name]} = X,
%%  io:format("SENDER NAME: ~p~n", [Name]),
%%  {reply, ok, State};

handle_call({get_all_db, Node}, _From, State = #database_server_state{}) ->
  Y = database_logic:get_all_dbe(Node),
  {_, [{Name, Location, Gender}]} = Y,
  io:format("SENDER NAME: ~p\t LOCATION: ~p\t GENDER: ~p~n", [Name,Location,Gender]),
  {reply, ok, State};

handle_call({delete, Node},  _From, State = #database_server_state{}) ->
  database_logic:delete_db(Node),
  io:format("~p node data deleted!", [Node]),
  {reply, ok, State}.

handle_cast(_Request, State = #database_server_state{}) ->
  {noreply, State}.

handle_info(_Info, State = #database_server_state{}) ->
  {noreply, State}.

terminate(_Reason, _State = #database_server_state{}) ->
  ok.

code_change(_OldVsn, State = #database_server_state{}, _Extra) ->
  {ok, State}.

这个错误是发现当我做存储,getdb等,数据库操作.(它说NODEDOWN,但所有节点都在运行)

(john@DESKTOP-RD414DV)9> database_server: getalldb("mary@DESKTOP-RD414DV").
** exception exit: {{nodedown,database_server},
                    {gen_server,call,
                                [{local,database_server},
                                 {get_all_db,"mary@DESKTOP-RD414DV"}]}}
     in function  gen_server:call/2 (gen_server.erl, line 367)
6ie5vjzr

6ie5vjzr1#

问题出在gen_server:call调用中:

gen_server:call({local, ?MODULE}, {get_all_db, Node}).

start_linkcall中,指定服务器进程名称的方式是不同的。在start_link中,指定{local, myname}以便在本地节点上注册名称myname,但在call中,只需指定myname
如果你把一个有两个元素的元组传递给gen_server:call,它会把第一个元素当作进程名,把第二个元素当作节点名。所以在上面的代码中,它试图远程调用节点database_server上注册为local的进程--但这失败了。因为不存在名为database_server的节点(尤其是因为没有@符号的节点名是无效的)。
因此,将带有gen_server:call的行更改为如下所示:

gen_server:call(?MODULE, {get_all_db, Node}).

您可以在gen_server文档中看到这两种约定之间的区别。比较start_link中使用的server_name()类型规范和call中使用的server_ref()类型规范。

相关问题