org.apache.rocketmq.remoting.netty.RequestTask Java Examples

The following examples show how to use org.apache.rocketmq.remoting.netty.RequestTask. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BrokerFastFailure.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private void cleanExpiredRequest() {
    while (this.brokerController.getMessageStore().isOSPageCacheBusy()) {
        try {
            if (!this.brokerController.getSendThreadPoolQueue().isEmpty()) {
                final Runnable runnable = this.brokerController.getSendThreadPoolQueue().poll(0, TimeUnit.SECONDS);
                if (null == runnable) {
                    break;
                }

                final RequestTask rt = castRunnable(runnable);
                rt.returnResponse(RemotingSysResponseCode.SYSTEM_BUSY, String.format("[PCBUSY_CLEAN_QUEUE]broker busy, start flow control for a while, period in queue: %sms, size of queue: %d", System.currentTimeMillis() - rt.getCreateTimestamp(), this.brokerController.getSendThreadPoolQueue().size()));
            } else {
                break;
            }
        } catch (Throwable ignored) {
        }
    }

    cleanExpiredRequestInQueue(this.brokerController.getSendThreadPoolQueue(),
        this.brokerController.getBrokerConfig().getWaitTimeMillsInSendQueue());

    cleanExpiredRequestInQueue(this.brokerController.getPullThreadPoolQueue(),
        this.brokerController.getBrokerConfig().getWaitTimeMillsInPullQueue());
}
 
Example #2
Source File: BrokerFastFailure.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private void cleanExpiredRequest() {
    while (this.brokerController.getMessageStore().isOSPageCacheBusy()) {
        try {
            if (!this.brokerController.getSendThreadPoolQueue().isEmpty()) {
                final Runnable runnable = this.brokerController.getSendThreadPoolQueue().poll(0, TimeUnit.SECONDS);
                if (null == runnable) {
                    break;
                }

                final RequestTask rt = castRunnable(runnable);
                rt.returnResponse(RemotingSysResponseCode.SYSTEM_BUSY, String.format("[PCBUSY_CLEAN_QUEUE]broker busy, start flow control for a while, period in queue: %sms, size of queue: %d", System.currentTimeMillis() - rt.getCreateTimestamp(), this.brokerController.getSendThreadPoolQueue().size()));
            } else {
                break;
            }
        } catch (Throwable ignored) {
        }
    }

    cleanExpiredRequestInQueue(this.brokerController.getSendThreadPoolQueue(),
        this.brokerController.getBrokerConfig().getWaitTimeMillsInSendQueue());

    cleanExpiredRequestInQueue(this.brokerController.getPullThreadPoolQueue(),
        this.brokerController.getBrokerConfig().getWaitTimeMillsInPullQueue());
}
 
Example #3
Source File: PullMessageProcessor.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
public void executeRequestWhenWakeup(final Channel channel, final RemotingCommand request) throws RemotingCommandException {
    Runnable run = new Runnable() {
        @Override
        public void run() {
            try {
                final RemotingCommand response = PullMessageProcessor.this.processRequest(channel, request, false);

                if (response != null) {
                    response.setOpaque(request.getOpaque());
                    response.markResponseType();
                    try {
                        channel.writeAndFlush(response).addListener(new ChannelFutureListener() {
                            @Override
                            public void operationComplete(ChannelFuture future) throws Exception {
                                if (!future.isSuccess()) {
                                    LOG.error("ProcessRequestWrapper response to {} failed", future.channel().remoteAddress(), future.cause());
                                    LOG.error(request.toString());
                                    LOG.error(response.toString());
                                }
                            }
                        });
                    } catch (Throwable e) {
                        LOG.error("ProcessRequestWrapper process request over, but response failed", e);
                        LOG.error(request.toString());
                        LOG.error(response.toString());
                    }
                }
            } catch (RemotingCommandException e1) {
                LOG.error("ExecuteRequestWhenWakeup run", e1);
            }
        }
    };
    this.brokerController.getPullMessageExecutor().submit(new RequestTask(run, channel, request));
}
 
Example #4
Source File: BrokerController.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public long headSlowTimeMills(BlockingQueue<Runnable> q) {
    long slowTimeMills = 0;
    final Runnable peek = q.peek();
    if (peek != null) {
        RequestTask rt = BrokerFastFailure.castRunnable(peek);
        slowTimeMills = rt == null ? 0 : this.messageStore.now() - rt.getCreateTimestamp();
    }

    if (slowTimeMills < 0)
        slowTimeMills = 0;

    return slowTimeMills;
}
 
