我正在使用 nfs-client 包实现 NFS 协议连接到 linux 服务器进行文件上传/下载。基本上目的是 NFS 协议测试。
这是我的示例代码库
import com.emc.ecs.nfsclient.nfs.io.Nfs3File;
import com.emc.ecs.nfsclient.nfs.io.NfsFileInputStream;
import com.emc.ecs.nfsclient.nfs.io.NfsFileOutputStream;
import com.emc.ecs.nfsclient.nfs.nfs3.Nfs3;
import com.emc.ecs.nfsclient.rpc.CredentialUnix;
int thread_count = ctx.getThreadNum();
String lrandom_name = service_name + "_" + replica_id + "_" + thread_count;
public static void uploadFileToNfs() {
String localDir = "F:\\look\\"+lrandom_name;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
//Create a local file object
File localFile = new File(localDir);
//Get the file name of the local file, this name is used to create a file with the same name in the specified directory on the remote Nfs server
String localFileName = localFile.getName();
Nfs3 nfs3 = new Nfs3(NFS_IP, NFS_DIR, new CredentialUnix(0, 0, null), 3);
//Create Nfs file object on remote server
Nfs3File NfsFile = new Nfs3File(nfs3, "/" + localFileName);
//Open a file input stream
inputStream = new BufferedInputStream(new FileInputStream(localFile));
//Open a remote Nfs file output stream and copy the file to the destination
outputStream = new BufferedOutputStream(new NfsFileOutputStream(NfsFile));
//Buffer memory
byte[] buffer = new byte[1024];
while ((inputStream.read(buffer)) != -1) {
outputStream.write(buffer);
}
System.out.println("File upload complete!");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
我正在从 Jmeter 运行脚本,但是经过 n 次迭代(线程方式)后,我遇到了错误。从 NFS 下载文件的类似脚本以线程方式并行工作。
com.emc.ecs.nfsclient.nfs.NfsException: rpc error, server: 192.168.0.101, RPC error: RPC call is ACCEPTED, but the status is not success, acceptStat=4
at com.emc.ecs.nfsclient.rpc.RpcWrapper.handleRpcException(RpcWrapper.java:311)
at com.emc.ecs.nfsclient.rpc.RpcWrapper.callRpcWrapped(RpcWrapper.java:159)
at com.emc.ecs.nfsclient.nfs.nfs3.Nfs3.wrapped_sendWrite(Nfs3.java:756)
at com.emc.ecs.nfsclient.nfs.nfs3.Nfs3.wrapped_sendWrite(Nfs3.java:90)
at com.emc.ecs.nfsclient.nfs.io.NfsFileBase.write(NfsFileBase.java:866)
at com.emc.ecs.nfsclient.nfs.io.NfsFileOutputStream.writeBufferToFile(NfsFileOutputStream.java:296)
at com.emc.ecs.nfsclient.nfs.io.NfsFileOutputStream.write(NfsFileOutputStream.java:262)
at java.base/java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:81)
at java.base/java.io.BufferedOutputStream.write(BufferedOutputStream.java:127)
at java.base/java.io.FilterOutputStream.write(FilterOutputStream.java:108)
at java_io_FilterOutputStream$write.call(Unknown Source)
at com.emc.ecs.nfsclient.nfs.io.Script1.run(Script1.groovy:77)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317)
at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:71)
at java.scripting/javax.script.CompiledScript.eval(CompiledScript.java:89)
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:217)
at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:72)
at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:635)
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:558)
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256)
at java.base/java.lang.Thread.run(Thread.java:834)
更新 基于反馈而不是所有线程(用户)的一个静态文件,现在我有每个线程(用户)的唯一文件来上传/下载
更新
在下面尝试了另一个用例,从一个 NFS 共享文件夹中随机选择文件到另一个,同样的问题来了。仅怀疑 NFS 配置有问题
package com.emc.ecs.nfsclient.nfs.io;
import org.junit.Test;
import com.emc.ecs.nfsclient.nfs.NfsSetAttributes;
import com.emc.ecs.nfsclient.nfs.io.Nfs3File;
import com.emc.ecs.nfsclient.nfs.io.NfsFileInputStream;
import com.emc.ecs.nfsclient.nfs.io.NfsFileOutputStream;
import com.emc.ecs.nfsclient.nfs.nfs3.Nfs3;
import com.emc.ecs.nfsclient.rpc.CredentialUnix;
import java.util.Random;
import java.io.IOException;
import org.junit.Test;
String nfs_folder = vars.get("nfs_folder");
String files_folder = vars.get("files_folder");
String NFS_IP = vars.get("server_ip");
String NFS_DIR = "/"+nfs_folder+"/"+files_folder;
String NFS_DIR_ROOT = "/"+nfs_folder;
int thread_count = ctx.getThreadNum();
String service_name = vars.get("Service_Name");
String replica_id = vars.get("Replica_ID");
String lrandom_name = service_name + "_" + replica_id + "_" + thread_count;
int transfer_size = Integer.parseInt(vars.get("transfer_size"))
InputStream inputStream = null;
OutputStream outputStream = null;
try {
Nfs3 nfs3 = new Nfs3(NFS_IP, NFS_DIR, new CredentialUnix(0, 0, null), 3);
Nfs3 nfs4 = new Nfs3(NFS_IP, NFS_DIR_ROOT, new CredentialUnix(0, 0, null), 3);
nfsFiles = new Nfs3File(nfs3,"/");
List<String> children = nfsFiles.list();
Random randomGenerator = new Random();
int fileIndex = randomGenerator.nextInt(children.size());
OUT.println(fileIndex)
String NfsFileDir = "/"+children.get(fileIndex);
OUT.println(children.size())
OUT.println(children.get(fileIndex))
//Create Nfs file object on remote server
Nfs3File nfsFile = new Nfs3File(nfs3, NfsFileDir);
Nfs3File NfsFile = new Nfs3File(nfs4, "/" + lrandom_name);
//String localFileName = localDir + lrandom_name;
//Create a local file object
//File localFile = new File(localFileName);
//Open a file input stream
inputStream = new BufferedInputStream(new NfsFileInputStream(nfsFile));
//Open a remote Nfs file output stream and copy the file to the destination
outputStream = new BufferedOutputStream(new NfsFileOutputStream(NfsFile));
//Buffer memory
byte[] buffer = new byte[transfer_size];
while (inputStream.read(buffer) != -1) {
outputStream.write(buffer);
}
System.out.println("File copy complete!");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}