Skip to content
This repository has been archived by the owner on Nov 17, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' into rabbitmq-server-500
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelklishin committed Mar 30, 2016
2 parents b78d3d2 + 11233ff commit d4a9eca
Show file tree
Hide file tree
Showing 19 changed files with 1,601 additions and 295 deletions.
18 changes: 16 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,27 @@ addons:
otp_release:
- "R16B03-1"
- "17.5"
- "18.0"
- "18.2"

# The checkout made by Travis is a "detached HEAD". We switch back
# to a tag or a branch. This pleases our git_rmq fetch method in
# rabbitmq-components.mk and the proper tag/branch is selected in
# dependencies too.
before_script: (test "$TRAVIS_TAG" && git checkout "$TRAVIS_TAG") || (test "$TRAVIS_BRANCH" && git checkout -b "$TRAVIS_BRANCH")
#
# FIXME: There is still one problem: for builds triggered by a pull
# request, $TRAVIS_BRANCH contains the target branch name, not the
# source branch name. Therefore, we can't rely on automatic checkout
# of corresponding branches in dependencies. For instance, if the pull
# request comes from a branch "rabbitmq-server-123", based on "stable",
# then this command will checkout "stable" and we won't try to checkout
# "rabbitmq-server-123" in dependencies.
#
# We also make sure the "master" branch exists, because
# rabbitmq-components.mk expects it. If it's missing, we just create a
# fake branch pointing to the same commit as $TRAVIS_BRANCH.
before_script:
- git checkout -B "${TRAVIS_TAG:-${TRAVIS_BRANCH}}"
- git rev-parse --verify -q master -- || git branch master

script: make tests

Expand Down
2 changes: 2 additions & 0 deletions include/rabbit.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@

-define(EXCHANGE_DELETE_IN_PROGRESS_COMPONENT, <<"exchange-delete-in-progress">>).

-define(CHANNEL_OPERATION_TIMEOUT, rabbit_misc:get_channel_operation_timeout()).

