void CommunicationCache::insert(const UUID& uuid, const OSegEntry& sID) { BoundingBoxList bboxes = mCSeg->serverRegion(sID.server()); BoundingBox3f bbox = bboxes[0]; for(uint32 i = 1; i< bboxes.size(); i++) bbox.mergeIn(bboxes[i]); float xer = mCentralX - ((bbox.max().x + bbox.min().x)/2); float yer = mCentralY - ((bbox.max().y + bbox.min().y)/2); float zer = mCentralZ - ((bbox.max().z + bbox.min().z)/2); float distance = sqrt(xer*xer + yer*yer + zer*zer); float logger = log(mDistScaledUnits*distance); float lookupWeight = sID.radius()/(mDistScaledUnits* distance*distance*logger*logger); boost::lock_guard<boost::mutex> lck(mMutex); mCompleteCache.insert(uuid,sID.server(),0,0,0,0,sID.radius(),lookupWeight,1); }
CommunicationCache::CommunicationCache(SpaceContext* spctx, float scalingUnits, CoordinateSegmentation* cseg,uint32 cacheSize) : mCompleteCache(.2,"CommunicationCache",&commCacheScoreFunction,&commCacheScoreFunctionPrint,spctx,cacheSize,FLT_MAX), mDistScaledUnits(scalingUnits), mCSeg(cseg), ctx(spctx), mCacheSize(cacheSize) { BoundingBoxList bboxes = mCSeg->serverRegion(ctx->id()); BoundingBox3f bbox = bboxes[0]; for(uint32 i = 1; i< bboxes.size(); i++) bbox.mergeIn(bboxes[i]); mCentralX = (bbox.max().x + bbox.min().x)/2; mCentralY = (bbox.max().y + bbox.min().y)/2; mCentralZ = (bbox.max().z + bbox.min().z)/2; }
bool LoadMonitor::isAdjacent(BoundingBox3f& box1, BoundingBox3f& box2) { if (box1.min().x == box2.max().x || box1.max().x == box2.min().x) { //if box1.miny or box1.maxy lies between box2.maxy and box2.miny, return true; //if box2.miny or box2.maxy lies between box1.maxy and box1.miny, return true; if (box2.min().y <= box1.min().y && box1.min().y <= box2.max().y) return true; if (box2.min().y <= box1.max().y && box1.max().y <= box2.max().y) return true; if (box1.min().y <= box2.min().y && box2.min().y <= box1.max().y) return true; if (box1.min().y <= box2.max().y && box2.max().y <= box1.max().y) return true; } if (box1.min().y == box2.max().y || box1.max().y == box2.min().y) { //if box1.minx or box1.maxx lies between box2.maxx and box2.minx, return true; //if box2.minx or box2.maxx lies between box1.maxx and box1.minx, return true; if (box2.min().x <= box1.min().x && box1.min().x <= box2.max().x) return true; if (box2.min().x <= box1.max().x && box1.max().x <= box2.max().x) return true; if (box1.min().x <= box2.min().x && box2.min().x <= box1.max().x) return true; if (box1.min().x <= box2.max().x && box2.max().x <= box1.max().x) return true; } return false; }
void LoadBalancer::service() { boost::mutex::scoped_lock overloadedRegionsListLock(mOverloadedRegionsListMutex); boost::mutex::scoped_lock underloadedRegionsListLock(mUnderloadedRegionsListMutex); //splitting overloaded regions for (std::vector<SegmentedRegion*>::iterator it = mOverloadedRegionsList.begin(); it != mOverloadedRegionsList.end(); it++) { uint32 availableSvrIndex = getAvailableServerIndex(); if (availableSvrIndex != INT_MAX) { ServerID availableServer = mAvailableServers[availableSvrIndex].mServer; mAvailableServers[availableSvrIndex].mAvailable = false; SegmentedRegion* overloadedRegion = *it; overloadedRegion->mLeftChild = new SegmentedRegion(overloadedRegion); overloadedRegion->mRightChild = new SegmentedRegion(overloadedRegion); BoundingBox3f region = overloadedRegion->mBoundingBox; float minX = region.min().x; float minY = region.min().y; float maxX = region.max().x; float maxY = region.max().y; float minZ = region.min().z; float maxZ = region.max().z; assert(overloadedRegion->mParent == NULL || overloadedRegion->mSplitAxis != SegmentedRegion::UNDEFINED); if (overloadedRegion->mSplitAxis == SegmentedRegion::Y) { overloadedRegion->mLeftChild->mBoundingBox = BoundingBox3f( region.min(), Vector3f( (minX+maxX)/2.0, maxY, maxZ) ); overloadedRegion->mRightChild->mBoundingBox = BoundingBox3f( Vector3f( (minX+maxX)/2.0,minY,minZ), region.max() ); overloadedRegion->mLeftChild->mSplitAxis = overloadedRegion->mRightChild->mSplitAxis = SegmentedRegion::X; } else { overloadedRegion->mLeftChild->mBoundingBox = BoundingBox3f( region.min(), Vector3f( maxX, (minY+maxY)/2.0, maxZ) ); overloadedRegion->mRightChild->mBoundingBox = BoundingBox3f( Vector3f( minX,(minY+maxY)/2.0,minZ), region.max() ); overloadedRegion->mLeftChild->mSplitAxis = overloadedRegion->mRightChild->mSplitAxis = SegmentedRegion::Y; } overloadedRegion->mLeftChild->mServer = overloadedRegion->mServer; overloadedRegion->mRightChild->mServer = availableServer; std::cout << "Split\n"; std::cout << overloadedRegion->mServer << " : " << overloadedRegion->mLeftChild->mBoundingBox << "\n"; std::cout << availableServer << " : " << overloadedRegion->mRightChild->mBoundingBox << "\n"; mCSeg->mWholeTreeServerRegionMap.erase(overloadedRegion->mServer); mCSeg->mWholeTreeServerRegionMap.erase(availableServer); mCSeg->mLowerTreeServerRegionMap.erase(overloadedRegion->mServer); mCSeg->mLowerTreeServerRegionMap.erase(availableServer); std::vector<SegmentationInfo> segInfoVector; SegmentationInfo segInfo, segInfo2; segInfo.server = overloadedRegion->mServer; segInfo.region = mCSeg->serverRegion(overloadedRegion->mServer); segInfoVector.push_back( segInfo ); segInfo2.server = availableServer; segInfo2.region = mCSeg->serverRegion(availableServer); segInfoVector.push_back(segInfo2); Thread thrd(boost::bind(&DistributedCoordinateSegmentation::notifySpaceServersOfChange,mCSeg,segInfoVector)); mOverloadedRegionsList.erase(it); return; //enough work for this iteration. No further splitting or merging. } else { //No idle servers are available at this time... break; } } //merging underloaded regions for (std::vector<SegmentedRegion*>::iterator it = mUnderloadedRegionsList.begin(); it != mUnderloadedRegionsList.end(); it++) { SegmentedRegion* underloadedRegion = *it; if (underloadedRegion->mParent == NULL) { mUnderloadedRegionsList.erase(it); break; } bool isRightChild = (underloadedRegion->mParent->mRightChild == underloadedRegion); SegmentedRegion* sibling = NULL; if (isRightChild) { sibling = underloadedRegion->mParent->mLeftChild; } else { sibling = underloadedRegion->mParent->mRightChild; } std::vector<SegmentedRegion*>::iterator sibling_it = std::find(mUnderloadedRegionsList.begin(), mUnderloadedRegionsList.end(), sibling); if (sibling_it == mUnderloadedRegionsList.end()) { mUnderloadedRegionsList.erase(it); break; } SegmentedRegion* parent = underloadedRegion->mParent; parent->mServer = parent->mLeftChild->mServer; for (uint32 i=0; i<mAvailableServers.size(); i++) { if (mAvailableServers[i].mServer == parent->mRightChild->mServer) { mAvailableServers[i].mAvailable = true; break; } } mCSeg->mWholeTreeServerRegionMap.erase(parent->mRightChild->mServer); mCSeg->mLowerTreeServerRegionMap.erase(parent->mRightChild->mServer); mCSeg->mWholeTreeServerRegionMap.erase(parent->mLeftChild->mServer); mCSeg->mLowerTreeServerRegionMap.erase(parent->mLeftChild->mServer); std::vector<SegmentationInfo> segInfoVector; SegmentationInfo segInfo, segInfo2; segInfo.server = parent->mRightChild->mServer; segInfo.region = mCSeg->serverRegion(parent->mRightChild->mServer); segInfoVector.push_back( segInfo ); segInfo2.server = parent->mLeftChild->mServer; segInfo2.region = mCSeg->serverRegion(parent->mLeftChild->mServer); segInfoVector.push_back(segInfo2); Thread thrd(boost::bind(&DistributedCoordinateSegmentation::notifySpaceServersOfChange,mCSeg,segInfoVector)); mUnderloadedRegionsList.erase(it); sibling_it = std::find(mUnderloadedRegionsList.begin(), mUnderloadedRegionsList.end(), sibling); mUnderloadedRegionsList.erase(sibling_it); std::cout << "Merged " << parent->mLeftChild->mServer << " : " << parent->mRightChild->mServer << "!\n"; delete parent->mLeftChild; delete parent->mRightChild; parent->mLeftChild = NULL; parent->mRightChild = NULL; break; } }