Example #5
Source File: PullMessageProcessor.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public void executeRequestWhenWakeup(final Channel channel,
    final RemotingCommand request) throws RemotingCommandException {
    Runnable run = new Runnable() {
        @Override
        public void run() {
            try {
                final RemotingCommand response = PullMessageProcessor.this.processRequest(channel, request, false);

                if (response != null) {
                    response.setOpaque(request.getOpaque());
                    response.markResponseType();
                    try {
                        channel.writeAndFlush(response).addListener(new ChannelFutureListener() {
                            @Override
                            public void operationComplete(ChannelFuture future) throws Exception {
                                if (!future.isSuccess()) {
                                    log.error("processRequestWrapper response to {} failed",
                                        future.channel().remoteAddress(), future.cause());
                                    log.error(request.toString());
                                    log.error(response.toString());
                                }
                            }
                        });
                    } catch (Throwable e) {
                        log.error("processRequestWrapper process request over, but response failed", e);
                        log.error(request.toString());
                        log.error(response.toString());
                    }
                }
            } catch (RemotingCommandException e1) {
                log.error("excuteRequestWhenWakeup run", e1);
            }
        }
    };
    this.brokerController.getPullMessageExecutor().submit(new RequestTask(run, channel, request));
}
 
Example #6
Source File: BrokerFastFailure.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public static RequestTask castRunnable(final Runnable runnable) {
    try {
        FutureTaskExt object = (FutureTaskExt) runnable;
        return (RequestTask) object.getRunnable();
    } catch (Throwable e) {
        log.error(String.format("castRunnable exception, %s", runnable.getClass().getName()), e);
    }

    return null;
}
 
Example #7
Source File: BrokerFastFailure.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
void cleanExpiredRequestInQueue(final BlockingQueue<Runnable> blockingQueue, final long maxWaitTimeMillsInQueue) {
    while (true) {
        try {
            if (!blockingQueue.isEmpty()) {
                final Runnable runnable = blockingQueue.peek();
                if (null == runnable) {
                    break;
                }
                final RequestTask rt = castRunnable(runnable);
                if (rt == null || rt.isStopRun()) {
                    break;
                }

                final long behind = System.currentTimeMillis() - rt.getCreateTimestamp();
                if (behind >= maxWaitTimeMillsInQueue) {
                    if (blockingQueue.remove(runnable)) {
                        rt.setStopRun(true);
                        rt.returnResponse(RemotingSysResponseCode.SYSTEM_BUSY, String.format("[TIMEOUT_CLEAN_QUEUE]broker busy, start flow control for a while, period in queue: %sms, size of queue: %d", behind, blockingQueue.size()));
                    }
                } else {
                    break;
                }
            } else {
                break;
            }
        } catch (Throwable ignored) {
        }
    }
}
 
Example #8
Source File: BrokerFastFailureTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Test
public void testCleanExpiredRequestInQueue() throws Exception {
    BrokerFastFailure brokerFastFailure = new BrokerFastFailure(null);

    BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
    brokerFastFailure.cleanExpiredRequestInQueue(queue, 1);
    assertThat(queue.size()).isZero();

    //Normal Runnable
    Runnable runnable = new Runnable() {
        @Override
        public void run() {

        }
    };
    queue.add(runnable);

    assertThat(queue.size()).isEqualTo(1);
    brokerFastFailure.cleanExpiredRequestInQueue(queue, 1);
    assertThat(queue.size()).isEqualTo(1);

    queue.clear();

    //With expired request
    RequestTask expiredRequest = new RequestTask(runnable, null, null);
    queue.add(new FutureTaskExt<>(expiredRequest, null));
    TimeUnit.MILLISECONDS.sleep(100);

    RequestTask requestTask = new RequestTask(runnable, null, null);
    queue.add(new FutureTaskExt<>(requestTask, null));

    assertThat(queue.size()).isEqualTo(2);
    brokerFastFailure.cleanExpiredRequestInQueue(queue, 100);
    assertThat(queue.size()).isEqualTo(1);
    assertThat(((FutureTaskExt) queue.peek()).getRunnable()).isEqualTo(requestTask);
}
 
