Search code examples
dockererlangmnesia

Mnesia doesn't recognize data after sys restart (no_exists error)


I'm building an erlang app in a docker container. My data directory is connected as a docker volume. I'm using: application:set_env(mnesia, schema_location, disc), and {disc_copies, Nodes}.

When I restart the app with:

docker-composer stop myapp && docker-composer up myapp

all works fine but when I turn it off completely with:

sudo systemctl restart docker
docker-composer up -d

Mnesia stops recognizing my data. The same is true when I restart my computer.

Basically (after this "hard" restart) Mnesia starts normally but when I try to read anything from it, I get an error:

{running_method,getTasksByProjectId}
terminate reason: {aborted,{no_exists,mtm_tasks}}
terminate reason: {{aborted,{no_exists,mtm_tasks}},
                   {gen_server,call,
                       [{global,tasks_da_serv},
                        {get_tasks_by_project_id,
                            "j1xvqric-2oj2q784xieccowg8880"}]}}

=ERROR REPORT==== 8-Jun-2017::10:29:04 ===
** Generic server tasks_da_serv terminating 
** Last message in was {get_tasks_by_project_id,
                           "j1xvqric-2oj2q784xieccowg8880"}
** When Server state == {tasks_da_state}
** Reason for termination == 
** {{aborted,{no_exists,mtm_tasks}},
    [{mnesia,wrap_trans,6,[{file,"mnesia.erl"},{line,404}]},
     {tasks_logic,get_tasks_by_project_id,1,
                  [{file,"/release/_build/default/lib/tasks/src/tasks_logic.erl"},
                   {line,70}]},
     {tasks_da_serv,handle_call,3,
                    [{file,"/release/_build/default/lib/tasks/src/tasks_da_serv.erl"},
                     {line,52}]},
     {gen_server,try_handle_call,4,[{file,"gen_server.erl"},{line,615}]},
     {gen_server,handle_msg,5,[{file,"gen_server.erl"},{line,647}]},
     {proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,247}]}]}
Starting {global,tasks_da_serv} (<0.492.0>)
Starting {global,tasks_manager_serv} (<0.497.0>)

=ERROR REPORT==== 8-Jun-2017::10:29:04 ===
** Generic server tasks_manager_serv terminating 
** Last message in was {{'basic.deliver',
                            <<"amq.ctag-pt-OySikx5BShVLRASvIPw">>,1,false,
                            <<>>,<<"tasks">>},
                        {amqp_msg,
                            {'P_basic',undefined,undefined,[],undefined,
                                undefined,undefined,undefined,undefined,
                                undefined,undefined,undefined,undefined,
                                undefined,undefined},
                            <<"{\"correlationId\":\"j3oa5b84-pcxn9ld6mswsck8kw48\",\"replyTo\":\"amq.gen-8h_kLqz4sHB-qjJ2cj8Y6w\",\"method\":\"getTasksByProjectId\",\"params\":{\"projectId\":\"j1xvqric-2oj2q784xieccowg8880\"}}">>}}
** When Server state == {tasks_manager_state,<0.481.0>,<0.490.0>}
** Reason for termination == 
** {{{aborted,{no_exists,mtm_tasks}},
     {gen_server,call,
                 [{global,tasks_da_serv},
                  {get_tasks_by_project_id,"j1xvqric-2oj2q784xieccowg8880"}]}},
    [{gen_server,call,2,[{file,"gen_server.erl"},{line,204}]},
     {tasks_manager_logic,handle_message,2,
                          [{file,"/release/_build/default/lib/tasks/src/tasks_manager_logic.erl"},
                           {line,7}]},
     {tasks_manager_serv,handle_info,2,
                         [{file,"/release/_build/default/lib/tasks/src/tasks_manager_serv.erl"},
                          {line,45}]},
     {gen_server,try_dispatch,4,[{file,"gen_server.erl"},{line,601}]},
     {gen_server,handle_msg,5,[{file,"gen_server.erl"},{line,667}]},
     {proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,247}]}]}

At this point the only way to make it work again is to remove the data from the docker volume.


Solution

  • I have figured it out. The problem is that docker is assigning a custom hostnames to containers and Mnesia disc copy apparently is bound to hostname. Now I have fixed hostname in docker-compose.yaml file:

    version: '2'
    services:
      myapp:
        build: './myapp'
        hostname: 'myapp'
        volumes:
          ...
    

    And now can restart it without loosing data.