Search in sources :

Example 1 with RemoteCallEntry

use of org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry in project kontraktor by RuedigerMoeller.

the class AbstractKrouter method forwardMultiCallInternal.

// tweak handling such it can deal with double results, runs in caller thread (but single threaded => remoteref registry)
@CallerSideMethod
protected void forwardMultiCallInternal(RemoteCallEntry rceIn, Actor remoteRef, ConnectionRegistry clientRemoteRegistry, boolean[] done, Callback[] selected) {
    RemoteCallEntry rce = rceIn.createCopy();
    Runnable toRun = () -> {
        ConnectionRegistry serviceRemoteReg = remoteRef.__self.__clientConnection;
        if (// targets krouter facade
        rce.getReceiverKey() == 1)
            rce.setReceiverKey(remoteRef.__remoteId);
        if (rce.getFutureKey() != 0) {
            long prevFuturekey = rce.getFutureKey();
            Promise p = new Promise();
            long cbid = serviceRemoteReg.registerPublishedCallback(p);
            rce.setFutureKey(-cbid);
            p.then((r, e) -> {
                self().execute(() -> {
                    serviceRemoteReg.removePublishedObject(cbid);
                    // websocket close is unreliable
                    if (serviceRemoteReg.isTerminated()) {
                        handleServiceDiscon(remoteRef);
                        return;
                    }
                    if (!done[0]) {
                        done[0] = true;
                        RemoteCallEntry cbrce = (RemoteCallEntry) r;
                        cbrce.setReceiverKey(prevFuturekey);
                        clientRemoteRegistry.forwardRemoteMessage(cbrce);
                    }
                });
            });
        }
        if (rce.getCB() != null) {
            Callback cb = rce.getCB();
            CallbackWrapper[] wrapperTrick = { null };
            CallbackWrapper wrapper = wrapperTrick[0] = new CallbackWrapper(self(), (r, e) -> {
                if (selected[0] == null) {
                    selected[0] = wrapperTrick[0];
                }
                if (selected[0] == wrapperTrick[0])
                    cb.complete(r, e);
            });
            rce.setCB(wrapper);
        }
        // websocket close is unreliable
        if (!serviceRemoteReg.isTerminated())
            serviceRemoteReg.forwardRemoteMessage(rce);
        else {
            handleServiceDiscon(remoteRef);
        }
    };
    if (Thread.currentThread() != getCurrentDispatcher())
        self().execute(toRun);
    else
        toRun.run();
}
Also used : java.util(java.util) CallbackWrapper(org.nustaq.kontraktor.impl.CallbackWrapper) RemoteCallEntry(org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod) BiFunction(java.util.function.BiFunction) CallbackRefSerializer(org.nustaq.kontraktor.remoting.encoding.CallbackRefSerializer) Log(org.nustaq.kontraktor.util.Log) org.nustaq.kontraktor(org.nustaq.kontraktor) Local(org.nustaq.kontraktor.annotations.Local) org.nustaq.kontraktor.remoting.base(org.nustaq.kontraktor.remoting.base) RemoteCallEntry(org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry) CallbackWrapper(org.nustaq.kontraktor.impl.CallbackWrapper) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod)

Example 2 with RemoteCallEntry

use of org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry in project kontraktor by RuedigerMoeller.

the class AbstractKrouter method __dispatchRemoteCall.

@Override
@CallerSideMethod
public boolean __dispatchRemoteCall(ObjectSocket objSocket, RemoteCallEntry rce, // registry of client connection
ConnectionRegistry clientRemoteRegistry, List<IPromise> createdFutures, Object authContext, BiFunction<Actor, String, Boolean> callInterceptor) {
    boolean isCB = rce.getMethod() != null;
    if (isCB && rce.getMethod().startsWith("router$")) {
        return super.__dispatchRemoteCall(objSocket, rce, clientRemoteRegistry, createdFutures, authContext, callInterceptor);
    }
    // if ( isCB ) {
    // getActor().responseCounter.count();
    // } else {
    // getActor().requestCounter.count();
    // }
    // getActor().trafficCounter.count(rce.getSerializedArgs().length);
    boolean success = dispatchRemoteCall(rce, clientRemoteRegistry);
    if (!success) {
        if (rce.getCB() != null) {
            rce.getCB().reject(SERVICE_UNAVAILABLE);
        }
        if (rce.getFutureKey() != 0) {
            RemoteCallEntry cbrce = createErrorPromiseResponse(rce, clientRemoteRegistry);
            clientRemoteRegistry.inFacadeThread(() -> {
                clientRemoteRegistry.forwardRemoteMessage(cbrce);
            });
        }
    }
    return false;
}
Also used : RemoteCallEntry(org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod)

Example 3 with RemoteCallEntry

use of org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry in project kontraktor by RuedigerMoeller.

the class AbstractKrouter method forwardCallInternal.