Example #9
Source File: BrokerController.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public long headSlowTimeMills(BlockingQueue<Runnable> q) {
    long slowTimeMills = 0;
    final Runnable peek = q.peek();
    if (peek != null) {
        RequestTask rt = BrokerFastFailure.castRunnable(peek);
        slowTimeMills = rt == null ? 0 : this.messageStore.now() - rt.getCreateTimestamp();
    }

    if (slowTimeMills < 0)
        slowTimeMills = 0;

    return slowTimeMills;
}
 
Example #10
Source File: PullMessageProcessor.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public void executeRequestWhenWakeup(final Channel channel, final RemotingCommand request) throws RemotingCommandException {
    Runnable run = new Runnable() {
        @Override
        public void run() {
            try {
                final RemotingCommand response = PullMessageProcessor.this.processRequest(channel, request, false);

                if (response != null) {
                    response.setOpaque(request.getOpaque());
                    response.markResponseType();
                    try {
                        channel.writeAndFlush(response).addListener(new ChannelFutureListener() {
                            @Override
                            public void operationComplete(ChannelFuture future) throws Exception {
                                if (!future.isSuccess()) {
                                    LOG.error("ProcessRequestWrapper response to {} failed", future.channel().remoteAddress(), future.cause());
                                    LOG.error(request.toString());
                                    LOG.error(response.toString());
                                }
                            }
                        });
                    } catch (Throwable e) {
                        LOG.error("ProcessRequestWrapper process request over, but response failed", e);
                        LOG.error(request.toString());
                        LOG.error(response.toString());
                    }
                }
            } catch (RemotingCommandException e1) {
                LOG.error("ExecuteRequestWhenWakeup run", e1);
            }
        }
    };
    this.brokerController.getPullMessageExecutor().submit(new RequestTask(run, channel, request));
}
 
Example #11
Source File: BrokerFastFailure.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public static RequestTask castRunnable(final Runnable runnable) {
    try {
        FutureTaskExt object = (FutureTaskExt) runnable;
        return (RequestTask) object.getRunnable();
    } catch (Throwable e) {
        log.error(String.format("castRunnable exception, %s", runnable.getClass().getName()), e);
    }

    return null;
}
 
Example #12
Source File: PullMessageProcessor.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
/**
 * 执行请求唤醒,即再次拉取消息。
 * 该方法调用线程池,因此,不会阻塞。
 *
 * @param channel 通道
 * @param request 请求
 * @throws RemotingCommandException 当远程调用发生异常时。but,实际应该不会发生
 */
public void executeRequestWhenWakeup(final Channel channel, final RemotingCommand request) throws RemotingCommandException {
    Runnable run = new Runnable() {
        @Override
        public void run() {
            try {
                // 调用拉取请求。本次调用,设置不挂起请求。
                final RemotingCommand response = PullMessageProcessor.this.processRequest(channel, request, false);

                if (response != null) {
                    response.setOpaque(request.getOpaque());
                    response.markResponseType();
                    try {
                        channel.writeAndFlush(response).addListener(new ChannelFutureListener() {
                            @Override
                            public void operationComplete(ChannelFuture future) throws Exception {
                                if (!future.isSuccess()) {
                                    LOG.error("ProcessRequestWrapper response to {} failed", future.channel().remoteAddress(), future.cause());
                                    LOG.error(request.toString());
                                    LOG.error(response.toString());
                                }
                            }
                        });
                    } catch (Throwable e) {
                        LOG.error("ProcessRequestWrapper process request over, but response failed", e);
                        LOG.error(request.toString());
                        LOG.error(response.toString());
                    }
                }
            } catch (RemotingCommandException e1) {
                LOG.error("ExecuteRequestWhenWakeup run", e1);
            }
        }
    };
    // 提交拉取请求到线程池
    this.brokerController.getPullMessageExecutor().submit(new RequestTask(run, channel, request));
}
 
Example #13
Source File: BrokerFastFailure.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
public static RequestTask castRunnable(final Runnable runnable) {
    try {
        FutureTaskExt object = (FutureTaskExt) runnable;
        return (RequestTask) object.getRunnable();
    } catch (Throwable e) {
        log.error(String.format("castRunnable exception, %s", runnable.getClass().getName()), e);
    }

    return null;
}
 
