Search in sources :

Example 1 with DeliverResponse

use of org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse in project fabric-sdk-java by hyperledger.

the class Channel method getLatestBlock.

private Block getLatestBlock(Orderer orderer) throws TransactionException {
    logger.debug(format("getConfigurationBlock for channel %s", name));
    SeekPosition seekPosition = SeekPosition.newBuilder().setNewest(Ab.SeekNewest.getDefaultInstance()).build();
    SeekInfo seekInfo = SeekInfo.newBuilder().setStart(seekPosition).setStop(seekPosition).setBehavior(SeekInfo.SeekBehavior.BLOCK_UNTIL_READY).build();
    ArrayList<DeliverResponse> deliverResponses = new ArrayList<>();
    seekBlock(seekInfo, deliverResponses, orderer);
    DeliverResponse blockresp = deliverResponses.get(1);
    Block latestBlock = blockresp.getBlock();
    if (latestBlock == null) {
        throw new TransactionException(format("newest block for channel %s fetch bad deliver returned null:", name));
    }
    logger.trace(format("Received latest  block for channel %s, block no:%d", name, latestBlock.getHeader().getNumber()));
    return latestBlock;
}
Also used : SeekInfo(org.hyperledger.fabric.protos.orderer.Ab.SeekInfo) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) SeekPosition(org.hyperledger.fabric.protos.orderer.Ab.SeekPosition) ArrayList(java.util.ArrayList) Block(org.hyperledger.fabric.protos.common.Common.Block) DeliverResponse(org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse)

Example 2 with DeliverResponse

use of org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse in project fabric-sdk-java by hyperledger.

the class Channel method getBlockByNumber.

private Block getBlockByNumber(final long number) throws TransactionException {
    logger.trace(format("getConfigurationBlock for channel %s", name));
    try {
        logger.trace(format("Last config index is %d", number));
        SeekSpecified seekSpecified = SeekSpecified.newBuilder().setNumber(number).build();
        SeekPosition seekPosition = SeekPosition.newBuilder().setSpecified(seekSpecified).build();
        SeekInfo seekInfo = SeekInfo.newBuilder().setStart(seekPosition).setStop(seekPosition).setBehavior(SeekInfo.SeekBehavior.BLOCK_UNTIL_READY).build();
        ArrayList<DeliverResponse> deliverResponses = new ArrayList<>();
        seekBlock(seekInfo, deliverResponses, getRandomOrderer());
        DeliverResponse blockresp = deliverResponses.get(1);
        Block retBlock = blockresp.getBlock();
        if (retBlock == null) {
            throw new TransactionException(format("newest block for channel %s fetch bad deliver returned null:", name));
        }
        int dataCount = retBlock.getData().getDataCount();
        if (dataCount < 1) {
            throw new TransactionException(format("Bad config block data count %d", dataCount));
        }
        logger.trace(format("Received  block for channel %s, block no:%d, transaction count: %d", name, retBlock.getHeader().getNumber(), retBlock.getData().getDataCount()));
        return retBlock;
    } catch (TransactionException e) {
        logger.error(e.getMessage(), e);
        throw e;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new TransactionException(e);
    }
}
Also used : SeekInfo(org.hyperledger.fabric.protos.orderer.Ab.SeekInfo) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) SeekPosition(org.hyperledger.fabric.protos.orderer.Ab.SeekPosition) SeekSpecified(org.hyperledger.fabric.protos.orderer.Ab.SeekSpecified) ArrayList(java.util.ArrayList) Block(org.hyperledger.fabric.protos.common.Common.Block) DeliverResponse(org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse) EventHubException(org.hyperledger.fabric.sdk.exception.EventHubException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) TransactionEventException(org.hyperledger.fabric.sdk.exception.TransactionEventException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) StatusRuntimeException(io.grpc.StatusRuntimeException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) TimeoutException(java.util.concurrent.TimeoutException) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException)

Example 3 with DeliverResponse

use of org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse in project fabric-sdk-java by hyperledger.

the class OrdererClient method sendDeliver.

