From 736966f38b9f16fccae7bc84d0ec3ba85d1c70e1 Mon Sep 17 00:00:00 2001 From: Noah Watkins Date: Wed, 16 Jan 2013 12:27:16 -0800 Subject: [PATCH] java: support get pool id/replication interface Signed-off-by: Noah Watkins --- src/java/Makefile.am | 3 +- src/java/java/com/ceph/fs/CephMount.java | 38 ++++++++++++ .../java/com/ceph/fs/CephPoolException.java | 42 +++++++++++++ src/java/native/libcephfs_jni.cc | 60 +++++++++++++++++++ src/java/test/com/ceph/fs/CephMountTest.java | 33 ++++++++++ .../test/com/ceph/fs/CephUnmountedTest.java | 10 ++++ 6 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 src/java/java/com/ceph/fs/CephPoolException.java diff --git a/src/java/Makefile.am b/src/java/Makefile.am index 788bea2c29b..b8ab5f4b3f5 100644 --- a/src/java/Makefile.am +++ b/src/java/Makefile.am @@ -8,7 +8,8 @@ JAVA_SRC = \ java/com/ceph/fs/CephNotMountedException.java \ java/com/ceph/fs/CephFileAlreadyExistsException.java \ java/com/ceph/fs/CephAlreadyMountedException.java \ - java/com/ceph/fs/CephNotDirectoryException.java + java/com/ceph/fs/CephNotDirectoryException.java \ + java/com/ceph/fs/CephPoolException.java JAVA_TEST_SRC = \ test/com/ceph/fs/CephDoubleMountTest.java \ diff --git a/src/java/java/com/ceph/fs/CephMount.java b/src/java/java/com/ceph/fs/CephMount.java index 11338585be8..10036f6c768 100644 --- a/src/java/java/com/ceph/fs/CephMount.java +++ b/src/java/java/com/ceph/fs/CephMount.java @@ -899,4 +899,42 @@ public class CephMount { } private static native int native_ceph_get_stripe_unit_granularity(long mountp); + + /** + * Get the pool id for the named pool. + * + * @param name The pool name. + * @return The pool id. + */ + public int get_pool_id(String name) throws CephPoolException { + rlock.lock(); + try { + return native_ceph_get_pool_id(instance_ptr, name); + } catch (FileNotFoundException e) { + throw new CephPoolException("pool name " + name + " not found"); + } finally { + rlock.unlock(); + } + } + + private static native int native_ceph_get_pool_id(long mountp, String name) throws FileNotFoundException; + + /** + * Get the pool replication factor. + * + * @param pool_id The pool id. + * @return Number of replicas stored in the pool. + */ + public int get_pool_replication(int pool_id) throws CephPoolException { + rlock.lock(); + try { + return native_ceph_get_pool_replication(instance_ptr, pool_id); + } catch (FileNotFoundException e) { + throw new CephPoolException("pool id " + pool_id + " not found"); + } finally { + rlock.unlock(); + } + } + + private static native int native_ceph_get_pool_replication(long mountp, int pool_id) throws FileNotFoundException; } diff --git a/src/java/java/com/ceph/fs/CephPoolException.java b/src/java/java/com/ceph/fs/CephPoolException.java new file mode 100644 index 00000000000..1bedd449996 --- /dev/null +++ b/src/java/java/com/ceph/fs/CephPoolException.java @@ -0,0 +1,42 @@ +/* + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ +package com.ceph.fs; + +import java.io.IOException; + +/** + * Exception related to Ceph pool. + */ +public class CephPoolException extends IOException { + + /** + * Construct CephPoolException. + */ + public CephPoolException() { + super(); + } + + /** + * Construct CephPoolException with message. + */ + public CephPoolException(String s) { + super(s); + } +} diff --git a/src/java/native/libcephfs_jni.cc b/src/java/native/libcephfs_jni.cc index 142cb2a5290..2fef171a976 100644 --- a/src/java/native/libcephfs_jni.cc +++ b/src/java/native/libcephfs_jni.cc @@ -2434,3 +2434,63 @@ JNIEXPORT jint JNICALL Java_com_ceph_fs_CephMount_native_1ceph_1get_1stripe_1uni return ret; } + +/* + * Class: com_ceph_fs_CephMount + * Method: native_ceph_get_pool_id + * Signature: (JLjava/lang/String;)I + */ +JNIEXPORT jint JNICALL Java_com_ceph_fs_CephMount_native_1ceph_1get_1pool_1id + (JNIEnv *env, jclass clz, jlong j_mntp, jstring jname) +{ + struct ceph_mount_info *cmount = get_ceph_mount(j_mntp); + CephContext *cct = ceph_get_mount_context(cmount); + const char *c_name; + int ret; + + CHECK_MOUNTED(cmount, -1); + CHECK_ARG_NULL(jname, "@name is null", -1); + + c_name = env->GetStringUTFChars(jname, NULL); + if (!c_name) { + cephThrowInternal(env, "failed to pin memory"); + return -1; + } + + ldout(cct, 10) << "jni: get_pool_id: name " << c_name << dendl; + + ret = ceph_get_pool_id(cmount, c_name); + if (ret < 0) + handle_error(env, ret); + + ldout(cct, 10) << "jni: get_pool_id: ret " << ret << dendl; + + env->ReleaseStringUTFChars(jname, c_name); + + return ret; +} + +/* + * Class: com_ceph_fs_CephMount + * Method: native_ceph_get_pool_replication + * Signature: (JI)I + */ +JNIEXPORT jint JNICALL Java_com_ceph_fs_CephMount_native_1ceph_1get_1pool_1replication + (JNIEnv *env, jclass clz, jlong j_mntp, jint jpoolid) +{ + struct ceph_mount_info *cmount = get_ceph_mount(j_mntp); + CephContext *cct = ceph_get_mount_context(cmount); + int ret; + + CHECK_MOUNTED(cmount, -1); + + ldout(cct, 10) << "jni: get_pool_replication: poolid " << jpoolid << dendl; + + ret = ceph_get_pool_replication(cmount, jpoolid); + if (ret < 0) + handle_error(env, ret); + + ldout(cct, 10) << "jni: get_pool_replication: ret " << ret << dendl; + + return ret; +} diff --git a/src/java/test/com/ceph/fs/CephMountTest.java b/src/java/test/com/ceph/fs/CephMountTest.java index 0ed74ec4b98..984c2cb7377 100644 --- a/src/java/test/com/ceph/fs/CephMountTest.java +++ b/src/java/test/com/ceph/fs/CephMountTest.java @@ -834,4 +834,37 @@ public class CephMountTest { public void test_get_stripe_unit_gran() throws Exception { assertTrue(mount.get_stripe_unit_granularity() > 0); } + + /* + * pool info. below we use "data" and "metadata" pool names which we assume + * to exist (they are the default pools created for file data / metadata in + * CephFS). + */ + + @Test + public void test_get_pool_id() throws Exception { + /* returns valid pool ids */ + assertTrue(mount.get_pool_id("data") >= 0); + assertTrue(mount.get_pool_id("metadata") >= 0); + + /* test non-existent pool name */ + try { + mount.get_pool_id("asdlfkjlsejflkjef"); + assertTrue(false); + } catch (CephPoolException e) {} + } + + @Test + public void test_get_pool_replication() throws Exception { + /* test invalid pool id */ + try { + mount.get_pool_replication(-1); + assertTrue(false); + } catch (CephPoolException e) {} + + /* test valid pool id */ + int poolid = mount.get_pool_id("data"); + assertTrue(poolid >= 0); + assertTrue(mount.get_pool_replication(poolid) > 0); + } } diff --git a/src/java/test/com/ceph/fs/CephUnmountedTest.java b/src/java/test/com/ceph/fs/CephUnmountedTest.java index 7169bbf6f6e..ae4d41e1e98 100644 --- a/src/java/test/com/ceph/fs/CephUnmountedTest.java +++ b/src/java/test/com/ceph/fs/CephUnmountedTest.java @@ -135,4 +135,14 @@ public class CephUnmountedTest { public void test_get_stripe_unit_gran() throws Exception { mount.get_stripe_unit_granularity(); } + + @Test(expected=CephNotMountedException.class) + public void test_get_pool_id() throws Exception { + mount.get_pool_id("data"); + } + + @Test(expected=CephNotMountedException.class) + public void test_get_pool_replication() throws Exception { + mount.get_pool_replication(1); + } }