From 4739f2c15f40715693399605251eb0b7f757c99f Mon Sep 17 00:00:00 2001 From: Greg Farnum Date: Mon, 19 Oct 2009 13:54:45 -0700 Subject: [PATCH] Hadoop: Add the CephFaker source. --- src/client/hadoop/ceph/CephFaker.java | 385 ++++++++++++++++++++++++++ 1 file changed, 385 insertions(+) create mode 100644 src/client/hadoop/ceph/CephFaker.java diff --git a/src/client/hadoop/ceph/CephFaker.java b/src/client/hadoop/ceph/CephFaker.java new file mode 100644 index 0000000000000..b17045e8b8658 --- /dev/null +++ b/src/client/hadoop/ceph/CephFaker.java @@ -0,0 +1,385 @@ +// -*- mode:Java; tab-width:2; c-basic-offset:2; indent-tabs-mode:t -*- +/** + * + * Licensed under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. See the License for the specific language governing + * permissions and limitations under the License. + * + * + * This uses the local Filesystem but pretends to be communicating + * with a Ceph deployment, for unit testing the CephFileSystem. + */ + +package org.apache.hadoop.fs.ceph; + +import java.util.Hashtable; +import java.io.FileNotFoundException; +import java.io.IOException; + +import org.apache.commons.logging.Log; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.BlockLocation; +import org.apache.hadoop.fs.FileStatus; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.FsStatus; +import org.apache.hadoop.fs.FSDataInputStream; +import org.apache.hadoop.fs.FSDataOutputStream; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.fs.permission.FsPermission; + +class CephFaker extends CephFS { + + FileSystem localFS; + int blockSize; + Configuration conf; + Hashtable files; + Hashtable filenames; + int fileCount = 0; + + public CephFaker(Configuration con, Log log) { + super(con, log); + conf = con; + files = new Hashtable(); + filenames = new Hashtable(); + } + + protected boolean ceph_initializeClient(String args, int block_size) { + //let's remember the default block_size + blockSize = block_size; + /* for a real Ceph deployment, this starts up the client, + * sets debugging levels, etc. We just need to get the + * local FileSystem to use, and we'll ignore any + * command-line arguments. */ + try { + localFS = FileSystem.getLocal(conf); + localFS.setWorkingDirectory(new Path(conf.get("test.build.data", "/tmp"))); + } + catch (IOException e) { + return false; + } + return true; + } + + protected String ceph_getcwd() { + return localFS.getWorkingDirectory().toString(); + } + + protected boolean ceph_setcwd(String path) { + localFS.setWorkingDirectory(new Path(path)); + return true; + } + + //the caller is responsible for ensuring empty dirs + protected boolean ceph_rmdir(String pth) { + Path path = new Path(pth); + boolean ret = false; + try { + if (localFS.listStatus(path).length <= 1) { + ret = localFS.delete(path, true); + } + } + catch (IOException e){ } + return ret; + } + + //this needs to work on (empty) directories too + protected boolean ceph_unlink(String path) { + boolean ret = false; + if (ceph_isdirectory(path)) { + ret = ceph_rmdir(path); + } + else { + try { + ret = localFS.delete(new Path(path), false); + } + catch (IOException e){ } + } + return ret; + } + + protected boolean ceph_rename(String oldName, String newName) { + boolean ret = false; + try { + ret = localFS.rename(new Path(oldName), new Path(newName)); + } + catch (IOException e) { } + return ret; + } + + protected boolean ceph_exists(String path) { + boolean ret = false; + try { + ret = localFS.exists(new Path(path)); + } + catch (IOException e){ } + return ret; + } + + protected long ceph_getblocksize(String path) { + try { + FileStatus status = localFS.getFileStatus(new Path(path)); + return status.getBlockSize(); + } + catch (FileNotFoundException e) { + return -CephFS.ENOENT; + } + catch (IOException e) { + return -1; //just fail generically + } + } + + protected boolean ceph_isdirectory(String path) { + boolean ret = false; + try { + FileStatus status = localFS.getFileStatus(new Path(path)); + ret = status.isDir(); + } + catch (IOException e) {} + return ret; + } + + protected boolean ceph_isfile(String path) { + boolean ret = false; + try { + FileStatus status = localFS.getFileStatus(new Path(path)); + ret = !status.isDir(); + } + catch (Exception e) {} + return ret; + } + + protected String[] ceph_getdir(String path) { + if (!ceph_isdirectory(path)) { + return null; + } + try { + FileStatus[] stats = localFS.listStatus(new Path(path)); + String[] names = new String[stats.length]; + for (int i=0; i