From 87ba3b980c4c865b504faa416fb0792b58ca9e48 Mon Sep 17 00:00:00 2001 From: salah Date: Fri, 18 May 2018 13:40:55 +0800 Subject: [PATCH 01/11] Java client support for plasma -------------------------- This commit includes the support of java client for plasma, which is part of the java worker support of Ray. In addition to some minor changes in build system, it consists of the following modules: - java/plasma: java client support for plasma - cpp/src/plasma/lib/java: JNI support for plasma client --- cpp/src/plasma/CMakeLists.txt | 24 ++ ...org_apache_arrow_plasma_PlasmaClientJNI.cc | 322 ++++++++++++++++++ .../org_apache_arrow_plasma_PlasmaClientJNI.h | 101 ++++++ java/plasma/pom.xml | 33 ++ .../org/apache/arrow/plasma/ObjectBuffer.java | 55 +++ .../org/apache/arrow/plasma/ObjectId.java | 24 ++ .../apache/arrow/plasma/ObjectStoreLink.java | 103 ++++++ .../org/apache/arrow/plasma/PlasmaClient.java | 162 +++++++++ .../apache/arrow/plasma/PlasmaClientJNI.java | 51 +++ java/pom.xml | 1 + 10 files changed, 876 insertions(+) create mode 100644 cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc create mode 100644 cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.h create mode 100644 java/plasma/pom.xml create mode 100644 java/plasma/src/main/java/org/apache/arrow/plasma/ObjectBuffer.java create mode 100644 java/plasma/src/main/java/org/apache/arrow/plasma/ObjectId.java create mode 100644 java/plasma/src/main/java/org/apache/arrow/plasma/ObjectStoreLink.java create mode 100644 java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClient.java create mode 100644 java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClientJNI.java diff --git a/cpp/src/plasma/CMakeLists.txt b/cpp/src/plasma/CMakeLists.txt index ed425f6b81a1..f59ea3c61525 100644 --- a/cpp/src/plasma/CMakeLists.txt +++ b/cpp/src/plasma/CMakeLists.txt @@ -160,6 +160,30 @@ install( FILES "${CMAKE_CURRENT_BINARY_DIR}/plasma.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig/") +# Plasma java client support +add_compile_options("-I$ENV{JAVA_HOME}/include/") +if(WIN32) + add_compile_options("-I$ENV{JAVA_HOME}/include/win32") +elseif(APPLE) + add_compile_options("-I$ENV{JAVA_HOME}/include/darwin") +else() # linux + add_compile_options("-I$ENV{JAVA_HOME}/include/linux") +endif() + +include_directories("${CMAKE_CURRENT_LIST_DIR}/lib/java") + +file(GLOB PLASMA_LIBRARY_EXT_java_SRC + lib/java/*.cc lib/*.cc) +add_library(plasma_java SHARED + ${PLASMA_LIBRARY_EXT_java_SRC} + ) + +if(APPLE) + target_link_libraries(plasma_java plasma_static ${PLASMA_LINK_LIBS} "-undefined dynamic_lookup" -Wl,-force_load,${FLATBUFFERS_STATIC_LIB} ${FLATBUFFERS_STATIC_LIB} ${PTHREAD_LIBRARY}) +else(APPLE) + target_link_libraries(plasma_java plasma_static ${PLASMA_LINK_LIBS} -Wl,--whole-archive ${FLATBUFFERS_STATIC_LIB} -Wl,--no-whole-archive ${FLATBUFFERS_STATIC_LIB} ${PTHREAD_LIBRARY}) +endif(APPLE) + ####################################### # Unit tests ####################################### diff --git a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc new file mode 100644 index 000000000000..a036c9cf911f --- /dev/null +++ b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc @@ -0,0 +1,322 @@ +#include +#include +#include +#include +#include +#include +#include "plasma/client.h" +#include "org_apache_arrow_plasma_PlasmaClientJNI.h" + +using namespace std; +using namespace plasma; +using namespace arrow; + +const jsize LEN_OF_OBJECTID = sizeof(ObjectID) / sizeof(jbyte); + +inline void jbyteArray_to_object_id(JNIEnv *env, jbyteArray a, ObjectID *oid) +{ + env->GetByteArrayRegion(a, 0, LEN_OF_OBJECTID, (jbyte *) oid); +} + +inline void object_id_to_jbyteArray(JNIEnv *env, jbyteArray a, ObjectID *oid) +{ + env->SetByteArrayRegion(a, 0, LEN_OF_OBJECTID, (jbyte *) oid); +} + +class JByteArrayGetter { +private: + JNIEnv *_env; + jbyteArray _a; + jbyte *bp; + +public: + + JByteArrayGetter(JNIEnv *env, jbyteArray a, jbyte **out) { + _env = env; + _a = a; + + bp = _env->GetByteArrayElements(_a, NULL); + *out = bp; + } + + ~JByteArrayGetter() { + _env->ReleaseByteArrayElements(_a, bp, 0); + } +}; + +JNIEXPORT jlong JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_connect + (JNIEnv *env, jclass cls, jstring config, jstring overwrites, jstring store_socket_name, jstring manager_socket_name, jint release_delay) +{ + const char *s_name = env->GetStringUTFChars(store_socket_name, NULL); + const char *m_name = env->GetStringUTFChars(manager_socket_name, NULL); + + PlasmaClient *client = new PlasmaClient(); + ARROW_CHECK_OK( + client->Connect(s_name, m_name, release_delay)); + + //fprintf (stdout, "JNI plasma client init, fd = %d\n", client->get_store_fd()); + //fflush (stdout); + + env->ReleaseStringUTFChars(store_socket_name, s_name); + env->ReleaseStringUTFChars(manager_socket_name, m_name); + return (long) client; +} + +JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_disconnect + (JNIEnv *env, jclass cls, jlong conn) +{ + PlasmaClient *client = (PlasmaClient *) conn; + + //fprintf (stdout, "JNI plasma client disconnect, fd = %d\n", client->get_store_fd()); + //fflush (stdout); + + ARROW_CHECK_OK(client->Disconnect()); + delete client; + return; +} + +JNIEXPORT jobject JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_create + (JNIEnv *env, jclass cls, jlong conn, jbyteArray object_id, jint size, jbyteArray metadata) +{ + PlasmaClient *client = (PlasmaClient *) conn; + ObjectID oid; + jbyteArray_to_object_id(env, object_id, &oid); + + // prepare metadata buffer + uint8_t *md = NULL; + jsize md_size = 0; + std::unique_ptr md_getter; + if (metadata != NULL) { + md_size = env->GetArrayLength(metadata); + } + if (md_size > 0) { + md_getter.reset(new JByteArrayGetter(env, metadata, (jbyte **)&md)); + } + + std::shared_ptr data; + Status s = client->Create(oid, size, md, md_size, &data); + if (s.IsPlasmaObjectExists()) { + jclass Exception = env->FindClass("org/ray/spi/impl/PlasmaObjectExistsException"); + env->ThrowNew(Exception, "An object with this ID already exists in the plasma store."); + return NULL; + } + if (s.IsPlasmaStoreFull()) { + jclass Exception = env->FindClass("org/ray/spi/impl/PlasmaOutOfMemoryException"); + env->ThrowNew(Exception, "The plasma store ran out of memory and could not create this object."); + return NULL; + } + ARROW_CHECK(s.ok()); + + return env->NewDirectByteBuffer(data->mutable_data(), size); +} + +JNIEXPORT jbyteArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_hash + (JNIEnv *env, jclass cls, jlong conn, jbyteArray object_id) +{ + PlasmaClient *client = (PlasmaClient *) conn; + ObjectID oid; + jbyteArray_to_object_id(env, object_id, &oid); + + unsigned char digest[kDigestSize]; + bool success = client->Hash(oid, digest).ok(); + + if (success) { + jbyteArray ret = env->NewByteArray(kDigestSize); + env->SetByteArrayRegion(ret, 0, kDigestSize, (jbyte *) digest); + return ret; + } + else { + return NULL; + } +} + +JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_seal + (JNIEnv *env, jclass cls, jlong conn, jbyteArray object_id) +{ + PlasmaClient *client = (PlasmaClient *) conn; + ObjectID oid; + jbyteArray_to_object_id(env, object_id, &oid); + + ARROW_CHECK_OK(client->Seal(oid)); +} + +JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_release + (JNIEnv *env, jclass cls, jlong conn, jbyteArray object_id) +{ + PlasmaClient *client = (PlasmaClient *) conn; + ObjectID oid; + jbyteArray_to_object_id(env, object_id, &oid); + + //fprintf (stdout, "JNI plasma client release, fd = %d\n", client->get_store_fd()); + //fflush (stdout); + + ARROW_CHECK_OK(client->Release(oid)); +} + +JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_get + (JNIEnv *env, jclass cls, jlong conn, jobjectArray object_ids, jint timeout_ms) +{ + PlasmaClient *client = (PlasmaClient *) conn; + //fprintf (stdout, "JNI plasma client get, fd = %d\n", client->get_store_fd()); + //fflush (stdout); + + jsize num_oids = env->GetArrayLength(object_ids); + std::vector oids(num_oids); + std::vector obufs(num_oids); + for (int i = 0; i < num_oids; ++i) { + jbyteArray_to_object_id(env, + (jbyteArray) env->GetObjectArrayElement(object_ids, i), &oids[i]); + } + // TODO: may be blocked. consider to add the thread support + ARROW_CHECK_OK( + client->Get(oids.data(), num_oids, timeout_ms, obufs.data())); + + jclass clsByteBuffer = env->FindClass("java/nio/ByteBuffer"); + jclass clsByteBufferArray = env->FindClass("[Ljava/nio/ByteBuffer;"); + + jobjectArray ret = env->NewObjectArray(num_oids, clsByteBufferArray, NULL); + jobjectArray o = NULL; + jobject dataBuf, metadataBuf; + for (int i = 0; i < num_oids; ++i) { + o = env->NewObjectArray(2, clsByteBuffer, NULL); + if (obufs[i].data && obufs[i].data->size() != -1 ) { + dataBuf = env->NewDirectByteBuffer(const_cast(obufs[i].data->data()), obufs[i].data->size()); + if (obufs[i].metadata && obufs[i].metadata->size() > 0) { + metadataBuf = env->NewDirectByteBuffer(const_cast(obufs[i].metadata->data()), obufs[i].metadata->size()); + } + else { + metadataBuf = NULL; + } + } + else { + dataBuf = NULL; + metadataBuf = NULL; + } + + env->SetObjectArrayElement(o, 0, dataBuf); + env->SetObjectArrayElement(o, 1, metadataBuf); + env->SetObjectArrayElement(ret, i, o); + } + return ret; +} + +JNIEXPORT jboolean JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_contains + (JNIEnv *env, jclass cls, jlong conn, jbyteArray object_id) +{ + PlasmaClient *client = (PlasmaClient *) conn; + ObjectID oid; + jbyteArray_to_object_id(env, object_id, &oid); + + bool has_object; + ARROW_CHECK_OK(client->Contains(oid, &has_object)); + + if (has_object) { + return true; + } + else { + return false; + } +} + +JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_fetch + (JNIEnv *env, jclass cls, jlong conn, jobjectArray object_ids) +{ + PlasmaClient *client = (PlasmaClient *) conn; + jsize num_oids = env->GetArrayLength(object_ids); + + /* + if (!plasma_manager_is_connected(client)) { + jclass Exception = env->FindClass("java/lang/RuntimeException"); + env->ThrowNew(Exception, "Not connected to the plasma manager."); + return; + } + */ + + std::vector oids(num_oids); + for (int i = 0; i < num_oids; ++i) { + jbyteArray_to_object_id(env, + (jbyteArray) env->GetObjectArrayElement(object_ids, i), &oids[i]); + } + + ARROW_CHECK_OK(client->Fetch((int) num_oids, oids.data())); + + return; +} + +JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_wait + (JNIEnv *env, jclass cls, jlong conn, jobjectArray object_ids, jint timeout_ms, jint num_returns) +{ + PlasmaClient *client = (PlasmaClient *) conn; + jsize num_oids = env->GetArrayLength(object_ids); + + /* + if (!plasma_manager_is_connected(client)) { + jclass Exception = env->FindClass("java/lang/RuntimeException"); + env->ThrowNew(Exception, "Not connected to the plasma manager."); + return NULL; + } + */ + + if (num_returns < 0) { + jclass Exception = env->FindClass("java/lang/RuntimeException"); + env->ThrowNew(Exception, "The argument num_returns cannot be less than zero."); + return NULL; + } + if (num_returns > num_oids) { + jclass Exception = env->FindClass("java/lang/RuntimeException"); + env->ThrowNew(Exception, + "The argument num_returns cannot be greater than len(object_ids)."); + return NULL; + } + + std::vector oreqs(num_oids); + + for (int i = 0; i < num_oids; ++i) { + jbyteArray_to_object_id(env, + (jbyteArray) env->GetObjectArrayElement(object_ids, i), &oreqs[i].object_id); + oreqs[i].type = PLASMA_QUERY_ANYWHERE; + } + + int num_return_objects; + // TODO: may be blocked. consider to add the thread support + ARROW_CHECK_OK(client->Wait((int) num_oids, oreqs.data(), num_returns, + (uint64_t) timeout_ms, &num_return_objects)); + + int num_to_return = min(num_return_objects, num_returns); + jclass clsByteArray = env->FindClass("[B"); + jobjectArray ret = env->NewObjectArray(num_to_return, clsByteArray, NULL); + + int num_returned = 0; + jbyteArray oid = NULL; + for (int i = 0; i < num_oids; ++i) { + if (num_returned >= num_to_return) { + break; + } + + if (oreqs[i].status == ObjectStatusLocal + || oreqs[i].status == ObjectStatusRemote) { + oid = env->NewByteArray(LEN_OF_OBJECTID); + object_id_to_jbyteArray(env, oid, &oreqs[i].object_id); + env->SetObjectArrayElement(ret, num_returned, oid); + num_returned++; + } + else { + //ARROW_CHECK(oreqs[i].status == ObjectStatus_Nonexistent); + } + } + ARROW_CHECK(num_returned == num_to_return); + + return ret; +} + +JNIEXPORT jlong JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_evict + (JNIEnv *env, jclass cls, jlong conn, jlong num_bytes) +{ + PlasmaClient *client = (PlasmaClient *) conn; + + int64_t evicted_bytes; + ARROW_CHECK_OK(client->Evict((int64_t) num_bytes, evicted_bytes)); + + return (jlong) evicted_bytes; +} + diff --git a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.h b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.h new file mode 100644 index 000000000000..e79bdbf71575 --- /dev/null +++ b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.h @@ -0,0 +1,101 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class org_apache_arrow_plasma_PlasmaClientJNI */ + +#ifndef _Included_org_apache_arrow_plasma_PlasmaClientJNI +#define _Included_org_apache_arrow_plasma_PlasmaClientJNI +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: org_apache_arrow_plasma_PlasmaClientJNI + * Method: connect + * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)J + */ +JNIEXPORT jlong JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_connect + (JNIEnv *, jclass, jstring, jstring, jstring, jstring, jint); + +/* + * Class: org_apache_arrow_plasma_PlasmaClientJNI + * Method: disconnect + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_disconnect + (JNIEnv *, jclass, jlong); + +/* + * Class: org_apache_arrow_plasma_PlasmaClientJNI + * Method: create + * Signature: (J[BI[B)Ljava/nio/ByteBuffer; + */ +JNIEXPORT jobject JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_create + (JNIEnv *, jclass, jlong, jbyteArray, jint, jbyteArray); + +/* + * Class: org_apache_arrow_plasma_PlasmaClientJNI + * Method: hash + * Signature: (J[B)[B + */ +JNIEXPORT jbyteArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_hash + (JNIEnv *, jclass, jlong, jbyteArray); + +/* + * Class: org_apache_arrow_plasma_PlasmaClientJNI + * Method: seal + * Signature: (J[B)V + */ +JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_seal + (JNIEnv *, jclass, jlong, jbyteArray); + +/* + * Class: org_apache_arrow_plasma_PlasmaClientJNI + * Method: release + * Signature: (J[B)V + */ +JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_release + (JNIEnv *, jclass, jlong, jbyteArray); + +/* + * Class: org_apache_arrow_plasma_PlasmaClientJNI + * Method: get + * Signature: (J[[BI)[[Ljava/nio/ByteBuffer; + */ +JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_get + (JNIEnv *, jclass, jlong, jobjectArray, jint); + +/* + * Class: org_apache_arrow_plasma_PlasmaClientJNI + * Method: contains + * Signature: (J[B)Z + */ +JNIEXPORT jboolean JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_contains + (JNIEnv *, jclass, jlong, jbyteArray); + +/* + * Class: org_apache_arrow_plasma_PlasmaClientJNI + * Method: fetch + * Signature: (J[[B)V + */ +JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_fetch + (JNIEnv *, jclass, jlong, jobjectArray); + +/* + * Class: org_apache_arrow_plasma_PlasmaClientJNI + * Method: wait + * Signature: (J[[BII)[[B + */ +JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_wait + (JNIEnv *, jclass, jlong, jobjectArray, jint, jint); + +/* + * Class: org_apache_arrow_plasma_PlasmaClientJNI + * Method: evict + * Signature: (JJ)J + */ +JNIEXPORT jlong JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_evict + (JNIEnv *, jclass, jlong, jlong); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/java/plasma/pom.xml b/java/plasma/pom.xml new file mode 100644 index 000000000000..6b8a91c38ec8 --- /dev/null +++ b/java/plasma/pom.xml @@ -0,0 +1,33 @@ + + + + 4.0.0 + + org.apache.arrow + arrow-java-root + 0.10.0-SNAPSHOT + + arrow-plasma + Arrow Plasma Client + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + diff --git a/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectBuffer.java b/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectBuffer.java new file mode 100644 index 000000000000..949e3f5b8088 --- /dev/null +++ b/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectBuffer.java @@ -0,0 +1,55 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.apache.arrow.plasma; + +import java.util.function.Consumer; + +/** + * Represents a managed raw byte buffer. + */ +public class ObjectBuffer { + + private final byte[] buffer; + private final Consumer finalizer; + private boolean isReleased = false; + + public ObjectBuffer(byte[] buffer) { + this(buffer, null); + } + + public ObjectBuffer(byte[] buffer, Consumer finalizer) { + this.buffer = buffer; + this.finalizer = finalizer; + } + + public byte[] buffer() { + return buffer; + } + + public void release() { + if (finalizer != null) { + assert !isReleased; + finalizer.accept(buffer); + isReleased = true; + } + } + + protected void finalize() { + assert finalizer == null || isReleased; + } +} diff --git a/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectId.java b/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectId.java new file mode 100644 index 000000000000..3efe70a7705e --- /dev/null +++ b/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectId.java @@ -0,0 +1,24 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.apache.arrow.plasma; + +public interface ObjectId { + + byte[] getBytes(); + +} diff --git a/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectStoreLink.java b/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectStoreLink.java new file mode 100644 index 000000000000..860a4758fff1 --- /dev/null +++ b/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectStoreLink.java @@ -0,0 +1,103 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.apache.arrow.plasma; + +import java.util.Collections; +import java.util.List; + +/** + * Object store interface, which provides the capabilities to put and get raw byte array, and serves + */ +public interface ObjectStoreLink { + + /** + * Put value in the local plasma store with object ID objectId. + * + * @param objectId The object ID of the value to be put. + * @param value The value to put in the object store. + * @param metadata encodes whatever metadata the user wishes to encode. + */ + void put(ObjectId objectId, byte[] value, byte[] metadata); + + /** + * Create a buffer from the PlasmaStore based on the objectId. + * + * @param objectId The object ID used to identify the object. + * @param timeoutMs The number of milliseconds that the get call should block before timing out + * and returning. Pass -1 if the call should block and 0 if the call should return immediately. + * @param isMetadata false if get data, otherwise get metadata. + * @return A PlasmaBuffer wrapping the object. + */ + default ObjectBuffer get(ObjectId objectId, int timeoutMs, boolean isMetadata) { + return get(Collections.singletonList(objectId), timeoutMs, isMetadata).get(0); + } + + /** + * Create buffers from the PlasmaStore based on objectIds. + * + * @param objectIds List of object IDs used to identify some objects. + * @param timeoutMs The number of milliseconds that the get call should block before timing out + * and returning. Pass -1 if the call should block and 0 if the call should return immediately. + * @param isMetadata false if get data, otherwise get metadata. + * @return List of PlasmaBuffers wrapping objects. + */ + List get(List objectIds, int timeoutMs, boolean isMetadata); + + /** + * Wait until numReturns objects in objectIds are ready. + * + * @param objectIds List of object IDs to wait for. + * @param timeoutMs Return to the caller after timeoutMs milliseconds. + * @param numReturns We are waiting for this number of objects to be ready. + * @return List of object IDs that are ready + */ + List wait(List objectIds, int timeoutMs, int numReturns); + + /** + * Compute the hash of an object in the object store. + * + * @param objectId The object ID used to identify the object. + * @return A digest byte array contains object's SHA256 hash. null means that the object + * isn't in the object store. + */ + byte[] hash(ObjectId objectId); + + /** + * Fetch the object with the given ID from other plasma manager instances. + * + * @param objectId The object ID used to identify the object. + */ + default void fetch(ObjectId objectId) { + fetch(Collections.singletonList(objectId)); + } + + /** + * Fetch the objects with the given IDs from other plasma manager instances. + * + * @param objectIds List of object IDs used to identify the objects. + */ + void fetch(List objectIds); + + /** + * Evict some objects to recover given count of bytes. + * + * @param numBytes The number of bytes to attempt to recover. + * @return The number of bytes that have been evicted. + */ + long evict(long numBytes); +} diff --git a/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClient.java b/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClient.java new file mode 100644 index 000000000000..f76c730abec5 --- /dev/null +++ b/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClient.java @@ -0,0 +1,162 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.apache.arrow.plasma; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + + + +/** + * The PlasmaClient is used to interface with a plasma store and manager. + * + * The PlasmaClient can ask the PlasmaStore to allocate a new buffer, seal a buffer, and get a + * buffer. Buffers are referred to by object IDs. + */ +public class PlasmaClient implements ObjectStoreLink { + + private final long conn; + + protected void finalize() { + PlasmaClientJNI.disconnect(this.conn); + } + + // use plasma client to initialize the underlying jni system as well via config and config-overwrites + public PlasmaClient(String configFile, String configOverwrites, String storeSocketName, + String managerSocketName, int releaseDelay) { + this.conn = PlasmaClientJNI.connect( + configFile, configOverwrites, storeSocketName, managerSocketName, releaseDelay); + } + + // interface methods -------------------- + + @Override + public void put(ObjectId objectId, byte[] value, byte[] metadata) { + ByteBuffer buf = null; + try { + buf = PlasmaClientJNI.create(conn, objectId.getBytes(), value.length, metadata); + } catch (Exception e) { + System.err.println("ObjectId " + objectId + " error at PlasmaClient put"); + e.printStackTrace(); + } + if (buf == null) { + return; + } + + buf.put(value); + PlasmaClientJNI.seal(conn, objectId.getBytes()); + PlasmaClientJNI.release(conn, objectId.getBytes()); + } + + @Override + public List get(List objectIds, int timeoutMs, boolean isMetadata) { + byte[][] ids = getIdBytes(objectIds); + ByteBuffer[][] bufs = PlasmaClientJNI.get(conn, ids, timeoutMs); + assert bufs.length == objectIds.size(); + + List ret = new ArrayList<>(); + for (int i = 0; i < bufs.length; i++) { + ObjectId oid = objectIds.get(i); + ByteBuffer buf = bufs[i][isMetadata ? 1 : 0]; + if (buf == null) { + ret.add(new ObjectBuffer(null, null)); + } else { + byte[] bb = new byte[buf.remaining()]; + buf.get(bb); + ret.add(new ObjectBuffer(bb, (byte[] b) -> this.release(oid))); + } + } + return ret; + } + + private static byte[][] getIdBytes(List objectIds) { + int size = objectIds.size(); + byte[][] ids = new byte[size][]; + for (int i = 0; i < size; i++) { + ids[i] = objectIds.get(i).getBytes(); + } + return ids; + } + + @Override + public List wait(List objectIds, int timeoutMs, int numReturns) { + byte[][] ids = getIdBytes(objectIds); + byte[][] readys = PlasmaClientJNI.wait(conn, ids, timeoutMs, numReturns); + + List ret = new ArrayList<>(); + for (byte[] ready : readys) { + for (ObjectId id : objectIds) { + if (Arrays.equals(ready, id.getBytes())) { + ret.add(id); + break; + } + } + } + + assert (ret.size() == readys.length); + return ret; + } + + @Override + public byte[] hash(ObjectId objectId) { + return PlasmaClientJNI.hash(conn, objectId.getBytes()); + } + + @Override + public void fetch(List objectIds) { + byte[][] ids = getIdBytes(objectIds); + PlasmaClientJNI.fetch(conn, ids); + } + + @Override + public long evict(long numBytes) { + return PlasmaClientJNI.evict(conn, numBytes); + } + + // wrapper methods -------------------- + + /** + * Seal the buffer in the PlasmaStore for a particular object ID. + * Once a buffer has been sealed, the buffer is immutable and can only be accessed through get. + * + * @param objectId used to identify an object. + */ + public void seal(ObjectId objectId) { + PlasmaClientJNI.seal(conn, objectId.getBytes()); + } + + /** + * Notify Plasma that the object is no longer needed. + * + * @param objectId used to identify an object. + */ + public void release(ObjectId objectId) { + PlasmaClientJNI.release(conn, objectId.getBytes()); + } + + /** + * Check if the object is present and has been sealed in the PlasmaStore. + * + * @param objectId used to identify an object. + */ + public boolean contains(ObjectId objectId) { + return PlasmaClientJNI.contains(conn, objectId.getBytes()); + } +} diff --git a/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClientJNI.java b/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClientJNI.java new file mode 100644 index 000000000000..1fdf817d9114 --- /dev/null +++ b/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClientJNI.java @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.apache.arrow.plasma; + +import java.nio.ByteBuffer; + +/** + * JNI static methods for PlasmaClient + */ +public class PlasmaClientJNI { + + native public static long connect(String configFile, String overwrites, String store_socket_name, + String manager_socket_name, int release_delay); + + native public static void disconnect(long conn); + + native public static ByteBuffer create(long conn, byte[] object_id, int size, byte[] metadata); + + native public static byte[] hash(long conn, byte[] object_id); + + native public static void seal(long conn, byte[] object_id); + + native public static void release(long conn, byte[] object_id); + + native public static ByteBuffer[][] get(long conn, byte[][] object_ids, int timeout_ms); + + native public static boolean contains(long conn, byte[] object_id); + + native public static void fetch(long conn, byte[][] object_ids); + + native public static byte[][] wait(long conn, byte[][] object_ids, int timeout_ms, + int num_returns); + + native public static long evict(long conn, long num_bytes); + +} diff --git a/java/pom.xml b/java/pom.xml index ae9fc86a09b4..ce7c550ddcc6 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -623,6 +623,7 @@ memory vector tools + plasma From 9da40d02621283ca1e871a576e1528be0cedc573 Mon Sep 17 00:00:00 2001 From: salah-man Date: Sat, 19 May 2018 14:44:17 +0800 Subject: [PATCH 02/11] Delete the ObjectId.java and ObjectBuffer.java, change the input and output of plasma java client api from custem type to byte[] --- .../org/apache/arrow/plasma/ObjectBuffer.java | 55 ---------------- .../org/apache/arrow/plasma/ObjectId.java | 24 ------- .../apache/arrow/plasma/ObjectStoreLink.java | 28 ++++++--- .../org/apache/arrow/plasma/PlasmaClient.java | 63 ++++++++----------- 4 files changed, 43 insertions(+), 127 deletions(-) delete mode 100644 java/plasma/src/main/java/org/apache/arrow/plasma/ObjectBuffer.java delete mode 100644 java/plasma/src/main/java/org/apache/arrow/plasma/ObjectId.java diff --git a/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectBuffer.java b/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectBuffer.java deleted file mode 100644 index 949e3f5b8088..000000000000 --- a/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectBuffer.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ -package org.apache.arrow.plasma; - -import java.util.function.Consumer; - -/** - * Represents a managed raw byte buffer. - */ -public class ObjectBuffer { - - private final byte[] buffer; - private final Consumer finalizer; - private boolean isReleased = false; - - public ObjectBuffer(byte[] buffer) { - this(buffer, null); - } - - public ObjectBuffer(byte[] buffer, Consumer finalizer) { - this.buffer = buffer; - this.finalizer = finalizer; - } - - public byte[] buffer() { - return buffer; - } - - public void release() { - if (finalizer != null) { - assert !isReleased; - finalizer.accept(buffer); - isReleased = true; - } - } - - protected void finalize() { - assert finalizer == null || isReleased; - } -} diff --git a/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectId.java b/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectId.java deleted file mode 100644 index 3efe70a7705e..000000000000 --- a/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectId.java +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ -package org.apache.arrow.plasma; - -public interface ObjectId { - - byte[] getBytes(); - -} diff --git a/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectStoreLink.java b/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectStoreLink.java index 860a4758fff1..86edfa1ffea1 100644 --- a/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectStoreLink.java +++ b/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectStoreLink.java @@ -17,7 +17,6 @@ */ package org.apache.arrow.plasma; -import java.util.Collections; import java.util.List; /** @@ -32,7 +31,7 @@ public interface ObjectStoreLink { * @param value The value to put in the object store. * @param metadata encodes whatever metadata the user wishes to encode. */ - void put(ObjectId objectId, byte[] value, byte[] metadata); + void put(byte[] objectId, byte[] value, byte[] metadata); /** * Create a buffer from the PlasmaStore based on the objectId. @@ -43,8 +42,9 @@ public interface ObjectStoreLink { * @param isMetadata false if get data, otherwise get metadata. * @return A PlasmaBuffer wrapping the object. */ - default ObjectBuffer get(ObjectId objectId, int timeoutMs, boolean isMetadata) { - return get(Collections.singletonList(objectId), timeoutMs, isMetadata).get(0); + default byte[] get(byte[] objectId, int timeoutMs, boolean isMetadata) { + byte[][] objectIds = {objectId}; + return get(objectIds, timeoutMs, isMetadata).get(0); } /** @@ -56,7 +56,7 @@ default ObjectBuffer get(ObjectId objectId, int timeoutMs, boolean isMetadata) { * @param isMetadata false if get data, otherwise get metadata. * @return List of PlasmaBuffers wrapping objects. */ - List get(List objectIds, int timeoutMs, boolean isMetadata); + List get(byte[][] objectIds, int timeoutMs, boolean isMetadata); /** * Wait until numReturns objects in objectIds are ready. @@ -66,7 +66,7 @@ default ObjectBuffer get(ObjectId objectId, int timeoutMs, boolean isMetadata) { * @param numReturns We are waiting for this number of objects to be ready. * @return List of object IDs that are ready */ - List wait(List objectIds, int timeoutMs, int numReturns); + List wait(byte[][] objectIds, int timeoutMs, int numReturns); /** * Compute the hash of an object in the object store. @@ -75,15 +75,16 @@ default ObjectBuffer get(ObjectId objectId, int timeoutMs, boolean isMetadata) { * @return A digest byte array contains object's SHA256 hash. null means that the object * isn't in the object store. */ - byte[] hash(ObjectId objectId); + byte[] hash(byte[] objectId); /** * Fetch the object with the given ID from other plasma manager instances. * * @param objectId The object ID used to identify the object. */ - default void fetch(ObjectId objectId) { - fetch(Collections.singletonList(objectId)); + default void fetch(byte[] objectId) { + byte[][] objectIds = {objectId}; + fetch(objectIds); } /** @@ -91,7 +92,7 @@ default void fetch(ObjectId objectId) { * * @param objectIds List of object IDs used to identify the objects. */ - void fetch(List objectIds); + void fetch(byte[][] objectIds); /** * Evict some objects to recover given count of bytes. @@ -100,4 +101,11 @@ default void fetch(ObjectId objectId) { * @return The number of bytes that have been evicted. */ long evict(long numBytes); + + /** + * Release the reference of the object. + * + * @param objectId The object ID used to release the reference of the object. + */ + void release(byte[] objectId); } diff --git a/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClient.java b/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClient.java index f76c730abec5..279d1a6edaa9 100644 --- a/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClient.java +++ b/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClient.java @@ -48,10 +48,10 @@ public PlasmaClient(String configFile, String configOverwrites, String storeSock // interface methods -------------------- @Override - public void put(ObjectId objectId, byte[] value, byte[] metadata) { + public void put(byte[] objectId, byte[] value, byte[] metadata) { ByteBuffer buf = null; try { - buf = PlasmaClientJNI.create(conn, objectId.getBytes(), value.length, metadata); + buf = PlasmaClientJNI.create(conn, objectId, value.length, metadata); } catch (Exception e) { System.err.println("ObjectId " + objectId + " error at PlasmaClient put"); e.printStackTrace(); @@ -61,49 +61,37 @@ public void put(ObjectId objectId, byte[] value, byte[] metadata) { } buf.put(value); - PlasmaClientJNI.seal(conn, objectId.getBytes()); - PlasmaClientJNI.release(conn, objectId.getBytes()); + PlasmaClientJNI.seal(conn, objectId); + PlasmaClientJNI.release(conn, objectId); } @Override - public List get(List objectIds, int timeoutMs, boolean isMetadata) { - byte[][] ids = getIdBytes(objectIds); - ByteBuffer[][] bufs = PlasmaClientJNI.get(conn, ids, timeoutMs); - assert bufs.length == objectIds.size(); + public List get(byte[][] objectIds, int timeoutMs, boolean isMetadata) { + ByteBuffer[][] bufs = PlasmaClientJNI.get(conn, objectIds, timeoutMs); + assert bufs.length == objectIds.length; - List ret = new ArrayList<>(); + List ret = new ArrayList<>(); for (int i = 0; i < bufs.length; i++) { - ObjectId oid = objectIds.get(i); ByteBuffer buf = bufs[i][isMetadata ? 1 : 0]; if (buf == null) { - ret.add(new ObjectBuffer(null, null)); + ret.add(null); } else { byte[] bb = new byte[buf.remaining()]; buf.get(bb); - ret.add(new ObjectBuffer(bb, (byte[] b) -> this.release(oid))); + ret.add(bb); } } return ret; } - private static byte[][] getIdBytes(List objectIds) { - int size = objectIds.size(); - byte[][] ids = new byte[size][]; - for (int i = 0; i < size; i++) { - ids[i] = objectIds.get(i).getBytes(); - } - return ids; - } - @Override - public List wait(List objectIds, int timeoutMs, int numReturns) { - byte[][] ids = getIdBytes(objectIds); - byte[][] readys = PlasmaClientJNI.wait(conn, ids, timeoutMs, numReturns); + public List wait(byte[][] objectIds, int timeoutMs, int numReturns) { + byte[][] readys = PlasmaClientJNI.wait(conn, objectIds, timeoutMs, numReturns); - List ret = new ArrayList<>(); + List ret = new ArrayList<>(); for (byte[] ready : readys) { - for (ObjectId id : objectIds) { - if (Arrays.equals(ready, id.getBytes())) { + for (byte[] id : objectIds) { + if (Arrays.equals(ready, id)) { ret.add(id); break; } @@ -115,14 +103,13 @@ public List wait(List objectIds, int timeoutMs, in } @Override - public byte[] hash(ObjectId objectId) { - return PlasmaClientJNI.hash(conn, objectId.getBytes()); + public byte[] hash(byte[] objectId) { + return PlasmaClientJNI.hash(conn, objectId); } @Override - public void fetch(List objectIds) { - byte[][] ids = getIdBytes(objectIds); - PlasmaClientJNI.fetch(conn, ids); + public void fetch(byte[][] objectIds) { + PlasmaClientJNI.fetch(conn, objectIds); } @Override @@ -138,8 +125,8 @@ public long evict(long numBytes) { * * @param objectId used to identify an object. */ - public void seal(ObjectId objectId) { - PlasmaClientJNI.seal(conn, objectId.getBytes()); + public void seal(byte[] objectId) { + PlasmaClientJNI.seal(conn, objectId); } /** @@ -147,8 +134,8 @@ public void seal(ObjectId objectId) { * * @param objectId used to identify an object. */ - public void release(ObjectId objectId) { - PlasmaClientJNI.release(conn, objectId.getBytes()); + public void release(byte[] objectId) { + PlasmaClientJNI.release(conn, objectId); } /** @@ -156,7 +143,7 @@ public void release(ObjectId objectId) { * * @param objectId used to identify an object. */ - public boolean contains(ObjectId objectId) { - return PlasmaClientJNI.contains(conn, objectId.getBytes()); + public boolean contains(byte[] objectId) { + return PlasmaClientJNI.contains(conn, objectId); } } From 3e9ba0dba60c9b8d37365ab2a17dc448f6ff3a71 Mon Sep 17 00:00:00 2001 From: salah-man Date: Mon, 21 May 2018 10:48:10 +0800 Subject: [PATCH 03/11] Changes for the feedback from pr -------------------------- It consists of the following changes: add java/plasma/README.md for help document add java/plasma/test.sh for plasma java client test case fix some ci error delete the useless parameters of connect api from jni --- cpp/src/plasma/CMakeLists.txt | 11 ++ ...org_apache_arrow_plasma_PlasmaClientJNI.cc | 18 ++- .../org_apache_arrow_plasma_PlasmaClientJNI.h | 21 ++- java/plasma/README.md | 39 +++++ .../org/apache/arrow/plasma/PlasmaClient.java | 6 +- .../apache/arrow/plasma/PlasmaClientJNI.java | 3 +- .../apache/arrow/plasma/PlasmaClientTest.java | 153 ++++++++++++++++++ java/plasma/test.sh | 46 ++++++ 8 files changed, 288 insertions(+), 9 deletions(-) create mode 100644 java/plasma/README.md create mode 100644 java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java create mode 100755 java/plasma/test.sh diff --git a/cpp/src/plasma/CMakeLists.txt b/cpp/src/plasma/CMakeLists.txt index f59ea3c61525..c2bd6762571c 100644 --- a/cpp/src/plasma/CMakeLists.txt +++ b/cpp/src/plasma/CMakeLists.txt @@ -161,6 +161,17 @@ install( DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig/") # Plasma java client support +find_package(JNI REQUIRED) +# add jni support +include_directories(${JAVA_INCLUDE_PATH}) +include_directories(${JAVA_INCLUDE_PATH2}) +if (JNI_FOUND) + message (STATUS "JNI_INCLUDE_DIRS=${JNI_INCLUDE_DIRS}") + message (STATUS "JNI_LIBRARIES=${JNI_LIBRARIES}") +else() + message (WARNING "NOT FIND JNI") +endif() + add_compile_options("-I$ENV{JAVA_HOME}/include/") if(WIN32) add_compile_options("-I$ENV{JAVA_HOME}/include/win32") diff --git a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc index a036c9cf911f..08d3c10c7ff3 100644 --- a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc +++ b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc @@ -1,3 +1,19 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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. #include #include #include @@ -45,7 +61,7 @@ class JByteArrayGetter { }; JNIEXPORT jlong JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_connect - (JNIEnv *env, jclass cls, jstring config, jstring overwrites, jstring store_socket_name, jstring manager_socket_name, jint release_delay) + (JNIEnv *env, jclass cls, jstring store_socket_name, jstring manager_socket_name, jint release_delay) { const char *s_name = env->GetStringUTFChars(store_socket_name, NULL); const char *m_name = env->GetStringUTFChars(manager_socket_name, NULL); diff --git a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.h b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.h index e79bdbf71575..698711b8fee6 100644 --- a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.h +++ b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.h @@ -1,3 +1,20 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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. + /* DO NOT EDIT THIS FILE - it is machine generated */ #include /* Header for class org_apache_arrow_plasma_PlasmaClientJNI */ @@ -10,10 +27,10 @@ extern "C" { /* * Class: org_apache_arrow_plasma_PlasmaClientJNI * Method: connect - * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)J + * Signature: (Ljava/lang/String;Ljava/lang/String;I)J */ JNIEXPORT jlong JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_connect - (JNIEnv *, jclass, jstring, jstring, jstring, jstring, jint); + (JNIEnv *, jclass, jstring, jstring, jint); /* * Class: org_apache_arrow_plasma_PlasmaClientJNI diff --git a/java/plasma/README.md b/java/plasma/README.md new file mode 100644 index 000000000000..549f46a36bb7 --- /dev/null +++ b/java/plasma/README.md @@ -0,0 +1,39 @@ + + +# Plasma client of java + +## Setup Build Environment + +install: + - java 8 or later + - maven 3.3 or later + - the same requirement of build [Arrow C++](https://github.com/apache/arrow/tree/master/cpp) + +## Build the jar of plasma client + +``` +mvn clean install -Dmaven.test.skip +``` + +## Building and running tests +``` +./test.sh +``` + diff --git a/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClient.java b/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClient.java index 279d1a6edaa9..d0fdc9b8aec3 100644 --- a/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClient.java +++ b/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClient.java @@ -39,10 +39,8 @@ protected void finalize() { } // use plasma client to initialize the underlying jni system as well via config and config-overwrites - public PlasmaClient(String configFile, String configOverwrites, String storeSocketName, - String managerSocketName, int releaseDelay) { - this.conn = PlasmaClientJNI.connect( - configFile, configOverwrites, storeSocketName, managerSocketName, releaseDelay); + public PlasmaClient(String storeSocketName, String managerSocketName, int releaseDelay) { + this.conn = PlasmaClientJNI.connect(storeSocketName, managerSocketName, releaseDelay); } // interface methods -------------------- diff --git a/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClientJNI.java b/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClientJNI.java index 1fdf817d9114..17f63080c3a0 100644 --- a/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClientJNI.java +++ b/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClientJNI.java @@ -24,8 +24,7 @@ */ public class PlasmaClientJNI { - native public static long connect(String configFile, String overwrites, String store_socket_name, - String manager_socket_name, int release_delay); + native public static long connect(String store_socket_name, String manager_socket_name, int release_delay); native public static void disconnect(long conn); diff --git a/java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java b/java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java new file mode 100644 index 000000000000..3d217d30327e --- /dev/null +++ b/java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java @@ -0,0 +1,153 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.apache.arrow.plasma; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +public class PlasmaClientTest { + + private String storeSuffix = "/tmp/store"; + + private Process storeProcess; + + private int storePort; + + private ObjectStoreLink pLink; + + + public PlasmaClientTest() throws Exception{ + String plasmaStorePath = System.getenv("PLASMA_STORE"); + if(plasmaStorePath == null) { + throw new Exception("Please set plasma store path in env PLASMA_STORE"); + } + + this.startObjectStore(plasmaStorePath); + System.loadLibrary("plasma_java"); + pLink = new PlasmaClient(this.getStoreAddress(), "", 0); + } + + private Process startProcess(String[] cmd) { + ProcessBuilder builder; + List newCmd = Arrays.stream(cmd).filter(s -> s.length() > 0).collect(Collectors.toList()); + builder = new ProcessBuilder(newCmd); + Process p = null; + try { + p = builder.start(); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + System.out.println("Start process " + p.hashCode() + " OK, cmd = " + Arrays.toString(cmd).replace(',', ' ')); + return p; + } + + private void startObjectStore(String plasmaStorePath) { + int occupiedMemoryMB = 10; + long memoryBytes = occupiedMemoryMB * 1000000; + int numRetries = 10; + Process p = null; + while (numRetries-- > 0) { + int currentPort = java.util.concurrent.ThreadLocalRandom.current().nextInt(0, 100000); + String name = storeSuffix + currentPort; + String cmd = plasmaStorePath + " -s " + name + " -m " + memoryBytes; + + p = startProcess(cmd.split(" ")); + + if (p != null && p.isAlive()) { + try { + TimeUnit.MILLISECONDS.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + if (p.isAlive()) { + storePort = currentPort; + break; + } + } + } + + + if (p == null || !p.isAlive()) { + throw new RuntimeException("Start object store failed ..."); + } else { + storeProcess = p; + System.out.println("Start object store success"); + } + } + + private void cleanup() { + if (storeProcess != null && killProcess(storeProcess)) { + System.out.println("Kill plasma store process forcely"); + } + } + + private static boolean killProcess(Process p) { + if (p.isAlive()) { + p.destroyForcibly(); + return true; + } else { + return false; + } + } + + public void doTest() { + System.out.println("Start test."); + int timeoutMs = 3000; + byte[] id1 = new byte[20]; + Arrays.fill(id1, (byte)1); + byte[] value1 = new byte[20]; + Arrays.fill(value1, (byte)11); + pLink.put(id1, value1, null); + + byte[] id2 = new byte[20]; + Arrays.fill(id2, (byte)2); + byte[] value2 = new byte[20]; + Arrays.fill(value2, (byte)12); + pLink.put(id2, value2, null); + System.out.println("Plasma java client put test success."); + byte[] getValue1 = pLink.get(id1, timeoutMs, false); + assert Arrays.equals(value1, getValue1); + + byte[] getValue2 = pLink.get(id2, timeoutMs, false); + assert Arrays.equals(value2, getValue2); + System.out.println("Plasma java client get single object test success."); + byte[][] ids = {id1, id2}; + List values = pLink.get(ids, timeoutMs, false); + assert Arrays.equals(values.get(0), value1); + assert Arrays.equals(values.get(1), value2); + System.out.println("Plasma java client get multi-object test success."); + cleanup(); + System.out.println("All test success."); + + } + + public String getStoreAddress() { + return storeSuffix+storePort; + } + public static void main(String[] args) throws Exception { + + PlasmaClientTest plasmaClientTest = new PlasmaClientTest(); + plasmaClientTest.doTest(); + + } + +} diff --git a/java/plasma/test.sh b/java/plasma/test.sh new file mode 100755 index 000000000000..e102aa88c324 --- /dev/null +++ b/java/plasma/test.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +ROOT_DIR=$(cd "$(dirname "${BASH_SOURCE:-$0}")"; pwd) +pushd ../../cpp + if [ ! -d "release" ]; then + mkdir release + fi + pushd release + cmake -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_C_FLAGS="-g -O3" \ + -DCMAKE_CXX_FLAGS="-g -O3" \ + -DARROW_BUILD_TESTS=off \ + -DARROW_HDFS=on \ + -DARROW_BOOST_USE_SHARED=off \ + -DARROW_PYTHON=on \ + -DARROW_PLASMA=on \ + -DPLASMA_PYTHON=on \ + -DARROW_JEMALLOC=off \ + -DARROW_WITH_BROTLI=off \ + -DARROW_WITH_LZ4=off \ + -DARROW_WITH_ZLIB=off \ + -DARROW_WITH_ZSTD=off \ + .. + make VERBOSE=1 -j$PARALLEL + make install + popd +popd + +mvn clean install +export PLASMA_STORE=$ROOT_DIR/../../cpp/release/release/plasma_store +java -cp target/test-classes:target/classes -Djava.library.path=$ROOT_DIR/../../cpp/release/release/ org.apache.arrow.plasma.PlasmaClientTest From 14393f5f304860166e87b0db0c9f46e99ec05668 Mon Sep 17 00:00:00 2001 From: salah-man Date: Mon, 21 May 2018 14:12:52 +0800 Subject: [PATCH 04/11] fix the ci problem --- cpp/CMakeLists.txt | 4 + cpp/src/plasma/CMakeLists.txt | 63 +-- ...org_apache_arrow_plasma_PlasmaClientJNI.cc | 511 +++++++++--------- .../org_apache_arrow_plasma_PlasmaClientJNI.h | 49 +- java/plasma/test.sh | 11 +- 5 files changed, 320 insertions(+), 318 deletions(-) diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index 521e6850b61d..0fd100789f60 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -199,6 +199,10 @@ if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}") "Build the plasma object store along with Arrow" OFF) + option(ARROW_PLASMA_JAVA_CLIENT + "Build the plasma object store java client" + OFF) + option(ARROW_USE_SSE "Build with SSE4 optimizations" OFF) diff --git a/cpp/src/plasma/CMakeLists.txt b/cpp/src/plasma/CMakeLists.txt index c2bd6762571c..e033f6e6ee9b 100644 --- a/cpp/src/plasma/CMakeLists.txt +++ b/cpp/src/plasma/CMakeLists.txt @@ -160,41 +160,42 @@ install( FILES "${CMAKE_CURRENT_BINARY_DIR}/plasma.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig/") -# Plasma java client support -find_package(JNI REQUIRED) -# add jni support -include_directories(${JAVA_INCLUDE_PATH}) -include_directories(${JAVA_INCLUDE_PATH2}) -if (JNI_FOUND) - message (STATUS "JNI_INCLUDE_DIRS=${JNI_INCLUDE_DIRS}") - message (STATUS "JNI_LIBRARIES=${JNI_LIBRARIES}") -else() - message (WARNING "NOT FIND JNI") -endif() - -add_compile_options("-I$ENV{JAVA_HOME}/include/") -if(WIN32) - add_compile_options("-I$ENV{JAVA_HOME}/include/win32") -elseif(APPLE) - add_compile_options("-I$ENV{JAVA_HOME}/include/darwin") -else() # linux - add_compile_options("-I$ENV{JAVA_HOME}/include/linux") -endif() +if(ARROW_PLASMA_JAVA_CLIENT) + # Plasma java client support + find_package(JNI REQUIRED) + # add jni support + include_directories(${JAVA_INCLUDE_PATH}) + include_directories(${JAVA_INCLUDE_PATH2}) + if (JNI_FOUND) + message (STATUS "JNI_INCLUDE_DIRS=${JNI_INCLUDE_DIRS}") + message (STATUS "JNI_LIBRARIES=${JNI_LIBRARIES}") + else() + message (WARNING "NOT FIND JNI") + endif() -include_directories("${CMAKE_CURRENT_LIST_DIR}/lib/java") + add_compile_options("-I$ENV{JAVA_HOME}/include/") + if(WIN32) + add_compile_options("-I$ENV{JAVA_HOME}/include/win32") + elseif(APPLE) + add_compile_options("-I$ENV{JAVA_HOME}/include/darwin") + else() # linux + add_compile_options("-I$ENV{JAVA_HOME}/include/linux") + endif() -file(GLOB PLASMA_LIBRARY_EXT_java_SRC - lib/java/*.cc lib/*.cc) -add_library(plasma_java SHARED - ${PLASMA_LIBRARY_EXT_java_SRC} - ) + include_directories("${CMAKE_CURRENT_LIST_DIR}/lib/java") -if(APPLE) - target_link_libraries(plasma_java plasma_static ${PLASMA_LINK_LIBS} "-undefined dynamic_lookup" -Wl,-force_load,${FLATBUFFERS_STATIC_LIB} ${FLATBUFFERS_STATIC_LIB} ${PTHREAD_LIBRARY}) -else(APPLE) - target_link_libraries(plasma_java plasma_static ${PLASMA_LINK_LIBS} -Wl,--whole-archive ${FLATBUFFERS_STATIC_LIB} -Wl,--no-whole-archive ${FLATBUFFERS_STATIC_LIB} ${PTHREAD_LIBRARY}) -endif(APPLE) + file(GLOB PLASMA_LIBRARY_EXT_java_SRC + lib/java/*.cc lib/*.cc) + add_library(plasma_java SHARED + ${PLASMA_LIBRARY_EXT_java_SRC} + ) + if(APPLE) + target_link_libraries(plasma_java plasma_static ${PLASMA_LINK_LIBS} "-undefined dynamic_lookup" -Wl,-force_load,${FLATBUFFERS_STATIC_LIB} ${FLATBUFFERS_STATIC_LIB} ${PTHREAD_LIBRARY}) + else(APPLE) + target_link_libraries(plasma_java plasma_static ${PLASMA_LINK_LIBS} -Wl,--whole-archive ${FLATBUFFERS_STATIC_LIB} -Wl,--no-whole-archive ${FLATBUFFERS_STATIC_LIB} ${PTHREAD_LIBRARY}) + endif(APPLE) +endif() ####################################### # Unit tests ####################################### diff --git a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc index 08d3c10c7ff3..f0dddc445c8f 100644 --- a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc +++ b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc @@ -14,325 +14,308 @@ // KIND, either express or implied. See the License for the // specific language governing permissions and limitations // under the License. -#include -#include -#include -#include +#include "plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.h" #include +#include #include +#include +#include +#include +#include +#include +#include #include "plasma/client.h" -#include "org_apache_arrow_plasma_PlasmaClientJNI.h" -using namespace std; -using namespace plasma; -using namespace arrow; +const jsize LEN_OF_OBJECTID = sizeof(plasma::ObjectID) / sizeof(jbyte); -const jsize LEN_OF_OBJECTID = sizeof(ObjectID) / sizeof(jbyte); - -inline void jbyteArray_to_object_id(JNIEnv *env, jbyteArray a, ObjectID *oid) -{ - env->GetByteArrayRegion(a, 0, LEN_OF_OBJECTID, (jbyte *) oid); +inline void jbyteArray_to_object_id(JNIEnv* env, jbyteArray a, plasma::ObjectID* oid) { + env->GetByteArrayRegion(a, 0, LEN_OF_OBJECTID, reinterpret_cast(oid)); } -inline void object_id_to_jbyteArray(JNIEnv *env, jbyteArray a, ObjectID *oid) -{ - env->SetByteArrayRegion(a, 0, LEN_OF_OBJECTID, (jbyte *) oid); +inline void object_id_to_jbyteArray(JNIEnv* env, jbyteArray a, plasma::ObjectID* oid) { + env->SetByteArrayRegion(a, 0, LEN_OF_OBJECTID, reinterpret_cast(oid)); } class JByteArrayGetter { -private: - JNIEnv *_env; - jbyteArray _a; - jbyte *bp; + private: + JNIEnv* _env; + jbyteArray _a; + jbyte* bp; -public: + public: + JByteArrayGetter(JNIEnv* env, jbyteArray a, jbyte** out) { + _env = env; + _a = a; - JByteArrayGetter(JNIEnv *env, jbyteArray a, jbyte **out) { - _env = env; - _a = a; + bp = _env->GetByteArrayElements(_a, NULL); + *out = bp; + } - bp = _env->GetByteArrayElements(_a, NULL); - *out = bp; - } - - ~JByteArrayGetter() { - _env->ReleaseByteArrayElements(_a, bp, 0); - } + ~JByteArrayGetter() { _env->ReleaseByteArrayElements(_a, bp, 0); } }; -JNIEXPORT jlong JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_connect - (JNIEnv *env, jclass cls, jstring store_socket_name, jstring manager_socket_name, jint release_delay) -{ - const char *s_name = env->GetStringUTFChars(store_socket_name, NULL); - const char *m_name = env->GetStringUTFChars(manager_socket_name, NULL); +JNIEXPORT jlong JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_connect( + JNIEnv* env, jclass cls, jstring store_socket_name, jstring manager_socket_name, + jint release_delay) { + const char* s_name = env->GetStringUTFChars(store_socket_name, NULL); + const char* m_name = env->GetStringUTFChars(manager_socket_name, NULL); - PlasmaClient *client = new PlasmaClient(); - ARROW_CHECK_OK( - client->Connect(s_name, m_name, release_delay)); + plasma::PlasmaClient* client = new plasma::PlasmaClient(); + ARROW_CHECK_OK(client->Connect(s_name, m_name, release_delay)); - //fprintf (stdout, "JNI plasma client init, fd = %d\n", client->get_store_fd()); - //fflush (stdout); + // fprintf (stdout, "JNI plasma client init, fd = %d\n", client->get_store_fd()); + // fflush (stdout); - env->ReleaseStringUTFChars(store_socket_name, s_name); - env->ReleaseStringUTFChars(manager_socket_name, m_name); - return (long) client; + env->ReleaseStringUTFChars(store_socket_name, s_name); + env->ReleaseStringUTFChars(manager_socket_name, m_name); + return reinterpret_cast(client); } -JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_disconnect - (JNIEnv *env, jclass cls, jlong conn) -{ - PlasmaClient *client = (PlasmaClient *) conn; +JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_disconnect( + JNIEnv* env, jclass cls, jlong conn) { + plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; - //fprintf (stdout, "JNI plasma client disconnect, fd = %d\n", client->get_store_fd()); - //fflush (stdout); + // fprintf (stdout, "JNI plasma client disconnect, fd = %d\n", client->get_store_fd()); + // fflush (stdout); - ARROW_CHECK_OK(client->Disconnect()); - delete client; - return; + ARROW_CHECK_OK(client->Disconnect()); + delete client; + return; } -JNIEXPORT jobject JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_create - (JNIEnv *env, jclass cls, jlong conn, jbyteArray object_id, jint size, jbyteArray metadata) -{ - PlasmaClient *client = (PlasmaClient *) conn; - ObjectID oid; - jbyteArray_to_object_id(env, object_id, &oid); - - // prepare metadata buffer - uint8_t *md = NULL; - jsize md_size = 0; - std::unique_ptr md_getter; - if (metadata != NULL) { - md_size = env->GetArrayLength(metadata); - } - if (md_size > 0) { - md_getter.reset(new JByteArrayGetter(env, metadata, (jbyte **)&md)); - } - - std::shared_ptr data; - Status s = client->Create(oid, size, md, md_size, &data); - if (s.IsPlasmaObjectExists()) { - jclass Exception = env->FindClass("org/ray/spi/impl/PlasmaObjectExistsException"); - env->ThrowNew(Exception, "An object with this ID already exists in the plasma store."); - return NULL; - } - if (s.IsPlasmaStoreFull()) { - jclass Exception = env->FindClass("org/ray/spi/impl/PlasmaOutOfMemoryException"); - env->ThrowNew(Exception, "The plasma store ran out of memory and could not create this object."); - return NULL; - } - ARROW_CHECK(s.ok()); - - return env->NewDirectByteBuffer(data->mutable_data(), size); +JNIEXPORT jobject JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_create( + JNIEnv* env, jclass cls, jlong conn, jbyteArray object_id, jint size, + jbyteArray metadata) { + plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; + plasma::ObjectID oid; + jbyteArray_to_object_id(env, object_id, &oid); + + // prepare metadata buffer + uint8_t* md = NULL; + jsize md_size = 0; + std::unique_ptr md_getter; + if (metadata != NULL) { + md_size = env->GetArrayLength(metadata); + } + if (md_size > 0) { + md_getter.reset(new JByteArrayGetter(env, metadata, reinterpret_cast(&md))); + } + + std::shared_ptr data; + Status s = client->Create(oid, size, md, md_size, &data); + if (s.IsPlasmaObjectExists()) { + jclass Exception = env->FindClass("org/ray/spi/impl/PlasmaObjectExistsException"); + env->ThrowNew(Exception, + "An object with this ID already exists in the plasma store."); + return NULL; + } + if (s.IsPlasmaStoreFull()) { + jclass Exception = env->FindClass("org/ray/spi/impl/PlasmaOutOfMemoryException"); + env->ThrowNew(Exception, + "The plasma store ran out of memory and could not create this object."); + return NULL; + } + ARROW_CHECK(s.ok()); + + return env->NewDirectByteBuffer(data->mutable_data(), size); } -JNIEXPORT jbyteArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_hash - (JNIEnv *env, jclass cls, jlong conn, jbyteArray object_id) -{ - PlasmaClient *client = (PlasmaClient *) conn; - ObjectID oid; - jbyteArray_to_object_id(env, object_id, &oid); +JNIEXPORT jbyteArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_hash( + JNIEnv* env, jclass cls, jlong conn, jbyteArray object_id) { + plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; + plasma::ObjectID oid; + jbyteArray_to_object_id(env, object_id, &oid); - unsigned char digest[kDigestSize]; - bool success = client->Hash(oid, digest).ok(); + unsigned char digest[plasma::kDigestSize]; + bool success = client->Hash(oid, digest).ok(); - if (success) { - jbyteArray ret = env->NewByteArray(kDigestSize); - env->SetByteArrayRegion(ret, 0, kDigestSize, (jbyte *) digest); - return ret; - } - else { - return NULL; - } + if (success) { + jbyteArray ret = env->NewByteArray(plasma::kDigestSize); + env->SetByteArrayRegion(ret, 0, plasma::kDigestSize, + reinterpret_cast(digest)); + return ret; + } else { + return NULL; + } } -JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_seal - (JNIEnv *env, jclass cls, jlong conn, jbyteArray object_id) -{ - PlasmaClient *client = (PlasmaClient *) conn; - ObjectID oid; - jbyteArray_to_object_id(env, object_id, &oid); +JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_seal( + JNIEnv* env, jclass cls, jlong conn, jbyteArray object_id) { + plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; + plasma::ObjectID oid; + jbyteArray_to_object_id(env, object_id, &oid); - ARROW_CHECK_OK(client->Seal(oid)); + ARROW_CHECK_OK(client->Seal(oid)); } -JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_release - (JNIEnv *env, jclass cls, jlong conn, jbyteArray object_id) -{ - PlasmaClient *client = (PlasmaClient *) conn; - ObjectID oid; - jbyteArray_to_object_id(env, object_id, &oid); +JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_release( + JNIEnv* env, jclass cls, jlong conn, jbyteArray object_id) { + plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; + plasma::ObjectID oid; + jbyteArray_to_object_id(env, object_id, &oid); - //fprintf (stdout, "JNI plasma client release, fd = %d\n", client->get_store_fd()); - //fflush (stdout); + // fprintf (stdout, "JNI plasma client release, fd = %d\n", client->get_store_fd()); + // fflush (stdout); - ARROW_CHECK_OK(client->Release(oid)); + ARROW_CHECK_OK(client->Release(oid)); } -JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_get - (JNIEnv *env, jclass cls, jlong conn, jobjectArray object_ids, jint timeout_ms) -{ - PlasmaClient *client = (PlasmaClient *) conn; - //fprintf (stdout, "JNI plasma client get, fd = %d\n", client->get_store_fd()); - //fflush (stdout); - - jsize num_oids = env->GetArrayLength(object_ids); - std::vector oids(num_oids); - std::vector obufs(num_oids); - for (int i = 0; i < num_oids; ++i) { - jbyteArray_to_object_id(env, - (jbyteArray) env->GetObjectArrayElement(object_ids, i), &oids[i]); - } - // TODO: may be blocked. consider to add the thread support - ARROW_CHECK_OK( - client->Get(oids.data(), num_oids, timeout_ms, obufs.data())); - - jclass clsByteBuffer = env->FindClass("java/nio/ByteBuffer"); - jclass clsByteBufferArray = env->FindClass("[Ljava/nio/ByteBuffer;"); - - jobjectArray ret = env->NewObjectArray(num_oids, clsByteBufferArray, NULL); - jobjectArray o = NULL; - jobject dataBuf, metadataBuf; - for (int i = 0; i < num_oids; ++i) { - o = env->NewObjectArray(2, clsByteBuffer, NULL); - if (obufs[i].data && obufs[i].data->size() != -1 ) { - dataBuf = env->NewDirectByteBuffer(const_cast(obufs[i].data->data()), obufs[i].data->size()); - if (obufs[i].metadata && obufs[i].metadata->size() > 0) { - metadataBuf = env->NewDirectByteBuffer(const_cast(obufs[i].metadata->data()), obufs[i].metadata->size()); - } - else { - metadataBuf = NULL; - } - } - else { - dataBuf = NULL; - metadataBuf = NULL; - } - - env->SetObjectArrayElement(o, 0, dataBuf); - env->SetObjectArrayElement(o, 1, metadataBuf); - env->SetObjectArrayElement(ret, i, o); +JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_get( + JNIEnv* env, jclass cls, jlong conn, jobjectArray object_ids, jint timeout_ms) { + plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; + // fprintf (stdout, "JNI plasma client get, fd = %d\n", client->get_store_fd()); + // fflush (stdout); + + jsize num_oids = env->GetArrayLength(object_ids); + std::vector oids(num_oids); + std::vector obufs(num_oids); + for (int i = 0; i < num_oids; ++i) { + jbyteArray_to_object_id(env, (jbyteArray)env->GetObjectArrayElement(object_ids, i), + &oids[i]); + } + // TODO: may be blocked. consider to add the thread support + ARROW_CHECK_OK(client->Get(oids.data(), num_oids, timeout_ms, obufs.data())); + + jclass clsByteBuffer = env->FindClass("java/nio/ByteBuffer"); + jclass clsByteBufferArray = env->FindClass("[Ljava/nio/ByteBuffer;"); + + jobjectArray ret = env->NewObjectArray(num_oids, clsByteBufferArray, NULL); + jobjectArray o = NULL; + jobject dataBuf, metadataBuf; + for (int i = 0; i < num_oids; ++i) { + o = env->NewObjectArray(2, clsByteBuffer, NULL); + if (obufs[i].data && obufs[i].data->size() != -1) { + dataBuf = env->NewDirectByteBuffer(const_cast(obufs[i].data->data()), + obufs[i].data->size()); + if (obufs[i].metadata && obufs[i].metadata->size() > 0) { + metadataBuf = env->NewDirectByteBuffer( + const_cast(obufs[i].metadata->data()), obufs[i].metadata->size()); + } else { + metadataBuf = NULL; + } + } else { + dataBuf = NULL; + metadataBuf = NULL; } - return ret; + + env->SetObjectArrayElement(o, 0, dataBuf); + env->SetObjectArrayElement(o, 1, metadataBuf); + env->SetObjectArrayElement(ret, i, o); + } + return ret; } -JNIEXPORT jboolean JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_contains - (JNIEnv *env, jclass cls, jlong conn, jbyteArray object_id) -{ - PlasmaClient *client = (PlasmaClient *) conn; - ObjectID oid; - jbyteArray_to_object_id(env, object_id, &oid); +JNIEXPORT jboolean JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_contains( + JNIEnv* env, jclass cls, jlong conn, jbyteArray object_id) { + plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; + plasma::ObjectID oid; + jbyteArray_to_object_id(env, object_id, &oid); - bool has_object; - ARROW_CHECK_OK(client->Contains(oid, &has_object)); + bool has_object; + ARROW_CHECK_OK(client->Contains(oid, &has_object)); - if (has_object) { - return true; - } - else { - return false; - } + if (has_object) { + return true; + } else { + return false; + } } -JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_fetch - (JNIEnv *env, jclass cls, jlong conn, jobjectArray object_ids) -{ - PlasmaClient *client = (PlasmaClient *) conn; - jsize num_oids = env->GetArrayLength(object_ids); - - /* - if (!plasma_manager_is_connected(client)) { - jclass Exception = env->FindClass("java/lang/RuntimeException"); - env->ThrowNew(Exception, "Not connected to the plasma manager."); - return; - } - */ - - std::vector oids(num_oids); - for (int i = 0; i < num_oids; ++i) { - jbyteArray_to_object_id(env, - (jbyteArray) env->GetObjectArrayElement(object_ids, i), &oids[i]); - } +JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_fetch( + JNIEnv* env, jclass cls, jlong conn, jobjectArray object_ids) { + plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; + jsize num_oids = env->GetArrayLength(object_ids); - ARROW_CHECK_OK(client->Fetch((int) num_oids, oids.data())); + /* + if (!plasma_manager_is_connected(client)) { + jclass Exception = env->FindClass("java/lang/RuntimeException"); + env->ThrowNew(Exception, "Not connected to the plasma manager."); + return; + } + */ - return; -} + std::vector oids(num_oids); + for (int i = 0; i < num_oids; ++i) { + jbyteArray_to_object_id(env, (jbyteArray)env->GetObjectArrayElement(object_ids, i), + &oids[i]); + } -JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_wait - (JNIEnv *env, jclass cls, jlong conn, jobjectArray object_ids, jint timeout_ms, jint num_returns) -{ - PlasmaClient *client = (PlasmaClient *) conn; - jsize num_oids = env->GetArrayLength(object_ids); - - /* - if (!plasma_manager_is_connected(client)) { - jclass Exception = env->FindClass("java/lang/RuntimeException"); - env->ThrowNew(Exception, "Not connected to the plasma manager."); - return NULL; - } - */ + ARROW_CHECK_OK(client->Fetch(static_cast(num_oids), oids.data())); - if (num_returns < 0) { - jclass Exception = env->FindClass("java/lang/RuntimeException"); - env->ThrowNew(Exception, "The argument num_returns cannot be less than zero."); - return NULL; - } - if (num_returns > num_oids) { - jclass Exception = env->FindClass("java/lang/RuntimeException"); - env->ThrowNew(Exception, - "The argument num_returns cannot be greater than len(object_ids)."); - return NULL; - } - - std::vector oreqs(num_oids); + return; +} - for (int i = 0; i < num_oids; ++i) { - jbyteArray_to_object_id(env, - (jbyteArray) env->GetObjectArrayElement(object_ids, i), &oreqs[i].object_id); - oreqs[i].type = PLASMA_QUERY_ANYWHERE; +JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_wait( + JNIEnv* env, jclass cls, jlong conn, jobjectArray object_ids, jint timeout_ms, + jint num_returns) { + plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; + jsize num_oids = env->GetArrayLength(object_ids); + + /* + if (!plasma_manager_is_connected(client)) { + jclass Exception = env->FindClass("java/lang/RuntimeException"); + env->ThrowNew(Exception, "Not connected to the plasma manager."); + return NULL; + } + */ + + if (num_returns < 0) { + jclass Exception = env->FindClass("java/lang/RuntimeException"); + env->ThrowNew(Exception, "The argument num_returns cannot be less than zero."); + return NULL; + } + if (num_returns > num_oids) { + jclass Exception = env->FindClass("java/lang/RuntimeException"); + env->ThrowNew(Exception, + "The argument num_returns cannot be greater than len(object_ids)."); + return NULL; + } + + std::vector oreqs(num_oids); + + for (int i = 0; i < num_oids; ++i) { + jbyteArray_to_object_id(env, (jbyteArray)env->GetObjectArrayElement(object_ids, i), + &oreqs[i].object_id); + oreqs[i].type = plasma::PLASMA_QUERY_ANYWHERE; + } + + int num_return_objects; + // TODO: may be blocked. consider to add the thread support + ARROW_CHECK_OK(client->Wait(static_cast(num_oids), oreqs.data(), num_returns, + (uint64_t)timeout_ms, &num_return_objects)); + + int num_to_return = std::min(num_return_objects, num_returns); + jclass clsByteArray = env->FindClass("[B"); + jobjectArray ret = env->NewObjectArray(num_to_return, clsByteArray, NULL); + + int num_returned = 0; + jbyteArray oid = NULL; + for (int i = 0; i < num_oids; ++i) { + if (num_returned >= num_to_return) { + break; } - int num_return_objects; - // TODO: may be blocked. consider to add the thread support - ARROW_CHECK_OK(client->Wait((int) num_oids, oreqs.data(), num_returns, - (uint64_t) timeout_ms, &num_return_objects)); - - int num_to_return = min(num_return_objects, num_returns); - jclass clsByteArray = env->FindClass("[B"); - jobjectArray ret = env->NewObjectArray(num_to_return, clsByteArray, NULL); - - int num_returned = 0; - jbyteArray oid = NULL; - for (int i = 0; i < num_oids; ++i) { - if (num_returned >= num_to_return) { - break; - } - - if (oreqs[i].status == ObjectStatusLocal - || oreqs[i].status == ObjectStatusRemote) { - oid = env->NewByteArray(LEN_OF_OBJECTID); - object_id_to_jbyteArray(env, oid, &oreqs[i].object_id); - env->SetObjectArrayElement(ret, num_returned, oid); - num_returned++; - } - else { - //ARROW_CHECK(oreqs[i].status == ObjectStatus_Nonexistent); - } + if (oreqs[i].status == plasma::ObjectStatusLocal || + oreqs[i].status == plasma::ObjectStatusRemote) { + oid = env->NewByteArray(LEN_OF_OBJECTID); + object_id_to_jbyteArray(env, oid, &oreqs[i].object_id); + env->SetObjectArrayElement(ret, num_returned, oid); + num_returned++; + } else { + // ARROW_CHECK(oreqs[i].status == ObjectStatus_Nonexistent); } - ARROW_CHECK(num_returned == num_to_return); + } + ARROW_CHECK(num_returned == num_to_return); - return ret; + return ret; } -JNIEXPORT jlong JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_evict - (JNIEnv *env, jclass cls, jlong conn, jlong num_bytes) -{ - PlasmaClient *client = (PlasmaClient *) conn; +JNIEXPORT jlong JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_evict( + JNIEnv* env, jclass cls, jlong conn, jlong num_bytes) { + plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; - int64_t evicted_bytes; - ARROW_CHECK_OK(client->Evict((int64_t) num_bytes, evicted_bytes)); + int64_t evicted_bytes; + ARROW_CHECK_OK(client->Evict((int64_t)num_bytes, evicted_bytes)); - return (jlong) evicted_bytes; + return (jlong)evicted_bytes; } - diff --git a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.h b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.h index 698711b8fee6..697a960484cc 100644 --- a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.h +++ b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.h @@ -29,88 +29,93 @@ extern "C" { * Method: connect * Signature: (Ljava/lang/String;Ljava/lang/String;I)J */ -JNIEXPORT jlong JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_connect - (JNIEnv *, jclass, jstring, jstring, jint); +JNIEXPORT jlong JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_connect( + JNIEnv*, jclass, jstring, jstring, jint); /* * Class: org_apache_arrow_plasma_PlasmaClientJNI * Method: disconnect * Signature: (J)V */ -JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_disconnect - (JNIEnv *, jclass, jlong); +JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_disconnect(JNIEnv*, + jclass, + jlong); /* * Class: org_apache_arrow_plasma_PlasmaClientJNI * Method: create * Signature: (J[BI[B)Ljava/nio/ByteBuffer; */ -JNIEXPORT jobject JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_create - (JNIEnv *, jclass, jlong, jbyteArray, jint, jbyteArray); +JNIEXPORT jobject JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_create( + JNIEnv*, jclass, jlong, jbyteArray, jint, jbyteArray); /* * Class: org_apache_arrow_plasma_PlasmaClientJNI * Method: hash * Signature: (J[B)[B */ -JNIEXPORT jbyteArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_hash - (JNIEnv *, jclass, jlong, jbyteArray); +JNIEXPORT jbyteArray JNICALL +Java_org_apache_arrow_plasma_PlasmaClientJNI_hash(JNIEnv*, jclass, jlong, jbyteArray); /* * Class: org_apache_arrow_plasma_PlasmaClientJNI * Method: seal * Signature: (J[B)V */ -JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_seal - (JNIEnv *, jclass, jlong, jbyteArray); +JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_seal(JNIEnv*, jclass, + jlong, + jbyteArray); /* * Class: org_apache_arrow_plasma_PlasmaClientJNI * Method: release * Signature: (J[B)V */ -JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_release - (JNIEnv *, jclass, jlong, jbyteArray); +JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_release(JNIEnv*, + jclass, jlong, + jbyteArray); /* * Class: org_apache_arrow_plasma_PlasmaClientJNI * Method: get * Signature: (J[[BI)[[Ljava/nio/ByteBuffer; */ -JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_get - (JNIEnv *, jclass, jlong, jobjectArray, jint); +JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_get( + JNIEnv*, jclass, jlong, jobjectArray, jint); /* * Class: org_apache_arrow_plasma_PlasmaClientJNI * Method: contains * Signature: (J[B)Z */ -JNIEXPORT jboolean JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_contains - (JNIEnv *, jclass, jlong, jbyteArray); +JNIEXPORT jboolean JNICALL +Java_org_apache_arrow_plasma_PlasmaClientJNI_contains(JNIEnv*, jclass, jlong, jbyteArray); /* * Class: org_apache_arrow_plasma_PlasmaClientJNI * Method: fetch * Signature: (J[[B)V */ -JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_fetch - (JNIEnv *, jclass, jlong, jobjectArray); +JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_fetch(JNIEnv*, jclass, + jlong, + jobjectArray); /* * Class: org_apache_arrow_plasma_PlasmaClientJNI * Method: wait * Signature: (J[[BII)[[B */ -JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_wait - (JNIEnv *, jclass, jlong, jobjectArray, jint, jint); +JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_wait( + JNIEnv*, jclass, jlong, jobjectArray, jint, jint); /* * Class: org_apache_arrow_plasma_PlasmaClientJNI * Method: evict * Signature: (JJ)J */ -JNIEXPORT jlong JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_evict - (JNIEnv *, jclass, jlong, jlong); +JNIEXPORT jlong JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_evict(JNIEnv*, + jclass, jlong, + jlong); #ifdef __cplusplus } diff --git a/java/plasma/test.sh b/java/plasma/test.sh index e102aa88c324..9a014fe56d60 100755 --- a/java/plasma/test.sh +++ b/java/plasma/test.sh @@ -16,6 +16,15 @@ # specific language governing permissions and limitations # under the License. ROOT_DIR=$(cd "$(dirname "${BASH_SOURCE:-$0}")"; pwd) +unamestr="$(uname)" +if [[ "$unamestr" == "Linux" ]]; then + PARALLEL=$(nproc) +elif [[ "$unamestr" == "Darwin" ]]; then + PARALLEL=$(sysctl -n hw.ncpu) +else + echo "Unrecognized platform." + exit 1 +fi pushd ../../cpp if [ ! -d "release" ]; then mkdir release @@ -35,9 +44,9 @@ pushd ../../cpp -DARROW_WITH_LZ4=off \ -DARROW_WITH_ZLIB=off \ -DARROW_WITH_ZSTD=off \ + -DARROW_PLASMA_JAVA_CLIENT=on \ .. make VERBOSE=1 -j$PARALLEL - make install popd popd From 1c327a79ca892dded06277e6f7413091a117d736 Mon Sep 17 00:00:00 2001 From: salah-man Date: Tue, 22 May 2018 09:50:44 +0800 Subject: [PATCH 05/11] add ci of plasma java client --- .travis.yml | 3 ++ ci/travis_before_script_cpp.sh | 4 +++ ci/travis_script_plasma_java_client.sh | 30 +++++++++++++++++++ .../apache/arrow/plasma/PlasmaClientTest.java | 19 ++++++++---- 4 files changed, 50 insertions(+), 6 deletions(-) create mode 100755 ci/travis_script_plasma_java_client.sh diff --git a/.travis.yml b/.travis.yml index b94a63245639..eca26c8bc2d0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -130,6 +130,8 @@ matrix: env: ARROW_TEST_GROUP=integration jdk: openjdk8 env: + - ARROW_TRAVIS_PLASMA=1 + - ARROW_TRAVIS_PLASMA_JAVA_CLIENT=1 - CC="clang-6.0" - CXX="clang++-6.0" before_script: @@ -141,6 +143,7 @@ matrix: - $TRAVIS_BUILD_DIR/ci/travis_before_script_cpp.sh script: - $TRAVIS_BUILD_DIR/ci/travis_script_integration.sh + - $TRAVIS_BUILD_DIR/ci/travis_script_plasma_java_client.sh # NodeJS - language: node_js os: linux diff --git a/ci/travis_before_script_cpp.sh b/ci/travis_before_script_cpp.sh index 379007d5bf07..541d5fd937d9 100755 --- a/ci/travis_before_script_cpp.sh +++ b/ci/travis_before_script_cpp.sh @@ -74,6 +74,10 @@ if [ $ARROW_TRAVIS_PLASMA == "1" ]; then CMAKE_COMMON_FLAGS="$CMAKE_COMMON_FLAGS -DARROW_PLASMA=ON" fi +if [ $ARROW_TRAVIS_PLASMA_JAVA_CLIENT == "1" ]; then + CMAKE_COMMON_FLAGS="$CMAKE_COMMON_FLAGS -DARROW_PLASMA_JAVA_CLIENT=ON" +fi + if [ $ARROW_TRAVIS_ORC == "1" ]; then CMAKE_COMMON_FLAGS="$CMAKE_COMMON_FLAGS -DARROW_ORC=ON" fi diff --git a/ci/travis_script_plasma_java_client.sh b/ci/travis_script_plasma_java_client.sh new file mode 100755 index 000000000000..628796dac25a --- /dev/null +++ b/ci/travis_script_plasma_java_client.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +set -e + +PLASMA_JAVA_DIR=${TRAVIS_BUILD_DIR}/java/plasma + +pushd $PLASMA_JAVA_DIR + +mvn clean install +export PLASMA_STORE=${TRAVIS_BUILD_DIR}/cpp-install/bin/plasma_store +java -cp target/test-classes:target/classes -Djava.library.path=${TRAVIS_BUILD_DIR}/cpp-build/debug/ org.apache.arrow.plasma.PlasmaClientTest + +popd diff --git a/java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java b/java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java index 3d217d30327e..c53fd2735a99 100644 --- a/java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java +++ b/java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java @@ -35,14 +35,21 @@ public class PlasmaClientTest { public PlasmaClientTest() throws Exception{ - String plasmaStorePath = System.getenv("PLASMA_STORE"); - if(plasmaStorePath == null) { - throw new Exception("Please set plasma store path in env PLASMA_STORE"); + try { + String plasmaStorePath = System.getenv("PLASMA_STORE"); + if(plasmaStorePath == null) { + throw new Exception("Please set plasma store path in env PLASMA_STORE"); + } + + this.startObjectStore(plasmaStorePath); + System.loadLibrary("plasma_java"); + pLink = new PlasmaClient(this.getStoreAddress(), "", 0); + } + catch (Throwable t) { + cleanup(); + throw t; } - this.startObjectStore(plasmaStorePath); - System.loadLibrary("plasma_java"); - pLink = new PlasmaClient(this.getStoreAddress(), "", 0); } private Process startProcess(String[] cmd) { From 2f3f129baeaecb189362489fb13c743801615acb Mon Sep 17 00:00:00 2001 From: Philipp Moritz Date: Tue, 22 May 2018 20:18:49 -0700 Subject: [PATCH 06/11] cleanups --- cpp/src/plasma/CMakeLists.txt | 9 ++++----- .../java/org_apache_arrow_plasma_PlasmaClientJNI.cc | 12 +----------- java/plasma/README.md | 5 ++--- .../org/apache/arrow/plasma/ObjectStoreLink.java | 1 + .../org/apache/arrow/plasma/PlasmaClientJNI.java | 1 + .../org/apache/arrow/plasma/PlasmaClientTest.java | 1 + java/plasma/test.sh | 3 ++- 7 files changed, 12 insertions(+), 20 deletions(-) diff --git a/cpp/src/plasma/CMakeLists.txt b/cpp/src/plasma/CMakeLists.txt index e033f6e6ee9b..fd6c7ac3be17 100644 --- a/cpp/src/plasma/CMakeLists.txt +++ b/cpp/src/plasma/CMakeLists.txt @@ -167,10 +167,10 @@ if(ARROW_PLASMA_JAVA_CLIENT) include_directories(${JAVA_INCLUDE_PATH}) include_directories(${JAVA_INCLUDE_PATH2}) if (JNI_FOUND) - message (STATUS "JNI_INCLUDE_DIRS=${JNI_INCLUDE_DIRS}") - message (STATUS "JNI_LIBRARIES=${JNI_LIBRARIES}") + message (STATUS "JNI_INCLUDE_DIRS = ${JNI_INCLUDE_DIRS}") + message (STATUS "JNI_LIBRARIES = ${JNI_LIBRARIES}") else() - message (WARNING "NOT FIND JNI") + message (WARNING "Could not find JNI") endif() add_compile_options("-I$ENV{JAVA_HOME}/include/") @@ -187,8 +187,7 @@ if(ARROW_PLASMA_JAVA_CLIENT) file(GLOB PLASMA_LIBRARY_EXT_java_SRC lib/java/*.cc lib/*.cc) add_library(plasma_java SHARED - ${PLASMA_LIBRARY_EXT_java_SRC} - ) + ${PLASMA_LIBRARY_EXT_java_SRC}) if(APPLE) target_link_libraries(plasma_java plasma_static ${PLASMA_LINK_LIBS} "-undefined dynamic_lookup" -Wl,-force_load,${FLATBUFFERS_STATIC_LIB} ${FLATBUFFERS_STATIC_LIB} ${PTHREAD_LIBRARY}) diff --git a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc index f0dddc445c8f..e5701778643f 100644 --- a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc +++ b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc @@ -14,6 +14,7 @@ // KIND, either express or implied. See the License for the // specific language governing permissions and limitations // under the License. + #include "plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.h" #include #include @@ -63,9 +64,6 @@ JNIEXPORT jlong JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_connect( plasma::PlasmaClient* client = new plasma::PlasmaClient(); ARROW_CHECK_OK(client->Connect(s_name, m_name, release_delay)); - // fprintf (stdout, "JNI plasma client init, fd = %d\n", client->get_store_fd()); - // fflush (stdout); - env->ReleaseStringUTFChars(store_socket_name, s_name); env->ReleaseStringUTFChars(manager_socket_name, m_name); return reinterpret_cast(client); @@ -75,9 +73,6 @@ JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_disconnect( JNIEnv* env, jclass cls, jlong conn) { plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; - // fprintf (stdout, "JNI plasma client disconnect, fd = %d\n", client->get_store_fd()); - // fflush (stdout); - ARROW_CHECK_OK(client->Disconnect()); delete client; return; @@ -154,17 +149,12 @@ JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_release( plasma::ObjectID oid; jbyteArray_to_object_id(env, object_id, &oid); - // fprintf (stdout, "JNI plasma client release, fd = %d\n", client->get_store_fd()); - // fflush (stdout); - ARROW_CHECK_OK(client->Release(oid)); } JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_get( JNIEnv* env, jclass cls, jlong conn, jobjectArray object_ids, jint timeout_ms) { plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; - // fprintf (stdout, "JNI plasma client get, fd = %d\n", client->get_store_fd()); - // fflush (stdout); jsize num_oids = env->GetArrayLength(object_ids); std::vector oids(num_oids); diff --git a/java/plasma/README.md b/java/plasma/README.md index 549f46a36bb7..cfcdf7f36f4a 100644 --- a/java/plasma/README.md +++ b/java/plasma/README.md @@ -17,11 +17,11 @@ under the License. --> -# Plasma client of java +# Java Plasma Client ## Setup Build Environment -install: +Install: - java 8 or later - maven 3.3 or later - the same requirement of build [Arrow C++](https://github.com/apache/arrow/tree/master/cpp) @@ -36,4 +36,3 @@ mvn clean install -Dmaven.test.skip ``` ./test.sh ``` - diff --git a/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectStoreLink.java b/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectStoreLink.java index 86edfa1ffea1..79861883be9b 100644 --- a/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectStoreLink.java +++ b/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectStoreLink.java @@ -15,6 +15,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.arrow.plasma; import java.util.List; diff --git a/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClientJNI.java b/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClientJNI.java index 17f63080c3a0..f0cf385a5767 100644 --- a/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClientJNI.java +++ b/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClientJNI.java @@ -15,6 +15,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.arrow.plasma; import java.nio.ByteBuffer; diff --git a/java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java b/java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java index c53fd2735a99..0f7dfc07b02c 100644 --- a/java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java +++ b/java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java @@ -15,6 +15,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.arrow.plasma; import java.io.IOException; diff --git a/java/plasma/test.sh b/java/plasma/test.sh index 9a014fe56d60..ef0f28be1019 100755 --- a/java/plasma/test.sh +++ b/java/plasma/test.sh @@ -15,6 +15,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. + ROOT_DIR=$(cd "$(dirname "${BASH_SOURCE:-$0}")"; pwd) unamestr="$(uname)" if [[ "$unamestr" == "Linux" ]]; then @@ -35,7 +36,7 @@ pushd ../../cpp -DCMAKE_CXX_FLAGS="-g -O3" \ -DARROW_BUILD_TESTS=off \ -DARROW_HDFS=on \ - -DARROW_BOOST_USE_SHARED=off \ + -DARROW_BOOST_USE_SHARED=on \ -DARROW_PYTHON=on \ -DARROW_PLASMA=on \ -DPLASMA_PYTHON=on \ From 52b26e74ae6c35162c32908104b07a3fbcd44755 Mon Sep 17 00:00:00 2001 From: Philipp Moritz Date: Tue, 22 May 2018 20:24:10 -0700 Subject: [PATCH 07/11] Google C++ convention --- .../plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc index e5701778643f..a42a7893cdaf 100644 --- a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc +++ b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc @@ -16,15 +16,18 @@ // under the License. #include "plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.h" + #include #include #include + #include #include #include #include #include #include + #include "plasma/client.h" const jsize LEN_OF_OBJECTID = sizeof(plasma::ObjectID) / sizeof(jbyte); From b745465ca8f96321ea59c1afd9169be40c9dd21d Mon Sep 17 00:00:00 2001 From: salah-man Date: Wed, 23 May 2018 13:10:08 +0800 Subject: [PATCH 08/11] fix the problems after the feedback --- ...org_apache_arrow_plasma_PlasmaClientJNI.cc | 56 +++++++------------ java/plasma/README.md | 3 +- .../apache/arrow/plasma/PlasmaClientTest.java | 3 + 3 files changed, 25 insertions(+), 37 deletions(-) diff --git a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc index a42a7893cdaf..b24cb7172d19 100644 --- a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc +++ b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc @@ -30,14 +30,14 @@ #include "plasma/client.h" -const jsize LEN_OF_OBJECTID = sizeof(plasma::ObjectID) / sizeof(jbyte); +const jsize OBJECT_ID_SIZE = sizeof(plasma::ObjectID) / sizeof(jbyte); inline void jbyteArray_to_object_id(JNIEnv* env, jbyteArray a, plasma::ObjectID* oid) { - env->GetByteArrayRegion(a, 0, LEN_OF_OBJECTID, reinterpret_cast(oid)); + env->GetByteArrayRegion(a, 0, OBJECT_ID_SIZE, reinterpret_cast(oid)); } inline void object_id_to_jbyteArray(JNIEnv* env, jbyteArray a, plasma::ObjectID* oid) { - env->SetByteArrayRegion(a, 0, LEN_OF_OBJECTID, reinterpret_cast(oid)); + env->SetByteArrayRegion(a, 0, OBJECT_ID_SIZE, reinterpret_cast(oid)); } class JByteArrayGetter { @@ -74,7 +74,7 @@ JNIEXPORT jlong JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_connect( JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_disconnect( JNIEnv* env, jclass cls, jlong conn) { - plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; + plasma::PlasmaClient* client = reinterpret_cast(conn); ARROW_CHECK_OK(client->Disconnect()); delete client; @@ -84,7 +84,7 @@ JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_disconnect( JNIEXPORT jobject JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_create( JNIEnv* env, jclass cls, jlong conn, jbyteArray object_id, jint size, jbyteArray metadata) { - plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; + plasma::PlasmaClient* client = reinterpret_cast(conn); plasma::ObjectID oid; jbyteArray_to_object_id(env, object_id, &oid); @@ -102,13 +102,13 @@ JNIEXPORT jobject JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_create( std::shared_ptr data; Status s = client->Create(oid, size, md, md_size, &data); if (s.IsPlasmaObjectExists()) { - jclass Exception = env->FindClass("org/ray/spi/impl/PlasmaObjectExistsException"); + jclass Exception = env->FindClass("java/lang/Exception"); env->ThrowNew(Exception, "An object with this ID already exists in the plasma store."); return NULL; } if (s.IsPlasmaStoreFull()) { - jclass Exception = env->FindClass("org/ray/spi/impl/PlasmaOutOfMemoryException"); + jclass Exception = env->FindClass("java/lang/Exception"); env->ThrowNew(Exception, "The plasma store ran out of memory and could not create this object."); return NULL; @@ -120,7 +120,7 @@ JNIEXPORT jobject JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_create( JNIEXPORT jbyteArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_hash( JNIEnv* env, jclass cls, jlong conn, jbyteArray object_id) { - plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; + plasma::PlasmaClient* client = reinterpret_cast(conn); plasma::ObjectID oid; jbyteArray_to_object_id(env, object_id, &oid); @@ -139,7 +139,7 @@ JNIEXPORT jbyteArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_hash( JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_seal( JNIEnv* env, jclass cls, jlong conn, jbyteArray object_id) { - plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; + plasma::PlasmaClient* client = reinterpret_cast(conn); plasma::ObjectID oid; jbyteArray_to_object_id(env, object_id, &oid); @@ -148,7 +148,7 @@ JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_seal( JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_release( JNIEnv* env, jclass cls, jlong conn, jbyteArray object_id) { - plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; + plasma::PlasmaClient* client = reinterpret_cast(conn); plasma::ObjectID oid; jbyteArray_to_object_id(env, object_id, &oid); @@ -157,13 +157,13 @@ JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_release( JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_get( JNIEnv* env, jclass cls, jlong conn, jobjectArray object_ids, jint timeout_ms) { - plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; + plasma::PlasmaClient* client = reinterpret_cast(conn); jsize num_oids = env->GetArrayLength(object_ids); std::vector oids(num_oids); std::vector obufs(num_oids); for (int i = 0; i < num_oids; ++i) { - jbyteArray_to_object_id(env, (jbyteArray)env->GetObjectArrayElement(object_ids, i), + jbyteArray_to_object_id(env, reinterpret_cast(env->GetObjectArrayElement(object_ids, i)), &oids[i]); } // TODO: may be blocked. consider to add the thread support @@ -200,7 +200,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_get( JNIEXPORT jboolean JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_contains( JNIEnv* env, jclass cls, jlong conn, jbyteArray object_id) { - plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; + plasma::PlasmaClient* client = reinterpret_cast(conn); plasma::ObjectID oid; jbyteArray_to_object_id(env, object_id, &oid); @@ -216,20 +216,12 @@ JNIEXPORT jboolean JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_contains JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_fetch( JNIEnv* env, jclass cls, jlong conn, jobjectArray object_ids) { - plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; + plasma::PlasmaClient* client = reinterpret_cast(conn); jsize num_oids = env->GetArrayLength(object_ids); - /* - if (!plasma_manager_is_connected(client)) { - jclass Exception = env->FindClass("java/lang/RuntimeException"); - env->ThrowNew(Exception, "Not connected to the plasma manager."); - return; - } - */ - std::vector oids(num_oids); for (int i = 0; i < num_oids; ++i) { - jbyteArray_to_object_id(env, (jbyteArray)env->GetObjectArrayElement(object_ids, i), + jbyteArray_to_object_id(env, reinterpret_cast(env->GetObjectArrayElement(object_ids, i)), &oids[i]); } @@ -241,17 +233,9 @@ JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_fetch( JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_wait( JNIEnv* env, jclass cls, jlong conn, jobjectArray object_ids, jint timeout_ms, jint num_returns) { - plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; + plasma::PlasmaClient* client = reinterpret_cast(conn); jsize num_oids = env->GetArrayLength(object_ids); - /* - if (!plasma_manager_is_connected(client)) { - jclass Exception = env->FindClass("java/lang/RuntimeException"); - env->ThrowNew(Exception, "Not connected to the plasma manager."); - return NULL; - } - */ - if (num_returns < 0) { jclass Exception = env->FindClass("java/lang/RuntimeException"); env->ThrowNew(Exception, "The argument num_returns cannot be less than zero."); @@ -267,7 +251,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_wait std::vector oreqs(num_oids); for (int i = 0; i < num_oids; ++i) { - jbyteArray_to_object_id(env, (jbyteArray)env->GetObjectArrayElement(object_ids, i), + jbyteArray_to_object_id(env, reinterpret_cast(env->GetObjectArrayElement(object_ids, i)), &oreqs[i].object_id); oreqs[i].type = plasma::PLASMA_QUERY_ANYWHERE; } @@ -275,7 +259,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_wait int num_return_objects; // TODO: may be blocked. consider to add the thread support ARROW_CHECK_OK(client->Wait(static_cast(num_oids), oreqs.data(), num_returns, - (uint64_t)timeout_ms, &num_return_objects)); + static_cast(timeout_ms), &num_return_objects)); int num_to_return = std::min(num_return_objects, num_returns); jclass clsByteArray = env->FindClass("[B"); @@ -290,7 +274,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_wait if (oreqs[i].status == plasma::ObjectStatusLocal || oreqs[i].status == plasma::ObjectStatusRemote) { - oid = env->NewByteArray(LEN_OF_OBJECTID); + oid = env->NewByteArray(OBJECT_ID_SIZE); object_id_to_jbyteArray(env, oid, &oreqs[i].object_id); env->SetObjectArrayElement(ret, num_returned, oid); num_returned++; @@ -305,7 +289,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_wait JNIEXPORT jlong JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_evict( JNIEnv* env, jclass cls, jlong conn, jlong num_bytes) { - plasma::PlasmaClient* client = (plasma::PlasmaClient*)conn; + plasma::PlasmaClient* client = reinterpret_cast(conn); int64_t evicted_bytes; ARROW_CHECK_OK(client->Evict((int64_t)num_bytes, evicted_bytes)); diff --git a/java/plasma/README.md b/java/plasma/README.md index cfcdf7f36f4a..0dcb4e21f864 100644 --- a/java/plasma/README.md +++ b/java/plasma/README.md @@ -29,7 +29,8 @@ Install: ## Build the jar of plasma client ``` -mvn clean install -Dmaven.test.skip +cd .. +mvn clean install -pl plasma -am -Dmaven.test.skip ``` ## Building and running tests diff --git a/java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java b/java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java index 0f7dfc07b02c..8d4d9ffeefda 100644 --- a/java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java +++ b/java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java @@ -143,6 +143,9 @@ public void doTest() { assert Arrays.equals(values.get(0), value1); assert Arrays.equals(values.get(1), value2); System.out.println("Plasma java client get multi-object test success."); + pLink.put(id1, value1, null); + System.out.println("Plasma java client put same object twice exception test success."); + cleanup(); System.out.println("All test success."); From 80e9580f0db1edb662fba7c1f28950c0983001b9 Mon Sep 17 00:00:00 2001 From: salah-man Date: Wed, 23 May 2018 14:39:13 +0800 Subject: [PATCH 09/11] small change for feedback --- ...org_apache_arrow_plasma_PlasmaClientJNI.cc | 50 ++++++++----------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc index b24cb7172d19..d6b2ccdd0a07 100644 --- a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc +++ b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc @@ -51,7 +51,7 @@ class JByteArrayGetter { _env = env; _a = a; - bp = _env->GetByteArrayElements(_a, NULL); + bp = _env->GetByteArrayElements(_a, nullptr); *out = bp; } @@ -61,8 +61,8 @@ class JByteArrayGetter { JNIEXPORT jlong JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_connect( JNIEnv* env, jclass cls, jstring store_socket_name, jstring manager_socket_name, jint release_delay) { - const char* s_name = env->GetStringUTFChars(store_socket_name, NULL); - const char* m_name = env->GetStringUTFChars(manager_socket_name, NULL); + const char* s_name = env->GetStringUTFChars(store_socket_name, nullptr); + const char* m_name = env->GetStringUTFChars(manager_socket_name, nullptr); plasma::PlasmaClient* client = new plasma::PlasmaClient(); ARROW_CHECK_OK(client->Connect(s_name, m_name, release_delay)); @@ -89,10 +89,10 @@ JNIEXPORT jobject JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_create( jbyteArray_to_object_id(env, object_id, &oid); // prepare metadata buffer - uint8_t* md = NULL; + uint8_t* md = nullptr; jsize md_size = 0; std::unique_ptr md_getter; - if (metadata != NULL) { + if (metadata != nullptr) { md_size = env->GetArrayLength(metadata); } if (md_size > 0) { @@ -105,13 +105,13 @@ JNIEXPORT jobject JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_create( jclass Exception = env->FindClass("java/lang/Exception"); env->ThrowNew(Exception, "An object with this ID already exists in the plasma store."); - return NULL; + return nullptr; } if (s.IsPlasmaStoreFull()) { jclass Exception = env->FindClass("java/lang/Exception"); env->ThrowNew(Exception, "The plasma store ran out of memory and could not create this object."); - return NULL; + return nullptr; } ARROW_CHECK(s.ok()); @@ -133,7 +133,7 @@ JNIEXPORT jbyteArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_hash( reinterpret_cast(digest)); return ret; } else { - return NULL; + return nullptr; } } @@ -172,11 +172,11 @@ JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_get( jclass clsByteBuffer = env->FindClass("java/nio/ByteBuffer"); jclass clsByteBufferArray = env->FindClass("[Ljava/nio/ByteBuffer;"); - jobjectArray ret = env->NewObjectArray(num_oids, clsByteBufferArray, NULL); - jobjectArray o = NULL; + jobjectArray ret = env->NewObjectArray(num_oids, clsByteBufferArray, nullptr); + jobjectArray o = nullptr; jobject dataBuf, metadataBuf; for (int i = 0; i < num_oids; ++i) { - o = env->NewObjectArray(2, clsByteBuffer, NULL); + o = env->NewObjectArray(2, clsByteBuffer, nullptr); if (obufs[i].data && obufs[i].data->size() != -1) { dataBuf = env->NewDirectByteBuffer(const_cast(obufs[i].data->data()), obufs[i].data->size()); @@ -184,11 +184,11 @@ JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_get( metadataBuf = env->NewDirectByteBuffer( const_cast(obufs[i].metadata->data()), obufs[i].metadata->size()); } else { - metadataBuf = NULL; + metadataBuf = nullptr; } } else { - dataBuf = NULL; - metadataBuf = NULL; + dataBuf = nullptr; + metadataBuf = nullptr; } env->SetObjectArrayElement(o, 0, dataBuf); @@ -207,11 +207,7 @@ JNIEXPORT jboolean JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_contains bool has_object; ARROW_CHECK_OK(client->Contains(oid, &has_object)); - if (has_object) { - return true; - } else { - return false; - } + return has_object; } JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_fetch( @@ -239,13 +235,13 @@ JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_wait if (num_returns < 0) { jclass Exception = env->FindClass("java/lang/RuntimeException"); env->ThrowNew(Exception, "The argument num_returns cannot be less than zero."); - return NULL; + return nullptr; } if (num_returns > num_oids) { jclass Exception = env->FindClass("java/lang/RuntimeException"); env->ThrowNew(Exception, "The argument num_returns cannot be greater than len(object_ids)."); - return NULL; + return nullptr; } std::vector oreqs(num_oids); @@ -263,10 +259,10 @@ JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_wait int num_to_return = std::min(num_return_objects, num_returns); jclass clsByteArray = env->FindClass("[B"); - jobjectArray ret = env->NewObjectArray(num_to_return, clsByteArray, NULL); + jobjectArray ret = env->NewObjectArray(num_to_return, clsByteArray, nullptr); int num_returned = 0; - jbyteArray oid = NULL; + jbyteArray oid = nullptr; for (int i = 0; i < num_oids; ++i) { if (num_returned >= num_to_return) { break; @@ -278,9 +274,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_wait object_id_to_jbyteArray(env, oid, &oreqs[i].object_id); env->SetObjectArrayElement(ret, num_returned, oid); num_returned++; - } else { - // ARROW_CHECK(oreqs[i].status == ObjectStatus_Nonexistent); - } + } } ARROW_CHECK(num_returned == num_to_return); @@ -292,7 +286,7 @@ JNIEXPORT jlong JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_evict( plasma::PlasmaClient* client = reinterpret_cast(conn); int64_t evicted_bytes; - ARROW_CHECK_OK(client->Evict((int64_t)num_bytes, evicted_bytes)); + ARROW_CHECK_OK(client->Evict(static_cast(num_bytes), evicted_bytes)); - return (jlong)evicted_bytes; + return static_cast(evicted_bytes); } From b7f5e94fc74eea1026af609d65baf400e92cdb71 Mon Sep 17 00:00:00 2001 From: salah-man Date: Wed, 23 May 2018 14:52:49 +0800 Subject: [PATCH 10/11] fix ci lint error --- ...org_apache_arrow_plasma_PlasmaClientJNI.cc | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc index d6b2ccdd0a07..8ddfeebe698b 100644 --- a/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc +++ b/cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc @@ -30,7 +30,7 @@ #include "plasma/client.h" -const jsize OBJECT_ID_SIZE = sizeof(plasma::ObjectID) / sizeof(jbyte); +constexpr jsize OBJECT_ID_SIZE = sizeof(plasma::ObjectID) / sizeof(jbyte); inline void jbyteArray_to_object_id(JNIEnv* env, jbyteArray a, plasma::ObjectID* oid) { env->GetByteArrayRegion(a, 0, OBJECT_ID_SIZE, reinterpret_cast(oid)); @@ -163,8 +163,9 @@ JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_get( std::vector oids(num_oids); std::vector obufs(num_oids); for (int i = 0; i < num_oids; ++i) { - jbyteArray_to_object_id(env, reinterpret_cast(env->GetObjectArrayElement(object_ids, i)), - &oids[i]); + jbyteArray_to_object_id( + env, reinterpret_cast(env->GetObjectArrayElement(object_ids, i)), + &oids[i]); } // TODO: may be blocked. consider to add the thread support ARROW_CHECK_OK(client->Get(oids.data(), num_oids, timeout_ms, obufs.data())); @@ -217,8 +218,9 @@ JNIEXPORT void JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_fetch( std::vector oids(num_oids); for (int i = 0; i < num_oids; ++i) { - jbyteArray_to_object_id(env, reinterpret_cast(env->GetObjectArrayElement(object_ids, i)), - &oids[i]); + jbyteArray_to_object_id( + env, reinterpret_cast(env->GetObjectArrayElement(object_ids, i)), + &oids[i]); } ARROW_CHECK_OK(client->Fetch(static_cast(num_oids), oids.data())); @@ -247,8 +249,9 @@ JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_wait std::vector oreqs(num_oids); for (int i = 0; i < num_oids; ++i) { - jbyteArray_to_object_id(env, reinterpret_cast(env->GetObjectArrayElement(object_ids, i)), - &oreqs[i].object_id); + jbyteArray_to_object_id( + env, reinterpret_cast(env->GetObjectArrayElement(object_ids, i)), + &oreqs[i].object_id); oreqs[i].type = plasma::PLASMA_QUERY_ANYWHERE; } @@ -274,7 +277,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_apache_arrow_plasma_PlasmaClientJNI_wait object_id_to_jbyteArray(env, oid, &oreqs[i].object_id); env->SetObjectArrayElement(ret, num_returned, oid); num_returned++; - } + } } ARROW_CHECK(num_returned == num_to_return); From be067e836ece8743401c506b6c04aa9ad58d4be8 Mon Sep 17 00:00:00 2001 From: salah-man Date: Thu, 24 May 2018 10:03:01 +0800 Subject: [PATCH 11/11] add the test case of contains and hash of plasma java client --- .../java/org/apache/arrow/plasma/ObjectStoreLink.java | 7 +++++++ .../java/org/apache/arrow/plasma/PlasmaClient.java | 1 + .../org/apache/arrow/plasma/PlasmaClientTest.java | 11 ++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectStoreLink.java b/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectStoreLink.java index 79861883be9b..e84427302640 100644 --- a/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectStoreLink.java +++ b/java/plasma/src/main/java/org/apache/arrow/plasma/ObjectStoreLink.java @@ -109,4 +109,11 @@ default void fetch(byte[] objectId) { * @param objectId The object ID used to release the reference of the object. */ void release(byte[] objectId); + + /** + * Check if the object is present and has been sealed in the PlasmaStore. + * + * @param objectId used to identify an object. + */ + boolean contains(byte[] objectId); } diff --git a/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClient.java b/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClient.java index d0fdc9b8aec3..33e4e7d9b63f 100644 --- a/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClient.java +++ b/java/plasma/src/main/java/org/apache/arrow/plasma/PlasmaClient.java @@ -141,6 +141,7 @@ public void release(byte[] objectId) { * * @param objectId used to identify an object. */ + @Override public boolean contains(byte[] objectId) { return PlasmaClientJNI.contains(conn, objectId); } diff --git a/java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java b/java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java index 8d4d9ffeefda..6b67fc8c9308 100644 --- a/java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java +++ b/java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java @@ -145,7 +145,16 @@ public void doTest() { System.out.println("Plasma java client get multi-object test success."); pLink.put(id1, value1, null); System.out.println("Plasma java client put same object twice exception test success."); - + byte[] id1Hash = pLink.hash(id1); + assert id1Hash != null; + System.out.println("Plasma java client hash test success."); + boolean exsit = pLink.contains(id2); + assert exsit; + byte[] id3 = new byte[20]; + Arrays.fill(id3, (byte)3); + boolean notExsit = pLink.contains(id3); + assert !notExsit; + System.out.println("Plasma java client contains test success."); cleanup(); System.out.println("All test success.");