weixin_39623355
weixin_39623355
2020-12-25 20:06

补充使用SDK操作区块服务抛The accessing index is out of BytesSlice's bounds!的原因

创建用户时抛如下异常,网关可以正常连接,秘钥认证没有问题:


Caused by: java.lang.IndexOutOfBoundsException: The accessing index is out of BytesSlice's bounds!
    at com.jd.blockchain.utils.io.BytesSlice.checkBoundary(BytesSlice.java:174)
    at com.jd.blockchain.utils.io.BytesSlice.getInt(BytesSlice.java:97)
    at com.jd.blockchain.utils.io.BytesSlice.getInt(BytesSlice.java:86)
    at com.jd.blockchain.binaryproto.impl.HeaderEncoder.resolveCode(HeaderEncoder.java:71)
    at com.jd.blockchain.binaryproto.BinaryProtocol.decode(BinaryProtocol.java:41)
    at com.jd.blockchain.sdk.converters.BinarySerializeResponseConverter.getResponse(BinarySerializeResponseConverter.java:33)
    at com.jd.blockchain.utils.http.agent.HttpServiceAgent.invoke(HttpServiceAgent.java:587)
    ... 7 more

网关里的异常


11:57:05.537 ERROR com.jd.blockchain.gateway.web.GatewayGlobalExceptionHandler 34 json - Unexpected exception occurred! --[RequestURL=[POST] http://192.168.1.190:8081/rpc/tx][class java.lang.IllegalStateException]Returned object not currently part of this pool java.lang.IllegalStateException: Returned object not currently part of this pool
    at org.apache.commons.pool2.impl.GenericObjectPool.returnObject(GenericObjectPool.java:530) ~[commons-pool2-2.5.0.jar!/:2.5.0]
    at com.jd.blockchain.consensus.bftsmart.client.BftsmartMessageService.sendOrderedMessage(BftsmartMessageService.java:46) ~[consensus-bftsmart-1.1.1.RELEASE.jar!/:1.1.1.RELEASE]
    at com.jd.blockchain.consensus.bftsmart.client.BftsmartMessageService.sendOrdered(BftsmartMessageService.java:22) ~[consensus-bftsmart-1.1.1.RELEASE.jar!/:1.1.1.RELEASE]
    at com.jd.blockchain.sdk.service.NodeSigningAppender.process(NodeSigningAppender.java:84) ~[sdk-base-1.1.1.RELEASE.jar!/:1.1.1.RELEASE]
    at com.jd.blockchain.sdk.service.PeerServiceProxy.process(PeerServiceProxy.java:89) ~[sdk-base-1.1.1.RELEASE.jar!/:1.1.1.RELEASE]
    at com.jd.blockchain.gateway.web.TxProcessingController.process(TxProcessingController.java:70) ~[gateway-1.1.1.RELEASE.jar!/:1.1.1.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_231]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_231]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_231]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_231]

最终查明异常是由于创建AsynchServiceProxy失败导致的,这部分实现采用了Apache-commons-pool2。实现如下:


public class BftsmartPeerProxyFactory extends BasePooledObjectFactory<asynchserviceproxy> {

    private BftsmartClientSettings bftsmartClientSettings;

    private int gatewayId;

    private AtomicInteger index = new AtomicInteger(1);

    public BftsmartPeerProxyFactory(BftsmartClientSettings bftsmartClientSettings, int gatewayId) {
        this.bftsmartClientSettings = bftsmartClientSettings;
        this.gatewayId = gatewayId;
    }


    public AsynchServiceProxy create() throws Exception {

        BftsmartTopology topology = BinarySerializeUtils.deserialize(bftsmartClientSettings.getTopology());

        MemoryBasedViewStorage viewStorage = new MemoryBasedViewStorage(topology.getView());
        TOMConfiguration tomConfiguration = BinarySerializeUtils.deserialize(bftsmartClientSettings.getTomConfig());

        //every proxy client has unique id;
        tomConfiguration.setProcessId(gatewayId + index.getAndIncrement());
        AsynchServiceProxy peerProxy = new AsynchServiceProxy(tomConfiguration, viewStorage);
        return peerProxy;
    }


