void processContainersAfterDecreaseMemoryQuota(GRMContainerSet ctns, bool kicked)
{
	GRMContainer ctn = NULL;

    /* Handle the containers. */
    if ( kicked ) {
    	moveGRMContainerSetToKicked(ctns);
    }
    else {
    	while( ctns->Containers != NULL )
    	{
    		ctn = popGRMContainerSetContainerList(ctns);
    		ctn->Life += 1;

    		PRESPOOL->RetPendingContainerCount--;
    		/* Add container to ToKickContainers if lifetime is not too long */
    		if( ctn->Life < RESOURCE_CONTAINER_MAX_LIFETIME )
    		{
    			/* Clear decrease pending quota because we will add back. */
    			minusResourceBundleData(&(ctn->Resource->DecPending),
    									ctn->MemoryMB,
										ctn->Core);
    			Assert( ctn->Resource->DecPending.MemoryMB >= 0 );
    			Assert( ctn->Resource->DecPending.Core >= 0 );

    			addGRMContainerToToBeKicked(ctn);
    		}
    		/* Add container to KickedContainers if lifetime is long enough */
    		else
    		{
    			addGRMContainerToKicked(ctn);
    		}
    	}
    }
}
void RB_clearResource(List **ctnl)
{
	while( (*ctnl) != NULL )
	{
		GRMContainer ctn = (GRMContainer)lfirst(list_head(*ctnl));
		MEMORY_CONTEXT_SWITCH_TO(PCONTEXT)
		(*ctnl) = list_delete_first(*ctnl);
		MEMORY_CONTEXT_SWITCH_BACK

		elog(LOG, "Resource broker dropped GRM container "INT64_FORMAT
				  "(%d MB, %d CORE) on host %s",
				  ctn->ID,
				  ctn->MemoryMB,
				  ctn->Core,
				  ctn->HostName == NULL ? "NULL" : ctn->HostName);

		if ( ctn->CalcDecPending )
		{
			minusResourceBundleData(&(ctn->Resource->DecPending), ctn->MemoryMB, ctn->Core);
			Assert( ctn->Resource->DecPending.Core >= 0 );
			Assert( ctn->Resource->DecPending.MemoryMB >= 0 );
		}

		/* Destroy resource container. */
		freeGRMContainer(ctn);
		PRESPOOL->RetPendingContainerCount--;
	}
}
int minusSessionInUseResource(ResqueueDeadLockDetector	detector,
							  int64_t					sessionid,
							  uint32_t 					memorymb,
							  double 					core)
{
	/* Build key */
	SimpArray key;
	setSimpleArrayRef(&key, (char *)&sessionid, sizeof(int64_t));

	/* Check if the session id exists. */
	PAIR pair = getHASHTABLENode(&(detector->Sessions), &key);
	if ( pair == NULL ) {
		return RESQUEMGR_NO_SESSIONID;
	}

	minusResourceBundleData(&(detector->InUseTotal), memorymb, core);

	SessionTrack sessiontrack = (SessionTrack)(pair->Value);
	Assert( sessiontrack != NULL );

	minusResourceBundleData(&(sessiontrack->InUseTotal), memorymb, core);

	Assert(detector->InUseTotal.Core >= 0.0 &&
		   detector->InUseTotal.MemoryMB >= 0);
	Assert(sessiontrack->InUseTotal.Core >= 0.0 &&
		   sessiontrack->InUseTotal.MemoryMB >= 0);

	/* If the session has no resource used, remove the session tracker. */
	if ( sessiontrack->InUseTotal.MemoryMB == 0 &&
		 sessiontrack->InUseTotal.Core == 0.0 )
	{
		rm_pfree(PCONTEXT, sessiontrack);
		removeHASHTABLENode(&(detector->Sessions), &key);
	}

	return FUNC_RETURN_OK;
}