risingwave_connector/source/cdc/
jni_source.rs

1// Copyright 2025 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use anyhow::Context;
16use risingwave_common::global_jvm::JVM;
17use risingwave_jni_core::jvm_runtime::execute_with_jni_env;
18use risingwave_jni_core::{call_method, call_static_method};
19
20pub fn commit_cdc_offset(source_id: u64, encoded_offset: String) -> anyhow::Result<()> {
21    let jvm = JVM.get_or_init()?;
22    execute_with_jni_env(jvm, |env| {
23        // get source handler by source id
24        let handler = call_static_method!(
25            env,
26            {com.risingwave.connector.source.core.JniDbzSourceRegistry},
27            {com.risingwave.connector.source.core.JniDbzSourceHandler getSourceHandler(long sourceId)},
28            source_id
29        )?;
30
31        let offset_str = env.new_string(&encoded_offset).with_context(|| {
32            format!("Failed to create jni string from source offset: {encoded_offset}.")
33        })?;
34        // commit offset to upstream
35        call_method!(env, handler, {void commitOffset(String)}, &offset_str).with_context(
36            || format!("Failed to commit offset to upstream for source: {source_id}."),
37        )?;
38        Ok(())
39    })
40}