Skip to content

Commit

Permalink
curvefs/sdk: log call stack to stdout for hadoop filesystem on debug …
Browse files Browse the repository at this point in the history
…mode.

Signed-off-by: Wine93 <[email protected]>
  • Loading branch information
Wine93 committed Nov 1, 2023
1 parent 8118cdb commit 33d3458
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.opencurve.curve.fs.common;

import java.text.SimpleDateFormat;
import java.util.Date;

public class StackLogger {
private String classname;
private int level;

private static final String CURVEFS_DEBUG_ENV_VAR = "CURVEFS_DEBUG";

public StackLogger(String classname, int level) {
this.classname = classname;
this.level = level;
}

private String now() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
return formatter.format(new Date());
}

public void log(String fn, Object... args) {
String value = System.getenv(CURVEFS_DEBUG_ENV_VAR);
if (!Boolean.valueOf(value)) {
return;
}

String[] params = new String[args.length];
for (int i = 0; i < args.length; i++) {
params[i] = args[i].toString();
}

String indent = " ".repeat(this.level * 4);
String param = String.join(",", params);
String message = String.format("+ [%s] %s%s.%s(%s)", now(), indent, this.classname, fn, param);
System.out.println(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import io.opencurve.curve.fs.libfs.CurveFSMount;
import io.opencurve.curve.fs.libfs.CurveFSStat;
import io.opencurve.curve.fs.libfs.CurveFSStatVFS;
import io.opencurve.curve.fs.common.StackLogger;

import java.util.UUID;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
Expand All @@ -37,9 +39,11 @@
class CurveFSTalker extends CurveFSProto {
private CurveFSMount mount;
private String fsname = null;
private String mountpoint = null;
private boolean inited = false;

private static final String PREFIX_KEY = "curvefs";
private static final StackLogger logger = new StackLogger("CurveFSTalker", 1);

CurveFSTalker(Configuration conf, Log log) {
mount = null;
Expand Down Expand Up @@ -67,36 +71,44 @@ private void loadCfg(Configuration conf) {

@Override
void initialize(URI uri, Configuration conf) throws IOException {
logger.log("initialize", uri);

mount = new CurveFSMount();
loadCfg(conf);
if (null == fsname || fsname.isEmpty()) {
throw new IOException("curvefs.name is not set");
}
mount.mount(fsname, "/");
mountpoint = UUID.randomUUID().toString();
mount.mount(fsname, mountpoint);
inited = true;
}

@Override
void shutdown() throws IOException {
logger.log("shutdown");

if (inited) {
mount.umount(fsname, "/");
mount.umount(fsname, mountpoint);
mount = null;
inited = false;
}
}

@Override
void mkdirs(Path path, int mode) throws IOException {
logger.log("mkdirs");
mount.mkdirs(tostr(path), mode);
}

@Override
void rmdir(Path path) throws IOException {
logger.log("rmdir");
mount.rmdir(tostr(path));
}

@Override
String[] listdir(Path path) throws IOException {
logger.log("listdir", path);
CurveFSStat stat = new CurveFSStat();
try {
mount.lstat(tostr(path), stat);
Expand All @@ -112,71 +124,85 @@ String[] listdir(Path path) throws IOException {

@Override
int open(Path path, int flags, int mode) throws IOException {
logger.log("open", path, flags, mode);
return mount.open(tostr(path), flags, mode);
}

@Override
long lseek(int fd, long offset, int whence) throws IOException {
logger.log("lseek", fd, offset, whence);
return mount.lseek(fd, offset, whence);
}

@Override
int write(int fd, byte[] buf, long size, long offset) throws IOException {
logger.log("lseek", fd, size, offset);
return mount.write(fd, buf, size, offset);
}

@Override
int read(int fd, byte[] buf, long size, long offset) throws IOException {
logger.log("lseek", fd, size, offset);
return mount.read(fd, buf, size, offset);
}

@Override
void fsync(int fd) throws IOException {
logger.log("fsync", fd);
mount.fsync(fd);
}

@Override
void close(int fd) throws IOException {
logger.log("close", fd);
mount.close(fd);
}

@Override
void unlink(Path path) throws IOException {
logger.log("unlink", path);
mount.unlink(tostr(path));
}

@Override
void statfs(Path path, CurveFSStatVFS stat) throws IOException {
logger.log("statfs", path);
mount.statfs(tostr(path), stat);
}

@Override
void lstat(Path path, CurveFSStat stat) throws IOException {
logger.log("lstat", path);
mount.lstat(tostr(path), stat);
}

@Override
void fstat(int fd, CurveFSStat stat) throws IOException {
logger.log("fstat", fd);
mount.fstat(fd, stat);
}

@Override
void setattr(Path path, CurveFSStat stat, int mask) throws IOException {
logger.log("setattr", path);
mount.setattr(tostr(path), stat, mask);
}

@Override
void chmod(Path path, int mode) throws IOException {
logger.log("chmod", path, mode);
mount.chmod(tostr(path), mode);
}

@Override
void chown(Path path, int uid, int gid) throws IOException {
logger.log("chown", path, uid, gid);
mount.chown(tostr(path), uid, gid);
}

@Override
void rename(Path src, Path dst) throws IOException {
logger.log("rename", src, dst);
mount.rename(tostr(src), tostr(dst));
}
}
Loading

0 comments on commit 33d3458

Please sign in to comment.