mnesia initialization

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

mnesia initialization

Karlo Kuna
Hi, 

I have problem with mnesia initialization, 

I have a db_init function that initializes db so that if tables do not exist they are created (ram with disc_copies). For example:

db_init() ->
    mnesia:create_table(some_record,[{attributes, record_info(fields, some_record)},
                                                                {type, bag}, {disc_copies, [node()]}]).

I'm running this function on the app start to make sure the table exists and is ready. 
However half of the time it destroys old table returning {atomic, ok} and another half it returns with already_exists. Also, this happens in the regular fashion, if I have n starts of application, even starts would destroy data and even wold keeps old data. I know that I am missing something probably obvious here but if anyone has any insights on why is this happening and how to fix it I would greatly appreciate it.

Thank You, 
Karlo. 
Reply | Threaded
Open this post in threaded view
|

Re: mnesia initialization

Oleksii Semilietov
Actually you need first to check does table exists on local node or not. Instead of just create_table you should have some routine like

init_table(TableName, TableDef) ->
   AllTables = mnesia:system_info(tables),
   case lists:member(TableName, AllTables) of 
       false ->
              mnesia:create_table(TableName, TableDef);
       true ->
             wait_table(TableName)
       end
   .
In wait_table you should implement mnesia loading table routine.  

The great starting point how to wrap up mnesia routine is in 

https://github.com/rabbitmq/rabbitmq-server/blob/6cbd36bd79dd72817ec1ebe4a01c0d8a4bc7d69e/src/rabbit_mnesia.erl
and in 
https://github.com/rabbitmq/rabbitmq-server/blob/6cbd36bd79dd72817ec1ebe4a01c0d8a4bc7d69e/src/rabbit_table.erl


On Fri, 8 Nov 2019 at 07:58, Karlo Kuna <[hidden email]> wrote:
Hi, 

I have problem with mnesia initialization, 

I have a db_init function that initializes db so that if tables do not exist they are created (ram with disc_copies). For example:

db_init() ->
    mnesia:create_table(some_record,[{attributes, record_info(fields, some_record)},
                                                                {type, bag}, {disc_copies, [node()]}]).

I'm running this function on the app start to make sure the table exists and is ready. 
However half of the time it destroys old table returning {atomic, ok} and another half it returns with already_exists. Also, this happens in the regular fashion, if I have n starts of application, even starts would destroy data and even wold keeps old data. I know that I am missing something probably obvious here but if anyone has any insights on why is this happening and how to fix it I would greatly appreciate it.

Thank You, 
Karlo. 


--
Best regards,
Alex [Oleksii Semilietov]

Reply | Threaded
Open this post in threaded view
|

Re: mnesia initialization

I Gusti Ngurah Oka Prinarjaya
In reply to this post by Karlo Kuna
Hi,

Make sure to:

1. Start your erlang node with -mnesia dir "/path/to/where/you/want/to/put/mnesia"
2. Create the schema first
3. start mnesia by using command mnesia:start()
4. The create your tables

>> I have a db_init function that initializes db so that if tables do not exist they are created (ram with disc_copies)
If you want to do a table existence detection, you can do like the following:

case catch mnesia:table(your_table_name, version) of
  {{_, _}, []} 
    -> table_exists;

  Fail when Fail =:= {'EXIT',{aborted,{no_exists,your_table_name,version}}}; Fail =:= {aborted,{no_exists,your_table_name,version}} 
    -> %% create your your_table_name table
end

Also, as i stated above, you have to create your schema first then start mnesia application. Example:

case mnesia:create_schema([node()]) of
  {error,{_,{already_exists,_}}} ->
      io:format("Schema Already exists~n");

    ok ->
      io:format("Schema Created~n")
end,

application:ensure_started(mnesia),

%% then init your tables here
%%
%%
.


>> However half of the time it destroys old table returning {atomic, ok} and another half it returns with already_exists
I'm not sure with this. But maybe i think your schema location was not consistent

I suggest you to use rebar3 to manage your project. 














Pada tanggal Jum, 8 Nov 2019 pukul 07.57 Karlo Kuna <[hidden email]> menulis:
Hi, 

I have problem with mnesia initialization, 

I have a db_init function that initializes db so that if tables do not exist they are created (ram with disc_copies). For example:

db_init() ->
    mnesia:create_table(some_record,[{attributes, record_info(fields, some_record)},
                                                                {type, bag}, {disc_copies, [node()]}]).

I'm running this function on the app start to make sure the table exists and is ready. 
However half of the time it destroys old table returning {atomic, ok} and another half it returns with already_exists. Also, this happens in the regular fashion, if I have n starts of application, even starts would destroy data and even wold keeps old data. I know that I am missing something probably obvious here but if anyone has any insights on why is this happening and how to fix it I would greatly appreciate it.

Thank You, 
Karlo. 
Reply | Threaded
Open this post in threaded view
|

