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