Search in sources :

Example 11 with Channel

use of org.apache.dubbo.remoting.Channel in project dubbo by alibaba.

the class TraceFilter method invoke.

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    long start = System.currentTimeMillis();
    Result result = invoker.invoke(invocation);
    long end = System.currentTimeMillis();
    if (TRACERS.size() > 0) {
        String key = invoker.getInterface().getName() + "." + invocation.getMethodName();
        Set<Channel> channels = TRACERS.get(key);
        if (channels == null || channels.isEmpty()) {
            key = invoker.getInterface().getName();
            channels = TRACERS.get(key);
        }
        if (CollectionUtils.isNotEmpty(channels)) {
            for (Channel channel : new ArrayList<>(channels)) {
                if (channel.isConnected()) {
                    try {
                        int max = 1;
                        Integer m = (Integer) channel.getAttribute(TRACE_MAX);
                        if (m != null) {
                            max = m;
                        }
                        int count = 0;
                        AtomicInteger c = (AtomicInteger) channel.getAttribute(TRACE_COUNT);
                        if (c == null) {
                            c = new AtomicInteger();
                            channel.setAttribute(TRACE_COUNT, c);
                        }
                        count = c.getAndIncrement();
                        if (count < max) {
                            String prompt = channel.getUrl().getParameter(Constants.PROMPT_KEY, Constants.DEFAULT_PROMPT);
                            channel.send("\r\n" + RpcContext.getContext().getRemoteAddress() + " -> " + invoker.getInterface().getName() + "." + invocation.getMethodName() + "(" + JSON.toJSONString(invocation.getArguments()) + ")" + " -> " + JSON.toJSONString(result.getValue()) + "\r\nelapsed: " + (end - start) + " ms." + "\r\n\r\n" + prompt);
                        }
                        if (count >= max - 1) {
                            channels.remove(channel);
                        }
                    } catch (Throwable e) {
                        channels.remove(channel);
                        logger.warn(e.getMessage(), e);
                    }
                } else {
                    channels.remove(channel);
                }
            }
        }
    }
    return result;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Channel(org.apache.dubbo.remoting.Channel) ArrayList(java.util.ArrayList) Result(org.apache.dubbo.rpc.Result)

Example 12 with Channel

use of org.apache.dubbo.remoting.Channel in project dubbo by alibaba.

the class TraceFilterTest method testAddAndRemoveTracer.

@Test
public void testAddAndRemoveTracer() throws Exception {
    String method = "sayHello";
    Class<?> type = DemoService.class;
    String key = type.getName() + "." + method;
    // add tracer
    TraceFilter.addTracer(type, method, mockChannel, 100);
    Assertions.assertEquals(100, mockChannel.getAttribute(TRACE_MAX));
    Assertions.assertTrue(mockChannel.getAttribute(TRACE_COUNT) instanceof AtomicInteger);
    Field tracers = TraceFilter.class.getDeclaredField(TRACERS_FIELD_NAME);
    tracers.setAccessible(true);
    ConcurrentHashMap<String, Set<Channel>> o = (ConcurrentHashMap<String, Set<Channel>>) tracers.get(new ConcurrentHashMap<String, Set<Channel>>());
    Assertions.assertTrue(o.containsKey(key));
    Set<Channel> channels = o.get(key);
    Assertions.assertNotNull(channels);
    Assertions.assertTrue(channels.contains(mockChannel));
    // remove tracer
    TraceFilter.removeTracer(type, method, mockChannel);
    Assertions.assertNull(mockChannel.getAttribute(TRACE_MAX));
    Assertions.assertNull(mockChannel.getAttribute(TRACE_COUNT));
    Assertions.assertFalse(channels.contains(mockChannel));
}
Also used : Field(java.lang.reflect.Field) Set(java.util.Set) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Channel(org.apache.dubbo.remoting.Channel) DemoService(org.apache.dubbo.rpc.protocol.dubbo.support.DemoService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.jupiter.api.Test)

Example 13 with Channel

use of org.apache.dubbo.remoting.Channel in project dubbo by alibaba.

the class DefaultFuture method closeChannel.

/**
 * close a channel when a channel is inactive
 * directly return the unfinished requests.
 *
 * @param channel channel to close
 */
public static void closeChannel(Channel channel) {
    for (Map.Entry<Long, Channel> entry : CHANNELS.entrySet()) {
        if (channel.equals(entry.getValue())) {
            DefaultFuture future = getFuture(entry.getKey());
            if (future != null && !future.isDone()) {
                Response disconnectResponse = new Response(future.getId());
                disconnectResponse.setStatus(Response.CHANNEL_INACTIVE);
                disconnectResponse.setErrorMessage("Channel " + channel + " is inactive. Directly return the unFinished request : " + (logger.isDebugEnabled() ? future.getRequest() : future.getRequest().copyWithoutData()));
                DefaultFuture.received(channel, disconnectResponse);
            }
        }
    }
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Channel(org.apache.dubbo.remoting.Channel) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map)

Example 14 with Channel

use of org.apache.dubbo.remoting.Channel in project dubbo by alibaba.

the class HeaderExchangeServer method getExchangeChannels.

@Override
public Collection<ExchangeChannel> getExchangeChannels() {
    Collection<ExchangeChannel> exchangeChannels = new ArrayList<ExchangeChannel>();
    Collection<Channel> channels = server.getChannels();
    if (CollectionUtils.isNotEmpty(channels)) {
        for (Channel channel : channels) {
            exchangeChannels.add(HeaderExchangeChannel.getOrAddChannel(channel));
        }
    }
    return exchangeChannels;
}
Also used : ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel) Channel(org.apache.dubbo.remoting.Channel) ArrayList(java.util.ArrayList) ExchangeChannel(org.apache.dubbo.remoting.exchange.ExchangeChannel)

Example 15 with Channel

use of org.apache.dubbo.remoting.Channel in project dubbo by alibaba.

the class AbstractClient method setAttribute.

@Override
public void setAttribute(String key, Object value) {
    Channel channel = getChannel();
    if (channel == null) {
        return;
    }
    channel.setAttribute(key, value);
}
Also used : Channel(org.apache.dubbo.remoting.Channel)

Aggregations

Channel (org.apache.dubbo.remoting.Channel)53 Test (org.junit.jupiter.api.Test)32 Request (org.apache.dubbo.remoting.exchange.Request)18 ChannelBuffer (org.apache.dubbo.remoting.buffer.ChannelBuffer)16 Response (org.apache.dubbo.remoting.exchange.Response)12 ExchangeChannel (org.apache.dubbo.remoting.exchange.ExchangeChannel)9 URL (org.apache.dubbo.common.URL)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 HeaderExchangeHandler (org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)6 RemotingException (org.apache.dubbo.remoting.RemotingException)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ExchangeHandler (org.apache.dubbo.remoting.exchange.ExchangeHandler)4 MockedChannel (org.apache.dubbo.remoting.handler.MockedChannel)4 AppResponse (org.apache.dubbo.rpc.AppResponse)4 Demo (org.apache.dubbo.rpc.gen.thrift.Demo)4 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)4 TMessage (org.apache.thrift.protocol.TMessage)4 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)4 IOException (java.io.IOException)3