Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

distributed computing - Erlang: Offload a client process/function to a server?

My scenario is as follows - I have a client C with function foo() which performs some computation.

I'd like a server S, which doesn't know about foo(), to execute this function instead, and send the result back to the client.

I am trying to determine the best way to perform this in Erlang. I am considering:

  • Hot code swapping - i.e. "upgrade" code in S such that it has the function foo(). Execute and send back to the client.
  • In a distributed manner where nodes are all appropriately registered, do something along the lines of S ! C:foo() - for the purpose of "sending" the function to process/node S

Are there other methods (or features of the language) that I am not thinking of?

Thanks for the help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If the computation function is self contained i.e. does not depend on any other modules or functions on the client C, then what you need to do is a fun (Functional Objects). A fun can be sent across the network and applied by a remote machine and in side the fun, the sender has embedded their address and a way of getting the answer back. So the executor may only see a fun to which they may or may not give an argument, yet inside the fun, the sender has forced a method where by the answer will automatically be sent back. The fun is an abstraction of very many tasks within one thing, and it can be moved around as arguments.
At the client, you can have code like this:


%% somewhere in the client
%% client runs on node() == '[email protected]'

-module(client).
-compile(export_all).
-define(SERVER,{server,'[email protected]'}).

give_a_server_a_job(Number)-> ?SERVER ! {build_fun(),Number}.

build_fun()->
    FunObject = fun(Param)-> 
                    Answer = Param * 20/1000, %% computation here
                    rpc:call('[email protected]',client,answer_ready,[Answer])
                end,
    FunObject.

answer_ready(Answer)-> 
    %%% use Answer for all sorts of funny things....
    io:format("
Answer is here: ~p~n",[Answer]).

The server then has code like this:


%%% somewhere on the server
%%% server runs on node() == '[email protected]'

-module(server).
-compile(export_all).

start()-> register(server,spawn(?MODULE,loop,[])).

loop()->
    receive
        {Fun,Arg} -> 
            Fun(Arg),   %% server executes job
                        %% job automatically sends answer back
                        %% to client
            loop();
        stop -> exit(normal);
        _ -> loop()
    end.

In this way, the job executor needs not know about how to send back the reply, The job itself comes knowing how it will send back the answer to however sent the job!. I have used this method of sending functional objects across the network in several project, its so cool !!!

#### EDIT #####

If you have a recursive problem, You manipulate recursion using funs. However, you will need at least one library function at the client and/or the server to assist in recursive manipulations. Create a function which should be in the code path of the client as well as the server.

Another option is to dynamically send code from the server to the client and then using the library: Dynamic Compile erlang to load and execute erlang code at the server from the client. Using dynamic compile, here is an example:

1> String = "-module(add).
 -export([add/2]). 
 add(A,B) -> A + B. 
".
"-module(add).
 -export([add/2]). 
 add(A,B) -> A + B. 
"
2> dynamic_compile:load_from_string(String).
{module,add}
3> add:add(2,5).
7
4>

What we see above is a piece of module code that is compiled and loaded dynamically from a string. If the library enabling this is available at the server and client , then each entity can send code as a string and its loaded and executed dynamically at the other. This code can be unloaded after use. Lets look at the Fibonacci function and how it can be sent and executed at the server:

%% This is the normal Fibonacci code which we are to convert into a string:

-module(fib).
-export([fib/1]).

fib(N) when N == 0 -> 0;
fib(N) when (N < 3) and (N > 0) -> 1;
fib(N) when N > 0 -> fib(N-1) + fib(N-2).

%% In String format, this would now become this piece of code
StringCode = " -module(fib).
 -export([fib/1]). 
fib(N) when N == 0 -> 0;
 fib(N) when (N < 3) and (N > 0) -> 1;
 fib(N) when N > 0 -> fib(N-1) + fib(N-2). 
".

%% Then the client would send this string above to the server and the server would 
%% dynamically load the code and execute it

send_fib_code(Arg)-> {ServerRegName,ServerNode} ! {string,StringCode,fib,Arg}, ok. get_answer({fib,of,This,is,That}) -> io:format("Fibonacci (from server) of ~p is: ~p~n",[This,That]). %%% At Server loop(ServerState)-> receive {string,StringCode,Fib,Arg} when Fib == fib -> try dynamic_compile:load_from_string(StringCode) of {module,AnyMod} -> Answer = AnyMod:fib(Arg), %%% send answer back to client %%% should be asynchronously %%% as the channels are different & not make %% client wait rpc:call('[email protected]',client,get_answer,[{fib,of,Arg,is,Answer}]) catch _:_ -> error_logger:error_report(["Failed to Dynamic Compile & Load Module from client"]) end, loop(ServerState); _ -> loop(ServerState) end.

That piece of rough code can show you what am trying to say. However, you remember to unload all un-usable dynamic modules. Also you can a have a way in which the server tries to check wether such a module was loaded already before loading it again. I advise that you donot copy and paste the above code. Look at it and understand it and then write your own version that can do the job.
success !!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...