项目作者: beardedeagle

项目描述 :
Mnesia autoclustering made easy!
高级语言: Elixir
项目地址: git://github.com/beardedeagle/mnesiac.git
创建时间: 2018-07-18T03:51:54Z
项目社区:https://github.com/beardedeagle/mnesiac

开源协议:MIT License

下载


Mnesiac

CI Coverage Status Hex.pm Hex.pm downloads

Mnesia auto clustering made easy!

Docs can be found at https://hexdocs.pm/mnesiac.

NOTICE: Mnesiac, while stable, is still considered pre 1.0. This means the API can, and may, change at any time. Please ensure you review the docs and changelog prior to updating, or pin the version of mnesiac you are using in your mix.exs if necessary.

NOTICE: Mnesiac allows a significant amount of freedom with how it behaves. This allows you to customize Mnesiac to suit your needs. However, this also allows for a fair amount of foot gunning. Please ensure you’ve done your due diligence when using this library, or Mnesia itself for that matter. It isn’t a silver bullet, and it shouldn’t be treated as one.

Installation

Simply add mnesiac to your list of dependencies in mix.exs:

  1. def deps do
  2. [
  3. {:mnesiac, "~> 0.3"}
  4. ]
  5. end

Edit your app’s config.exs to add the list of Mnesia stores:

  1. config :mnesiac,
  2. stores: [MyApp.ExampleStore, ...],
  3. schema_type: :disc_copies, # defaults to :ram_copies
  4. table_load_timeout: 600_000 # milliseconds, default is 600_000

Then add mnesiac to your supervision tree:

  • EXAMPLE: With libcluster using the Cluster.Strategy.Epmd strategy:
  1. ...
  2. topology = Application.get_env(:libcluster, :topologies)
  3. hosts = topology[:myapp][:config][:hosts]
  4. children = [
  5. {Cluster.Supervisor, [topology, [name: MyApp.ClusterSupervisor]]},
  6. {Mnesiac.Supervisor, [hosts, [name: MyApp.MnesiacSupervisor]]},
  7. ...
  8. ]
  9. ...
  • EXAMPLE: Without libcluster:
  1. ...
  2. children = [
  3. {
  4. Mnesiac.Supervisor,
  5. [
  6. [:"test01@127.0.0.1", :"test02@127.0.0.1"],
  7. [name: MyApp.MnesiacSupervisor]
  8. ]
  9. },
  10. ...
  11. ]
  12. ...

Usage

Table creation

Create a table store, use Mnesiac.Store, and add it to your app’s config.exs.

All stores MUST implement its own store_options/0, which returns a keyword list of store options.

There are three optional callbacks which can be implemented:

  • init_store/0, which allows users to implement custom store initialization logic. Triggered by Mnesiac.
  • copy_store/0, which allows users to implement a custom call to copy a store. Triggered by Mnesiac.
  • resolve_conflict/1, which allows a user to implement logic when Mnesiac detects a store with records on both the local and remote Mnesia cluster node. Triggered by Mnesiac. Default is to do nothing.

MINIMAL EXAMPLE:

  1. defmodule MyApp.ExampleStore do
  2. @moduledoc """
  3. Provides the structure of ExampleStore records for a minimal example of Mnesiac.
  4. """
  5. use Mnesiac.Store
  6. import Record, only: [defrecord: 3]
  7. @doc """
  8. Record definition for ExampleStore example record.
  9. """
  10. defrecord(
  11. :example,
  12. __MODULE__,
  13. id: nil,
  14. topic_id: nil,
  15. event: nil
  16. )
  17. @typedoc """
  18. ExampleStore example record field type definitions.
  19. """
  20. @type example ::
  21. record(
  22. :example,
  23. id: String.t(),
  24. topic_id: String.t(),
  25. event: String.t()
  26. )
  27. @impl true
  28. def store_options,
  29. do: [
  30. record_name: :example,
  31. attributes: example() |> example() |> Keyword.keys(),
  32. index: [:topic_id],
  33. ram_copies: [node()]
  34. ]
  35. end

Clustering

If you are using libcluster or another clustering library, ensure that the clustering library starts before mnesiac. That’s all, you don’t need to do anything else.

If you are not using libcluster or similar clustering libraries then:

  • When a node joins to an erlang/elixir cluster, run the Mnesiac.init_mnesia/1 function on the new node. This will initialize and copy the store contents from the other online nodes in the Mnesia cluster.

Development

Ensure you have the proper language versions installed. To do this, an asdf tools file has been provided. Run the following:

  1. git clone https://github.com/beardedeagle/mnesiac.git
  2. git checkout -b MyFeature
  3. asdf install
  4. mix local.hex --force
  5. mix local.rebar --force
  6. mix deps.get --force
  7. mix deps.compile --force
  8. mix compile --force

NOTICE: You can find the asdf tool here.

Testing

Before you run any tests, ensure that you have cleaned up Mnesia:

  1. mix purge.db

Test results and coverage reports are generated by running the following:

  1. mix coveralls.html --trace --slowest 10 --no-start

Notice

This library was built standing on the shoulders of giants. A big thanks goes out to Mustafa Turan. The original library this was forked from can be found here: https://github.com/mustafaturan/mnesiam.

Happy coding!