NodeMetaInfoProvider { public static boolean increaseConcurrentOperationCount( String nodeId ) { boolean canPerformOperationOnNode = true; nodeServiceLock.lock(); try { logger.debug( "Got the lock for determining feasibility to call operation service for node:" + nodeId ); Integer currentOperationCount = (Integer) nodeMetaInfoMap.get( nodeId ); if ( currentOperationCount == null ) { currentOperationCount = 0; } int maxConcurrentTasksPerNode = CommonProperties.getMaxConcurrentTasksPerNode(); if ( currentOperationCount >= maxConcurrentTasksPerNode ) { logger.warn( "Will not perform further operation. Reached Max concurrent operation count for Node:" + nodeId + ". Current count:" + maxConcurrentTasksPerNode ); canPerformOperationOnNode = false; } else { ++currentOperationCount; logger.debug( "Node can perform operation. Increasing current concurrent operation count for Node:" + nodeId + " set to:" + currentOperationCount ); nodeMetaInfoMap.put( nodeId, currentOperationCount ); } } catch ( Exception ex ) { logger.warn( "Error while checking for current operation count for Node:" + nodeId, ex ); } finally { nodeServiceLock.unlock(); } return canPerformOperationOnNode; }  static boolean increaseConcurrentOperationCount( String nodeId ); static boolean decreaseConcurrentOperationCount( String nodeId );  }
@Test public void increaseConcurrentOperationCountTest() { int noOfConcurrentOperations = CommonProperties.getMaxConcurrentTasksPerNode(); for ( int currentCount = 0; currentCount < noOfConcurrentOperations - 1; ++currentCount ) { NodeMetaInfoProvider.increaseConcurrentOperationCount( "N1" ); } Boolean lockAcquired = NodeMetaInfoProvider.increaseConcurrentOperationCount( "N1" ); assertTrue( lockAcquired ); for ( int currentCount = 0; currentCount <= noOfConcurrentOperations; ++currentCount ) { NodeMetaInfoProvider.decreaseConcurrentOperationCount( "N1" ); } }
@Ignore @Test public void increaseConcurrentOperationCountFailTest() { int noOfConcurrentOperations = CommonProperties.getMaxConcurrentTasksPerNode(); for ( int currentCount = 1; currentCount < noOfConcurrentOperations + 1; ++currentCount ) { NodeMetaInfoProvider.increaseConcurrentOperationCount( "N1" ); } Boolean lockAcquired = NodeMetaInfoProvider.increaseConcurrentOperationCount( "N1" ); assertFalse( lockAcquired ); }