Example #14
Source File: BrokerController.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public long headSlowTimeMills(BlockingQueue<Runnable> q) {
    long slowTimeMills = 0;
    final Runnable peek = q.peek();
    if (peek != null) {
        RequestTask rt = BrokerFastFailure.castRunnable(peek);
        slowTimeMills = rt == null ? 0 : this.messageStore.now() - rt.getCreateTimestamp();
    }

    if (slowTimeMills < 0) {
        slowTimeMills = 0;
    }

    return slowTimeMills;
}
 
Example #15
Source File: PullMessageProcessor.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public void executeRequestWhenWakeup(final Channel channel,
    final RemotingCommand request) throws RemotingCommandException {
    Runnable run = new Runnable() {
        @Override
        public void run() {
            try {
                final RemotingCommand response = PullMessageProcessor.this.processRequest(channel, request, false);

                if (response != null) {
                    response.setOpaque(request.getOpaque());
                    response.markResponseType();
                    try {
                        channel.writeAndFlush(response).addListener(new ChannelFutureListener() {
                            @Override
                            public void operationComplete(ChannelFuture future) throws Exception {
                                if (!future.isSuccess()) {
                                    log.error("processRequestWrapper response to {} failed",
                                        future.channel().remoteAddress(), future.cause());
                                    log.error(request.toString());
                                    log.error(response.toString());
                                }
                            }
                        });
                    } catch (Throwable e) {
                        log.error("processRequestWrapper process request over, but response failed", e);
                        log.error(request.toString());
                        log.error(response.toString());
                    }
                }
            } catch (RemotingCommandException e1) {
                log.error("excuteRequestWhenWakeup run", e1);
            }
        }
    };
    this.brokerController.getPullMessageExecutor().submit(new RequestTask(run, channel, request));
}
 
Example #16
Source File: BrokerFastFailure.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public static RequestTask castRunnable(final Runnable runnable) {
    try {
        if (runnable instanceof FutureTaskExt) {
            FutureTaskExt object = (FutureTaskExt) runnable;
            return (RequestTask) object.getRunnable();
        }
    } catch (Throwable e) {
        log.error(String.format("castRunnable exception, %s", runnable.getClass().getName()), e);
    }

    return null;
}
 
Example #17
Source File: BrokerFastFailure.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private void cleanExpiredRequest() {
    while (this.brokerController.getMessageStore().isOSPageCacheBusy()) {
        try {
            if (!this.brokerController.getSendThreadPoolQueue().isEmpty()) {
                final Runnable runnable = this.brokerController.getSendThreadPoolQueue().poll(0, TimeUnit.SECONDS);
                if (null == runnable) {
                    break;
                }

                final RequestTask rt = castRunnable(runnable);
                rt.returnResponse(RemotingSysResponseCode.SYSTEM_BUSY, String.format("[PCBUSY_CLEAN_QUEUE]broker busy, start flow control for a while, period in queue: %sms, size of queue: %d", System.currentTimeMillis() - rt.getCreateTimestamp(), this.brokerController.getSendThreadPoolQueue().size()));
            } else {
                break;
            }
        } catch (Throwable ignored) {
        }
    }

    cleanExpiredRequestInQueue(this.brokerController.getSendThreadPoolQueue(),
        this.brokerController.getBrokerConfig().getWaitTimeMillsInSendQueue());

    cleanExpiredRequestInQueue(this.brokerController.getPullThreadPoolQueue(),
        this.brokerController.getBrokerConfig().getWaitTimeMillsInPullQueue());

    cleanExpiredRequestInQueue(this.brokerController.getHeartbeatThreadPoolQueue(),
        this.brokerController.getBrokerConfig().getWaitTimeMillsInHeartbeatQueue());

    cleanExpiredRequestInQueue(this.brokerController.getEndTransactionThreadPoolQueue(), this
        .brokerController.getBrokerConfig().getWaitTimeMillsInTransactionQueue());
}
 
