Skip to content

Commit

Permalink
(feat) code format (sofastack#271)
Browse files Browse the repository at this point in the history
* (feat) code format for storage pkg

* (feat) atomically move file method (sofastack#272)
  • Loading branch information
fengjiachun authored and killme2008 committed Sep 18, 2019
1 parent 27dadb2 commit 588db25
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ private ProtoBufFile newPbFile() {

private boolean save() {
final long start = Utils.monotonicMs();
final StablePBMeta meta = StablePBMeta.newBuilder(). //
setTerm(this.term). //
setVotedfor(this.votedFor.toString()). //
build();
final StablePBMeta meta = StablePBMeta.newBuilder() //
.setTerm(this.term) //
.setVotedfor(this.votedFor.toString()) //
.build();
final ProtoBufFile pbFile = newPbFile();
try {
if (!pbFile.save(meta, this.raftOptions.isSyncMeta())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,10 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alipay.sofa.jraft.rpc.ProtobufMsgFactory;
import com.alipay.sofa.jraft.util.Bits;
import com.alipay.sofa.jraft.util.Utils;
import com.google.protobuf.Message;

/**
Expand All @@ -49,14 +43,12 @@
*/
public class ProtoBufFile {

private static final Logger LOG = LoggerFactory.getLogger(ProtoBufFile.class);

static {
ProtobufMsgFactory.load();
}

/** file path */
private final String path;
private final String path;

public ProtoBufFile(final String path) {
this.path = path;
Expand All @@ -72,19 +64,20 @@ public <T extends Message> T load() throws IOException {
return null;
}

byte[] lenBytes = new byte[4];
try (FileInputStream fin = new FileInputStream(file); BufferedInputStream input = new BufferedInputStream(fin)) {
final byte[] lenBytes = new byte[4];
try (final FileInputStream fin = new FileInputStream(file);
final BufferedInputStream input = new BufferedInputStream(fin)) {
readBytes(lenBytes, input);
int len = Bits.getInt(lenBytes, 0);
final int len = Bits.getInt(lenBytes, 0);
if (len <= 0) {
throw new IOException("Invalid message fullName.");
}
byte[] nameBytes = new byte[len];
final byte[] nameBytes = new byte[len];
readBytes(nameBytes, input);
String name = new String(nameBytes);
final String name = new String(nameBytes);
readBytes(lenBytes, input);
int msgLen = Bits.getInt(lenBytes, 0);
byte[] msgBytes = new byte[msgLen];
final int msgLen = Bits.getInt(lenBytes, 0);
final byte[] msgBytes = new byte[msgLen];
readBytes(msgBytes, input);
return ProtobufMsgFactory.newMessageByProtoClassName(name, msgBytes);
}
Expand All @@ -102,24 +95,23 @@ private void readBytes(final byte[] bs, final InputStream input) throws IOExcept
*
* @param msg protobuf message
* @param sync if sync flush data to disk
* @return true if save success
* @return true if save success
*/
@SuppressWarnings("ConstantConditions")
public boolean save(final Message msg, final boolean sync) throws IOException {
// Write message into temp file
File file = new File(this.path + ".tmp");
try (FileOutputStream fOut = new FileOutputStream(file);
BufferedOutputStream output = new BufferedOutputStream(fOut)) {
byte[] lenBytes = new byte[4];
final File file = new File(this.path + ".tmp");
try (final FileOutputStream fOut = new FileOutputStream(file);
final BufferedOutputStream output = new BufferedOutputStream(fOut)) {
final byte[] lenBytes = new byte[4];

// name len + name
String fullName = msg.getDescriptorForType().getFullName();
int nameLen = fullName.length();
final String fullName = msg.getDescriptorForType().getFullName();
final int nameLen = fullName.length();
Bits.putInt(lenBytes, 0, nameLen);
output.write(lenBytes);
output.write(fullName.getBytes());
// msg len + msg
int msgLen = msg.getSerializedSize();
final int msgLen = msg.getSerializedSize();
Bits.putInt(lenBytes, 0, msgLen);
output.write(lenBytes);
msg.writeTo(output);
Expand All @@ -128,41 +120,6 @@ public boolean save(final Message msg, final boolean sync) throws IOException {
}
}

// Move temp file to target path atomically.
// The code comes from https://github.com/jenkinsci/jenkins/blob/master/core/src/main/java/hudson/util/AtomicFileWriter.java#L187
Path tmpPath = file.toPath();
File destFile = new File(this.path);
Path destPath = destFile.toPath();
try {
return Files.move(tmpPath, destPath, StandardCopyOption.ATOMIC_MOVE) != null;
} catch (final IOException e) {
// If it falls here that can mean many things. Either that the atomic move is not supported,
// or something wrong happened. Anyway, let's try to be over-diagnosing
if (e instanceof AtomicMoveNotSupportedException) {
LOG.warn("Atomic move not supported. falling back to non-atomic move, error: {}.", e.getMessage());
} else {
LOG.warn("Unable to move atomically, falling back to non-atomic move, error: {}.", e.getMessage());
}

if (destFile.exists()) {
LOG.info("The target file {} was already existing.", destPath);
}

try {
return Files.move(tmpPath, destPath, StandardCopyOption.REPLACE_EXISTING) != null;
} catch (final IOException e1) {
e1.addSuppressed(e);
LOG.warn("Unable to move {} to {}. Attempting to delete {} and abandoning.", tmpPath, destPath, tmpPath);
try {
Files.deleteIfExists(tmpPath);
} catch (IOException e2) {
e2.addSuppressed(e1);
LOG.warn("Unable to delete {}, good bye then!", tmpPath);
throw e2;
}

throw e1;
}
}
return Utils.atomicMoveFile(file, new File(this.path));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public boolean init(final SnapshotExecutorOptions opts) {
}
this.loadingSnapshotMeta = reader.load();
if (this.loadingSnapshotMeta == null) {
LOG.error("Fail to load meta from {}", opts.getUri());
LOG.error("Fail to load meta from {}.", opts.getUri());
Utils.closeQuietly(reader);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Set<String> listFiles() {
}

@Override
public Message getFileMeta(String fileName) {
public Message getFileMeta(final String fileName) {
return this.metaTable.getFileMeta(fileName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public void cancel() {
return;
}
if (isOk()) {
this.setError(RaftError.ECANCELED, "Cancel the copier manually.");
setError(RaftError.ECANCELED, "Cancel the copier manually.");
}
this.cancelled = true;
if (this.curSession != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ public LocalSnapshotMetaTable(RaftOptions raftOptions) {
* Save metadata infos into byte buffer.
*/
public ByteBuffer saveToByteBufferAsRemote() {
LocalSnapshotPbMeta.Builder pbMetaBuilder = LocalSnapshotPbMeta.newBuilder();
if (this.hasMeta()) {
final LocalSnapshotPbMeta.Builder pbMetaBuilder = LocalSnapshotPbMeta.newBuilder();
if (hasMeta()) {
pbMetaBuilder.setMeta(this.meta);
}
for (Map.Entry<String, LocalFileMeta> entry : this.fileMap.entrySet()) {
File.Builder fb = File.newBuilder();
fb.setName(entry.getKey());
fb.setMeta(entry.getValue());
for (final Map.Entry<String, LocalFileMeta> entry : this.fileMap.entrySet()) {
final File.Builder fb = File.newBuilder() //
.setName(entry.getKey()) //
.setMeta(entry.getValue());
pbMetaBuilder.addFiles(fb.build());
}
return ByteBuffer.wrap(pbMetaBuilder.build().toByteArray());
Expand All @@ -75,19 +75,19 @@ public ByteBuffer saveToByteBufferAsRemote() {
/**
* Load metadata infos from byte buffer.
*/
public boolean loadFromIoBufferAsRemote(ByteBuffer buf) {
public boolean loadFromIoBufferAsRemote(final ByteBuffer buf) {
if (buf == null) {
LOG.error("Null buf to load.");
return false;
}
try {
LocalSnapshotPbMeta pbMeta = LocalSnapshotPbMeta.parseFrom(ZeroByteStringHelper.wrap(buf));
final LocalSnapshotPbMeta pbMeta = LocalSnapshotPbMeta.parseFrom(ZeroByteStringHelper.wrap(buf));
if (pbMeta == null) {
LOG.error("Fail to load meta from buffer");
LOG.error("Fail to load meta from buffer.");
return false;
}
return loadFromPbMeta(pbMeta);
} catch (InvalidProtocolBufferException e) {
} catch (final InvalidProtocolBufferException e) {
LOG.error("Fail to parse LocalSnapshotPbMeta from byte buffer", e);
return false;
}
Expand All @@ -96,14 +96,14 @@ public boolean loadFromIoBufferAsRemote(ByteBuffer buf) {
/**
* Adds a file metadata.
*/
public boolean addFile(String fileName, LocalFileMeta meta) {
public boolean addFile(final String fileName, final LocalFileMeta meta) {
return this.fileMap.putIfAbsent(fileName, meta) == null;
}

/**
* Removes a file metadata.
*/
public boolean removeFile(String fileName) {
public boolean removeFile(final String fileName) {
return this.fileMap.remove(fileName) != null;
}

Expand Down Expand Up @@ -165,21 +165,21 @@ public boolean loadFromFile(String path) throws IOException {
ProtoBufFile pbFile = new ProtoBufFile(path);
LocalSnapshotPbMeta pbMeta = pbFile.load();
if (pbMeta == null) {
LOG.error("Fail to load meta from {}", path);
LOG.error("Fail to load meta from {}.", path);
return false;
}
return loadFromPbMeta(pbMeta);
}

private boolean loadFromPbMeta(LocalSnapshotPbMeta pbMeta) {
private boolean loadFromPbMeta(final LocalSnapshotPbMeta pbMeta) {
if (pbMeta.hasMeta()) {
this.meta = pbMeta.getMeta();
} else {
this.meta = null;
}
this.fileMap.clear();
for (File f : pbMeta.getFilesList()) {
fileMap.put(f.getName(), f.getMeta());
for (final File f : pbMeta.getFilesList()) {
this.fileMap.put(f.getName(), f.getMeta());
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,25 @@ long getReaderId() {
}

@Override
public boolean init(Void v) {
final File dir = new File(path);
public boolean init(final Void v) {
final File dir = new File(this.path);
if (!dir.exists()) {
LOG.error("No such path %s for snapshot reader", this.path);
setError(RaftError.ENOENT, "No such path %s for snapshot reader", path);
LOG.error("No such path %s for snapshot reader.", this.path);
setError(RaftError.ENOENT, "No such path %s for snapshot reader", this.path);
return false;
}
final String metaPath = this.path + File.separator + JRAFT_SNAPSHOT_META_FILE;
try {
return this.metaTable.loadFromFile(metaPath);
} catch (final IOException e) {
LOG.error("Fail to load meta {}", metaPath);
setError(RaftError.EIO, "Fail to load snapshot meta from path:" + metaPath);
setError(RaftError.EIO, "Fail to load metatable from path %s", path);
LOG.error("Fail to load snapshot meta {}.", metaPath);
setError(RaftError.EIO, "Fail to load snapshot meta from path %s", metaPath);
return false;
}
}

private long getSnapshotIndex() {
final File file = new File(path);
final File file = new File(this.path);
final String name = file.getName();
if (!name.startsWith(JRAFT_SNAPSHOT_PREFIX)) {
throw new IllegalStateException("Invalid snapshot path name:" + name);
Expand All @@ -123,21 +122,21 @@ public String generateURIForCopy() {
LOG.error("Address is not specified");
return null;
}
if (readerId == 0) {
final SnapshotFileReader reader = new SnapshotFileReader(path, this.snapshotThrottle);
if (this.readerId == 0) {
final SnapshotFileReader reader = new SnapshotFileReader(this.path, this.snapshotThrottle);
reader.setMetaTable(this.metaTable);
if (!reader.open()) {
LOG.error("Open snapshot {} failed", this.path);
LOG.error("Open snapshot {} failed.", this.path);
return null;
}
this.readerId = FileService.getInstance().addReader(reader);
if (this.readerId < 0) {
LOG.error("Fail to add reader to file_service");
LOG.error("Fail to add reader to file_service.");
return null;
}
}

return String.format(REMOTE_SNAPSHOT_URI_SCHEME + "%s/%d", this.addr.toString(), readerId);
return String.format(REMOTE_SNAPSHOT_URI_SCHEME + "%s/%d", this.addr.toString(), this.readerId);
}

private void destroyReaderInFileService() {
Expand All @@ -162,7 +161,7 @@ public Set<String> listFiles() {
}

@Override
public Message getFileMeta(String fileName) {
public Message getFileMeta(final String fileName) {
return this.metaTable.getFileMeta(fileName);
}
}
Loading

0 comments on commit 588db25

Please sign in to comment.