File 0001-remove-riak_ensemble_clock-module.patch of Package riak_ensemble
From bafee4b395888af5590536ea8258f6245395bbdb Mon Sep 17 00:00:00 2001
From: Led <ledest@gmail.com>
Date: Sun, 13 Aug 2017 21:13:19 +0300
Subject: [PATCH 1/3] remove riak_ensemble_clock module
---
c_src/riak_ensemble_clock.c | 184 -------------------------------------------
doc/Readme.md | 8 +-
rebar.config | 6 --
src/riak_ensemble_clock.erl | 42 ----------
src/riak_ensemble_lease.erl | 14 +---
test/TESTS | 1 -
test/ensemble_tests_pure.erl | 19 -----
7 files changed, 4 insertions(+), 270 deletions(-)
delete mode 100644 c_src/riak_ensemble_clock.c
delete mode 100644 src/riak_ensemble_clock.erl
delete mode 100644 test/ensemble_tests_pure.erl
diff --git a/c_src/riak_ensemble_clock.c b/c_src/riak_ensemble_clock.c
deleted file mode 100644
index de5a7d2..0000000
--- a/c_src/riak_ensemble_clock.c
+++ /dev/null
@@ -1,184 +0,0 @@
-/********************************************************************
- *
- * Copyright (c) 2014 Basho Technologies, Inc. All Rights Reserved.
- *
- * This file is provided to you under the Apache License,
- * Version 2.0 (the "License"); you may not use this file
- * except in compliance with the License. You may obtaine
- * a copy of the License at
- *
- * http: www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
- ********************************************************************/
-#include "erl_nif.h"
-
-#include <unistd.h>
-#include <time.h>
-#include <sys/time.h>
-#include <stdint.h>
-
-#if defined(__MACH__) && defined(__APPLE__)
-#include <mach/mach.h>
-#include <mach/mach_time.h>
-#endif
-
-static ERL_NIF_TERM ATOM_OK;
-static ERL_NIF_TERM ATOM_ERROR;
-
-#if defined(__MACH__) && defined(__APPLE__)
-static mach_timebase_info_data_t timebase_info;
-#endif
-
-/*********************************************************************/
-
-#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
-uint64_t posix_get_clock(clockid_t clock)
-{
- struct timespec ts;
- if(clock_gettime(clock, &ts) == -1)
- return 0;
- return ((uint64_t)ts.tv_sec * 1000000000) + ts.tv_nsec;
-}
-
-/* Note: Prefer CLOCK_BOOTTIME on Linux where supported, as this
- includes time spent in suspend. CLOCK_MONOTONIC may or may
- not include time spent in suspend -- it's CPU dependent. In
- practice, this shouldn't matter -- people don't typically
- suspend/resume production servers while under client load.
- Likewise, client TCP connections are unlikely to survive
- across reasonable suspend durations.
-*/
-
-uint64_t posix_monotonic_time(void)
-{
- uint64_t time;
-#if defined(CLOCK_BOOTTIME)
- if((time = posix_get_clock(CLOCK_BOOTTIME)))
- return time;
-#elif defined(CLOCK_MONOTONIC)
- if((time = posix_get_clock(CLOCK_MONOTONIC)))
- return time;
-#endif
- return 0;
-}
-#endif
-
-/*********************************************************************
- * See Apple technical note: *
- * https://developer.apple.com/library/mac/qa/qa1398/_index.html *
- *********************************************************************/
-
-/* Note: mach_absolute_time() is based on the CPU timestamp counter,
- which is synchronized across all CPUs since Intel Nehalem.
- Earlier CPUs do not provide this guarantee. It's unclear if
- Apple provides any correction for this behavior on older CPUs.
- We assume this doesn't matter in practice -- people don't use
- ancient OS X machines as production servers.
-*/
-
-#if defined(__MACH__) && defined(__APPLE__)
-uint64_t osx_monotonic_time(void)
-{
- uint64_t time;
- uint64_t timeNano;
-
- time = mach_absolute_time();
-
- // Do the maths. We hope that the multiplication doesn't
- // overflow; the price you pay for working in fixed point.
-
- timeNano = time * timebase_info.numer / timebase_info.denom;
-
- return timeNano;
-}
-#endif
-
-/*********************************************************************/
-
-static uint64_t get_monotonic_time()
-{
- uint64_t time = 0;
-
-#if defined(__MACH__) && defined(__APPLE__)
- time = osx_monotonic_time();
-#endif
-
-#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
- time = posix_monotonic_time();
-#endif
-
- return time;
-}
-
-/*********************************************************************/
-
-static ERL_NIF_TERM monotonic_time(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
-{
- uint64_t time = get_monotonic_time();
-
- if(time) {
- return enif_make_tuple2(env, ATOM_OK, enif_make_uint64(env, time));
- }
- else {
- return ATOM_ERROR;
- }
-}
-
-/*********************************************************************/
-
-static ERL_NIF_TERM monotonic_time_ms(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
-{
- uint64_t time = get_monotonic_time() / 1000000;
-
- if(time) {
- return enif_make_tuple2(env, ATOM_OK, enif_make_uint64(env, time));
- }
- else {
- return ATOM_ERROR;
- }
-}
-
-/*********************************************************************/
-
-static void init(ErlNifEnv *env)
-{
- ATOM_OK = enif_make_atom(env, "ok");
- ATOM_ERROR = enif_make_atom(env, "error");
-
-#if defined(__MACH__) && defined(__APPLE__)
- (void) mach_timebase_info(&timebase_info);
-#endif
-}
-
-static int on_load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info)
-{
- init(env);
- return 0;
-}
-
-static int on_upgrade(ErlNifEnv* env, void** priv_data, void** old_priv_data,
- ERL_NIF_TERM load_info)
-{
- init(env);
- return 0;
-}
-
-static void on_unload(ErlNifEnv *env, void *priv_data)
-{
-}
-
-/*********************************************************************/
-
-static ErlNifFunc nif_funcs[] = {
- {"monotonic_time", 0, monotonic_time},
- {"monotonic_time_ms", 0, monotonic_time_ms}
-};
-
-ERL_NIF_INIT(riak_ensemble_clock, nif_funcs, &on_load, NULL, &on_upgrade, &on_unload)
diff --git a/doc/Readme.md b/doc/Readme.md
index 5fc1688..0d1f661 100644
--- a/doc/Readme.md
+++ b/doc/Readme.md
@@ -438,9 +438,8 @@ Each peer has an accompanying lease process which keeps track of the status of a
1. [riak_ensemble_lease](#riak_ensemble_lease)
2. [Erlang time correction](http://www.erlang.org/doc/apps/erts/time_correction.html)
-3. [riak_ensemble_clock](#riak_ensemble_clock)
-4. [riak_ensemble_peer:leader_tick/1][]
-5. [riak_ensemble_peer:step_down/2][]
+3. [riak_ensemble_peer:leader_tick/1][]
+4. [riak_ensemble_peer:step_down/2][]
------------------------------------------------------------
# Message routing
@@ -488,9 +487,6 @@ Simple backend that stores data as a term_to_binary'ed blob to a file. Used by t
## riak_ensemble_client
KV operations API.
[src](../src/riak_ensemble_client.erl) | [edoc](http://basho.github.io/riak_ensemble/riak_ensemble_client.html)
-## riak_ensemble_clock
-Interface to the monotonic clock NIFs. The NIF is implemented in [riak_ensemble_clock.c](../c_src/riak_ensemble_clock.c).
-[src](../src/riak_ensemble_clock.erl) | [edoc](http://basho.github.io/riak_ensemble/riak_ensemble_clock.html)
## riak_ensemble_config
Configuration related wrappers and utilities.
[src](../src/riak_ensemble_config.erl) | [edoc](http://basho.github.io/riak_ensemble/riak_ensemble_config.html)
diff --git a/rebar.config b/rebar.config
index 471fb49..979b03f 100644
--- a/rebar.config
+++ b/rebar.config
@@ -12,9 +12,3 @@
{lager, ".*", {git, "git://github.com/basho/lager.git", {tag, "3.2.4"}}},
{eleveldb, ".*", {git, "git://github.com/basho/eleveldb.git", {tag, "2.0.34"}}}
]}.
-
-{port_specs,
- [{".*", "priv/riak_ensemble.so",
- ["c_src/*.c*"],
- [{env, [{"CFLAGS", "$CFLAGS"}]}]
- }]}.
diff --git a/src/riak_ensemble_clock.erl b/src/riak_ensemble_clock.erl
deleted file mode 100644
index 6bd6b4e..0000000
--- a/src/riak_ensemble_clock.erl
+++ /dev/null
@@ -1,42 +0,0 @@
-%% -------------------------------------------------------------------
-%%
-%% Copyright (c) 2014 Basho Technologies, Inc. All Rights Reserved.
-%%
-%% This file is provided to you under the Apache License,
-%% Version 2.0 (the "License"); you may not use this file
-%% except in compliance with the License. You may obtain
-%% a copy of the License at
-%%
-%% http://www.apache.org/licenses/LICENSE-2.0
-%%
-%% Unless required by applicable law or agreed to in writing,
-%% software distributed under the License is distributed on an
-%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-%% KIND, either express or implied. See the License for the
-%% specific language governing permissions and limitations
-%% under the License.
-%%
-%% -------------------------------------------------------------------
--module(riak_ensemble_clock).
--on_load(init/0).
--export([monotonic_time/0, monotonic_time_ms/0]).
-
-monotonic_time() ->
- erlang:nif_error({error, not_loaded}).
-
-monotonic_time_ms() ->
- erlang:nif_error({error, not_loaded}).
-
-init() ->
- case code:priv_dir(riak_ensemble) of
- {error, bad_name} ->
- case code:which(?MODULE) of
- Filename when is_list(Filename) ->
- SoName = filename:join([filename:dirname(Filename),"../priv", "riak_ensemble"]);
- _ ->
- SoName = filename:join("../priv", "riak_ensemble")
- end;
- Dir ->
- SoName = filename:join(Dir, "riak_ensemble")
- end,
- erlang:load_nif(SoName, 0).
diff --git a/src/riak_ensemble_lease.erl b/src/riak_ensemble_lease.erl
index 732b092..a3f0a3f 100644
--- a/src/riak_ensemble_lease.erl
+++ b/src/riak_ensemble_lease.erl
@@ -79,12 +79,7 @@ check_lease({_, T}) ->
undefined ->
false;
Until ->
- case riak_ensemble_clock:monotonic_time_ms() of
- {ok, Time} when Time < Until ->
- true;
- _ ->
- false
- end
+ erlang:monotonic_time(milli_seconds) < Until
end.
-spec lease(lease_ref(), timeout()) -> ok.
@@ -109,12 +104,7 @@ init(Parent, Ref) ->
loop(T, Timeout) ->
receive
{{lease, Duration}, From} ->
- case riak_ensemble_clock:monotonic_time_ms() of
- {ok, Time} ->
- ets:insert(T, {lease, Time + Duration});
- error ->
- ets:insert(T, {lease, undefined})
- end,
+ ets:insert(T, {lease, erlang:monotonic_time(milli_seconds) + Duration}),
reply(From, ok),
?MODULE:loop(T, Duration);
{unlease, From} ->
diff --git a/test/TESTS b/test/TESTS
index 2fd8ab8..4bf8afa 100644
--- a/test/TESTS
+++ b/test/TESTS
@@ -10,7 +10,6 @@ corrupt_exchange_test
corrupt_follower_test
synctree_path_test
lease_test
-ensemble_tests_pure
replace_members_test
read_tombstone_test
leadership_watchers
diff --git a/test/ensemble_tests_pure.erl b/test/ensemble_tests_pure.erl
deleted file mode 100644
index a21e490..0000000
--- a/test/ensemble_tests_pure.erl
+++ /dev/null
@@ -1,19 +0,0 @@
-%% Various pure tests
--module(ensemble_tests_pure).
--compile(export_all).
--include_lib("eunit/include/eunit.hrl").
-
--define(TEST(X), {timeout, 60, {test, ?MODULE, X}}).
-
-run_test_() ->
- [?TEST(test_monotonic_time)].
-
-test_monotonic_time() ->
- {ok, N1} = riak_ensemble_clock:monotonic_time(),
- {ok, M1} = riak_ensemble_clock:monotonic_time_ms(),
- timer:sleep(1000),
- {ok, N2} = riak_ensemble_clock:monotonic_time(),
- {ok, M2} = riak_ensemble_clock:monotonic_time_ms(),
- ?assert((N2 - N1) >= 1000000000),
- ?assert((M2 - M1) >= 1000),
- ok.
--
2.14.1