Example #18
Source File: BrokerFastFailure.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
void cleanExpiredRequestInQueue(final BlockingQueue<Runnable> blockingQueue, final long maxWaitTimeMillsInQueue) {
    while (true) {
        try {
            if (!blockingQueue.isEmpty()) {
                final Runnable runnable = blockingQueue.peek();
                if (null == runnable) {
                    break;
                }
                final RequestTask rt = castRunnable(runnable);
                if (rt == null || rt.isStopRun()) {
                    break;
                }

                final long behind = System.currentTimeMillis() - rt.getCreateTimestamp();
                if (behind >= maxWaitTimeMillsInQueue) {
                    if (blockingQueue.remove(runnable)) {
                        rt.setStopRun(true);
                        rt.returnResponse(RemotingSysResponseCode.SYSTEM_BUSY, String.format("[TIMEOUT_CLEAN_QUEUE]broker busy, start flow control for a while, period in queue: %sms, size of queue: %d", behind, blockingQueue.size()));
                    }
                } else {
                    break;
                }
            } else {
                break;
            }
        } catch (Throwable ignored) {
        }
    }
}
 
Example #19
Source File: BrokerFastFailureTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void testCleanExpiredRequestInQueue() throws Exception {
    BrokerFastFailure brokerFastFailure = new BrokerFastFailure(null);

    BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
    brokerFastFailure.cleanExpiredRequestInQueue(queue, 1);
    assertThat(queue.size()).isZero();

    //Normal Runnable
    Runnable runnable = new Runnable() {
        @Override
        public void run() {

        }
    };
    queue.add(runnable);

    assertThat(queue.size()).isEqualTo(1);
    brokerFastFailure.cleanExpiredRequestInQueue(queue, 1);
    assertThat(queue.size()).isEqualTo(1);

    queue.clear();

    //With expired request
    RequestTask expiredRequest = new RequestTask(runnable, null, null);
    queue.add(new FutureTaskExt<>(expiredRequest, null));
    TimeUnit.MILLISECONDS.sleep(100);

    RequestTask requestTask = new RequestTask(runnable, null, null);
    queue.add(new FutureTaskExt<>(requestTask, null));

    assertThat(queue.size()).isEqualTo(2);
    brokerFastFailure.cleanExpiredRequestInQueue(queue, 100);
    assertThat(queue.size()).isEqualTo(1);
    assertThat(((FutureTaskExt) queue.peek()).getRunnable()).isEqualTo(requestTask);
}
 
Example #20
Source File: BrokerFastFailure.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
void cleanExpiredRequestInQueue(final BlockingQueue<Runnable> blockingQueue, final long maxWaitTimeMillsInQueue) {
    while (true) {
        try {
            if (!blockingQueue.isEmpty()) {
                final Runnable runnable = blockingQueue.peek();
                if (null == runnable) {
                    break;
                }
                final RequestTask rt = castRunnable(runnable);
                if (rt == null || rt.isStopRun()) {
                    break;
                }

                final long behind = System.currentTimeMillis() - rt.getCreateTimestamp();
                if (behind >= maxWaitTimeMillsInQueue) {
                    if (blockingQueue.remove(runnable)) {
                        rt.setStopRun(true);
                        rt.returnResponse(RemotingSysResponseCode.SYSTEM_BUSY, String.format("[TIMEOUT_CLEAN_QUEUE]broker busy, start flow control for a while, period in queue: %sms, size of queue: %d", behind, blockingQueue.size()));
                    }
                } else {
                    break;
                }
            } else {
                break;
            }
        } catch (Throwable ignored) {
        }
    }
}
 
Example #21
Source File: PullMessageProcessor.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public void executeRequestWhenWakeup(final Channel channel,
    final RemotingCommand request) throws RemotingCommandException {
    Runnable run = new Runnable() {
        @Override
        public void run() {
            try {
                final RemotingCommand response = PullMessageProcessor.this.processRequest(channel, request, false);

                if (response != null) {
                    response.setOpaque(request.getOpaque());
                    response.markResponseType();
                    try {
                        channel.writeAndFlush(response).addListener(new ChannelFutureListener() {
                            @Override
                            public void operationComplete(ChannelFuture future) throws Exception {
                                if (!future.isSuccess()) {
                                    log.error("processRequestWrapper response to {} failed",
                                        future.channel().remoteAddress(), future.cause());
                                    log.error(request.toString());
                                    log.error(response.toString());
                                }
                            }
                        });
                    } catch (Throwable e) {
                        log.error("processRequestWrapper process request over, but response failed", e);
                        log.error(request.toString());
                        log.error(response.toString());
                    }
                }
            } catch (RemotingCommandException e1) {
                log.error("excuteRequestWhenWakeup run", e1);
            }
        }
    };
    this.brokerController.getPullMessageExecutor().submit(new RequestTask(run, channel, request));
}
 
