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
00024
00025 #include "eirods_resource_backport.h"
00026 #include "eirods_resource_redirect.h"
00027
00028 int rsGetHostForGet(
00029 rsComm_t* rsComm,
00030 dataObjInp_t* dataObjInp,
00031 char** outHost ) {
00032
00033
00034
00035
00036 std::string hier;
00037 if( getValByKey( &dataObjInp->condInput, RESC_HIER_STR_KW ) == NULL ) {
00038 eirods::error ret = eirods::resolve_resource_hierarchy( eirods::EIRODS_OPEN_OPERATION, rsComm,
00039 dataObjInp, hier );
00040 if( !ret.ok() ) {
00041 std::stringstream msg;
00042 msg << __FUNCTION__;
00043 msg << " :: failed in eirods::resolve_resource_hierarchy for [";
00044 msg << dataObjInp->objPath << "]";
00045 eirods::log( PASSMSG( msg.str(), ret ) );
00046 return ret.code();
00047 }
00048
00049
00050
00051 addKeyVal( &dataObjInp->condInput, RESC_HIER_STR_KW, hier.c_str() );
00052
00053 }
00054
00055
00056
00057 std::string location;
00058 eirods::error ret = eirods::get_loc_for_hier_string( hier, location );
00059 if( !ret.ok() ) {
00060 eirods::log( PASSMSG( "rsGetHostForGet - failed in get_loc_for_hier_String", ret ) );
00061 return -1;
00062 }
00063
00064
00065
00066 *outHost = strdup( location.c_str() );
00067 return 0;
00068
00069
00070
00071
00072 #if 0
00073 int status;
00074 rescInfo_t *myRescInfo;
00075 char *myResc;
00076 rodsServerHost_t *rodsServerHost;
00077 rodsHostAddr_t addr;
00078 specCollCache_t *specCollCache = NULL;
00079 char *myHost;
00080 int remoteFlag = 0;
00081
00082 *outHost = NULL;
00083 #if 0 // JMC - backport 4746
00084 if (isLocalZone (dataObjInp->objPath) == 0) {
00085
00086 *outHost = strdup (THIS_ADDRESS);
00087 return 0;
00088 }
00089 #endif // JMC - backport 4746
00090
00091 resolveLinkedPath (rsComm, dataObjInp->objPath, &specCollCache, NULL);
00092 if (isLocalZone (dataObjInp->objPath) == 0) {
00093 #if 0 // JMC - backport 4746
00094
00095 *outHost = strdup (THIS_ADDRESS);
00096 return 0;
00097 #else // JMC - backport 4746
00098 resolveLinkedPath (rsComm, dataObjInp->objPath, &specCollCache,
00099 &dataObjInp->condInput);
00100 remoteFlag = getAndConnRcatHost (rsComm, SLAVE_RCAT,
00101 dataObjInp->objPath, &rodsServerHost);
00102 if (remoteFlag < 0) {
00103 return (remoteFlag);
00104 } else if (remoteFlag == LOCAL_HOST) {
00105 *outHost = strdup (THIS_ADDRESS);
00106 return 0;
00107 } else {
00108 status = rcGetHostForGet (rodsServerHost->conn, dataObjInp,
00109 outHost);
00110 if (status >= 0 && *outHost != NULL &&
00111 strcmp (*outHost, THIS_ADDRESS) == 0) {
00112 free (*outHost);
00113 *outHost = strdup (rodsServerHost->hostName->name);
00114 }
00115 return (status);
00116 }
00117 #endif // JMC - backport 4746
00118 }
00119
00120 eirods::resource_ptr resc;
00121 status = getSpecCollCache (rsComm, dataObjInp->objPath, 0, &specCollCache);
00122 if (status >= 0 && NULL != specCollCache ) {
00123 if (specCollCache->specColl.collClass == MOUNTED_COLL) {
00124
00125
00126
00127
00128
00129
00130
00131 eirods::error err = eirods::get_resc_info( specCollCache->specColl.resource, *myRescInfo );
00132 if( !err.ok() ) {
00133 std::stringstream msg;
00134 msg << "rsGetHostForGet - failed to get resc info for [";
00135 msg << specCollCache->specColl.resource;
00136 msg << "]";
00137 eirods::log( PASS( false, -1, msg.str(), err ) );
00138 }
00139
00140
00141 } else {
00142 *outHost = strdup (THIS_ADDRESS);
00143 return 0;
00144 }
00145 } else if( ( myResc = getValByKey( &dataObjInp->condInput, RESC_NAME_KW ) ) != NULL &&
00146 eirods::get_resc_info( myResc, *myRescInfo ).ok() ) {
00147
00148 } else {
00149
00150 status = getBestRescForGet (rsComm, dataObjInp, &myRescInfo);
00151 if (myRescInfo == NULL) {
00152 *outHost = strdup (THIS_ADDRESS);
00153 return status;
00154 }
00155 }
00156
00157
00158 #if 0 // JMC - legacy resource
00159 if (getRescClass (myRescInfo) == COMPOUND_CL) {
00160 *outHost = strdup (THIS_ADDRESS);
00161 return 0;
00162 }
00163 #endif // JMC - legacy resource
00164
00165 bzero (&addr, sizeof (addr));
00166
00167 status = resolveHost (&addr, &rodsServerHost);
00168 if (status < 0) return status;
00169 if (rodsServerHost->localFlag == LOCAL_HOST) {
00170 *outHost = strdup (THIS_ADDRESS);
00171 return 0;
00172 }
00173
00174 myHost = getSvrAddr (rodsServerHost);
00175 if (myHost != NULL) {
00176 *outHost = strdup (myHost);
00177 return 0;
00178 } else {
00179 *outHost = NULL;
00180 return SYS_INVALID_SERVER_HOST;
00181 }
00182 #endif
00183 }
00184
00185 int
00186 getBestRescForGet (rsComm_t *rsComm, dataObjInp_t *dataObjInp,
00187 rescInfo_t **outRescInfo)
00188 {
00189 collInp_t collInp;
00190 hostSearchStat_t hostSearchStat;
00191 int status, i;
00192 int maxInx = -1;
00193
00194 *outRescInfo = NULL;
00195
00196 if (dataObjInp == NULL || outRescInfo == NULL) return USER__NULL_INPUT_ERR;
00197 bzero (&hostSearchStat, sizeof (hostSearchStat));
00198 bzero (&collInp, sizeof (collInp));
00199 rstrcpy (collInp.collName, dataObjInp->objPath, MAX_NAME_LEN);
00200
00201 status = getRescForGetInColl (rsComm, &collInp, &hostSearchStat);
00202 if (status < 0) {
00203
00204 status = getRescForGetInDataObj (rsComm, dataObjInp, &hostSearchStat);
00205 }
00206
00207 for (i = 0; i < hostSearchStat.numHost; i++) {
00208 if (maxInx < 0 ||
00209 hostSearchStat.count[i] > hostSearchStat.count[maxInx]) {
00210 maxInx = i;
00211 }
00212 }
00213 if (maxInx >= 0) *outRescInfo = hostSearchStat.rescInfo[maxInx];
00214
00215 return status;
00216 }
00217
00218 int
00219 getRescForGetInColl (rsComm_t *rsComm, collInp_t *collInp,
00220 hostSearchStat_t *hostSearchStat)
00221 {
00222 collEnt_t *collEnt;
00223 int handleInx;
00224 int status;
00225
00226 if (collInp == NULL || hostSearchStat == NULL) return USER__NULL_INPUT_ERR;
00227
00228 handleInx = rsOpenCollection (rsComm, collInp);
00229 if (handleInx < 0) return handleInx;
00230
00231 while ((status = rsReadCollection (rsComm, &handleInx, &collEnt)) >= 0) {
00232 if (collEnt->objType == DATA_OBJ_T) {
00233 dataObjInp_t dataObjInp;
00234 bzero (&dataObjInp, sizeof (dataObjInp));
00235 snprintf (dataObjInp.objPath, MAX_NAME_LEN, "%s/%s",
00236 collEnt->collName, collEnt->dataName);
00237 status = getRescForGetInDataObj (rsComm, &dataObjInp,
00238 hostSearchStat);
00239 if (status < 0) {
00240 rodsLog (LOG_NOTICE,
00241 "getRescForGetInColl: getRescForGetInDataObj %s err, stat=%d",
00242 dataObjInp.objPath, status);
00243 }
00244 } else if (collEnt->objType == COLL_OBJ_T) {
00245 collInp_t myCollInp;
00246 bzero (&myCollInp, sizeof (myCollInp));
00247 rstrcpy (myCollInp.collName, collEnt->collName, MAX_NAME_LEN);
00248 status = getRescForGetInColl (rsComm, &myCollInp, hostSearchStat);
00249 if (status < 0) {
00250 rodsLog (LOG_NOTICE,
00251 "getRescForGetInColl: getRescForGetInColl %s err, stat=%d",
00252 collEnt->collName, status);
00253 }
00254 }
00255 free (collEnt);
00256 if (hostSearchStat->totalCount >= MAX_HOST_TO_SEARCH) {
00257
00258 rsCloseCollection (rsComm, &handleInx);
00259 return 0;
00260 }
00261 }
00262 rsCloseCollection (rsComm, &handleInx);
00263 return 0;
00264 }
00265
00266 int
00267 getRescForGetInDataObj (rsComm_t *rsComm, dataObjInp_t *dataObjInp,
00268 hostSearchStat_t *hostSearchStat)
00269 {
00270 int status, i;
00271 dataObjInfo_t *dataObjInfoHead = NULL;
00272
00273 if (dataObjInp == NULL || hostSearchStat == NULL)
00274 return USER__NULL_INPUT_ERR;
00275
00276 status = getDataObjInfoIncSpecColl (rsComm, dataObjInp, &dataObjInfoHead);
00277 if (status < 0) return status;
00278
00279 sortObjInfoForOpen (rsComm, &dataObjInfoHead, &dataObjInp->condInput, 0);
00280 if (dataObjInfoHead != NULL && dataObjInfoHead->rescInfo != NULL) {
00281 if (hostSearchStat->numHost >= MAX_HOST_TO_SEARCH ||
00282 hostSearchStat->totalCount >= MAX_HOST_TO_SEARCH) {
00283 freeAllDataObjInfo (dataObjInfoHead);
00284 return 0;
00285 }
00286 for (i = 0; i < hostSearchStat->numHost; i++) {
00287 if (dataObjInfoHead->rescInfo == hostSearchStat->rescInfo[i]) {
00288 hostSearchStat->count[i]++;
00289 hostSearchStat->totalCount++;
00290 freeAllDataObjInfo (dataObjInfoHead);
00291 return 0;
00292 }
00293 }
00294
00295 hostSearchStat->rescInfo[hostSearchStat->numHost] =
00296 dataObjInfoHead->rescInfo;
00297 hostSearchStat->count[hostSearchStat->numHost] = 1;
00298 hostSearchStat->numHost++;
00299 hostSearchStat->totalCount++;
00300 }
00301 freeAllDataObjInfo (dataObjInfoHead);
00302 return 0;
00303 }