1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "simple_task_environment_wrapper_impl.h"
18
19 #include "example_iterator_wrapper_impl.h"
20 #include "fcp/protos/federatedcompute/common.pb.h"
21 #include "more_jni_util.h"
22 #include "nativehelper/scoped_local_ref.h"
23
24 namespace fcp {
25 namespace client {
26 namespace engine {
27 namespace jni {
28
29 using ::fcp::jni::ParseProtoFromJByteArray;
30 using ::fcp::jni::ScopedJniEnv;
31 using ::fcp::jni::SerializeProtoToJByteArray;
32 using ::google::internal::federated::plan::ExampleSelector;
33
SimpleTaskEnvironmentWrapperImpl(JavaVM * jvm,jobject simple_task_env)34 SimpleTaskEnvironmentWrapperImpl::SimpleTaskEnvironmentWrapperImpl(
35 JavaVM *jvm, jobject simple_task_env)
36 : jvm_(jvm) {
37 ScopedJniEnv scoped_env(jvm_);
38 JNIEnv *env = scoped_env.env();
39 jthis_ = env->NewGlobalRef(simple_task_env);
40 FCP_CHECK(jthis_ != nullptr);
41
42 ScopedLocalRef<jclass> simple_task_env_class(
43 env, env->GetObjectClass(simple_task_env));
44 FCP_CHECK(simple_task_env_class.get() != nullptr);
45
46 training_conditions_satisfied_id_ = MoreJniUtil::GetMethodIdOrAbort(
47 env, simple_task_env_class.get(),
48 SimpleTaskEnvironmentImplClassDesc::kTrainingConditionsSatisfied);
49 create_example_iterator_id_ = MoreJniUtil::GetMethodIdOrAbort(
50 env, simple_task_env_class.get(),
51 SimpleTaskEnvironmentImplClassDesc::kCreateExampleIterator);
52 }
53
~SimpleTaskEnvironmentWrapperImpl()54 SimpleTaskEnvironmentWrapperImpl::~SimpleTaskEnvironmentWrapperImpl() {
55 ScopedJniEnv scoped_env(jvm_);
56 JNIEnv *env = scoped_env.env();
57 env->DeleteGlobalRef(jthis_);
58 }
59
60 absl::StatusOr<std::unique_ptr<ExampleIterator>>
CreateExampleIterator(const ExampleSelector & example_selector,const SelectorContext & selector_context)61 SimpleTaskEnvironmentWrapperImpl::CreateExampleIterator(
62 const ExampleSelector &example_selector,
63 const SelectorContext &selector_context) {
64 ScopedJniEnv scoped_env(jvm_);
65 JNIEnv *env = scoped_env.env();
66 ScopedLocalRef<jbyteArray> serialized_example_selector(
67 env, SerializeProtoToJByteArray(env, example_selector));
68 ScopedLocalRef<jbyteArray> serialized_selector_context(
69 env, SerializeProtoToJByteArray(env, selector_context));
70
71 jobject java_example_iterator = env->CallObjectMethod(
72 jthis_, create_example_iterator_id_, serialized_example_selector.get(),
73 serialized_selector_context.get());
74 FCP_RETURN_IF_ERROR(
75 MoreJniUtil::GetExceptionStatus(env, "call JavaCreateExampleIterator"));
76 FCP_CHECK(java_example_iterator != nullptr);
77 // TODO(b/301323421): check if abseil can be used in JNI layer.
78 return absl::StatusOr<std::unique_ptr<ExampleIterator>>(
79 std::make_unique<ExampleIteratorWrapperImpl>(jvm_,
80 java_example_iterator));
81 }
82
83 absl::StatusOr<std::unique_ptr<ExampleIterator>>
CreateExampleIterator(const ExampleSelector & example_selector)84 SimpleTaskEnvironmentWrapperImpl::CreateExampleIterator(
85 const ExampleSelector &example_selector) {
86 return CreateExampleIterator(example_selector, SelectorContext());
87 }
88
89 // Don't call to java implementation because training runs in isolated process
90 // which doesn't have file system access.
GetBaseDir()91 std::string SimpleTaskEnvironmentWrapperImpl::GetBaseDir() { return ""; }
92
93 // Don't call to java implementation because training runs in isolated process
94 // which doesn't have file system access.
GetCacheDir()95 std::string SimpleTaskEnvironmentWrapperImpl::GetCacheDir() { return ""; }
96
TrainingConditionsSatisfied()97 bool SimpleTaskEnvironmentWrapperImpl::TrainingConditionsSatisfied() {
98 ScopedJniEnv scoped_env(jvm_);
99 JNIEnv *env = scoped_env.env();
100 bool training_conditions_satisfied =
101 env->CallBooleanMethod(jthis_, training_conditions_satisfied_id_);
102 FCP_CHECK(!MoreJniUtil::CheckForJniException(env));
103 return training_conditions_satisfied;
104 }
105
106 } // namespace jni
107 } // namespace engine
108 } // namespace client
109 } // namespace fcp