Re: mnesia initialization

Karlo Kuna
thank you all, 

but it seems that my problem is connected to "Cannot get connection id for node" when mnesia starts 
I'm having a node at [hidden email] could that be the issue??

On Fri, Nov 8, 2019 at 6:46 AM I Gusti Ngurah Oka Prinarjaya <[hidden email]> wrote:
Hi,

Make sure to:

1. Start your erlang node with -mnesia dir "/path/to/where/you/want/to/put/mnesia"
2. Create the schema first
3. start mnesia by using command mnesia:start()
4. The create your tables

>> I have a db_init function that initializes db so that if tables do not exist they are created (ram with disc_copies)
If you want to do a table existence detection, you can do like the following:

case catch mnesia:table(your_table_name, version) of
  {{_, _}, []} 
    -> table_exists;

  Fail when Fail =:= {'EXIT',{aborted,{no_exists,your_table_name,version}}}; Fail =:= {aborted,{no_exists,your_table_name,version}} 
    -> %% create your your_table_name table
end

Also, as i stated above, you have to create your schema first then start mnesia application. Example:

case mnesia:create_schema([node()]) of
  {error,{_,{already_exists,_}}} ->
      io:format("Schema Already exists~n");

    ok ->
      io:format("Schema Created~n")
end,

application:ensure_started(mnesia),

%% then init your tables here
%%
%%
.


>> However half of the time it destroys old table returning {atomic, ok} and another half it returns with already_exists
I'm not sure with this. But maybe i think your schema location was not consistent

I suggest you to use rebar3 to manage your project. 














Pada tanggal Jum, 8 Nov 2019 pukul 07.57 Karlo Kuna <[hidden email]> menulis:
Hi, 

I have problem with mnesia initialization, 

I have a db_init function that initializes db so that if tables do not exist they are created (ram with disc_copies). For example:

db_init() ->
    mnesia:create_table(some_record,[{attributes, record_info(fields, some_record)},
                                                                {type, bag}, {disc_copies, [node()]}]).

I'm running this function on the app start to make sure the table exists and is ready. 
However half of the time it destroys old table returning {atomic, ok} and another half it returns with already_exists. Also, this happens in the regular fashion, if I have n starts of application, even starts would destroy data and even wold keeps old data. I know that I am missing something probably obvious here but if anyone has any insights on why is this happening and how to fix it I would greatly appreciate it.

Thank You, 
Karlo. 
Reply | Threaded
Open this post in threaded view
|

Re: mnesia initialization

I Gusti Ngurah Oka Prinarjaya
Hi,

>> but it seems that my problem is connected to "Cannot get connection id for node" when mnesia starts 
How you start mnesia? It's so rare to happen. it looks like auto-connect things.

>> I'm having a node at [hidden email] could that be the issue??
That's strange, if you run node() in your erlang node shell you should get nonode@nohost. Except, if you run your erlang node with command: erl -sname [hidden email].

Try to start your erlang node with better node name such: erl -sname node1@myhost where myhost mapped in `/etc/hosts` as 127.0.0.1 

So then combined with your mnesia needs, you should run erl -sname node1@myhost -mnesia dir "/home/yourusername/MY_MNESIA_PUT_HERE".

>> However half of the time it destroys old table returning {atomic, ok} and another half it returns with already_exists.
I think it's because you set your mnesia's directory at `/tmp/*` then your `/tmp/*` got auto-cleaned by the OS.



Pada tanggal Jum, 8 Nov 2019 pukul 22.29 Karlo Kuna <[hidden email]> menulis:
thank you all, 

but it seems that my problem is connected to "Cannot get connection id for node" when mnesia starts 
I'm having a node at [hidden email] could that be the issue??

On Fri, Nov 8, 2019 at 6:46 AM I Gusti Ngurah Oka Prinarjaya <[hidden email]> wrote:
Hi,

Make sure to:

1. Start your erlang node with -mnesia dir "/path/to/where/you/want/to/put/mnesia"
2. Create the schema first
3. start mnesia by using command mnesia:start()
4. The create your tables

>> I have a db_init function that initializes db so that if tables do not exist they are created (ram with disc_copies)
If you want to do a table existence detection, you can do like the following:

case catch mnesia:table(your_table_name, version) of
  {{_, _}, []} 
    -> table_exists;

  Fail when Fail =:= {'EXIT',{aborted,{no_exists,your_table_name,version}}}; Fail =:= {aborted,{no_exists,your_table_name,version}} 
    -> %% create your your_table_name table
end

Also, as i stated above, you have to create your schema first then start mnesia application. Example:

case mnesia:create_schema([node()]) of
  {error,{_,{already_exists,_}}} ->
      io:format("Schema Already exists~n");

    ok ->
      io:format("Schema Created~n")
end,

application:ensure_started(mnesia),