%% Trying to send a term across a cluster larger than 2^31 bytes will
%% cause the VM to exit with "Absurdly large distribution output data
%% buffer". So we limit the max message size to 2^31 - 10^6 bytes (1MB
Expand Down
17 changes: 17 additions & 0 deletions include/rabbit_misc.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
%% The contents of this file are subject to the Mozilla Public License
%% Version 1.1 (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.mozilla.org/MPL/
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and
%% limitations under the License.
%%
%% The Original Code is RabbitMQ.
%%
%% The Initial Developer of the Original Code is GoPivotal, Inc.
%% Copyright (c) 2007-2016 Pivotal Software, Inc. All rights reserved.
%%

-define(RPC_TIMEOUT, infinity).
4 changes: 4 additions & 0 deletions mk/rabbitmq-components.mk
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ dep_rabbitmq_test = git_rmq rabbitmq-test $(current_rmq_ref)
dep_rabbitmq_web_dispatch = git_rmq rabbitmq-web-dispatch $(current_rmq_ref) $(base_rmq_ref) master
dep_rabbitmq_web_stomp = git_rmq rabbitmq-web-stomp $(current_rmq_ref) $(base_rmq_ref) master
dep_rabbitmq_web_stomp_examples = git_rmq rabbitmq-web-stomp-examples $(current_rmq_ref) $(base_rmq_ref) master
dep_rabbitmq_web_mqtt = git_rmq rabbitmq-web-mqtt $(current_rmq_ref) $(base_rmq_ref) master
dep_rabbitmq_web_mqtt_examples = git_rmq rabbitmq-web-mqtt-examples $(current_rmq_ref) $(base_rmq_ref) master
dep_rabbitmq_website = git_rmq rabbitmq-website $(current_rmq_ref) $(base_rmq_ref) live master
dep_sockjs = git_rmq sockjs-erlang $(current_rmq_ref) $(base_rmq_ref) master
dep_toke = git_rmq toke $(current_rmq_ref) $(base_rmq_ref) master
Expand Down Expand Up @@ -117,6 +119,8 @@ RABBITMQ_COMPONENTS = amqp_client \
rabbitmq_top \
rabbitmq_tracing \
rabbitmq_web_dispatch \
rabbitmq_web_mqtt \
rabbitmq_web_mqtt_examples \
rabbitmq_web_stomp \
rabbitmq_web_stomp_examples \
rabbitmq_website
Expand Down
3 changes: 3 additions & 0 deletions mk/rabbitmq-run.mk
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ endif

export RABBITMQ_SCRIPTS_DIR RABBITMQCTL RABBITMQ_PLUGINS RABBITMQ_SERVER

# We export MAKE to be sure scripts and tests use the proper command.
export MAKE

# We need to pass the location of codegen to the Java client ant
# process.
CODEGEN_DIR = $(DEPS_DIR)/rabbitmq_codegen
Expand Down
250 changes: 250 additions & 0 deletions src/code_version.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
%% The contents of this file are subject to the Mozilla Public License
%% Version 1.1 (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.mozilla.org/MPL/
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and
%% limitations under the License.
%%
%% The Original Code is RabbitMQ Federation.
%%
%% The Initial Developer of the Original Code is GoPivotal, Inc.
%% Copyright (c) 2007-2016 Pivotal Software, Inc. All rights reserved.
%%
-module(code_version).

-export([update/1]).

%%----------------------------------------------------------------------------
%% API
%%----------------------------------------------------------------------------

%%----------------------------------------------------------------------------
%% @doc Reads the abstract code of the given `Module`, modifies it to adapt to
%% the current Erlang version, compiles and loads the result.
%% This function finds the current Erlang version and then selects the function
%% call for that version, removing all other versions declared in the original
%% beam file. `code_version:update/1` is triggered by the module itself the
%% first time an affected function is called.
%%
%% The purpose of this functionality is to support the new time API introduced
%% in ERTS 7.0, while providing compatibility with previous versions.
%%
%% `Module` must contain an attribute `erlang_version_support` containing a list of
%% tuples:
%%
%% {ErlangVersion, [{OriginalFuntion, Arity, PreErlangVersionFunction,
%% PostErlangVersionFunction}]}
%%
%% All these new functions may be exported, and implemented as follows:
%%
%% OriginalFunction() ->
%% code_version:update(?MODULE),
%% ?MODULE:OriginalFunction().
%%
%% PostErlangVersionFunction() ->
%% %% implementation using new time API
%% ..
%%
%% PreErlangVersionFunction() ->
%% %% implementation using fallback solution
%% ..
%%
%% See `time_compat.erl` for an example.
%%
%% end
%%----------------------------------------------------------------------------
-spec update(atom()) -> ok | no_return().
update(Module) ->
AbsCode = get_abs_code(Module),
Forms = replace_forms(Module, get_otp_version(), AbsCode),
Code = compile_forms(Forms),
load_code(Module, Code).

%%----------------------------------------------------------------------------
%% Internal functions
%%----------------------------------------------------------------------------
load_code(Module, Code) ->
unload(Module),
case code:load_binary(Module, "loaded by rabbit_common", Code) of
{module, _} ->
ok;
{error, Reason} ->
throw({cannot_load, Module, Reason})
end.

unload(Module) ->
code:soft_purge(Module),
code:delete(Module).

compile_forms(Forms) ->
case compile:forms(Forms, [debug_info]) of
{ok, _ModName, Code} ->
Code;
{ok, _ModName, Code, _Warnings} ->
Code;
Error ->
throw({cannot_compile_forms, Error})
end.

get_abs_code(Module) ->
get_forms(get_object_code(Module)).

get_object_code(Module) ->
case code:get_object_code(Module) of
{_Mod, Code, _File} ->
Code;
error ->
throw({not_found, Module})
end.

get_forms(Code) ->
case beam_lib:chunks(Code, [abstract_code]) of
{ok, {_, [{abstract_code, {raw_abstract_v1, Forms}}]}} ->
Forms;
{ok, {Module, [{abstract_code, no_abstract_code}]}} ->
throw({no_abstract_code, Module});
{error, beam_lib, Reason} ->
throw({no_abstract_code, Reason})
end.

get_otp_version() ->
Version = erlang:system_info(otp_release),
case re:run(Version, "^[0-9][0-9]", [{capture, first, list}]) of
{match, [V]} ->
list_to_integer(V);
_ ->
%% Could be anything below R17, we are not interested
0
end.

get_original_pairs(VersionSupport) ->
[{Orig, Arity} || {Orig, Arity, _Pre, _Post} <- VersionSupport].

get_delete_pairs(true, VersionSupport) ->
[{Pre, Arity} || {_Orig, Arity, Pre, _Post} <- VersionSupport];
get_delete_pairs(false, VersionSupport) ->
[{Post, Arity} || {_Orig, Arity, _Pre, Post} <- VersionSupport].

get_rename_pairs(true, VersionSupport) ->
[{Post, Arity} || {_Orig, Arity, _Pre, Post} <- VersionSupport];
get_rename_pairs(false, VersionSupport) ->
[{Pre, Arity} || {_Orig, Arity, Pre, _Post} <- VersionSupport].

%% Pairs of {Renamed, OriginalName} functions
get_name_pairs(true, VersionSupport) ->
[{{Post, Arity}, Orig} || {Orig, Arity, _Pre, Post} <- VersionSupport];
get_name_pairs(false, VersionSupport) ->
[{{Pre, Arity}, Orig} || {Orig, Arity, Pre, _Post} <- VersionSupport].

delete_abstract_functions(ToDelete) ->
fun(Tree, Function) ->
case lists:member(Function, ToDelete) of
true ->
erl_syntax:comment(["Deleted unused function"]);
false ->
Tree
end
end.

rename_abstract_functions(ToRename, ToName) ->
fun(Tree, Function) ->
case lists:member(Function, ToRename) of
true ->
FunctionName = proplists:get_value(Function, ToName),
erl_syntax:function(
erl_syntax:atom(FunctionName),
erl_syntax:function_clauses(Tree));
false ->
Tree
end
end.

replace_forms(Module, ErlangVersion, AbsCode) ->
%% Obtain attribute containing the list of functions that must be updated
Attr = Module:module_info(attributes),
VersionSupport = proplists:get_value(erlang_version_support, Attr),
{Pre, Post} = lists:splitwith(fun({Version, _Pairs}) ->
Version > ErlangVersion
end, VersionSupport),
%% Replace functions in two passes: replace for Erlang versions > current
%% first, Erlang versions =< current afterwards.
replace_version_forms(
true, replace_version_forms(false, AbsCode, get_version_functions(Pre)),
get_version_functions(Post)).

get_version_functions(List) ->
lists:append([Pairs || {_Version, Pairs} <- List]).

replace_version_forms(IsPost, AbsCode, VersionSupport) ->
%% Get pairs of {Function, Arity} for the triggering functions, which
%% are also the final function names.
Original = get_original_pairs(VersionSupport),
%% Get pairs of {Function, Arity} for the unused version
ToDelete = get_delete_pairs(IsPost, VersionSupport),
%% Delete original functions (those that trigger the code update) and
%% the unused version ones
DeleteFun = delete_abstract_functions(ToDelete ++ Original),
AbsCode0 = replace_function_forms(AbsCode, DeleteFun),
%% Get pairs of {Function, Arity} for the current version which must be
%% renamed
ToRename = get_rename_pairs(IsPost, VersionSupport),
%% Get paris of {Renamed, OriginalName} functions
ToName = get_name_pairs(IsPost, VersionSupport),
%% Rename versioned functions with their final name
RenameFun = rename_abstract_functions(ToRename, ToName),
%% Remove exports of all versioned functions
remove_exports(replace_function_forms(AbsCode0, RenameFun),
ToDelete ++ ToRename).

replace_function_forms(AbsCode, Fun) ->
ReplaceFunction =
fun(Tree) ->
Function = erl_syntax_lib:analyze_function(Tree),
Fun(Tree, Function)
end,
Filter = fun(Tree) ->
case erl_syntax:type(Tree) of
function -> ReplaceFunction(Tree);
_Other -> Tree
end
end,
fold_syntax_tree(Filter, AbsCode).

filter_export_pairs(Info, ToDelete) ->
lists:filter(fun(Pair) ->
not lists:member(Pair, ToDelete)
end, Info).

remove_exports(AbsCode, ToDelete) ->
RemoveExports =
fun(Tree) ->
case erl_syntax_lib:analyze_attribute(Tree) of
{export, Info} ->
Remaining = filter_export_pairs(Info, ToDelete),
rebuild_export(Remaining);
_Other -> Tree
end
end,
Filter = fun(Tree) ->
case erl_syntax:type(Tree) of
attribute -> RemoveExports(Tree);
_Other -> Tree
end
end,
fold_syntax_tree(Filter, AbsCode).

rebuild_export(Args) ->
erl_syntax:attribute(
erl_syntax:atom(export),
[erl_syntax:list(
[erl_syntax:arity_qualifier(erl_syntax:atom(N),
erl_syntax:integer(A))
|| {N, A} <- Args])]).

fold_syntax_tree(Filter, Forms) ->
Tree = erl_syntax:form_list(Forms),
NewTree = erl_syntax_lib:map(Filter, Tree),
erl_syntax:revert_forms(NewTree).
Loading

0 comments on commit d4a9eca

Please sign in to comment.