@@ -48,6 +48,7 @@ class OrdererClient {
4848 private final ManagedChannelBuilder channelBuilder ;
4949 private boolean shutdown = false ;
5050 private static final Log logger = LogFactory .getLog (OrdererClient .class );
51+ private static final boolean TRACELEVEL = logger .isTraceEnabled ();
5152 private ManagedChannel managedChannel = null ;
5253 private final String name ;
5354 private final String url ;
@@ -76,7 +77,7 @@ class OrdererClient {
7677 try {
7778 tempOrdererWaitTimeMilliSecs = Long .parseLong (ordererWaitTimeMilliSecsString );
7879 } catch (NumberFormatException e ) {
79- logger .warn (format ("Orderer %s wait time %s not parsable." , name , ordererWaitTimeMilliSecsString ), e );
80+ logger .warn (format ("Orderer %s wait time %s not parsable." , this . toString () , ordererWaitTimeMilliSecsString ), e );
8081 }
8182
8283 ordererWaitTimeMilliSecs = tempOrdererWaitTimeMilliSecs ;
@@ -90,6 +91,7 @@ synchronized void shutdown(boolean force) {
9091 return ;
9192 }
9293 shutdown = true ;
94+ logger .debug (format ("Shutdown %s" , this .toString ()));
9395 ManagedChannel lchannel = managedChannel ;
9496 managedChannel = null ;
9597 if (lchannel == null ) {
@@ -117,21 +119,38 @@ public void finalize() {
117119 }
118120
119121 Ab .BroadcastResponse sendTransaction (Common .Envelope envelope ) throws Exception {
122+ logger .trace (this .toString () + " OrdererClient.sendTransaction entered." );
120123 StreamObserver <Common .Envelope > nso = null ;
121124
122125 if (shutdown ) {
123126 throw new TransactionException ("Orderer client is shutdown" );
124127 }
125128
126129 ManagedChannel lmanagedChannel = managedChannel ;
130+ if (TRACELEVEL && lmanagedChannel != null ) {
131+ logger .trace (format ("%s managed channel isTerminated: %b, isShutdown: %b, state: %s" , this .toString (),
132+ lmanagedChannel .isTerminated (), lmanagedChannel .isShutdown (), lmanagedChannel .getState (false ).name ()));
133+ }
127134
128135 if (lmanagedChannel == null || lmanagedChannel .isTerminated () || lmanagedChannel .isShutdown ()) {
129136
137+ if (lmanagedChannel != null && lmanagedChannel .isTerminated ()) {
138+ logger .warn (format ("%s managed channel was marked terminated" , this .toString ()));
139+ }
140+ if (lmanagedChannel != null && lmanagedChannel .isShutdown ()) {
141+ logger .warn (format ("%s managed channel was marked shutdown." , this .toString ()));
142+ }
143+
130144 lmanagedChannel = channelBuilder .build ();
131145 managedChannel = lmanagedChannel ;
132146
133147 }
134148
149+ if (TRACELEVEL && lmanagedChannel != null ) {
150+ logger .trace (format ("%s managed channel isTerminated: %b, isShutdown: %b, state: %s" , this .toString (),
151+ lmanagedChannel .isTerminated (), lmanagedChannel .isShutdown (), lmanagedChannel .getState (false ).name ()));
152+ }
153+
135154 try {
136155 final CountDownLatch finishLatch = new CountDownLatch (1 );
137156 AtomicBroadcastGrpc .AtomicBroadcastStub broadcast = AtomicBroadcastGrpc .newStub (lmanagedChannel );
@@ -143,11 +162,11 @@ Ab.BroadcastResponse sendTransaction(Common.Envelope envelope) throws Exception
143162 @ Override
144163 public void onNext (Ab .BroadcastResponse resp ) {
145164 // logger.info("Got Broadcast response: " + resp);
146- logger .debug (" resp status value: " + resp .getStatusValue () + ", resp: " + resp .getStatus ());
165+ logger .debug (this . toString () + " resp status value: " + resp .getStatusValue () + ", resp: " + resp .getStatus ());
147166 if (resp .getStatus () == Common .Status .SUCCESS ) {
148167 ret [0 ] = resp ;
149168 } else {
150- throwable [0 ] = new TransactionException (format ("Channel %s orderer %s status returned failure code %d (%s) during order registration " ,
169+ throwable [0 ] = new TransactionException (format ("Channel %s orderer %s status returned failure code %d (%s) during orderer next " ,
151170 channelName , name , resp .getStatusValue (), resp .getStatus ().name ()));
152171 }
153172 finishLatch .countDown ();
@@ -157,6 +176,17 @@ public void onNext(Ab.BroadcastResponse resp) {
157176 @ Override
158177 public void onError (Throwable t ) {
159178 if (!shutdown ) {
179+ ManagedChannel lmanagedChannel = managedChannel ;
180+ managedChannel = null ;
181+ if (lmanagedChannel == null ) {
182+ logger .error (this .toString () + " managed channel was null." );
183+
184+ } else {
185+
186+ logger .error (format ("%s managed channel isTerminated: %b, isShutdown: %b, state: %s" , this .toString (),
187+ lmanagedChannel .isTerminated (), lmanagedChannel .isShutdown (), lmanagedChannel .getState (false ).name ()));
188+
189+ }
160190 logger .error (format ("Received error on channel %s, orderer %s, url %s, %s" ,
161191 channelName , name , url , t .getMessage ()), t );
162192 }
@@ -166,6 +196,7 @@ public void onError(Throwable t) {
166196
167197 @ Override
168198 public void onCompleted () {
199+ logger .trace (this .toString () + " onComplete received." );
169200 finishLatch .countDown ();
170201 }
171202 };
@@ -177,7 +208,7 @@ public void onCompleted() {
177208 try {
178209 if (!finishLatch .await (ordererWaitTimeMilliSecs , TimeUnit .MILLISECONDS )) {
179210 TransactionException ste = new TransactionException (format ("Channel %s, send transactions failed on orderer %s. Reason: timeout after %d ms." ,
180- channelName , name , ordererWaitTimeMilliSecs ));
211+ channelName , this . toString () , ordererWaitTimeMilliSecs ));
181212 logger .error ("sendTransaction error " + ste .getMessage (), ste );
182213 throw ste ;
183214 }
@@ -190,14 +221,14 @@ public void onCompleted() {
190221 }
191222 //get full stack trace
192223 TransactionException ste = new TransactionException (format ("Channel %s, send transaction failed on orderer %s. Reason: %s" ,
193- channelName , name , throwable [0 ].getMessage ()), throwable [0 ]);
194- logger .error ("sendTransaction error " + ste .getMessage (), ste );
224+ channelName , this . toString () , throwable [0 ].getMessage ()), throwable [0 ]);
225+ logger .error (this . toString () + "sendTransaction error " + ste .getMessage (), ste );
195226 throw ste ;
196227 }
197- logger .debug ("Done waiting for reply! Got:" + ret [0 ]);
228+ logger .debug (this . toString () + "Done waiting for reply! Got:" + ret [0 ]);
198229
199230 } catch (InterruptedException e ) {
200- logger .error (e );
231+ logger .error (this . toString (), e );
201232
202233 }
203234
@@ -223,21 +254,41 @@ public void onCompleted() {
223254
224255 DeliverResponse [] sendDeliver (Common .Envelope envelope ) throws TransactionException {
225256
257+ logger .trace (this .toString () + " OrdererClient.sendDeliver entered." );
258+
226259 if (shutdown ) {
227260 throw new TransactionException ("Orderer client is shutdown" );
228261 }
229262
230263 StreamObserver <Common .Envelope > nso = null ;
231264
232265 ManagedChannel lmanagedChannel = managedChannel ;
266+ if (TRACELEVEL && lmanagedChannel != null ) {
267+ logger .trace (format ("%s managed channel isTerminated: %b, isShutdown: %b, state: %s" , this .toString (),
268+ lmanagedChannel .isTerminated (), lmanagedChannel .isShutdown (), lmanagedChannel .getState (false ).name ()));
269+ }
233270
234271 if (lmanagedChannel == null || lmanagedChannel .isTerminated () || lmanagedChannel .isShutdown ()) {
235272
273+ if (lmanagedChannel != null && lmanagedChannel .isTerminated ()) {
274+ logger .warn (format ("%s managed channel was marked terminated" , this .toString ()));
275+ }
276+ if (lmanagedChannel != null && lmanagedChannel .isShutdown ()) {
277+ logger .warn (format ("%s managed channel was marked shutdown." , this .toString ()));
278+ }
236279 lmanagedChannel = channelBuilder .build ();
237280 managedChannel = lmanagedChannel ;
238281
239282 }
240283
284+ if (TRACELEVEL && lmanagedChannel != null ) {
285+ logger .trace (format ("%s managed channel isTerminated: %b, isShutdown: %b, state: %s" , this .toString (),
286+ lmanagedChannel .isTerminated (), lmanagedChannel .isShutdown (), lmanagedChannel .getState (false ).name ()));
287+ }
288+ /*
289+ return lchannel != null && !lchannel.isShutdown() && !lchannel.isTerminated() && ConnectivityState.READY.equals(lchannel.getState(true));
290+ */
291+
241292 try {
242293
243294 AtomicBroadcastGrpc .AtomicBroadcastStub broadcast = AtomicBroadcastGrpc .newStub (lmanagedChannel );
@@ -254,9 +305,10 @@ DeliverResponse[] sendDeliver(Common.Envelope envelope) throws TransactionExcept
254305 public void onNext (DeliverResponse resp ) {
255306
256307 // logger.info("Got Broadcast response: " + resp);
257- logger .debug (" resp status value: " + resp .getStatusValue () + ", resp: " + resp .getStatus () + ", type case: " + resp .getTypeCase ());
308+ logger .debug (this . toString () + "sendDeliver resp status value: " + resp .getStatusValue () + ", resp: " + resp .getStatus () + ", type case: " + resp .getTypeCase ());
258309
259310 if (done ) {
311+ logger .trace (this .toString () + " sendDeliver done!" );
260312 return ;
261313 }
262314
@@ -275,6 +327,18 @@ public void onNext(DeliverResponse resp) {
275327 @ Override
276328 public void onError (Throwable t ) {
277329 if (!shutdown ) {
330+
331+ ManagedChannel lmanagedChannel = managedChannel ;
332+ managedChannel = null ;
333+ if (lmanagedChannel == null ) {
334+ logger .error (this .toString () + " managed channel was null." );
335+
336+ } else {
337+
338+ logger .error (format ("%s managed channel isTerminated: %b, isShutdown: %b, state: %s" , this .toString (),
339+ lmanagedChannel .isTerminated (), lmanagedChannel .isShutdown (), lmanagedChannel .getState (false ).name ()));
340+
341+ }
278342 logger .error (format ("Received error on channel %s, orderer %s, url %s, %s" ,
279343 channelName , name , url , t .getMessage ()), t );
280344 }
@@ -284,7 +348,7 @@ public void onError(Throwable t) {
284348
285349 @ Override
286350 public void onCompleted () {
287- logger .trace ("onCompleted" );
351+ logger .trace (this . toString () + "onCompleted. " );
288352 finishLatch .countDown ();
289353 }
290354 };
@@ -296,43 +360,50 @@ public void onCompleted() {
296360 try {
297361 if (!finishLatch .await (ordererWaitTimeMilliSecs , TimeUnit .MILLISECONDS )) {
298362 TransactionException ex = new TransactionException (format (
299- "Channel %s sendDeliver time exceeded for orderer %s, timed out at %d ms." , channelName , name , ordererWaitTimeMilliSecs ));
363+ "Channel %s sendDeliver time exceeded for orderer %s, timed out at %d ms." , channelName , this . toString () , ordererWaitTimeMilliSecs ));
300364 logger .error (ex .getMessage (), ex );
301365 throw ex ;
302366 }
303- logger .trace (" Done waiting for reply!" );
367+ logger .trace (this . toString () + " Done waiting for reply!" );
304368
305369 } catch (InterruptedException e ) {
306- logger .error (e );
370+ logger .error (this . toString () + " " + e . getMessage (), e );
307371 }
308372
309373 if (!throwableList .isEmpty ()) {
310374 Throwable throwable = throwableList .get (0 );
311375 TransactionException e = new TransactionException (format (
312- "Channel %s sendDeliver failed on orderer %s. Reason: %s" , channelName , name , throwable .getMessage ()), throwable );
376+ "Channel %s sendDeliver failed on orderer %s. Reason: %s" , channelName , this . toString () , throwable .getMessage ()), throwable );
313377 logger .error (e .getMessage (), e );
314378 throw e ;
315379 }
316380
317381 return retList .toArray (new DeliverResponse [retList .size ()]);
318382 } catch (Throwable t ) {
319383 managedChannel = null ;
384+ logger .error (this .toString () + " received error " + t .getMessage (), t );
320385 throw t ;
321386
322387 } finally {
323388 if (null != nso ) {
324389
325390 try {
391+ logger .debug (this .toString () + "completed." );
326392 nso .onCompleted ();
327393 } catch (Exception e ) { //Best effort only report on debug
328394 logger .debug (format ("Exception completing sendDeliver with channel %s, name %s, url %s %s" ,
329- channelName , name , url , e .getMessage ()), e );
395+ channelName , this . toString () , url , e .getMessage ()), e );
330396 }
331397
332398 }
333399 }
334400 }
335401
402+ @ Override
403+ public String toString () {
404+ return "OrdererClient-" + channelName + "-" + name + "(" + url + ")" ;
405+ }
406+
336407 boolean isChannelActive () {
337408 ManagedChannel lchannel = managedChannel ;
338409 return lchannel != null && !lchannel .isShutdown () && !lchannel .isTerminated () && ConnectivityState .READY .equals (lchannel .getState (true ));
0 commit comments