Example #22
Source File: BrokerFastFailure.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
public static RequestTask castRunnable(final Runnable runnable) {
    try {
        FutureTaskExt object = (FutureTaskExt) runnable;
        return (RequestTask) object.getRunnable();
    } catch (Throwable e) {
        log.error(String.format("castRunnable exception, %s", runnable.getClass().getName()), e);
    }

    return null;
}
 
Example #23
Source File: BrokerFastFailure.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
void cleanExpiredRequestInQueue(final BlockingQueue<Runnable> blockingQueue, final long maxWaitTimeMillsInQueue) {
    while (true) {
        try {
            if (!blockingQueue.isEmpty()) {
                final Runnable runnable = blockingQueue.peek();
                if (null == runnable) {
                    break;
                }
                final RequestTask rt = castRunnable(runnable);
                if (rt == null || rt.isStopRun()) {
                    break;
                }

                final long behind = System.currentTimeMillis() - rt.getCreateTimestamp();
                if (behind >= maxWaitTimeMillsInQueue) {
                    if (blockingQueue.remove(runnable)) {
                        rt.setStopRun(true);
                        rt.returnResponse(RemotingSysResponseCode.SYSTEM_BUSY, String.format("[TIMEOUT_CLEAN_QUEUE]broker busy, start flow control for a while, period in queue: %sms, size of queue: %d", behind, blockingQueue.size()));
                    }
                } else {
                    break;
                }
            } else {
                break;
            }
        } catch (Throwable ignored) {
        }
    }
}
 
Example #24
Source File: BrokerFastFailureTest.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Test
public void testCleanExpiredRequestInQueue() throws Exception {
    BrokerFastFailure brokerFastFailure = new BrokerFastFailure(null);

    BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
    brokerFastFailure.cleanExpiredRequestInQueue(queue, 1);
    assertThat(queue.size()).isZero();

    //Normal Runnable
    Runnable runnable = new Runnable() {
        @Override
        public void run() {

        }
    };
    queue.add(runnable);

    assertThat(queue.size()).isEqualTo(1);
    brokerFastFailure.cleanExpiredRequestInQueue(queue, 1);
    assertThat(queue.size()).isEqualTo(1);

    queue.clear();

    //With expired request
    RequestTask expiredRequest = new RequestTask(runnable, null, null);
    queue.add(new FutureTaskExt<>(expiredRequest, null));
    TimeUnit.MILLISECONDS.sleep(100);

    RequestTask requestTask = new RequestTask(runnable, null, null);
    queue.add(new FutureTaskExt<>(requestTask, null));

    assertThat(queue.size()).isEqualTo(2);
    brokerFastFailure.cleanExpiredRequestInQueue(queue, 100);
    assertThat(queue.size()).isEqualTo(1);
    assertThat(((FutureTaskExt) queue.peek()).getRunnable()).isEqualTo(requestTask);
}
 
Example #25
Source File: BrokerController.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
public long headSlowTimeMills(BlockingQueue<Runnable> q) {
    long slowTimeMills = 0;
    final Runnable peek = q.peek();
    if (peek != null) {
        RequestTask rt = BrokerFastFailure.castRunnable(peek);
        slowTimeMills = rt == null ? 0 : this.messageStore.now() - rt.getCreateTimestamp();
    }

    if (slowTimeMills < 0) {
        slowTimeMills = 0;
    }

    return slowTimeMills;
}
 
Example #26
Source File: PullMessageProcessor.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
public void executeRequestWhenWakeup(final Channel channel,
        final RemotingCommand request) throws RemotingCommandException {
        Runnable run = new Runnable() {
            @Override
            public void run() {
                try {
//                    =》
                    final RemotingCommand response = PullMessageProcessor.this.processRequest(channel, request, false);

                    if (response != null) {
                        response.setOpaque(request.getOpaque());
                        response.markResponseType();
                        try {
                            channel.writeAndFlush(response).addListener(new ChannelFutureListener() {
                                @Override
                                public void operationComplete(ChannelFuture future) throws Exception {
                                    if (!future.isSuccess()) {
                                        log.error("processRequestWrapper response to {} failed",
                                            future.channel().remoteAddress(), future.cause());
                                        log.error(request.toString());
                                        log.error(response.toString());
                                    }
                                }
                            });
                        } catch (Throwable e) {
                            log.error("processRequestWrapper process request over, but response failed", e);
                            log.error(request.toString());
                            log.error(response.toString());
                        }
                    }
                } catch (RemotingCommandException e1) {
                    log.error("excuteRequestWhenWakeup run", e1);
                }
            }
        };
//        异步执行
        this.brokerController.getPullMessageExecutor().submit(new RequestTask(run, channel, request));
    }
 
