Skip to content

Commit 811273f

Browse files
committed
init
0 parents  commit 811273f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+189
-0
lines changed
Binary file not shown.
Binary file not shown.

.elixir_ls/build/test/lib/ex_hash_ring/.mix/compile.fetch

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
�haj
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{application,ex_hash_ring,
2+
[{applications,[kernel,stdlib,elixir]},
3+
{description,"ex_hash_ring"},
4+
{modules,['Elixir.ExHashRing','Elixir.ExHashRing.HashRing',
5+
'Elixir.ExHashRing.HashRing.ETS',
6+
'Elixir.ExHashRing.HashRing.ETS.Config',
7+
'Elixir.ExHashRing.HashRing.Utils']},
8+
{registered,[]},
9+
{vsn,"3.0.0"},
10+
{mod,{'Elixir.ExHashRing',[]}}]}.
Binary file not shown.
Binary file not shown.

.elixir_ls/build/test/lib/node_compass/.mix/compile.lock

Whitespace-only changes.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
�haj
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{application,node_compass,
2+
[{applications,[kernel,stdlib,elixir,logger,ex_hash_ring]},
3+
{description,"node_compass"},
4+
{modules,['Elixir.NodeCompass']},
5+
{registered,[]},
6+
{vsn,"0.1.0"}]}.
Binary file not shown.

.formatter.exs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]

.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where third-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
# Ignore .fetch files in case you like to edit your project deps locally.
14+
/.fetch
15+
16+
# If the VM crashes, it generates a dump, let's ignore it too.
17+
erl_crash.dump
18+
19+
# Also ignore archive artifacts (built via "mix archive.build").
20+
*.ez
21+
22+
# Ignore package tarball (built via "mix hex.build").
23+
node_compass-*.tar
24+

README.md

+15

lib/node_compass.ex

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
defmodule NodeCompass do
2+
use GenServer
3+
4+
require Logger
5+
alias ExHashRing.HashRing
6+
7+
defstruct [
8+
:ring_pid,
9+
:verify_node_tag
10+
]
11+
12+
@spec find_node(binary | integer) :: {:ok, binary} | {:error, atom}
13+
def find_node(key) do
14+
HashRing.ETS.find_node(:compass_ring, key)
15+
end
16+
17+
18+
def verify_node(ext_pid, remote_tag) do
19+
tag = GenServer.call(:node_compass, {:get_verify_tag})
20+
21+
with tag <- remote_tag do
22+
GenServer.cast(ext_pid, {:add_node, node()})
23+
end
24+
end
25+
26+
@doc """
27+
Start the node_compass controller.
28+
NodeCompass should be run under a supervision tree, it is not recommended to call this directly.
29+
"""
30+
def start_link(verify_node_tag \\ nil) do
31+
GenServer.start_link(__MODULE__, %__MODULE__{verify_node_tag: verify_node_tag}, name: :node_compass)
32+
end
33+
34+
def init(state) do
35+
{:ok, pid} = HashRing.ETS.start_link(:compass_ring)
36+
37+
pid
38+
|> HashRing.ETS.add_node(node())
39+
40+
:net_kernel.monitor_nodes(true)
41+
42+
{:ok, %__MODULE__{state | ring_pid: pid}}
43+
end
44+
45+
def handle_call({:get_verify_tag}, _from, state) do
46+
{:reply, state.verify_node_tag, state}
47+
end
48+
49+
def handle_cast({:add_node, remote_node}, state) do
50+
state.ring_pid
51+
|> HashRing.ETS.add_node(remote_node)
52+
53+
Logger.info("Node added to hash ring: #{remote_node}")
54+
{:noreply, state}
55+
end
56+
57+
def handle_info({:nodeup, remote_node}, state) do
58+
case state.verify_node_tag do
59+
nil ->
60+
GenServer.cast(self(), {:add_node, node})
61+
_ ->
62+
Node.spawn(remote_node, NodeCompass, :verify_node, [self(), state.verify_node_tag])
63+
end
64+
65+
{:noreply, state}
66+
end
67+
68+
def handle_info({:nodedown, node}, state) do
69+
state.ring_pid
70+
|> HashRing.ETS.remove_node(node)
71+
72+
{:noreply, state}
73+
end
74+
end

mix.exs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
defmodule NodeCompass.MixProject do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :node_compass,
7+
version: "0.1.0",
8+
elixir: "~> 1.9",
9+
start_permanent: Mix.env() == :prod,
10+
deps: deps(),
11+
package: package()
12+
]
13+
end
14+
15+
# Run "mix help compile.app" to learn about applications.
16+
def application do
17+
[
18+
extra_applications: [:logger]
19+
]
20+
end
21+
22+
# Run "mix help deps" to learn about dependencies.
23+
defp deps do
24+
[
25+
{:ex_hash_ring, "~> 3.0"}
26+
# {:dep_from_hexpm, "~> 0.3.0"},
27+
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
28+
]
29+
end
30+
31+
defp package do
32+
[
33+
name: :node_compass,
34+
description: "NodeCompass is an automated hash-ring management system for Elixir clusters",
35+
maintainers: ["Hiven", "phineas"],
36+
licenses: ["MIT"],
37+
links: %{
38+
"GitHub" => "https://github.com/hivenapp/node_compass"
39+
}
40+
]
41+
end
42+
end

mix.lock

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%{
2+
"ex_hash_ring": {:hex, :ex_hash_ring, "3.0.0", "da32c83d7c6d964b9537eb52f27bad0a3a6f7012efdc2749e11a5f268b120b6b", [:mix], [], "hexpm"},
3+
}

test/node_compass_test.exs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
defmodule NodeCompassTest do
2+
use ExUnit.Case
3+
doctest NodeCompass
4+
5+
test "greets the world" do
6+
assert NodeCompass.hello() == :world
7+
end
8+
end

test/test_helper.exs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ExUnit.start()

0 commit comments

Comments
 (0)