00001
00002
00003
00004
00005
00006 #include "getHostForGet.h"
00007 #include "getHostForPut.h"
00008 #include "rodsLog.h"
00009 #include "rsGlobalExtern.h"
00010 #include "rcGlobalExtern.h"
00011 #include "getRemoteZoneResc.h"
00012 #include "dataObjCreate.h"
00013 #include "objMetaOpr.h"
00014 #include "resource.h"
00015 #include "collection.h"
00016 #include "specColl.h"
00017 #include "miscServerFunct.h"
00018 #include "openCollection.h"
00019 #include "readCollection.h"
00020 #include "closeCollection.h"
00021 #include "dataObjOpr.h"
00022
00023 int
00024 rsGetHostForGet (rsComm_t *rsComm, dataObjInp_t *dataObjInp,
00025 char **outHost)
00026 {
00027 int status;
00028 rescInfo_t *myRescInfo;
00029 char *myResc;
00030 rodsServerHost_t *rodsServerHost;
00031 rodsHostAddr_t addr;
00032 specCollCache_t *specCollCache = NULL;
00033 char *myHost;
00034 int remoteFlag = 0;
00035
00036 *outHost = NULL;
00037 #if 0 // JMC - backport 4746
00038 if (isLocalZone (dataObjInp->objPath) == 0) {
00039
00040 *outHost = strdup (THIS_ADDRESS);
00041 return 0;
00042 }
00043 #endif // JMC - backport 4746
00044
00045 resolveLinkedPath (rsComm, dataObjInp->objPath, &specCollCache, NULL);
00046 if (isLocalZone (dataObjInp->objPath) == 0) {
00047 #if 0 // JMC - backport 4746
00048
00049 *outHost = strdup (THIS_ADDRESS);
00050 return 0;
00051 #else // JMC - backport 4746
00052 resolveLinkedPath (rsComm, dataObjInp->objPath, &specCollCache,
00053 &dataObjInp->condInput);
00054 remoteFlag = getAndConnRcatHost (rsComm, SLAVE_RCAT,
00055 dataObjInp->objPath, &rodsServerHost);
00056 if (remoteFlag < 0) {
00057 return (remoteFlag);
00058 } else if (remoteFlag == LOCAL_HOST) {
00059 *outHost = strdup (THIS_ADDRESS);
00060 return 0;
00061 } else {
00062 status = rcGetHostForGet (rodsServerHost->conn, dataObjInp,
00063 outHost);
00064 if (status >= 0 && *outHost != NULL &&
00065 strcmp (*outHost, THIS_ADDRESS) == 0) {
00066 free (*outHost);
00067 *outHost = strdup (rodsServerHost->hostName->name);
00068 }
00069 return (status);
00070 }
00071 #endif // JMC - backport 4746
00072 }
00073
00074 status = getSpecCollCache (rsComm, dataObjInp->objPath, 0, &specCollCache);
00075 if (status >= 0 && NULL != specCollCache ) {
00076 if (specCollCache->specColl.collClass == MOUNTED_COLL) {
00077 status = resolveResc (specCollCache->specColl.resource,
00078 &myRescInfo);
00079 if (status < 0) {
00080 rodsLog (LOG_ERROR,
00081 "rsGetHostForGet: resolveResc error for %s, status = %d",
00082 specCollCache->specColl.resource, status);
00083 return status;
00084 }
00085
00086 } else {
00087 *outHost = strdup (THIS_ADDRESS);
00088 return 0;
00089 }
00090 } else if ((myResc = getValByKey (&dataObjInp->condInput, RESC_NAME_KW))
00091 != NULL && resolveResc (myResc, &myRescInfo) >= 0) {
00092
00093 } else {
00094
00095 status = getBestRescForGet (rsComm, dataObjInp, &myRescInfo);
00096 if (myRescInfo == NULL) {
00097 *outHost = strdup (THIS_ADDRESS);
00098 return status;
00099 }
00100 }
00101
00102
00103
00104 if (getRescClass (myRescInfo) == COMPOUND_CL) {
00105 *outHost = strdup (THIS_ADDRESS);
00106 return 0;
00107 }
00108
00109 bzero (&addr, sizeof (addr));
00110 rstrcpy (addr.hostAddr, myRescInfo->rescLoc, NAME_LEN);
00111 status = resolveHost (&addr, &rodsServerHost);
00112 if (status < 0) return status;
00113 if (rodsServerHost->localFlag == LOCAL_HOST) {
00114 *outHost = strdup (THIS_ADDRESS);
00115 return 0;
00116 }
00117
00118 myHost = getSvrAddr (rodsServerHost);
00119 if (myHost != NULL) {
00120 *outHost = strdup (myHost);
00121 return 0;
00122 } else {
00123 *outHost = NULL;
00124 return SYS_INVALID_SERVER_HOST;
00125 }
00126 }
00127
00128 int
00129 getBestRescForGet (rsComm_t *rsComm, dataObjInp_t *dataObjInp,
00130 rescInfo_t **outRescInfo)
00131 {
00132 collInp_t collInp;
00133 hostSearchStat_t hostSearchStat;
00134 int status, i;
00135 int maxInx = -1;
00136
00137 *outRescInfo = NULL;
00138
00139 if (dataObjInp == NULL || outRescInfo == NULL) return USER__NULL_INPUT_ERR;
00140 bzero (&hostSearchStat, sizeof (hostSearchStat));
00141 bzero (&collInp, sizeof (collInp));
00142 rstrcpy (collInp.collName, dataObjInp->objPath, MAX_NAME_LEN);
00143
00144 status = getRescForGetInColl (rsComm, &collInp, &hostSearchStat);
00145 if (status < 0) {
00146
00147 status = getRescForGetInDataObj (rsComm, dataObjInp, &hostSearchStat);
00148 }
00149
00150 for (i = 0; i < hostSearchStat.numHost; i++) {
00151 if (maxInx < 0 ||
00152 hostSearchStat.count[i] > hostSearchStat.count[maxInx]) {
00153 maxInx = i;
00154 }
00155 }
00156 if (maxInx >= 0) *outRescInfo = hostSearchStat.rescInfo[maxInx];
00157
00158 return status;
00159 }
00160
00161 int
00162 getRescForGetInColl (rsComm_t *rsComm, collInp_t *collInp,
00163 hostSearchStat_t *hostSearchStat)
00164 {
00165 collEnt_t *collEnt;
00166 int handleInx;
00167 int status;
00168
00169 if (collInp == NULL || hostSearchStat == NULL) return USER__NULL_INPUT_ERR;
00170
00171 handleInx = rsOpenCollection (rsComm, collInp);
00172 if (handleInx < 0) return handleInx;
00173
00174 while ((status = rsReadCollection (rsComm, &handleInx, &collEnt)) >= 0) {
00175 if (collEnt->objType == DATA_OBJ_T) {
00176 dataObjInp_t dataObjInp;
00177 bzero (&dataObjInp, sizeof (dataObjInp));
00178 snprintf (dataObjInp.objPath, MAX_NAME_LEN, "%s/%s",
00179 collEnt->collName, collEnt->dataName);
00180 status = getRescForGetInDataObj (rsComm, &dataObjInp,
00181 hostSearchStat);
00182 if (status < 0) {
00183 rodsLog (LOG_NOTICE,
00184 "getRescForGetInColl: getRescForGetInDataObj %s err, stat=%d",
00185 dataObjInp.objPath, status);
00186 }
00187 } else if (collEnt->objType == COLL_OBJ_T) {
00188 collInp_t myCollInp;
00189 bzero (&myCollInp, sizeof (myCollInp));
00190 rstrcpy (myCollInp.collName, collEnt->collName, MAX_NAME_LEN);
00191 status = getRescForGetInColl (rsComm, &myCollInp, hostSearchStat);
00192 if (status < 0) {
00193 rodsLog (LOG_NOTICE,
00194 "getRescForGetInColl: getRescForGetInColl %s err, stat=%d",
00195 collEnt->collName, status);
00196 }
00197 }
00198 free (collEnt);
00199 if (hostSearchStat->totalCount >= MAX_HOST_TO_SEARCH) {
00200
00201 rsCloseCollection (rsComm, &handleInx);
00202 return 0;
00203 }
00204 }
00205 rsCloseCollection (rsComm, &handleInx);
00206 return 0;
00207 }
00208
00209 int
00210 getRescForGetInDataObj (rsComm_t *rsComm, dataObjInp_t *dataObjInp,
00211 hostSearchStat_t *hostSearchStat)
00212 {
00213 int status, i;
00214 dataObjInfo_t *dataObjInfoHead = NULL;
00215
00216 if (dataObjInp == NULL || hostSearchStat == NULL)
00217 return USER__NULL_INPUT_ERR;
00218
00219 status = getDataObjInfoIncSpecColl (rsComm, dataObjInp, &dataObjInfoHead);
00220 if (status < 0) return status;
00221
00222 sortObjInfoForOpen (rsComm, &dataObjInfoHead, &dataObjInp->condInput, 0);
00223 if (dataObjInfoHead != NULL && dataObjInfoHead->rescInfo != NULL) {
00224 if (hostSearchStat->numHost >= MAX_HOST_TO_SEARCH ||
00225 hostSearchStat->totalCount >= MAX_HOST_TO_SEARCH) {
00226 freeAllDataObjInfo (dataObjInfoHead);
00227 return 0;
00228 }
00229 for (i = 0; i < hostSearchStat->numHost; i++) {
00230 if (dataObjInfoHead->rescInfo == hostSearchStat->rescInfo[i]) {
00231 hostSearchStat->count[i]++;
00232 hostSearchStat->totalCount++;
00233 freeAllDataObjInfo (dataObjInfoHead);
00234 return 0;
00235 }
00236 }
00237
00238 hostSearchStat->rescInfo[hostSearchStat->numHost] =
00239 dataObjInfoHead->rescInfo;
00240 hostSearchStat->count[hostSearchStat->numHost] = 1;
00241 hostSearchStat->numHost++;
00242 hostSearchStat->totalCount++;
00243 }
00244 freeAllDataObjInfo (dataObjInfoHead);
00245 return 0;
00246 }