%% then init your tables here
%%
%%
.


>> However half of the time it destroys old table returning {atomic, ok} and another half it returns with already_exists
I'm not sure with this. But maybe i think your schema location was not consistent

I suggest you to use rebar3 to manage your project. 














Pada tanggal Jum, 8 Nov 2019 pukul 07.57 Karlo Kuna <[hidden email]> menulis:
Hi, 

I have problem with mnesia initialization, 

I have a db_init function that initializes db so that if tables do not exist they are created (ram with disc_copies). For example:

db_init() ->
    mnesia:create_table(some_record,[{attributes, record_info(fields, some_record)},
                                                                {type, bag}, {disc_copies, [node()]}]).

I'm running this function on the app start to make sure the table exists and is ready. 
However half of the time it destroys old table returning {atomic, ok} and another half it returns with already_exists. Also, this happens in the regular fashion, if I have n starts of application, even starts would destroy data and even wold keeps old data. I know that I am missing something probably obvious here but if anyone has any insights on why is this happening and how to fix it I would greatly appreciate it.

Thank You, 
Karlo. 
Reply | Threaded
Open this post in threaded view
|

Re: mnesia initialization

Karlo Kuna
Thank you once again, 

I really don't understand why it is happening but I have found a solution.
i am initializing node simply by -name name and then everything works as expected and cannot get connection error goes away 




On Fri, Nov 8, 2019 at 6:59 PM I Gusti Ngurah Oka Prinarjaya <[hidden email]> wrote:
Hi,

>> but it seems that my problem is connected to "Cannot get connection id for node" when mnesia starts 
How you start mnesia? It's so rare to happen. it looks like auto-connect things.

>> I'm having a node at [hidden email] could that be the issue??
That's strange, if you run node() in your erlang node shell you should get nonode@nohost. Except, if you run your erlang node with command: erl -sname [hidden email].

Try to start your erlang node with better node name such: erl -sname node1@myhost where myhost mapped in `/etc/hosts` as 127.0.0.1 

So then combined with your mnesia needs, you should run erl -sname node1@myhost -mnesia dir "/home/yourusername/MY_MNESIA_PUT_HERE".

>> However half of the time it destroys old table returning {atomic, ok} and another half it returns with already_exists.
I think it's because you set your mnesia's directory at `/tmp/*` then your `/tmp/*` got auto-cleaned by the OS.



Pada tanggal Jum, 8 Nov 2019 pukul 22.29 Karlo Kuna <[hidden email]> menulis:
thank you all, 

but it seems that my problem is connected to "Cannot get connection id for node" when mnesia starts 
I'm having a node at [hidden email] could that be the issue??

On Fri, Nov 8, 2019 at 6:46 AM I Gusti Ngurah Oka Prinarjaya <[hidden email]> wrote:
Hi,

Make sure to:

1. Start your erlang node with -mnesia dir "/path/to/where/you/want/to/put/mnesia"
2. Create the schema first
3. start mnesia by using command mnesia:start()
4. The create your tables

>> I have a db_init function that initializes db so that if tables do not exist they are created (ram with disc_copies)
If you want to do a table existence detection, you can do like the following:

case catch mnesia:table(your_table_name, version) of
  {{_, _}, []} 
    -> table_exists;

  Fail when Fail =:= {'EXIT',{aborted,{no_exists,your_table_name,version}}}; Fail =:= {aborted,{no_exists,your_table_name,version}} 
    -> %% create your your_table_name table
end

Also, as i stated above, you have to create your schema first then start mnesia application. Example:

case mnesia:create_schema([node()]) of
  {error,{_,{already_exists,_}}} ->
      io:format("Schema Already exists~n");

    ok ->
      io:format("Schema Created~n")
end,

application:ensure_started(mnesia),

%% then init your tables here
%%
%%
.


>> However half of the time it destroys old table returning {atomic, ok} and another half it returns with already_exists
I'm not sure with this. But maybe i think your schema location was not consistent

I suggest you to use rebar3 to manage your project. 














Pada tanggal Jum, 8 Nov 2019 pukul 07.57 Karlo Kuna <[hidden email]> menulis:
Hi, 

I have problem with mnesia initialization, 

I have a db_init function that initializes db so that if tables do not exist they are created (ram with disc_copies). For example:

db_init() ->
    mnesia:create_table(some_record,[{attributes, record_info(fields, some_record)},
                                                                {type, bag}, {disc_copies, [node()]}]).

I'm running this function on the app start to make sure the table exists and is ready. 
However half of the time it destroys old table returning {atomic, ok} and another half it returns with already_exists. Also, this happens in the regular fashion, if I have n starts of application, even starts would destroy data and even wold keeps old data. I know that I am missing something probably obvious here but if anyone has any insights on why is this happening and how to fix it I would greatly appreciate it.

Thank You, 
Karlo.