Example #27
Source File: BrokerFastFailure.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
public static RequestTask castRunnable(final Runnable runnable) {
    try {
        if (runnable instanceof FutureTaskExt) {
            FutureTaskExt object = (FutureTaskExt) runnable;
            return (RequestTask) object.getRunnable();
        }
    } catch (Throwable e) {
        log.error(String.format("castRunnable exception, %s", runnable.getClass().getName()), e);
    }

    return null;
}
 
Example #28
Source File: BrokerFastFailure.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private void cleanExpiredRequest() {
//        系统繁忙
        while (this.brokerController.getMessageStore().isOSPageCacheBusy()) {
            try {
                if (!this.brokerController.getSendThreadPoolQueue().isEmpty()) {
                    final Runnable runnable = this.brokerController.getSendThreadPoolQueue().poll(0, TimeUnit.SECONDS);
                    if (null == runnable) {
                        break;
                    }

                    final RequestTask rt = castRunnable(runnable);
                    rt.returnResponse(RemotingSysResponseCode.SYSTEM_BUSY, String.format("[PCBUSY_CLEAN_QUEUE]broker busy, start flow control for a while, period in queue: %sms, size of queue: %d", System.currentTimeMillis() - rt.getCreateTimestamp(), this.brokerController.getSendThreadPoolQueue().size()));
                } else {
                    break;
                }
            } catch (Throwable ignored) {
            }
        }

        cleanExpiredRequestInQueue(this.brokerController.getSendThreadPoolQueue(),
            this.brokerController.getBrokerConfig().getWaitTimeMillsInSendQueue());

        cleanExpiredRequestInQueue(this.brokerController.getPullThreadPoolQueue(),
            this.brokerController.getBrokerConfig().getWaitTimeMillsInPullQueue());

        cleanExpiredRequestInQueue(this.brokerController.getHeartbeatThreadPoolQueue(),
            this.brokerController.getBrokerConfig().getWaitTimeMillsInHeartbeatQueue());
    }
 
Example #29
Source File: BrokerFastFailure.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
public static RequestTask castRunnable(final Runnable runnable) {
    try {
        FutureTaskExt object = (FutureTaskExt) runnable;
        return (RequestTask) object.getRunnable();
    } catch (Throwable e) {
        log.error(String.format("castRunnable exception, %s", runnable.getClass().getName()), e);
    }

    return null;
}
 
Example #30
Source File: BrokerFastFailureTest.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCleanExpiredRequestInQueue() throws Exception {
    BrokerFastFailure brokerFastFailure = new BrokerFastFailure(null);

    BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
    brokerFastFailure.cleanExpiredRequestInQueue(queue, 1);
    assertThat(queue.size()).isZero();

    //Normal Runnable
    Runnable runnable = new Runnable() {
        @Override
        public void run() {

        }
    };
    queue.add(runnable);

    assertThat(queue.size()).isEqualTo(1);
    brokerFastFailure.cleanExpiredRequestInQueue(queue, 1);
    assertThat(queue.size()).isEqualTo(1);

    queue.clear();

    //With expired request
    RequestTask expiredRequest = new RequestTask(runnable, null, null);
    queue.add(new FutureTaskExt<>(expiredRequest, null));
    TimeUnit.MILLISECONDS.sleep(100);

    RequestTask requestTask = new RequestTask(runnable, null, null);
    queue.add(new FutureTaskExt<>(requestTask, null));

    assertThat(queue.size()).isEqualTo(2);
    brokerFastFailure.cleanExpiredRequestInQueue(queue, 100);
    assertThat(queue.size()).isEqualTo(1);
    assertThat(((FutureTaskExt) queue.peek()).getRunnable()).isEqualTo(requestTask);
}