    public PooledObject<asynchserviceproxy> wrap(AsynchServiceProxy asynchServiceProxy) {
        return new DefaultPooledObject<>(asynchServiceProxy);
    }
}
</asynchserviceproxy></asynchserviceproxy>

这个代码BinarySerializeUtils.deserialize(bftsmartClientSettings.getTopology())返回的是null,没有正确拿到Bftsmart的网络拓扑。进而查看peer节点,发现只有节点0成功了,其他都抛如下异常,初步判断bftsmart的副本服务因为端口占用启动失败了:


11:36:48.479 ERROR bftsmart.tom.ServiceReplica 247 init - null java.net.BindException: 地址已在使用
    at sun.nio.ch.Net.bind0(Native Method) ~[?:1.8.0_231]
    at sun.nio.ch.Net.bind(Net.java:433) ~[?:1.8.0_231]
    at sun.nio.ch.Net.bind(Net.java:425) ~[?:1.8.0_231]
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[?:1.8.0_231]
    at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:130) ~[netty-all-4.1.29.Final.jar:4.1.29.Final]
    at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:558) ~[netty-all-4.1.29.Final.jar:4.1.29.Final]
    at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1358) ~[netty-all-4.1.29.Final.jar:4.1.29.Final]
    at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:501) ~[netty-all-4.1.29.Final.jar:4.1.29.Final]
    at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:486) ~[netty-all-4.1.29.Final.jar:4.1.29.Final]
    at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:1019) ~[netty-all-4.1.29.Final.jar:4.1.29.Final]
    at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:254) ~[netty-all-4.1.29.Final.jar:4.1.29.Final]
    at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:366) ~[netty-all-4.1.29.Final.jar:4.1.29.Final]
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) ~[netty-all-4.1.29.Final.jar:4.1.29.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404) ~[netty-all-4.1.29.Final.jar:4.1.29.Final]
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:446) ~[netty-all-4.1.29.Final.jar:4.1.29.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884) ~[netty-all-4.1.29.Final.jar:4.1.29.Final]
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-all-4.1.29.Final.jar:4.1.29.Final]
    at java.lang.Thread.run(Thread.java:748) ~[?:1.8.0_231]

从bftsmart官方文档得知,如果在同一个主机运行,不能使用顺序的端口号,即一开始编排的peer的共识端口,6000、6001、6002、6003是不行的,原因如下: 如果在同一台计算机(127.0.0.1)中部署/执行了某些(或全部)副本,config/hosts.config则不能具有顺序的端口号(例如10000、10001、10002、10003)。这是因为每个副本都绑定了两个端口:一个用于接收来自客户端的消息,另一个用于接收来自其他副本的消息(通过获取下一个端口号选择) 。更一般而言,如果为副本R分配了端口号P,它将尝试将端口P(绑定到接收到的客户端请求)和端口P + 1(绑定到其他副本)进行绑定。如果不执行此准则,则副本可能无法绑定所有需要的端口。 文档地址:https://github.com/bft-smart/library/wiki/Deployment-Guidelines

该提问来源于开源项目:blockchain-jd-com/jdchain

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • weixin_39623355 weixin_39623355 4月前

    到了这一层,要了解Apache-commons-pool2的机制,才能定位到BftsmartPeerProxyFactory这边,然后才能定位到获取bftSmart的网络拓扑失败了,才知道bftsmart的副本端口冲突了,然后去找了bftsmart的文档才发现,共识端口和副本端口是连续的

    点赞 评论 复制链接分享
  • weixin_39588542 weixin_39588542 4月前

    是的,需要跳号。目前我的视频操作一般定义步长为10。你是个高手!

    点赞 评论 复制链接分享
  • weixin_39588542 weixin_39588542 4月前

    后续我们再完善一下这块,最近发现有几个用户都是因为端口冲突的问题导致失败,后续我们会把这个原因给暴露出来,方便排查。感谢您对JDChain的关注!

    点赞 评论 复制链接分享
  • weixin_40009393 weixin_40009393 4月前

    学习了,当时看了好久也没查明是什么原因。请教下是用什么方法调试看到bftsmart地址在占用呢? 我只能查到 Returned object not currently part of this pool 这一层

    点赞 评论 复制链接分享

相关推荐