@@ -360,6 +360,7 @@ public Answer sendTo(final Long dcId, final HypervisorType type, final Command c
360360 public Answer send (final Long hostId , final Command cmd ) throws AgentUnavailableException , OperationTimedoutException {
361361 final Commands cmds = new Commands (Command .OnError .Stop );
362362 cmds .addCommand (cmd );
363+ logger .debug (String .format ("Wait time set on the command %s is %d seconds" , cmd , cmd .getWait ()));
363364 send (hostId , cmds , cmd .getWait ());
364365 final Answer [] answers = cmds .getAnswers ();
365366 if (answers != null && !(answers [0 ] instanceof UnsupportedAnswer )) {
@@ -424,15 +425,75 @@ private void setEmptyAnswers(final Commands commands, final Command[] cmds) {
424425 }
425426 }
426427
428+ protected int getTimeout (final Commands commands , int timeout ) {
429+ int result ;
430+ if (timeout > 0 ) {
431+ result = timeout ;
432+ } else {
433+ logger .debug (String .format ("Considering the Wait global setting %d, since wait time set on command is 0" , Wait .value ()));
434+ result = Wait .value ();
435+ }
436+
437+ int granularTimeout = getTimeoutFromGranularWaitTime (commands );
438+ return (granularTimeout > 0 ) ? granularTimeout : result ;
439+ }
440+
441+ protected int getTimeoutFromGranularWaitTime (final Commands commands ) {
442+ logger .debug ("Looking for the commands.timeout global setting for any command-specific timeout value" );
443+ String commandWaits = GranularWaitTimeForCommands .value ().trim ();
444+
445+ int maxWait = 0 ;
446+ if (StringUtils .isNotEmpty (commandWaits )) {
447+ try {
448+ Map <String , Integer > commandTimeouts = getCommandTimeoutsMap (commandWaits );
449+
450+ for (final Command cmd : commands ) {
451+ String simpleCommandName = cmd .getClass ().getSimpleName ();
452+ Integer commandTimeout = commandTimeouts .get (simpleCommandName );
453+
454+ if (commandTimeout != null ) {
455+ logger .debug (String .format ("Timeout %d found for command %s in commands.timeout global setting" , commandTimeout , cmd .toString ()));
456+ if (commandTimeout > maxWait ) {
457+ maxWait = commandTimeout ;
458+ }
459+ }
460+ }
461+ } catch (Exception e ) {
462+ logger .error (String .format ("Error while processing the commands.timeout global setting for the granular timeouts for the command, " +
463+ "falling back to the command timeout: %s" , e .getMessage ()));
464+ }
465+ }
466+
467+ return maxWait ;
468+ }
469+
470+ private Map <String , Integer > getCommandTimeoutsMap (String commandWaits ) {
471+ String [] commandPairs = commandWaits .split ("," );
472+ Map <String , Integer > commandTimeouts = new HashMap <>();
473+
474+ for (String commandPair : commandPairs ) {
475+ String [] parts = commandPair .trim ().split ("=" );
476+ if (parts .length == 2 ) {
477+ String commandName = parts [0 ].trim ();
478+ int commandTimeout = Integer .parseInt (parts [1 ].trim ());
479+ commandTimeouts .put (commandName , commandTimeout );
480+ } else {
481+ logger .warn (String .format ("Invalid format in commands.timeout global setting: %s" , commandPair ));
482+ }
483+ }
484+ return commandTimeouts ;
485+ }
486+
427487 @ Override
428488 public Answer [] send (final Long hostId , final Commands commands , int timeout ) throws AgentUnavailableException , OperationTimedoutException {
429489 assert hostId != null : "Who's not checking the agent id before sending? ... (finger wagging)" ;
430490 if (hostId == null ) {
431491 throw new AgentUnavailableException (-1 );
432492 }
433493
434- if (timeout <= 0 ) {
435- timeout = Wait .value ();
494+ int wait = getTimeout (commands , timeout );
495+ for (Command cmd : commands ) {
496+ cmd .setWait (wait );
436497 }
437498
438499 if (CheckTxnBeforeSending .value ()) {
@@ -454,7 +515,7 @@ public Answer[] send(final Long hostId, final Commands commands, int timeout) th
454515
455516 final Request req = new Request (hostId , agent .getName (), _nodeId , cmds , commands .stopOnError (), true );
456517 req .setSequence (agent .getNextSequence ());
457- final Answer [] answers = agent .send (req , timeout );
518+ final Answer [] answers = agent .send (req , wait );
458519 notifyAnswersToMonitors (hostId , req .getSequence (), answers );
459520 commands .setAnswers (answers );
460521 return answers ;
@@ -988,7 +1049,13 @@ public Answer easySend(final Long hostId, final Command cmd) {
9881049 @ Override
9891050 public Answer [] send (final Long hostId , final Commands cmds ) throws AgentUnavailableException , OperationTimedoutException {
9901051 int wait = 0 ;
1052+ if (cmds .size () > 1 ) {
1053+ logger .debug (String .format ("Checking the wait time in seconds to be used for the following commands : %s. If there are multiple commands sent at once," +
1054+ "then max wait time of those will be used" , cmds ));
1055+ }
1056+
9911057 for (final Command cmd : cmds ) {
1058+ logger .debug (String .format ("Wait time set on the command %s is %d" , cmd , cmd .getWait ()));
9921059 if (cmd .getWait () > wait ) {
9931060 wait = cmd .getWait ();
9941061 }
@@ -1802,7 +1869,7 @@ public String getConfigComponentName() {
18021869 @ Override
18031870 public ConfigKey <?>[] getConfigKeys () {
18041871 return new ConfigKey <?>[] { CheckTxnBeforeSending , Workers , Port , Wait , AlertWait , DirectAgentLoadSize ,
1805- DirectAgentPoolSize , DirectAgentThreadCap , EnableKVMAutoEnableDisable , ReadyCommandWait };
1872+ DirectAgentPoolSize , DirectAgentThreadCap , EnableKVMAutoEnableDisable , ReadyCommandWait , GranularWaitTimeForCommands };
18061873 }
18071874
18081875 protected class SetHostParamsListener implements Listener {
0 commit comments