DeliverResponse[] sendDeliver(Common.Envelope envelope) throws TransactionException {
    if (shutdown) {
        throw new TransactionException("Orderer client is shutdown");
    }
    StreamObserver<Common.Envelope> nso = null;
    ManagedChannel lmanagedChannel = managedChannel;
    if (lmanagedChannel == null || lmanagedChannel.isTerminated() || lmanagedChannel.isShutdown()) {
        lmanagedChannel = channelBuilder.build();
        managedChannel = lmanagedChannel;
    }
    try {
        AtomicBroadcastGrpc.AtomicBroadcastStub broadcast = AtomicBroadcastGrpc.newStub(lmanagedChannel);
        // final DeliverResponse[] ret = new DeliverResponse[1];
        final List<DeliverResponse> retList = new ArrayList<>();
        final List<Throwable> throwableList = new ArrayList<>();
        final CountDownLatch finishLatch = new CountDownLatch(1);
        StreamObserver<DeliverResponse> so = new StreamObserver<DeliverResponse>() {

            boolean done = false;

            @Override
            public void onNext(DeliverResponse resp) {
                // logger.info("Got Broadcast response: " + resp);
                logger.debug("resp status value: " + resp.getStatusValue() + ", resp: " + resp.getStatus() + ", type case: " + resp.getTypeCase());
                if (done) {
                    return;
                }
                if (resp.getTypeCase() == STATUS) {
                    done = true;
                    retList.add(0, resp);
                    finishLatch.countDown();
                } else {
                    retList.add(resp);
                }
            }

            @Override
            public void onError(Throwable t) {
                if (!shutdown) {
                    logger.error(format("Received error on channel %s, orderer %s, url %s, %s", channelName, name, url, t.getMessage()), t);
                }
                throwableList.add(t);
                finishLatch.countDown();
            }

            @Override
            public void onCompleted() {
                logger.trace("onCompleted");
                finishLatch.countDown();
            }
        };
        nso = broadcast.deliver(so);
        nso.onNext(envelope);
        try {
            if (!finishLatch.await(ordererWaitTimeMilliSecs, TimeUnit.MILLISECONDS)) {
                TransactionException ex = new TransactionException(format("Channel %s sendDeliver time exceeded for orderer %s, timed out at %d ms.", channelName, name, ordererWaitTimeMilliSecs));
                logger.error(ex.getMessage(), ex);
                throw ex;
            }
            logger.trace("Done waiting for reply!");
        } catch (InterruptedException e) {
            logger.error(e);
        }
        if (!throwableList.isEmpty()) {
            Throwable throwable = throwableList.get(0);
            TransactionException e = new TransactionException(format("Channel %s sendDeliver failed on orderer %s. Reason: %s", channelName, name, throwable.getMessage()), throwable);
            logger.error(e.getMessage(), e);
            throw e;
        }
        return retList.toArray(new DeliverResponse[retList.size()]);
    } catch (Throwable t) {
        managedChannel = null;
        throw t;
    } finally {
        if (null != nso) {
            try {
                nso.onCompleted();
            } catch (Exception e) {
                // Best effort only report on debug
                logger.debug(format("Exception completing sendDeliver with channel %s,  name %s, url %s %s", channelName, name, url, e.getMessage()), e);
            }
        }
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) AtomicBroadcastGrpc(org.hyperledger.fabric.protos.orderer.AtomicBroadcastGrpc) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) ManagedChannel(io.grpc.ManagedChannel) DeliverResponse(org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse)

Example 4 with DeliverResponse

use of org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse in project fabric-sdk-java by hyperledger.

the class Channel method seekBlock.

private int seekBlock(SeekInfo seekInfo, List<DeliverResponse> deliverResponses, Orderer ordererIn) throws TransactionException {
    logger.trace(format("seekBlock for channel %s", name));
    final long start = System.currentTimeMillis();
    @SuppressWarnings("UnusedAssignment") int statusRC = 404;
    try {
        do {
            statusRC = 404;
            final Orderer orderer = ordererIn != null ? ordererIn : getRandomOrderer();
            TransactionContext txContext = getTransactionContext();
            DeliverResponse[] deliver = orderer.sendDeliver(createSeekInfoEnvelope(txContext, seekInfo, orderer.getClientTLSCertificateDigest()));
            if (deliver.length < 1) {
                logger.warn(format("Genesis block for channel %s fetch bad deliver missing status block only got blocks:%d", name, deliver.length));
                // odd so lets try again....
                statusRC = 404;
            } else {
                DeliverResponse status = deliver[0];
                statusRC = status.getStatusValue();
                if (statusRC == 404 || statusRC == 503) {
                    // 404 - block not found.  503 - service not available usually means kafka is not ready but starting.
                    logger.warn(format("Bad deliver expected status 200  got  %d, Channel %s", status.getStatusValue(), name));
                    // keep trying... else
                    statusRC = 404;
                } else if (statusRC != 200) {
                    // Assume for anything other than 200 we have a non retryable situation
                    throw new TransactionException(format("Bad newest block expected status 200  got  %d, Channel %s", status.getStatusValue(), name));
                } else {
                    if (deliver.length < 2) {
                        throw new TransactionException(format("Newest block for channel %s fetch bad deliver missing genesis block only got %d:", name, deliver.length));
                    } else {
                        deliverResponses.addAll(Arrays.asList(deliver));
                    }
                }
            }
            if (200 != statusRC) {
                long duration = System.currentTimeMillis() - start;
                if (duration > config.getGenesisBlockWaitTime()) {
                    throw new TransactionException(format("Getting block time exceeded %s seconds for channel %s", Long.toString(TimeUnit.MILLISECONDS.toSeconds(duration)), name));
                }
                try {
                    // try again
                    Thread.sleep(ORDERER_RETRY_WAIT_TIME);
                } catch (InterruptedException e) {
                    TransactionException te = new TransactionException("seekBlock thread Sleep", e);
                    logger.warn(te.getMessage(), te);
                }
            }
        } while (statusRC != 200);
    } catch (TransactionException e) {
        logger.error(e.getMessage(), e);
        throw e;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new TransactionException(e);
    }
    return statusRC;
}
Also used : TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) TransactionContext(org.hyperledger.fabric.sdk.transaction.TransactionContext) DeliverResponse(org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse) EventHubException(org.hyperledger.fabric.sdk.exception.EventHubException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) TransactionEventException(org.hyperledger.fabric.sdk.exception.TransactionEventException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) StatusRuntimeException(io.grpc.StatusRuntimeException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) TimeoutException(java.util.concurrent.TimeoutException) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException)

