Skip to content

Commit

Permalink
FAB-4869 remove hardcode orderer wait
Browse files Browse the repository at this point in the history
Change-Id: I1e880f53c8970e2aa9193a738e7271a235303ddb
Signed-off-by: rickr <cr22rc@gmail.com>
  • Loading branch information
cr22rc committed Jun 20, 2017
1 parent bc0e3fa commit 6431a0a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/hyperledger/fabric/sdk/HFClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ public Orderer newOrderer(String name, String grpcURL) throws InvalidArgumentExc
* grpc ManagedChannelBuilder. If more than one argument to the method is needed then the
* parameters need to be supplied in an array of Objects.
* </li>
* <li>
* ordererWaitTimeMilliSecs Time to wait in milliseconds for the
* Orderer to accept requests before timing out. The default is two seconds.
* </li>
* </ul>
* @return The orderer.
* @throws InvalidArgumentException
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/hyperledger/fabric/sdk/Orderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Ab.BroadcastResponse sendTransaction(Common.Envelope transaction) throws Excepti
OrdererClient localOrdererClient = ordererClient;

if (localOrdererClient == null || !localOrdererClient.isChannelActive()) {
ordererClient = new OrdererClient(this, new Endpoint(url, properties).getChannelBuilder());
ordererClient = new OrdererClient(this, new Endpoint(url, properties).getChannelBuilder(), properties);
localOrdererClient = ordererClient;
}

Expand Down Expand Up @@ -156,7 +156,7 @@ DeliverResponse[] sendDeliver(Common.Envelope transaction) throws TransactionExc

logger.debug(format("Order.sendDeliver name: %s, url: %s", name, url));
if (localOrdererClient == null || !localOrdererClient.isChannelActive()) {
localOrdererClient = new OrdererClient(this, new Endpoint(url, properties).getChannelBuilder());
localOrdererClient = new OrdererClient(this, new Endpoint(url, properties).getChannelBuilder(), properties);
ordererClient = localOrdererClient;
}

Expand Down
48 changes: 32 additions & 16 deletions src/main/java/org/hyperledger/fabric/sdk/OrdererClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

Expand All @@ -37,23 +38,39 @@
* Sample client code that makes gRPC calls to the server.
*/
class OrdererClient {
private static final long ORDERER_WAIT_TIME = 2000L;
private final String channelName;
private final ManagedChannelBuilder channelBuilder;
private boolean shutdown = false;
private static final Log logger = LogFactory.getLog(OrdererClient.class);
private ManagedChannel managedChannel = null;
private final String name;
private final String url;
private final long ordererWaitTimeMilliSecs;

/**
* Construct client for accessing Orderer server using the existing managedChannel.
*/
OrdererClient(Orderer orderer, ManagedChannelBuilder<?> channelBuilder) {
OrdererClient(Orderer orderer, ManagedChannelBuilder<?> channelBuilder, Properties properties) {

this.channelBuilder = channelBuilder;
name = orderer.getName();
url = orderer.getUrl();
channelName = orderer.getChannel().getName();

if (null == properties) {

ordererWaitTimeMilliSecs = ORDERER_WAIT_TIME;

} else {

String ordererWaitTimeMilliSecsString = properties.getProperty("ordererWaitTimeMilliSecs", Long.toString(ORDERER_WAIT_TIME));

ordererWaitTimeMilliSecs = Long.getLong(ordererWaitTimeMilliSecsString, ORDERER_WAIT_TIME);

}


}

synchronized void shutdown(boolean force) {
Expand Down Expand Up @@ -141,20 +158,19 @@ public void onCompleted() {

nso.onNext(envelope);

try {
if (!finishLatch.await(2, TimeUnit.MINUTES)) {
TransactionException ste = new TransactionException("Send transactions failed. Reason: timeout");
logger.error("sendTransaction error " + ste.getMessage(), ste);
throw ste;
}

if (throwable[0] != null) {
//get full stack trace
TransactionException ste = new TransactionException("Send transactions failed. Reason: " + throwable[0].getMessage(), throwable[0]);
logger.error("sendTransaction error " + ste.getMessage(), ste);
throw ste;
}
logger.debug("Done waiting for reply! Got:" + ret[0]);
try {
if (!finishLatch.await(ordererWaitTimeMilliSecs, TimeUnit.MILLISECONDS)) {
TransactionException ste = new TransactionException("Send transactions failed. Reason: timeout");
logger.error("sendTransaction error " + ste.getMessage(), ste);
throw ste;
}
if (throwable[0] != null) {
//get full stack trace
TransactionException ste = new TransactionException("Send transactions failed. Reason: " + throwable[0].getMessage(), throwable[0]);
logger.error("sendTransaction error " + ste.getMessage(), ste);
throw ste;
}
logger.debug("Done waiting for reply! Got:" + ret[0]);

} catch (InterruptedException e) {
logger.error(e);
Expand Down Expand Up @@ -254,7 +270,7 @@ public void onCompleted() {
//nso.onCompleted();

try {
if (!finishLatch.await(2, TimeUnit.MINUTES)) {
if (!finishLatch.await(ordererWaitTimeMilliSecs, TimeUnit.MILLISECONDS)) {
TransactionException ex = new TransactionException("sendDeliver time exceeded for orderer");
logger.error(ex.getMessage(), ex);
throw ex;
Expand Down

0 comments on commit 6431a0a

Please # to comment.