![Page 1: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/1.jpg)
Sistemas DistribuıdosChamada Remota de Procedimento
marco de 2015
Sistemas Distribuıdos
![Page 2: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/2.jpg)
RPC: motivacao
send (B, &pedido)receive (B, &resposta)
A B
como facilitar esse padrao tao comum?
encapsulamento de detalhes de comunicacao
criacao, envio e recebimento de mensagensempacotamento de argumentostratamento de reenvio, etc
Sistemas Distribuıdos
![Page 3: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/3.jpg)
RPC: motivacao
send (B, &pedido)receive (B, &resposta)
A B
receive (*, &pedido)trata pedidosend (B, &resposta)
lado do servidor tambem pode se beneficiar de abstracao
uma mesma funcao pode tratar pedido local ou remoto
Sistemas Distribuıdos
![Page 4: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/4.jpg)
RPC: abstracao
A B
res = f(a,b)
originalmente: enfase em transparencia
programa distribuıdo com mesma organizacao que programa local
tratamento automatico de empacotamento e desempacotamento
Sistemas Distribuıdos
![Page 5: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/5.jpg)
RPC: modelo de execucao tradicional
A B
res = f(a,b)
chamador permanece bloqueado ate chegada de resposta
analogia direta com caso local
modelo utilizado largamente em redes locais
servidores de arquivosservidores de impressao...
Sistemas Distribuıdos
![Page 6: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/6.jpg)
RPC – referencia classica
A. Birrell and B. Nelson. 1984. Implementing remote procedurecalls. ACM Transactions on Computer Systems, 2(1), 39-59.
Sistemas Distribuıdos
![Page 7: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/7.jpg)
RPC: implementacao
camadade
aplicação
stub serv.
camadade
aplicação
stub cliente
res = f(a,b)
stubs intermediam comunicacao
Sistemas Distribuıdos
![Page 8: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/8.jpg)
RPC: implementacao
res = foo(a, b)--> void foo (int a, float b) { marshall req send (A, request) receive (B, &resp) unmarshall resp return val }
A B
int foo (int n, float f){ return n+boo(f);}
receive (*, &request)unmarshall requestres = foo(val1, val2)marshall ressend(A, response)
Sistemas Distribuıdos
![Page 9: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/9.jpg)
RPC: implementacao tradicional
geracao automatica de stubs cliente e servidor a partir de interface
introducao de IDLs
Sistemas Distribuıdos
![Page 10: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/10.jpg)
RPC: especificacao de interfaces
struct intpair { int a; int b; }; program ADD_PROG { version ADD_VERS { int ADD(intpair) = 1; } = 1; } = 0x23451111;
exemplo Sun RPC:
pre-compilador (no caso rpcgen)
geracao de stub cliente e stub servidor, com chamadas a bibliotecaRPC
marshallingunmarshallingcomunicacao na redearquivo de interface para cliente
Sistemas Distribuıdos
![Page 11: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/11.jpg)
RPC: empacotamento de dados (1)
problemas com representacoes diferentes e alinhamento de dados
surgimento de protocolos e formatos padronizados
biblioteca XDR, formato ASN.1 (ISO), ...codificacoes com tipo explıcito X implıcito
Sistemas Distribuıdos
![Page 12: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/12.jpg)
RPC: empacotamento de dados (2)
referencias de memoria nao fazem sentido
empacotamento de estruturas complexas?
possibilidade do programador definir empacotamentos
referencias voltam a fazer sentido no contexto de objetos distribuıdos!
Sistemas Distribuıdos
![Page 13: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/13.jpg)
Localizacao de Servidores
binding
problema semelhante ao de localizacao de destinatario demensagem, mas agora com abstracao de mais alto nıvel
uso de bases de dados centralizadasuso de bases de dados por maquina
Sistemas Distribuıdos
![Page 14: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/14.jpg)
Java RMI
utilizacao de caracterısticas da linguagem
interfaces x classes
interface Remote define propriedades comuns a todos os objetosremotos usada nas declaracoes do cliente
excecao RemoteException
classe UnicastRemoteObject implementa funcionalidade basica deobjeto remoto estendida pela implementacao do objeto servidor
carga dinamica (download) de stubs e de implementacoes deargumentos
Sistemas Distribuıdos
![Page 15: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/15.jpg)
Java RMI – Cliente
package client;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
...
public class ComputePi {
public static void main(String args[]) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
try {
String name = "Compute";
Registry registry = LocateRegistry.getRegistry(args[0]);
Compute comp = (Compute) registry.lookup(name);
Pi task = new Pi(Integer.parseInt(args[1]));
BigDecimal pi = comp.executeTask(task);
System.out.println(pi);
} catch (Exception e) {
System.err.println("ComputePi exception:"); e.printStackTrace();
}
}
}
Sistemas Distribuıdos
![Page 16: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/16.jpg)
Java RMI – Servidor
/* SampleServer.java */
import java.rmi.*;
public interface SampleServer extends Remote
{
public int sum(int a,int b) throws RemoteException;
}
/* SampleServerImpl.java */
...
public class SampleServerImpl extends UnicastRemoteObject implements SampleServer
{
SampleServerImpl() throws RemoteException
{
super();
}
Sistemas Distribuıdos
![Page 17: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/17.jpg)
RPC: falhas e semantica
transparencia preconizada inicialmente “quebra” diante dapossibilidade de falhas
diferenca para chamadas locais
classificacao em diferentes modelos: exatamente uma vez, nomaximo uma vez, no mınimo uma vez
importancia de chamadas idempotentes
tratamento de excecoes
Sistemas Distribuıdos
![Page 18: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/18.jpg)
RPC: concorrencia
como sobrepor tempo de chamada com processamento?
solucao classica: uso de threads
A B
Sistemas Distribuıdos
![Page 19: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/19.jpg)
RPC: cenarios pouco acoplados
problemas com sincronizacao causada pelo bloqueio
escalabilidade de threads de OS
coleta de lixo: threads a espera de servidores que falham
sobrecarga de preempcao
Sistemas Distribuıdos
![Page 20: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/20.jpg)
RPC e assincronismo
oneway
futuros
chamadas assıncronas
Sistemas Distribuıdos
![Page 21: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/21.jpg)
Futuros
chamada retorna imediatamente devolvendo um descritor
descritor usado posteriormente para sincronizacao
muito popular atualmente!
Sistemas Distribuıdos
![Page 22: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/22.jpg)
Futuros em C++11
#include <future>
#include <iostream>
#include <vector>
int twice(int m) {
return 2 * m;
}
int main() {
std::vector<std::future<int>> futures;
for(int i = 0; i < 10; ++i) {
futures.push_back (std::async(twice, i));
}
//retrive and print the value stored in the future
for(auto &e : futures) {
std::cout << e.get() << std::endl;
}
return 0;
}
Sistemas Distribuıdos
![Page 23: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/23.jpg)
Futuros em ProActive
Sistemas Distribuıdos
![Page 24: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/24.jpg)
Futuros e avaliacao postergada
objetos retornados por operacoes assıncronas podem ser passadoscomo argumentos em novas operacoes
otimizacao da transferencia de dados
v1 = a.foo (...); // chamada assıncrona
v2 = a.bar(...); // chamada assıncrona
...
v1.f(v2)
Sistemas Distribuıdos
![Page 25: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/25.jpg)
RPC assıncrona
chamada retorna imediatamente
retorno dispara execucao de callback
em alguns casos, callback especificada na chamada
casamento com modelo de execucao em uso
Sistemas Distribuıdos
![Page 26: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/26.jpg)
Chamada assıncrona “em Lua”
function collect(val)
acc = acc + val
repl = repl + 1
if (repl==expected) then print ("Current Value: ",
acc/repl)
end
end
function askvals (peers)
repl = 0; expected = 0; acc = 0
for p in pairs (peers) do
expected = expected + 1
p:currValue{callback=collect}
end
end
estado registrado em globais
e se novo pedido for realizado antes do primeiro estar completo?
Sistemas Distribuıdos
![Page 27: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/27.jpg)
Chamada assıncrona “em Lua” com closures
function request(peers)
local acc, repl = 0, 0
local expected = table.getn(peers)
------------
function avrg (val)
repl = repl+1
acc = acc + val
if (repl==expected) then print ("Current Value: ",
acc/repl)
end
end
-------------
for _,p in ipairs (peers) do
rpc.async(p, "currValue", avrg)()
end
end
Sistemas Distribuıdos
![Page 28: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/28.jpg)
RPC: outras crıticas
acoplamento cliente-servidor tambem espacial
identificacao de servidor que deve tratar a requisicao
endereco bem conhecido funciona bem em ambientes controlados
Sistemas Distribuıdos
![Page 29: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/29.jpg)
Trabalho: RPC com Lua
luarpc — construcao dinamica de stubs
registerServant (idl, servantobject)
waitIncomingRequests ()
createProxy (idl, ip, port)
Sistemas Distribuıdos
![Page 30: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/30.jpg)
Trabalho: RPC com Lua
o1 = { foo = function(a, b) return a+b, "alo alo" end, boo = function (self, z) self.bar, self. foo = self.foo, self.bar end, bar = function(a, b) return a-b, "tchau tchau" end, }o2 = ( foo = function(m, n) … }ip, p = registerServant (idl, o1)print ("sou 1, estou esperando reqs na porta " .. p)ip, p = registerServant (idl, o2)print ("sou 2, estou esperando reqs na porta " .. p)waitIncoming()
rep1 = createProxy (idlserv, ip, porta)rep2 = createProxy(idlserv, ip, outraporta)…print (rep1:foo(4,5))rep1:boo()print (rep2:foo(x,y))
tanto cliente como servidor sao single-threaded
servidor deve poder receber pedidos para qualquer servente
Sistemas Distribuıdos
![Page 31: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/31.jpg)
Trabalho: RPC com Lua
o1 = { foo = function(a, b) return a+b, "alo alo" end, boo = function (self, z) self.bar, self. foo = self.foo, self.bar end, bar = function(a, b) return a-b, "tchau tchau" end, }o2 = {f = ...}ip, p = registerServant (idl, o1)print ("estou esperando reqs para xxx na porta " .. p)ip, p = registerServant (outraidl, o2)print ("estou esperando reqs para yyy na porta " .. p)waitIncoming()
rep = createProxy (idl1, ip, porta)…print (rep:foo(4,5))rep:boo()
p = createProxy (idl2, ip, porta)…p:f()...
Sistemas Distribuıdos
![Page 32: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/32.jpg)
RPC com Lua — IDL
interface { name = minhaInt,
methods = {
foo = {
resulttype = "double",
args = {{direction = "in",
type = "double"},
{direction = "in",
type = "double"},
{direction = "out",
type = "string"},
}
},
boo = {
resulttype = "void",
args = {{ direction = "inout",
type = "double"},
}
}
}
}Sistemas Distribuıdos
![Page 33: Sistemas Distribuídos - Chamada Remota de Procedimentonoemi/sd-15/aula4-rpc.pdf · return n+boo(f);} receive (*, &request) unmarshall request res = foo(val1, val2) marshall res send(A,](https://reader030.vdocuments.mx/reader030/viewer/2022040612/5f02384d7e708231d4032bac/html5/thumbnails/33.jpg)
trecho extraıdo de trabalho anterior
function createrpcproxy(hostname, port, interface)
local functions = {}
local prototypes = parser(interface)
for name,sig in pairs(prototypes) do
functions[name] = function(...)
-- validating params
local params = {...}
local values = {name}
local types = sig.input
for i=1,#types do
if (#params >= i) then
values[#values+1] = params[i]
if (type(params[i])~="number") then
values[#values] = "\"" .. values[#values] .. "\""
end
...
end
-- creating request
local request = pack(values)
-- creating socket
local client = socket.tcp()
...
local conn = client:connect(hostname, port)
...
local result = client:send(request .. ’\n’)
...
end
end
return functions;
end
Sistemas Distribuıdos