Skip to content

Commit 4ce9248

Browse files
committed
Use lock_guard instead of mutex lock
Signed-off-by: M1eyu2018 <857037797@qq.com>
1 parent 57e07b8 commit 4ce9248

File tree

1 file changed

+36
-50
lines changed

1 file changed

+36
-50
lines changed

src/client/InputStreamImpl.cpp

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,6 @@ int32_t InputStreamImpl::readOneBlock(char * buf, int32_t size, bool shouldUpdat
715715
*/
716716
int32_t InputStreamImpl::readInternal(char * buf, int32_t size) {
717717
int updateMetadataOnFailure = conf->getMaxReadBlockRetry();
718-
bool isInfoMutexLock = false;
719718

720719
try {
721720
do {
@@ -731,20 +730,16 @@ int32_t InputStreamImpl::readInternal(char * buf, int32_t size) {
731730
* Do RPC failover work in updateBlockInfos.
732731
*/
733732
updateBlockInfos();
734-
if (isInfoMutexLock) {
735-
isInfoMutexLock = false;
736-
infoMutex.unlock();
737-
}
733+
}
738734

739-
/*
740-
* We already have the up-to-date block information,
741-
* Check if we reach the end of file.
742-
*/
743-
if (cursor >= getFileLength()) {
744-
THROW_NO_STACK(HdfsEndOfStream,
745-
"InputStreamImpl: read over EOF, current position: %" PRId64 ", read size: %d, from file: %s",
746-
cursor, size, path.c_str());
747-
}
735+
/*
736+
* We already have the up-to-date block information,
737+
* Check if we reach the end of file.
738+
*/
739+
if (cursor >= getFileLength()) {
740+
THROW_NO_STACK(HdfsEndOfStream,
741+
"InputStreamImpl: read over EOF, current position: %" PRId64 ", read size: %d, from file: %s",
742+
cursor, size, path.c_str());
748743
}
749744

750745
/*
@@ -774,9 +769,15 @@ int32_t InputStreamImpl::readInternal(char * buf, int32_t size) {
774769
* We will update metadata once and try again.
775770
*/
776771
if (retval < 0) {
777-
infoMutex.lock();
778-
isInfoMutexLock = true;
779-
lbs.reset();
772+
{
773+
lock_guard<std::recursive_mutex> lock(infoMutex);
774+
lbs.reset();
775+
/*
776+
* update block infos right now after lbs is reset
777+
* to ensure pread can read non-empty lbs.
778+
*/
779+
updateBlockInfos();
780+
}
780781
endOfCurBlock = 0;
781782
--updateMetadataOnFailure;
782783

@@ -788,26 +789,13 @@ int32_t InputStreamImpl::readInternal(char * buf, int32_t size) {
788789
continue;
789790
}
790791

791-
if (isInfoMutexLock) {
792-
isInfoMutexLock = false;
793-
infoMutex.unlock();
794-
}
795792
return retval;
796793
} while (true);
797794
} catch (const HdfsCanceled & e) {
798-
if (isInfoMutexLock) {
799-
infoMutex.unlock();
800-
}
801795
throw;
802796
} catch (const HdfsEndOfStream & e) {
803-
if (isInfoMutexLock) {
804-
infoMutex.unlock();
805-
}
806797
throw;
807798
} catch (const HdfsException & e) {
808-
if (isInfoMutexLock) {
809-
infoMutex.unlock();
810-
}
811799
/*
812800
* wrap the underlying error and rethrow.
813801
*/
@@ -827,25 +815,22 @@ int32_t InputStreamImpl::readInternal(char * buf, int32_t size) {
827815
int32_t InputStreamImpl::preadInternal(char * buf, int32_t size, int64_t position) {
828816
int64_t cursor = position;
829817
try {
830-
infoMutex.lock();
831-
if (!lbs) {
832-
THROW(HdfsIOException, "InputStreamImpl: lbs is empty, cannot pread file: %s, from position %" PRId64 ", size: %d.",
833-
path.c_str(), cursor, size);
834-
}
835-
int64_t filelen = getFileLength();
836-
if ((position < 0) || (position >= filelen)) {
837-
infoMutex.unlock();
838-
return -1;
839-
}
840818
int32_t realLen = size;
841-
if ((position + size) > filelen) {
842-
realLen = (int32_t)(filelen - position);
843-
}
819+
std::vector<shared_ptr<LocatedBlock>> blockRange;
820+
{
821+
lock_guard<std::recursive_mutex> lock(infoMutex);
822+
int64_t filelen = getFileLength();
823+
if ((position < 0) || (position >= filelen)) {
824+
return -1;
825+
}
826+
if ((position + size) > filelen) {
827+
realLen = (int32_t) (filelen - position);
828+
}
844829

845-
// determine the block and byte range within the block
846-
// corresponding to position and realLen
847-
std::vector<shared_ptr<LocatedBlock>> blockRange = getBlockRange(position, (int64_t)realLen);
848-
infoMutex.unlock();
830+
// determine the block and byte range within the block
831+
// corresponding to position and realLen
832+
blockRange = getBlockRange(position, (int64_t) realLen);
833+
}
849834
int32_t remaining = realLen;
850835
int32_t bytesHasRead = 0;
851836
for (shared_ptr<LocatedBlock> blk : blockRange) {
@@ -1213,9 +1198,10 @@ void InputStreamImpl::close() {
12131198
prefetchSize = 0;
12141199
blockReader.reset();
12151200
curBlock.reset();
1216-
infoMutex.lock();
1217-
lbs.reset();
1218-
infoMutex.unlock();
1201+
{
1202+
lock_guard<std::recursive_mutex> lock(infoMutex);
1203+
lbs.reset();
1204+
}
12191205
conf.reset();
12201206
failedNodes.clear();
12211207
path.clear();

0 commit comments

Comments
 (0)