*** empty log message ***

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gsweb/trunk@6293 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
mguesdon 2000-03-16 16:16:49 +00:00
parent b45dc89478
commit b13501c8e3
62 changed files with 3645 additions and 1770 deletions

View file

@ -31,6 +31,7 @@
#include "GSWUtil.h"
#include "GSWDict.h"
#include "GSWList.h"
#include "GSWString.h"
#include "GSWURLUtil.h"
#include "GSWConfig.h"
#include "GSWAppRequestStruct.h"
@ -42,303 +43,68 @@
#include "GSWLoadBalancing.h"
#include "GSWLock.h"
static GSWLock g_lockAppList;
static GSWList* g_pAppList = NULL;
static time_t config_mtime = (time_t)0;
// Callback Functions
static int compareApps(CONST void *p1, CONST void *p2)
{
GSWApp* pApp1=*(GSWApp**)p1;
GSWApp* pApp2=*(GSWApp**)p2;
return strcmp(pApp1->pszName,pApp2->pszName);
}
static int compareInstances(CONST void *p1, CONST void *p2)
{
GSWAppInstance* pAppInstance1=*(GSWAppInstance**)p1;
GSWAppInstance* pAppInstance2=*(GSWAppInstance**)p2;
return (pAppInstance1->iInstance-pAppInstance2->iInstance);
}
static int compareAppNames(CONST void *p1, CONST void *p2)
{
GSWApp* pApp=*(GSWApp**)p2;
return strcmp((char*)p1,pApp->pszName);
}
void GSWLoadBalancing_Init(GSWDict* p_pDict)
{
if (p_pDict)
{
CONST char* pszPath=GSWDict_ValueForKey(p_pDict,g_szGSWeb_Conf_ConfigFilePath);
GSWConfig_SetConfigFilePath(pszPath);
};
GSWLock_Init(g_lockAppList);
};
static GSWLoadBalancing_ClearInstances()
{
int iAppIndex=0;
int iInstanceIndex=0;
GSWApp *pApp = NULL;
GSWAppInstance *pAppInstance = NULL;
for (iAppIndex=0;iAppIndex<g_pAppList->uCount;iAppIndex++)
{
pApp = GSWList_ElementAtIndex(g_pAppList,iAppIndex);
for (iInstanceIndex=0;iInstanceIndex<pApp->stInstances.uCount;iInstanceIndex++)
{
pAppInstance = GSWList_ElementAtIndex(&pApp->stInstances,iInstanceIndex);
pAppInstance->fValid=FALSE;
};
};
};
static EGSWConfigResult GSWLoadBalancing_NewInstance(STGSWConfigEntry* p_pConfigEntry,
void* p_pLogServerData)
{
EGSWConfigResult eResult=EGSWConfigResult__Ok;
int iAppIndex=0;
int iInstanceIndex=0;
GSWApp *pApp = NULL;
GSWAppInstance *pAppInstance = NULL;
if (p_pConfigEntry)
{
BOOL fFound=FALSE;
for (iAppIndex=0;! fFound && iAppIndex<g_pAppList->uCount;iAppIndex++)
{
pApp = GSWList_ElementAtIndex(g_pAppList,iAppIndex);
fFound=(strcmp(p_pConfigEntry->pszAppName,pApp->pszName)==0);
};
if (!fFound)
{
time_t now;
time(&now);
srand(now);
pApp=(GSWApp*)calloc(1,sizeof(GSWApp));
pApp->pszName=strdup(p_pConfigEntry->pszAppName);
pApp->iIndex = rand();
GSWList_Add(g_pAppList,pApp);
};
fFound = 0;
for (iInstanceIndex=0;!fFound && iInstanceIndex<pApp->stInstances.uCount;iInstanceIndex++)
{
pAppInstance = GSWList_ElementAtIndex(&(pApp->stInstances),iInstanceIndex);
if (pAppInstance->iInstance==p_pConfigEntry->iInstance)
{
fFound=TRUE;
free(pAppInstance->pszHost);
pAppInstance->pszHost=NULL;
pAppInstance->pszHost=strdup(p_pConfigEntry->pszHostName);
};
};
if (!fFound)
{
pAppInstance = (GSWAppInstance *)calloc(1,sizeof(GSWAppInstance));
pAppInstance->iInstance = p_pConfigEntry->iInstance;
pAppInstance->pszHost = strdup(p_pConfigEntry->pszHostName);
GSWList_Add(&pApp->stInstances,pAppInstance);
};
pAppInstance->iPort = p_pConfigEntry->iPort;
pAppInstance->timeNextRetryTime=0;
pAppInstance->fValid=TRUE;
GSWLog(GSW_INFO,p_pLogServerData,
"Config: %s instance %d host %s port %d Valid:%s timeNextRetryTime? %d",
pApp->pszName,
pAppInstance->iInstance,
pAppInstance->pszHost,
pAppInstance->iPort,
(pAppInstance->fValid ? "YES" : "NO"),
pAppInstance->timeNextRetryTime);
};
return eResult;
};
static void GSWLoadBalancing_VerifyConfiguration(void* p_pLogServerData)
{
proplist_t propListConfig=NULL;
if (!g_pAppList)
g_pAppList = GSWList_New(16);
if (GSWConfig_ReadIFND(GSWConfig_GetConfigFilePath(),
&config_mtime,
&propListConfig,
p_pLogServerData)==EGSWConfigResult__Ok)
{
proplist_t propListApps=NULL;
GSWLoadBalancing_ClearInstances();
propListApps=GSWConfig_GetApplicationsFromConfig(propListConfig);
if (propListApps)
{
int iAppIndex=0;
int iInstanceIndex=0;
GSWApp* pApp=NULL;
GSWAppInstance* pAppInstance=NULL;
proplist_t propListAppsNames=GSWConfig_ApplicationsKeysFromApplications(propListApps);
unsigned int uAppNb=PLGetNumberOfElements(propListAppsNames);
for(iAppIndex=0;iAppIndex<uAppNb;iAppIndex++)
{
proplist_t propListAppKey=GSWConfig_ApplicationKeyFromApplicationsKey(propListAppsNames,iAppIndex);
if (!propListAppKey)
{
//TODO
}
else
{
CONST char* pszAppName=PLGetString(propListAppKey);
proplist_t propListApp=GSWConfig_ApplicationFromApplications(propListApps,
propListAppKey);
if (!propListApp)
{
GSWLog(GSW_ERROR,p_pLogServerData,"no ppropListApp");
//TODO
}
else
{
proplist_t propListInstances=GSWConfig_InstancesFromApplication(propListApp);
if (!propListInstances)
{
GSWLog(GSW_ERROR,p_pLogServerData,"no propListInstances");
//TODO
}
else
{
unsigned int uInstancesNb=PLGetNumberOfElements(propListInstances);
for(iInstanceIndex=0;iInstanceIndex<uInstancesNb;iInstanceIndex++)
{
proplist_t propListInstance=PLGetArrayElement(propListInstances,iInstanceIndex);
if (!propListInstance)
{
GSWLog(GSW_ERROR,p_pLogServerData,"no propListInstance");
//TODO
}
else if (!PLIsDictionary(propListInstance))
{
GSWLog(GSW_ERROR,p_pLogServerData,"propListInstance is not a dictionary");
}
else
{
STGSWConfigEntry stEntry;
GSWConfig_PropListInstanceToInstanceEntry(&stEntry,
propListInstance,
pszAppName);
GSWLoadBalancing_NewInstance(&stEntry,p_pLogServerData);
};
};
};
};
};
};
// Changed !
for (iAppIndex=g_pAppList->uCount-1;iAppIndex>=0;iAppIndex--)
{
pApp=GSWList_ElementAtIndex(g_pAppList,iAppIndex);
for (iInstanceIndex=pApp->stInstances.uCount-1;iInstanceIndex>=0;iInstanceIndex--)
{
pAppInstance = GSWList_ElementAtIndex(&pApp->stInstances,iInstanceIndex);
if (!pAppInstance->fValid)
{
GSWLog(GSW_INFO,p_pLogServerData,"Removing %s instance %d %s",
pApp->pszName,
pAppInstance->iInstance,
pAppInstance->pszHost);
GSWList_RemoveAtIndex(&pApp->stInstances,iInstanceIndex);
if (pAppInstance->uOpenedRequestsNb==0)
{
free(pAppInstance->pszHost);
pAppInstance->pszHost=NULL;
free(pAppInstance);
pAppInstance=NULL;
};
};
};
if (pApp->stInstances.uCount==0)
{
GSWList_RemoveAtIndex(g_pAppList,iAppIndex);
GSWLog(GSW_INFO,p_pLogServerData,"Removing application %s as there is no instance left.",
pApp->pszName);
}
else
{
GSWList_Sort(&pApp->stInstances,compareInstances);
for (iInstanceIndex=0;iInstanceIndex<pApp->stInstances.uCount-1;iInstanceIndex++)
{
GSWAppInstance* pAppInstance0=GSWList_ElementAtIndex(&pApp->stInstances,iInstanceIndex);
GSWAppInstance* pAppInstance1=GSWList_ElementAtIndex(&pApp->stInstances,iInstanceIndex+1);
if (pAppInstance0->iInstance == pAppInstance1->iInstance)
{
GSWLog(GSW_ERROR,
p_pLogServerData,
"Configuration error: instance numbers must be unique:\n\t(%s:%d@%s) == (%s:%d@%s)",
pApp->pszName,pAppInstance0->iInstance,
pAppInstance0->pszHost,
pApp->pszName,pAppInstance1->iInstance,
pAppInstance1->pszHost);
};
};
};
};
GSWList_Sort(g_pAppList,compareApps);
};
};
};
BOOL GSWLoadBalancing_FindApp(void* p_pLogServerData,GSWAppRequest *p_pAppRequest)
//--------------------------------------------------------------------
BOOL GSWLoadBalancing_FindApp(GSWAppRequest* p_pAppRequest,
void* p_pLogServerData)
{
BOOL fFound=FALSE;
GSWApp* pApp=NULL;
GSWLog(GSW_DEBUG,p_pLogServerData,"Start GSWLoadBalancing_FindApp");
GSWLog(GSW_INFO,p_pLogServerData,"LoadBalance: looking for %s",
p_pAppRequest->pszName);
GSWConfig_LoadConfiguration(p_pLogServerData);
GSWLock_Lock(g_lockAppList);
GSWLoadBalancing_VerifyConfiguration(p_pLogServerData);
pApp = GSWList_BSearch(g_pAppList,
p_pAppRequest->pszName,
compareAppNames);
pApp = GSWConfig_GetApp(p_pAppRequest->pszName);
if (pApp)
{
int iTries=pApp->stInstances.uCount;
GSWList* pInstancesList=GSWDict_AllKeys(&pApp->stInstancesDict);
unsigned int uInstancesCount=GSWList_Count(pInstancesList);
int iTries=uInstancesCount;
GSWAppInstance* pAppInstance=NULL;
time_t curTime = (time_t)0;
while (!fFound && iTries-->0)
{
pApp->iIndex = (pApp->iIndex+1) % pApp->stInstances.uCount;
pAppInstance=GSWList_ElementAtIndex((&pApp->stInstances),pApp->iIndex);
if (pAppInstance->timeNextRetryTime!=0)
pApp->iIndex = (pApp->iIndex+1) % uInstancesCount;
pAppInstance=(GSWAppInstance*)GSWDict_ValueForKey(&pApp->stInstancesDict,
GSWList_ElementAtIndex(pInstancesList,pApp->iIndex));
if (pAppInstance)
{
if (!curTime)
time(&curTime);
if (pAppInstance->timeNextRetryTime<curTime)
if (!pAppInstance->pApp)
{
GSWLog(GSW_INFO,
p_pLogServerData,
"LoadBalance: Instance %s:%d was marked dead for %d secs. Now resurecting !",
p_pAppRequest->pszName,
pAppInstance->iInstance,
APP_CONNECT_RETRY_DELAY);
pAppInstance->timeNextRetryTime=0;
GSWLog(GSW_CRITICAL,p_pLogServerData,
"AppInstance pApp is null pAppInstance=%p",
pAppInstance);
};
if (pAppInstance->timeNextRetryTime!=0)
{
if (!curTime)
time(&curTime);
if (pAppInstance->timeNextRetryTime<curTime)
{
GSWLog(GSW_CRITICAL,
p_pLogServerData,
"LoadBalance: Instance %s:%d was marked dead for %d secs. Now resurecting !",
p_pAppRequest->pszName,
pAppInstance->iInstance,
APP_CONNECT_RETRY_DELAY);
pAppInstance->timeNextRetryTime=0;
};
};
if (pAppInstance->timeNextRetryTime==0 && pAppInstance->fValid)
{
fFound = TRUE;
strcpy(p_pAppRequest->pszName,pApp->pszName);
p_pAppRequest->iInstance = pAppInstance->iInstance;
p_pAppRequest->pszHost = pAppInstance->pszHostName;
p_pAppRequest->iPort = pAppInstance->iPort;
p_pAppRequest->eType = EAppType_LoadBalanced;
p_pAppRequest->pAppInstance = pAppInstance;
pAppInstance->uOpenedRequestsNb++;
};
};
if (pAppInstance->timeNextRetryTime==0 && pAppInstance->fValid)
{
fFound = TRUE;
strcpy(p_pAppRequest->pszName,pApp->pszName);
p_pAppRequest->iInstance = pAppInstance->iInstance;
p_pAppRequest->pszHost = pAppInstance->pszHost;
p_pAppRequest->iPort = pAppInstance->iPort;
p_pAppRequest->eType = EAppType_LoadBalanced;
p_pAppRequest->pLoadBalancingData = pAppInstance;
pAppInstance->uOpenedRequestsNb++;
};
};
GSWList_Free(pInstancesList,FALSE);
};
GSWLock_Unlock(g_lockAppList);
@ -347,60 +113,69 @@ BOOL GSWLoadBalancing_FindApp(void* p_pLogServerData,GSWAppRequest *p_pAppReques
p_pAppRequest->pszName,
p_pAppRequest->iInstance,
p_pAppRequest->pszHost,
p_pAppRequest->iPort);
p_pAppRequest->iPort);
else
GSWLog(GSW_INFO,p_pLogServerData,"LoadBalance: looking for %s, Not Found",
p_pAppRequest->pszName);
GSWLog(GSW_DEBUG,p_pLogServerData,"Stop GSWLoadBalancing_FindApp");
return fFound;
};
BOOL GSWLoadBalancing_FindInstance(void* p_pLogServerData,GSWAppRequest *p_pAppRequest)
//--------------------------------------------------------------------
BOOL GSWLoadBalancing_FindInstance(GSWAppRequest *p_pAppRequest,void* p_pLogServerData)
{
BOOL fFound=FALSE;
GSWApp* pApp=NULL;
int i=0;
GSWLog(GSW_DEBUG,p_pLogServerData,"Start GSWLoadBalancing_FindInstance");
GSWConfig_LoadConfiguration(p_pLogServerData);
GSWLock_Lock(g_lockAppList);
GSWLoadBalancing_VerifyConfiguration(p_pLogServerData);
pApp=GSWList_BSearch(g_pAppList,p_pAppRequest->pszName,compareAppNames);
pApp = (GSWApp*)GSWConfig_GetApp(p_pAppRequest->pszName);
if (pApp)
{
GSWAppInstance* pAppInstance=NULL;
for (i=0;i<pApp->stInstances.uCount && !fFound;i++)
char szInstanceNum[50]="";
sprintf(szInstanceNum,"%d",p_pAppRequest->iInstance);
pAppInstance=(GSWAppInstance*)GSWDict_ValueForKey(&pApp->stInstancesDict,szInstanceNum);
if (pAppInstance && pAppInstance->fValid)
{
pAppInstance = GSWList_ElementAtIndex((&pApp->stInstances),i);
if (pAppInstance->iInstance
&& pAppInstance->iInstance==p_pAppRequest->iInstance
&& pAppInstance->fValid)
{
fFound=TRUE;
p_pAppRequest->iInstance = pAppInstance->iInstance;
p_pAppRequest->pszHost = pAppInstance->pszHost;
p_pAppRequest->iPort = pAppInstance->iPort;
p_pAppRequest->eType = EAppType_LoadBalanced;
p_pAppRequest->pLoadBalancingData = pAppInstance;
pAppInstance->uOpenedRequestsNb++;
};
fFound=TRUE;
p_pAppRequest->iInstance = pAppInstance->iInstance;
p_pAppRequest->pszHost = pAppInstance->pszHostName;
p_pAppRequest->iPort = pAppInstance->iPort;
p_pAppRequest->eType = EAppType_LoadBalanced;
p_pAppRequest->pAppInstance = pAppInstance;
pAppInstance->uOpenedRequestsNb++;
};
};
GSWLock_Unlock(g_lockAppList);
GSWLog(GSW_DEBUG,p_pLogServerData,"Stop GSWLoadBalancing_FindInstance");
return fFound;
};
void GSWLoadBalancing_MarkNotRespondingApp(void* p_pLogServerData,GSWAppRequest *p_pAppRequest)
//--------------------------------------------------------------------
void GSWLoadBalancing_MarkNotRespondingApp(GSWAppRequest* p_pAppRequest,
void* p_pLogServerData)
{
GSWAppInstance* pAppInstance;
time_t now;
time(&now);
pAppInstance = (GSWAppInstance *)p_pAppRequest->pLoadBalancingData;
pAppInstance =p_pAppRequest->pAppInstance;
pAppInstance->uOpenedRequestsNb--;
pAppInstance->timeNextRetryTime=now+APP_CONNECT_RETRY_DELAY;
GSWLog(GSW_WARNING,p_pLogServerData,"Marking %s unresponsive",p_pAppRequest->pszName);
}
if (!pAppInstance->fValid)
{
if (GSWAppInstance_FreeIFND(pAppInstance))
pAppInstance=NULL;
};
};
void GSWLoadBalancing_StartAppRequest(void* p_pLogServerData,GSWAppRequest *p_pAppRequest)
//--------------------------------------------------------------------
void GSWLoadBalancing_StartAppRequest(GSWAppRequest* p_pAppRequest,
void* p_pLogServerData)
{
GSWAppInstance* pAppInstance=(GSWAppInstance*)p_pAppRequest->pLoadBalancingData;
GSWAppInstance* pAppInstance=p_pAppRequest->pAppInstance;
if (pAppInstance->timeNextRetryTime!=0)
{
pAppInstance->timeNextRetryTime=0;
@ -408,18 +183,19 @@ void GSWLoadBalancing_StartAppRequest(void* p_pLogServerData,GSWAppRequest *p_pA
};
}
void GSWLoadBalancing_StopAppRequest(void* p_pLogServerData,GSWAppRequest *p_pAppRequest)
//--------------------------------------------------------------------
void GSWLoadBalancing_StopAppRequest(GSWAppRequest *p_pAppRequest,
void* p_pLogServerData)
{
GSWAppInstance* pAppInstance=(GSWAppInstance*)p_pAppRequest->pLoadBalancingData;
GSWAppInstance* pAppInstance=p_pAppRequest->pAppInstance;
GSWLock_Lock(g_lockAppList);
pAppInstance->uOpenedRequestsNb--;
if (!pAppInstance->fValid && pAppInstance->uOpenedRequestsNb==0)
if (!pAppInstance->fValid)
{
GSWLog(GSW_ERROR,p_pLogServerData,"Not deleted (not implemented) %s (%d)",
p_pAppRequest->pszName,
p_pAppRequest->iInstance);
if (GSWAppInstance_FreeIFND(pAppInstance))
pAppInstance=NULL;
};
GSWLock_Unlock(g_lockAppList);
p_pAppRequest->pLoadBalancingData = NULL;
p_pAppRequest->pAppInstance = NULL;
};