Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use synchronized to protect fs stream in broker #171

Merged
merged 1 commit into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions be/src/exec/broker_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ Status BrokerReader::read(uint8_t* buf, size_t* buf_len, bool* eof) {
client->pread(response, request);
} catch (apache::thrift::transport::TTransportException& e) {
RETURN_IF_ERROR(client.reopen());
LOG(INFO) << "retry reading from broker: " << broker_addr << ". reason: " << e.what();
client->pread(response, request);
}
} catch (apache::thrift::TException& e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@

package com.baidu.palo.broker.hdfs;

import com.baidu.palo.thrift.TBrokerFD;
import com.baidu.palo.thrift.TBrokerFileStatus;
import com.baidu.palo.thrift.TBrokerOperationStatusCode;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -28,18 +40,6 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.log4j.Logger;

import com.baidu.palo.thrift.TBrokerFD;
import com.baidu.palo.thrift.TBrokerFileStatus;
import com.baidu.palo.thrift.TBrokerOperationStatusCode;

public class FileSystemManager {

private static Logger logger = Logger
Expand Down Expand Up @@ -230,43 +230,46 @@ public TBrokerFD openReader(String clientId, String path, long startOffset, Map<

public ByteBuffer pread(TBrokerFD fd, long offset, long length) {
FSDataInputStream fsDataInputStream = clientContextManager.getFsDataInputStream(fd);
long currentStreamOffset;
try {
currentStreamOffset = fsDataInputStream.getPos();
} catch (IOException e) {
logger.error("errors while get file pos from output stream", e);
throw new BrokerException(TBrokerOperationStatusCode.TARGET_STORAGE_SERVICE_ERROR,
"errors while get file pos from output stream");
}
if (currentStreamOffset != offset) {
logger.warn("invalid offset, current read offset is "
+ currentStreamOffset + " is not equal to request offset "
+ offset + " seek to it");
synchronized (fsDataInputStream) {

long currentStreamOffset;
try {
fsDataInputStream.seek(offset);
currentStreamOffset = fsDataInputStream.getPos();
} catch (IOException e) {
throw new BrokerException(TBrokerOperationStatusCode.INVALID_INPUT_OFFSET,
e, "current read offset {} is not equal to {}, and could not seek to it",
currentStreamOffset, offset);
logger.error("errors while get file pos from output stream", e);
throw new BrokerException(TBrokerOperationStatusCode.TARGET_STORAGE_SERVICE_ERROR,
"errors while get file pos from output stream");
}
}
byte[] buf;
if (length > readBufferSize) {
buf = new byte[readBufferSize];
} else {
buf = new byte[(int) length];
}
try {
int readLength = fsDataInputStream.read(buf);
if (readLength < 0) {
throw new BrokerException(TBrokerOperationStatusCode.END_OF_FILE,
"end of file reached");
if (currentStreamOffset != offset) {
logger.warn("invalid offset, current read offset is "
+ currentStreamOffset + " is not equal to request offset "
+ offset + " seek to it");
try {
fsDataInputStream.seek(offset);
} catch (IOException e) {
throw new BrokerException(TBrokerOperationStatusCode.INVALID_INPUT_OFFSET,
e, "current read offset {} is not equal to {}, and could not seek to it",
currentStreamOffset, offset);
}
}
byte[] buf;
if (length > readBufferSize) {
buf = new byte[readBufferSize];
} else {
buf = new byte[(int) length];
}
try {
int readLength = fsDataInputStream.read(buf);
if (readLength < 0) {
throw new BrokerException(TBrokerOperationStatusCode.END_OF_FILE,
"end of file reached");
}
return ByteBuffer.wrap(buf, 0, readLength);
} catch (IOException e) {
logger.error("errors while read data from stream", e);
throw new BrokerException(TBrokerOperationStatusCode.TARGET_STORAGE_SERVICE_ERROR,
e, "errors while write data to output stream");
}
return ByteBuffer.wrap(buf, 0, readLength);
} catch (IOException e) {
logger.error("errors while read data from stream", e);
throw new BrokerException(TBrokerOperationStatusCode.TARGET_STORAGE_SERVICE_ERROR,
e, "errors while write data to output stream");
}
}

Expand All @@ -277,12 +280,14 @@ public void seek(TBrokerFD fd, long offset) {

public void closeReader(TBrokerFD fd) {
FSDataInputStream fsDataInputStream = clientContextManager.getFsDataInputStream(fd);
try {
fsDataInputStream.close();
} catch (IOException e) {
logger.error("errors while close file input stream", e);
} finally {
clientContextManager.removeInputStream(fd);
synchronized (fsDataInputStream) {
try {
fsDataInputStream.close();
} catch (IOException e) {
logger.error("errors while close file input stream", e);
} finally {
clientContextManager.removeInputStream(fd);
}
}
}

Expand All @@ -307,36 +312,40 @@ public TBrokerFD openWriter(String clientId, String path, Map<String, String> pr

public void pwrite(TBrokerFD fd, long offset, byte[] data) {
FSDataOutputStream fsDataOutputStream = clientContextManager.getFsDataOutputStream(fd);
long currentStreamOffset;
try {
currentStreamOffset = fsDataOutputStream.getPos();
} catch (IOException e) {
logger.error("errors while get file pos from output stream", e);
throw new BrokerException(TBrokerOperationStatusCode.TARGET_STORAGE_SERVICE_ERROR,
"errors while get file pos from output stream");
}
if (currentStreamOffset != offset) {
throw new BrokerException(TBrokerOperationStatusCode.INVALID_INPUT_OFFSET,
"current outputstream offset is {} not equal to request {}",
currentStreamOffset, offset);
}
try {
fsDataOutputStream.write(data);
} catch (IOException e) {
logger.error("errors while write data to output stream", e);
throw new BrokerException(TBrokerOperationStatusCode.TARGET_STORAGE_SERVICE_ERROR,
e, "errors while write data to output stream");
synchronized (fsDataOutputStream) {
long currentStreamOffset;
try {
currentStreamOffset = fsDataOutputStream.getPos();
} catch (IOException e) {
logger.error("errors while get file pos from output stream", e);
throw new BrokerException(TBrokerOperationStatusCode.TARGET_STORAGE_SERVICE_ERROR,
"errors while get file pos from output stream");
}
if (currentStreamOffset != offset) {
throw new BrokerException(TBrokerOperationStatusCode.INVALID_INPUT_OFFSET,
"current outputstream offset is {} not equal to request {}",
currentStreamOffset, offset);
}
try {
fsDataOutputStream.write(data);
} catch (IOException e) {
logger.error("errors while write data to output stream", e);
throw new BrokerException(TBrokerOperationStatusCode.TARGET_STORAGE_SERVICE_ERROR,
e, "errors while write data to output stream");
}
}
}

public void closeWriter(TBrokerFD fd) {
FSDataOutputStream fsDataOutputStream = clientContextManager.getFsDataOutputStream(fd);
try {
fsDataOutputStream.close();
} catch (IOException e) {
logger.error("errors while close file output stream", e);
} finally {
clientContextManager.removeOutputStream(fd);
synchronized (fsDataOutputStream) {
try {
fsDataOutputStream.close();
} catch (IOException e) {
logger.error("errors while close file output stream", e);
} finally {
clientContextManager.removeOutputStream(fd);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,43 +235,45 @@ public TBrokerFD openReader(String clientId, String path, long startOffset, Map<

public ByteBuffer pread(TBrokerFD fd, long offset, long length) {
FSDataInputStream fsDataInputStream = clientContextManager.getFsDataInputStream(fd);
long currentStreamOffset;
try {
currentStreamOffset = fsDataInputStream.getPos();
} catch (IOException e) {
logger.error("errors while get file pos from output stream", e);
throw new BrokerException(TBrokerOperationStatusCode.TARGET_STORAGE_SERVICE_ERROR,
"errors while get file pos from output stream");
}
if (currentStreamOffset != offset) {
logger.warn("invalid offset, current read offset is "
+ currentStreamOffset + " is not equal to request offset "
+ offset + " seek to it");
synchronized (fsDataInputStream) {
long currentStreamOffset;
try {
fsDataInputStream.seek(offset);
currentStreamOffset = fsDataInputStream.getPos();
} catch (IOException e) {
throw new BrokerException(TBrokerOperationStatusCode.INVALID_INPUT_OFFSET,
e, "current read offset {} is not equal to {}, and could not seek to it",
currentStreamOffset, offset);
logger.error("errors while get file pos from output stream", e);
throw new BrokerException(TBrokerOperationStatusCode.TARGET_STORAGE_SERVICE_ERROR,
"errors while get file pos from output stream");
}
}
byte[] buf;
if (length > readBufferSize) {
buf = new byte[readBufferSize];
} else {
buf = new byte[(int) length];
}
try {
int readLength = fsDataInputStream.read(buf);
if (readLength < 0) {
throw new BrokerException(TBrokerOperationStatusCode.END_OF_FILE,
"end of file reached");
if (currentStreamOffset != offset) {
logger.warn("invalid offset, current read offset is "
+ currentStreamOffset + " is not equal to request offset "
+ offset + " seek to it");
try {
fsDataInputStream.seek(offset);
} catch (IOException e) {
throw new BrokerException(TBrokerOperationStatusCode.INVALID_INPUT_OFFSET,
e, "current read offset {} is not equal to {}, and could not seek to it",
currentStreamOffset, offset);
}
}
byte[] buf;
if (length > readBufferSize) {
buf = new byte[readBufferSize];
} else {
buf = new byte[(int) length];
}
try {
int readLength = fsDataInputStream.read(buf);
if (readLength < 0) {
throw new BrokerException(TBrokerOperationStatusCode.END_OF_FILE,
"end of file reached");
}
return ByteBuffer.wrap(buf, 0, readLength);
} catch (IOException e) {
logger.error("errors while read data from stream", e);
throw new BrokerException(TBrokerOperationStatusCode.TARGET_STORAGE_SERVICE_ERROR,
e, "errors while write data to output stream");
}
return ByteBuffer.wrap(buf, 0, readLength);
} catch (IOException e) {
logger.error("errors while read data from stream", e);
throw new BrokerException(TBrokerOperationStatusCode.TARGET_STORAGE_SERVICE_ERROR,
e, "errors while write data to output stream");
}
}

Expand All @@ -282,12 +284,14 @@ public void seek(TBrokerFD fd, long offset) {

public void closeReader(TBrokerFD fd) {
FSDataInputStream fsDataInputStream = clientContextManager.getFsDataInputStream(fd);
try {
fsDataInputStream.close();
} catch (IOException e) {
logger.error("errors while close file input stream", e);
} finally {
clientContextManager.removeInputStream(fd);
synchronized (fsDataInputStream) {
try {
fsDataInputStream.close();
} catch (IOException e) {
logger.error("errors while close file input stream", e);
} finally {
clientContextManager.removeInputStream(fd);
}
}
}

Expand All @@ -312,36 +316,40 @@ public TBrokerFD openWriter(String clientId, String path, Map<String, String> pr

public void pwrite(TBrokerFD fd, long offset, byte[] data) {
FSDataOutputStream fsDataOutputStream = clientContextManager.getFsDataOutputStream(fd);
long currentStreamOffset;
try {
currentStreamOffset = fsDataOutputStream.getPos();
} catch (IOException e) {
logger.error("errors while get file pos from output stream", e);
throw new BrokerException(TBrokerOperationStatusCode.TARGET_STORAGE_SERVICE_ERROR,
"errors while get file pos from output stream");
}
if (currentStreamOffset != offset) {
throw new BrokerException(TBrokerOperationStatusCode.INVALID_INPUT_OFFSET,
"current outputstream offset is {} not equal to request {}",
currentStreamOffset, offset);
}
try {
fsDataOutputStream.write(data);
} catch (IOException e) {
logger.error("errors while write data to output stream", e);
throw new BrokerException(TBrokerOperationStatusCode.TARGET_STORAGE_SERVICE_ERROR,
e, "errors while write data to output stream");
synchronized (fsDataOutputStream) {
long currentStreamOffset;
try {
currentStreamOffset = fsDataOutputStream.getPos();
} catch (IOException e) {
logger.error("errors while get file pos from output stream", e);
throw new BrokerException(TBrokerOperationStatusCode.TARGET_STORAGE_SERVICE_ERROR,
"errors while get file pos from output stream");
}
if (currentStreamOffset != offset) {
throw new BrokerException(TBrokerOperationStatusCode.INVALID_INPUT_OFFSET,
"current outputstream offset is {} not equal to request {}",
currentStreamOffset, offset);
}
try {
fsDataOutputStream.write(data);
} catch (IOException e) {
logger.error("errors while write data to output stream", e);
throw new BrokerException(TBrokerOperationStatusCode.TARGET_STORAGE_SERVICE_ERROR,
e, "errors while write data to output stream");
}
}
}

public void closeWriter(TBrokerFD fd) {
FSDataOutputStream fsDataOutputStream = clientContextManager.getFsDataOutputStream(fd);
try {
fsDataOutputStream.close();
} catch (IOException e) {
logger.error("errors while close file output stream", e);
} finally {
clientContextManager.removeOutputStream(fd);
synchronized (fsDataOutputStream) {
try {
fsDataOutputStream.close();
} catch (IOException e) {
logger.error("errors while close file output stream", e);
} finally {
clientContextManager.removeOutputStream(fd);
}
}
}

Expand Down