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;
}
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);
}
}
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);
}
}
}
}
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;
}
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;
}
Aggregations