Example 5 with DeliverResponse

use of org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse in project fabric-sdk-java by hyperledger.

the class Channel method getGenesisBlock.

private Block getGenesisBlock(Orderer orderer) throws TransactionException {
    try {
        if (genesisBlock != null) {
            logger.debug(format("Channel %s getGenesisBlock already present", name));
        } else {
            final long start = System.currentTimeMillis();
            SeekSpecified seekSpecified = SeekSpecified.newBuilder().setNumber(0).build();
            SeekPosition seekPosition = SeekPosition.newBuilder().setSpecified(seekSpecified).build();
            SeekSpecified seekStopSpecified = SeekSpecified.newBuilder().setNumber(0).build();
            SeekPosition seekStopPosition = SeekPosition.newBuilder().setSpecified(seekStopSpecified).build();
            SeekInfo seekInfo = SeekInfo.newBuilder().setStart(seekPosition).setStop(seekStopPosition).setBehavior(SeekInfo.SeekBehavior.BLOCK_UNTIL_READY).build();
            ArrayList<DeliverResponse> deliverResponses = new ArrayList<>();
            seekBlock(seekInfo, deliverResponses, orderer);
            DeliverResponse blockresp = deliverResponses.get(1);
            Block configBlock = blockresp.getBlock();
            if (configBlock == null) {
                throw new TransactionException(format("In getGenesisBlock newest block for channel %s fetch bad deliver returned null:", name));
            }
            int dataCount = configBlock.getData().getDataCount();
            if (dataCount < 1) {
                throw new TransactionException(format("In getGenesisBlock bad config block data count %d", dataCount));
            }
            genesisBlock = blockresp.getBlock();
        }
    } catch (TransactionException e) {
        logger.error(e.getMessage(), e);
        throw e;
    } catch (Exception e) {
        TransactionException exp = new TransactionException("getGenesisBlock " + e.getMessage(), e);
        logger.error(exp.getMessage(), exp);
        throw exp;
    }
    if (genesisBlock == null) {
        // make sure it was really set.
        TransactionException exp = new TransactionException("getGenesisBlock returned null");
        logger.error(exp.getMessage(), exp);
        throw exp;
    }
    logger.debug(format("Channel %s getGenesisBlock done.", name));
    return genesisBlock;
}
Also used : SeekInfo(org.hyperledger.fabric.protos.orderer.Ab.SeekInfo) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) SeekPosition(org.hyperledger.fabric.protos.orderer.Ab.SeekPosition) SeekSpecified(org.hyperledger.fabric.protos.orderer.Ab.SeekSpecified) ArrayList(java.util.ArrayList) Block(org.hyperledger.fabric.protos.common.Common.Block) DeliverResponse(org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse) EventHubException(org.hyperledger.fabric.sdk.exception.EventHubException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) TransactionException(org.hyperledger.fabric.sdk.exception.TransactionException) TransactionEventException(org.hyperledger.fabric.sdk.exception.TransactionEventException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) StatusRuntimeException(io.grpc.StatusRuntimeException) CryptoException(org.hyperledger.fabric.sdk.exception.CryptoException) TimeoutException(java.util.concurrent.TimeoutException) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException)

Aggregations

DeliverResponse (org.hyperledger.fabric.protos.orderer.Ab.DeliverResponse)5 TransactionException (org.hyperledger.fabric.sdk.exception.TransactionException)5 ArrayList (java.util.ArrayList)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)3 StatusRuntimeException (io.grpc.StatusRuntimeException)3 IOException (java.io.IOException)3 ExecutionException (java.util.concurrent.ExecutionException)3 TimeoutException (java.util.concurrent.TimeoutException)3 Block (org.hyperledger.fabric.protos.common.Common.Block)3 SeekInfo (org.hyperledger.fabric.protos.orderer.Ab.SeekInfo)3 SeekPosition (org.hyperledger.fabric.protos.orderer.Ab.SeekPosition)3 CryptoException (org.hyperledger.fabric.sdk.exception.CryptoException)3 EventHubException (org.hyperledger.fabric.sdk.exception.EventHubException)3 InvalidArgumentException (org.hyperledger.fabric.sdk.exception.InvalidArgumentException)3 ProposalException (org.hyperledger.fabric.sdk.exception.ProposalException)3 TransactionEventException (org.hyperledger.fabric.sdk.exception.TransactionEventException)3 SeekSpecified (org.hyperledger.fabric.protos.orderer.Ab.SeekSpecified)2 ManagedChannel (io.grpc.ManagedChannel)1 StreamObserver (io.grpc.stub.StreamObserver)1 CountDownLatch (java.util.concurrent.CountDownLatch)1