// call from client to service
// mission: patch remotecall ids such everything is mappe via Krouter registry
protected void forwardCallInternal(RemoteCallEntry rce, Actor remoteRef, ConnectionRegistry clientRemoteRegistry) {
    RemoteCallEntry finalRce = rce.createCopy();
    Runnable toRun = () -> {
        ConnectionRegistry serviceRemoteReg = (ConnectionRegistry) remoteRef.__self.__clientConnection;
        if (// targets krouter facade
        finalRce.getReceiverKey() == 1)
            finalRce.setReceiverKey(remoteRef.__remoteId);
        if (finalRce.getFutureKey() != 0) {
            long prevFuturekey = finalRce.getFutureKey();
            Promise p = new Promise();
            long cbid = serviceRemoteReg.registerPublishedCallback(p);
            finalRce.setFutureKey(-cbid);
            p.then((r, e) -> {
                serviceRemoteReg.removePublishedObject(cbid);
                // websocket close is unreliable
                if (serviceRemoteReg.isTerminated()) {
                    handleServiceDiscon(remoteRef);
                    return;
                }
                RemoteCallEntry cbrce = (RemoteCallEntry) r;
                cbrce.setReceiverKey(prevFuturekey);
                self().execute(() -> clientRemoteRegistry.forwardRemoteMessage(cbrce));
            });
        }
        if (finalRce.getCB() != null) {
            CallbackRefSerializer.MyRemotedCallback cb = (CallbackRefSerializer.MyRemotedCallback) finalRce.getCB();
            long id = cb.getId();
            finalRce.setCB(new CallbackWrapper(self(), (res, err) -> {
                RemoteCallEntry rcerouted = (RemoteCallEntry) res;
                rcerouted.setReceiverKey(id);
                clientRemoteRegistry.forwardRemoteMessage(rcerouted);
            }) {

                @Override
                public boolean isRouted() {
                    return true;
                }
            });
        }
        // websocket close is unreliable
        if (!serviceRemoteReg.isTerminated())
            serviceRemoteReg.forwardRemoteMessage(finalRce);
        else {
            handleServiceDiscon(remoteRef);
        }
    };
    if (Thread.currentThread() != getCurrentDispatcher())
        self().execute(toRun);
    else
        toRun.run();
}
Also used : java.util(java.util) CallbackWrapper(org.nustaq.kontraktor.impl.CallbackWrapper) RemoteCallEntry(org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod) BiFunction(java.util.function.BiFunction) CallbackRefSerializer(org.nustaq.kontraktor.remoting.encoding.CallbackRefSerializer) Log(org.nustaq.kontraktor.util.Log) org.nustaq.kontraktor(org.nustaq.kontraktor) Local(org.nustaq.kontraktor.annotations.Local) org.nustaq.kontraktor.remoting.base(org.nustaq.kontraktor.remoting.base) RemoteCallEntry(org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry) CallbackRefSerializer(org.nustaq.kontraktor.remoting.encoding.CallbackRefSerializer) CallbackWrapper(org.nustaq.kontraktor.impl.CallbackWrapper)

Example 4 with RemoteCallEntry

use of org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry in project kontraktor by RuedigerMoeller.

the class AbstractKrouter method sendFailoverNotificationInternal.

protected void sendFailoverNotificationInternal(ConnectionRegistry registry) {
    RemoteCallEntry rce = new RemoteCallEntry(0, 0, "krouterTargetDidChange", null, registry.getConf().asByteArray(new Object[] {}));
    registry.inFacadeThread(() -> registry.forwardRemoteMessage(rce));
}
Also used : RemoteCallEntry(org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry)

Example 5 with RemoteCallEntry

use of org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry in project kontraktor by RuedigerMoeller.

the class AbstractKrouter method router$clientPing.

@Override
public IPromise<Long> router$clientPing(long tim, long[] publishedActorIds) {
    for (int i = 0; i < publishedActorIds.length; i++) {
        long publishedActorId = publishedActorIds[i];
        nextAliveRemoteActors.add(publishedActorId);
    }
    if (lastSwitch == 0) {
        lastSwitch = System.currentTimeMillis();
    } else {
        long now = System.currentTimeMillis();
        if (now - lastSwitch > CLIENT_PING_INTERVAL_MS * 2) {
            Set<Long> tmp = this.nextAliveRemoteActors;
            getServices().forEach(serv -> tmp.add(serv.__remoteId));
            nextAliveRemoteActors = new HashSet<>();
            lastSwitch = now;
            // System.out.println("alive:");
            // tmp.forEach( l -> System.out.println("  "+l));
            ConnectionRegistry reg = connection.get();
            long[] alive = new long[tmp.size()];
            int idx = 0;
            for (Iterator<Long> iterator = tmp.iterator(); iterator.hasNext(); ) {
                Long next = iterator.next();
                alive[idx++] = next;
            }
            if (reg != null) {
                RemoteCallEntry rce = new RemoteCallEntry(0, 1, "zzRoutingRefGC", null, reg.getConf().asByteArray(new Object[] { alive }));
                getServices().forEach(service -> {
                    forwardMultiCall(rce, service, reg, null, null);
                });
            }
        }
    }
    return super.router$clientPing(tim, publishedActorIds);
}
Also used : RemoteCallEntry(org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry)

Aggregations

RemoteCallEntry (org.nustaq.kontraktor.remoting.encoding.RemoteCallEntry)7 CallerSideMethod (org.nustaq.kontraktor.annotations.CallerSideMethod)5 java.util (java.util)2 BiFunction (java.util.function.BiFunction)2 org.nustaq.kontraktor (org.nustaq.kontraktor)2 Local (org.nustaq.kontraktor.annotations.Local)2 CallbackWrapper (org.nustaq.kontraktor.impl.CallbackWrapper)2 org.nustaq.kontraktor.remoting.base (org.nustaq.kontraktor.remoting.base)2 CallbackRefSerializer (org.nustaq.kontraktor.remoting.encoding.CallbackRefSerializer)2 Log (org.nustaq.kontraktor.util.Log)2