From 2565262759c315a473b96e88a2441f390f966e93 Mon Sep 17 00:00:00 2001 From: Julien Enoch Date: Thu, 17 Dec 2020 15:12:03 +0100 Subject: [PATCH] First commit --- .github/workflows/ci.yml | 75 +++++ .gitignore | 19 ++ CONTRIBUTING.md | 51 ++++ CONTRIBUTORS.md | 13 + Cargo.toml | 51 ++++ Jenkinsfile | 254 ++++++++++++++++ LICENSE | 459 +++++++++++++++++++++++++++++ NOTICE.md | 40 +++ README.md | 116 ++++++++ src/lib.rs | 616 +++++++++++++++++++++++++++++++++++++++ 10 files changed, 1694 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 .gitignore create mode 100644 CONTRIBUTING.md create mode 100644 CONTRIBUTORS.md create mode 100644 Cargo.toml create mode 100644 Jenkinsfile create mode 100644 LICENSE create mode 100644 NOTICE.md create mode 100644 README.md create mode 100644 src/lib.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..5840b78 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,75 @@ +# +# Copyright (c) 2017, 2020 ADLINK Technology Inc. +# +# This program and the accompanying materials are made available under the +# terms of the Eclipse Public License 2.0 which is available at +# http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +# which is available at https://www.apache.org/licenses/LICENSE-2.0. +# +# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +# +# Contributors: +# ADLINK zenoh team, +# +name: CI + +on: + push: + branches: + - '**' + pull_request: + branches: + - '**' + +jobs: + build: + + name: Build on ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macOS-latest, windows-latest] + + steps: + - uses: actions/checkout@v2 + + # For Windows, install llvm and use a workaround to resolve link error with C:\msys64\mingw64\bin\libclang.dll + # Credits: https://github.com/rust-rocksdb/rust-rocksdb/pull/484 + - name: Remove msys64 + if: runner.os == 'Windows' + run: Remove-Item -LiteralPath "C:\msys64\" -Force -Recurse + - name: Install dependencies + if: runner.os == 'Windows' + run: choco install llvm -y + + - name: Install latest nightly + uses: actions-rs/toolchain@v1 + with: + toolchain: nightly + override: true + components: rustfmt, clippy + + - name: Code format check + uses: actions-rs/cargo@v1 + with: + command: fmt + args: -- --check + + - name: Clippy + uses: actions-rs/cargo@v1 + with: + command: clippy + args: --all --examples -- -D warnings + + - name: Build + uses: actions-rs/cargo@v1 + with: + command: build + args: --verbose --all-targets + + - name: Run tests + uses: actions-rs/cargo@v1 + with: + command: test + args: --verbose diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0a3b4b0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +# Generated by Cargo +# will have compiled files and executables +**/target + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# CLion project directory +.idea + +# Emacs temps +*~ + +# MacOS Related +.DS_Store \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..1dd0b82 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,51 @@ +# Contributing to Eclipse zenoh + +Thanks for your interest in this project. + +## Project description + +Eclipse zenoh provides is a stack designed to + 1. minimize network overhead, + 2. support extremely constrained devices, + 3. supports devices with low duty-cycle by allowing the negotiation of data exchange modes and schedules, + 4. provide a rich set of abstraction for distributing, querying and storing data along the entire system, and + 5. provide extremely low latency and high throughput. + +* https://projects.eclipse.org/projects/iot.zenoh + +## Developer resources + +Information regarding source code management, builds, coding standards, and +more. + +* https://projects.eclipse.org/projects/iot.zenoh/developer + +The project maintains the following source code repositories + +* https://github.com/eclipse-zenoh + +## Eclipse Contributor Agreement + +Before your contribution can be accepted by the project team contributors must +electronically sign the Eclipse Contributor Agreement (ECA). + +* http://www.eclipse.org/legal/ECA.php + +Commits that are provided by non-committers must have a Signed-off-by field in +the footer indicating that the author is aware of the terms by which the +contribution has been provided to the project. The non-committer must +additionally have an Eclipse Foundation account and must have a signed Eclipse +Contributor Agreement (ECA) on file. + +For more information, please see the Eclipse Committer Handbook: +https://www.eclipse.org/projects/handbook/#resources-commit + +## Contact + +Contact the project developers via the project's "dev" list. + +* https://accounts.eclipse.org/mailing-list/zenoh-dev + +Or via the Gitter channel. + +* https://gitter.im/atolab/zenoh diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md new file mode 100644 index 0000000..ba1e1a1 --- /dev/null +++ b/CONTRIBUTORS.md @@ -0,0 +1,13 @@ +# Contributors to Eclipse zenoh + +These are the contributors to Eclipse zenoh (the initial contributors and the contributors listed in the Git log). + + +| GitHub username | Name | +| --------------- | -----------------------------| +| kydos | Angelo Corsaro (ADLINK) | +| JEnoch | Julien Enoch (ADLINK) | +| OlivierHecart | Olivier Hécart (ADLINK) | +| gabrik | Gabriele Baldoni (ADLINK) | +| Mallets | Luca Cominardi (ADLINK) | +| IvanPaez | Ivan Paez (ADLINK) | diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..1e2a924 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,51 @@ +# +# Copyright (c) 2017, 2020 ADLINK Technology Inc. +# +# This program and the accompanying materials are made available under the +# terms of the Eclipse Public License 2.0 which is available at +# http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +# which is available at https://www.apache.org/licenses/LICENSE-2.0. +# +# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +# +# Contributors: +# ADLINK zenoh team, +# +[package] +name = "zenoh_backend_rocksdb" +version = "0.5.0-beta.6" +authors = ["kydos ", + "Julien Enoch ", + "Olivier Hécart ", + "Luca Cominardi "] +edition = "2018" + +[lib] +name = "zbackend_rocksdb" +crate-type = ["cdylib"] + +[dependencies] +# TODO: set zenoh version when 0.5.0-beta.6 is released +zenoh_backend_traits = { git = "https://github.com/eclipse-zenoh/zenoh.git" } +zenoh = { git = "https://github.com/eclipse-zenoh/zenoh.git" } +zenoh-util = { git = "https://github.com/eclipse-zenoh/zenoh.git" } +uhlc = "0.2" +async-trait = "0.1" +lazy_static = "1.4.0" +env_logger = "0.7.1" +log = "0.4" +git-version = "0.3.4" +rocksdb = "0.15.0" + + +[dependencies.async-std] +version = "=1.6.5" +features = ["unstable"] + +[package.metadata.deb] +name = "zenoh-backend-rocksdb" +maintainer = "zenoh-dev@eclipse.org" +copyright = "2017, 2020 ADLINK Technology Inc." +section = "net" +license-file = ["LICENSE", "0"] +depends = "zenoh-storages" \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..fad7516 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,254 @@ +pipeline { + agent { label 'MacMini' } + options { skipDefaultCheckout() } + parameters { + gitParameter(name: 'GIT_TAG', + type: 'PT_BRANCH_TAG', + description: 'The Git tag to checkout. If not specified "master" will be checkout.', + defaultValue: 'master') + booleanParam(name: 'BUILD_MACOSX', + description: 'Build macosx target.', + defaultValue: true) + booleanParam(name: 'BUILD_DOCKER', + description: 'Build for zenoh in Docker (Alpine x86_64-unknown-linux-musl target).', + defaultValue: true) + booleanParam(name: 'BUILD_LINUX64', + description: 'Build x86_64-unknown-linux-gnu target.', + defaultValue: true) + booleanParam(name: 'BUILD_LINUX32', + description: 'Build i686-unknown-linux-gnu target.', + defaultValue: true) + booleanParam(name: 'BUILD_AARCH64', + description: 'Build aarch64-unknown-linux-gnu target.', + defaultValue: true) + booleanParam(name: 'BUILD_WIN64', + description: 'Build x86_64-pc-windows-gnu target.', + defaultValue: true) + booleanParam(name: 'BUILD_WIN32', + description: 'Build i686-pc-windows-gnu target.', + defaultValue: true) + booleanParam(name: 'PUBLISH_ECLIPSE_DOWNLOAD', + description: 'Publish the resulting artifacts to Eclipse download.', + defaultValue: false) + } + environment { + LABEL = get_label() + DOWNLOAD_DIR="/home/data/httpd/download.eclipse.org/zenoh/zenoh-backend-rocksdb/${LABEL}" + MACOSX_DEPLOYMENT_TARGET=10.9 + } + + stages { + stage('Checkout Git TAG') { + steps { + deleteDir() + checkout([$class: 'GitSCM', + branches: [[name: "${params.GIT_TAG}"]], + doGenerateSubmoduleConfigurations: false, + extensions: [], + gitTool: 'Default', + submoduleCfg: [], + userRemoteConfigs: [[url: 'https://github.com/eclipse-zenoh/zenoh-backend-rocksdb.git']] + ]) + } + } + stage('Update Rust env') { + steps { + sh ''' + env + echo "Building zenoh-backend-rocksdb-${LABEL}" + rustup update + ''' + } + } + + stage('MacOS build') { + when { expression { return params.BUILD_MACOSX }} + steps { + sh ''' + cargo build --release + cargo test --release + tar -czvf zenoh-backend-rocksdb-${LABEL}-macosx${MACOSX_DEPLOYMENT_TARGET}-x86-64.tgz --strip-components 2 target/release/*.dylib + ''' + } + } + + stage('x86_64-unknown-linux-musl build') { + when { expression { return params.BUILD_DOCKER }} + steps { + sh ''' + docker run --init --rm -v $(pwd):/workdir -w /workdir adlinktech/zenoh-dev-x86_64-unknown-linux-musl cargo build --release + tar -czvf zenoh-backend-rocksdb-${LABEL}-x86_64-unknown-linux-musl.tgz --strip-components 3 target/x86_64-unknown-linux-musl/release/*.so + ''' + } + } + + stage('x86_64-unknown-linux-gnu build') { + when { expression { return params.BUILD_LINUX64 }} + steps { + sh ''' + docker run --init --rm -v $(pwd):/workdir -w /workdir adlinktech/zenoh-dev-manylinux2010-x86_64-gnu \ + cargo build --release + if [[ ${GIT_TAG} != origin/* ]]; then + docker run --init --rm -v $(pwd):/workdir -w /workdir adlinktech/zenoh-dev-manylinux2010-x86_64-gnu \ + cargo deb + fi + tar -czvf zenoh-backend-rocksdb-${LABEL}-x86_64-unknown-linux-gnu.tgz --strip-components 3 target/x86_64-unknown-linux-gnu/release/*.so + ''' + } + } + + stage('i686-unknown-linux-gnu build') { + when { expression { return params.BUILD_LINUX32 }} + steps { + sh ''' + docker run --init --rm -v $(pwd):/workdir -w /workdir adlinktech/zenoh-dev-manylinux2010-i686-gnu \ + cargo build --release + if [[ ${GIT_TAG} != origin/* ]]; then + docker run --init --rm -v $(pwd):/workdir -w /workdir adlinktech/zenoh-dev-manylinux2010-i686-gnu \ + cargo deb + fi + tar -czvf zenoh-backend-rocksdb-${LABEL}-i686-unknown-linux-gnu.tgz --strip-components 3 target/i686-unknown-linux-gnu/release/*.so + ''' + } + } + + stage('aarch64-unknown-linux-gnu build') { + when { expression { return params.BUILD_AARCH64 }} + steps { + sh ''' + docker run --init --rm -v $(pwd):/workdir -w /workdir adlinktech/zenoh-dev-manylinux2014-aarch64-gnu \ + cargo build --release + if [[ ${GIT_TAG} != origin/* ]]; then + docker run --init --rm -v $(pwd):/workdir -w /workdir adlinktech/zenoh-dev-manylinux2014-aarch64-gnu \ + cargo deb + fi + tar -czvf zenoh-backend-rocksdb-${LABEL}-aarch64-unknown-linux-gnu.tgz --strip-components 3 target/aarch64-unknown-linux-gnu/release/*.so + ''' + } + } + + stage('x86_64-pc-windows-gnu build') { + when { expression { return params.BUILD_WIN64 }} + steps { + sh ''' + cargo build --release --bins --lib --examples --target=x86_64-pc-windows-gnu + zip zenoh-backend-rocksdb-${LABEL}-x86_64-pc-windows-gnu.zip --junk-paths target/x86_64-pc-windows-gnu/release/*.dll + ''' + } + } + + stage('i686-pc-windows-gnu build') { + when { expression { return params.BUILD_WIN32 }} + steps { + sh ''' + cargo build --release --bins --lib --examples --target=i686-pc-windows-gnu + zip zenoh-backend-rocksdb-${LABEL}-i686-pc-windows-gnu.zip --junk-paths target/i686-pc-windows-gnu/release/*.dll + ''' + } + } + + stage('Prepare directory on download.eclipse.org') { + when { expression { return params.PUBLISH_ECLIPSE_DOWNLOAD }} + steps { + // Note: remove existing dir on download.eclipse.org only if it's for a branch + // (e.g. master that is rebuilt periodically from different commits) + sshagent ( ['projects-storage.eclipse.org-bot-ssh']) { + sh ''' + if [[ ${GIT_TAG} == origin/* ]]; then + ssh genie.zenoh@projects-storage.eclipse.org rm -fr ${DOWNLOAD_DIR} + ssh genie.zenoh@projects-storage.eclipse.org mkdir -p ${DOWNLOAD_DIR} + COMMIT_ID=`git log -n1 --format="%h"` + echo "https://github.com/eclipse-zenoh/zenoh-backend-rocksdb/tree/${COMMIT_ID}" > _git_commit_${COMMIT_ID}.txt + scp _git_commit_${COMMIT_ID}.txt genie.zenoh@projects-storage.eclipse.org:${DOWNLOAD_DIR} + else + ssh genie.zenoh@projects-storage.eclipse.org mkdir -p ${DOWNLOAD_DIR} + fi + ''' + } + } + } + + stage('Publish zenoh-macosx to download.eclipse.org') { + when { expression { return params.PUBLISH_ECLIPSE_DOWNLOAD && params.BUILD_MACOSX }} + steps { + sshagent ( ['projects-storage.eclipse.org-bot-ssh']) { + sh ''' + ssh genie.zenoh@projects-storage.eclipse.org mkdir -p ${DOWNLOAD_DIR} + scp zenoh-backend-rocksdb-${LABEL}-*macosx*.tgz genie.zenoh@projects-storage.eclipse.org:${DOWNLOAD_DIR}/ + ''' + } + } + } + + stage('Publish zenoh-x86_64-unknown-linux-musl to download.eclipse.org') { + when { expression { return params.PUBLISH_ECLIPSE_DOWNLOAD && params.BUILD_DOCKER }} + steps { + sshagent ( ['projects-storage.eclipse.org-bot-ssh']) { + sh ''' + ssh genie.zenoh@projects-storage.eclipse.org mkdir -p ${DOWNLOAD_DIR} + scp zenoh-backend-rocksdb-${LABEL}-x86_64-unknown-linux-musl.tgz genie.zenoh@projects-storage.eclipse.org:${DOWNLOAD_DIR}/ + ''' + } + } + } + + stage('Publish zenoh-x86_64-unknown-linux-gnu to download.eclipse.org') { + when { expression { return params.PUBLISH_ECLIPSE_DOWNLOAD && params.BUILD_LINUX64 }} + steps { + sshagent ( ['projects-storage.eclipse.org-bot-ssh']) { + sh ''' + ssh genie.zenoh@projects-storage.eclipse.org mkdir -p ${DOWNLOAD_DIR} + scp zenoh-backend-rocksdb-${LABEL}-x86_64-unknown-linux-gnu.tgz genie.zenoh@projects-storage.eclipse.org:${DOWNLOAD_DIR}/ + if [[ ${GIT_TAG} != origin/* ]]; then + scp target/x86_64-unknown-linux-gnu/debian/*.deb genie.zenoh@projects-storage.eclipse.org:${DOWNLOAD_DIR}/ + fi + ''' + } + } + } + + stage('Publish zenoh-i686-unknown-linux-gnu to download.eclipse.org') { + when { expression { return params.PUBLISH_ECLIPSE_DOWNLOAD && params.BUILD_LINUX32 }} + steps { + sshagent ( ['projects-storage.eclipse.org-bot-ssh']) { + sh ''' + ssh genie.zenoh@projects-storage.eclipse.org mkdir -p ${DOWNLOAD_DIR} + scp zenoh-backend-rocksdb-${LABEL}-i686-unknown-linux-gnu.tgz genie.zenoh@projects-storage.eclipse.org:${DOWNLOAD_DIR}/ + if [[ ${GIT_TAG} != origin/* ]]; then + scp target/i686-unknown-linux-gnu/debian/*.deb genie.zenoh@projects-storage.eclipse.org:${DOWNLOAD_DIR}/ + fi + ''' + } + } + } + + stage('Publish zenoh-x86_64-pc-windows-gnu to download.eclipse.org') { + when { expression { return params.PUBLISH_ECLIPSE_DOWNLOAD && params.BUILD_WIN64 }} + steps { + sshagent ( ['projects-storage.eclipse.org-bot-ssh']) { + sh ''' + ssh genie.zenoh@projects-storage.eclipse.org mkdir -p ${DOWNLOAD_DIR} + scp zenoh-backend-rocksdb-${LABEL}-x86_64-pc-windows-gnu.zip genie.zenoh@projects-storage.eclipse.org:${DOWNLOAD_DIR}/ + ''' + } + } + } + + stage('Publish zenoh-i686-pc-windows-gnu to download.eclipse.org') { + when { expression { return params.PUBLISH_ECLIPSE_DOWNLOAD && params.BUILD_WIN32 }} + steps { + sshagent ( ['projects-storage.eclipse.org-bot-ssh']) { + sh ''' + ssh genie.zenoh@projects-storage.eclipse.org mkdir -p ${DOWNLOAD_DIR} + scp zenoh-backend-rocksdb-${LABEL}-i686-pc-windows-gnu.zip genie.zenoh@projects-storage.eclipse.org:${DOWNLOAD_DIR}/ + ''' + } + } + } + + } +} + +def get_label() { + return env.GIT_TAG.startsWith('origin/') ? env.GIT_TAG.minus('origin/') : env.GIT_TAG +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8c3fbf6 --- /dev/null +++ b/LICENSE @@ -0,0 +1,459 @@ +apache-2.0 +epl-2.0 + + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + OR + +Eclipse Public License - v 2.0 + + THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE + PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION + OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. + +1. DEFINITIONS + +"Contribution" means: + + a) in the case of the initial Contributor, the initial content + Distributed under this Agreement, and + + b) in the case of each subsequent Contributor: + i) changes to the Program, and + ii) additions to the Program; + where such changes and/or additions to the Program originate from + and are Distributed by that particular Contributor. A Contribution + "originates" from a Contributor if it was added to the Program by + such Contributor itself or anyone acting on such Contributor's behalf. + Contributions do not include changes or additions to the Program that + are not Modified Works. + +"Contributor" means any person or entity that Distributes the Program. + +"Licensed Patents" mean patent claims licensable by a Contributor which +are necessarily infringed by the use or sale of its Contribution alone +or when combined with the Program. + +"Program" means the Contributions Distributed in accordance with this +Agreement. + +"Recipient" means anyone who receives the Program under this Agreement +or any Secondary License (as applicable), including Contributors. + +"Derivative Works" shall mean any work, whether in Source Code or other +form, that is based on (or derived from) the Program and for which the +editorial revisions, annotations, elaborations, or other modifications +represent, as a whole, an original work of authorship. + +"Modified Works" shall mean any work in Source Code or other form that +results from an addition to, deletion from, or modification of the +contents of the Program, including, for purposes of clarity any new file +in Source Code form that contains any contents of the Program. Modified +Works shall not include works that contain only declarations, +interfaces, types, classes, structures, or files of the Program solely +in each case in order to link to, bind by name, or subclass the Program +or Modified Works thereof. + +"Distribute" means the acts of a) distributing or b) making available +in any manner that enables the transfer of a copy. + +"Source Code" means the form of a Program preferred for making +modifications, including but not limited to software source code, +documentation source, and configuration files. + +"Secondary License" means either the GNU General Public License, +Version 2.0, or any later versions of that license, including any +exceptions or additional permissions as identified by the initial +Contributor. + +2. GRANT OF RIGHTS + + a) Subject to the terms of this Agreement, each Contributor hereby + grants Recipient a non-exclusive, worldwide, royalty-free copyright + license to reproduce, prepare Derivative Works of, publicly display, + publicly perform, Distribute and sublicense the Contribution of such + Contributor, if any, and such Derivative Works. + + b) Subject to the terms of this Agreement, each Contributor hereby + grants Recipient a non-exclusive, worldwide, royalty-free patent + license under Licensed Patents to make, use, sell, offer to sell, + import and otherwise transfer the Contribution of such Contributor, + if any, in Source Code or other form. This patent license shall + apply to the combination of the Contribution and the Program if, at + the time the Contribution is added by the Contributor, such addition + of the Contribution causes such combination to be covered by the + Licensed Patents. The patent license shall not apply to any other + combinations which include the Contribution. No hardware per se is + licensed hereunder. + + c) Recipient understands that although each Contributor grants the + licenses to its Contributions set forth herein, no assurances are + provided by any Contributor that the Program does not infringe the + patent or other intellectual property rights of any other entity. + Each Contributor disclaims any liability to Recipient for claims + brought by any other entity based on infringement of intellectual + property rights or otherwise. As a condition to exercising the + rights and licenses granted hereunder, each Recipient hereby + assumes sole responsibility to secure any other intellectual + property rights needed, if any. For example, if a third party + patent license is required to allow Recipient to Distribute the + Program, it is Recipient's responsibility to acquire that license + before distributing the Program. + + d) Each Contributor represents that to its knowledge it has + sufficient copyright rights in its Contribution, if any, to grant + the copyright license set forth in this Agreement. + + e) Notwithstanding the terms of any Secondary License, no + Contributor makes additional grants to any Recipient (other than + those set forth in this Agreement) as a result of such Recipient's + receipt of the Program under the terms of a Secondary License + (if permitted under the terms of Section 3). + +3. REQUIREMENTS + +3.1 If a Contributor Distributes the Program in any form, then: + + a) the Program must also be made available as Source Code, in + accordance with section 3.2, and the Contributor must accompany + the Program with a statement that the Source Code for the Program + is available under this Agreement, and informs Recipients how to + obtain it in a reasonable manner on or through a medium customarily + used for software exchange; and + + b) the Contributor may Distribute the Program under a license + different than this Agreement, provided that such license: + i) effectively disclaims on behalf of all other Contributors all + warranties and conditions, express and implied, including + warranties or conditions of title and non-infringement, and + implied warranties or conditions of merchantability and fitness + for a particular purpose; + + ii) effectively excludes on behalf of all other Contributors all + liability for damages, including direct, indirect, special, + incidental and consequential damages, such as lost profits; + + iii) does not attempt to limit or alter the recipients' rights + in the Source Code under section 3.2; and + + iv) requires any subsequent distribution of the Program by any + party to be under a license that satisfies the requirements + of this section 3. + +3.2 When the Program is Distributed as Source Code: + + a) it must be made available under this Agreement, or if the + Program (i) is combined with other material in a separate file or + files made available under a Secondary License, and (ii) the initial + Contributor attached to the Source Code the notice described in + Exhibit A of this Agreement, then the Program may be made available + under the terms of such Secondary Licenses, and + + b) a copy of this Agreement must be included with each copy of + the Program. + +3.3 Contributors may not remove or alter any copyright, patent, +trademark, attribution notices, disclaimers of warranty, or limitations +of liability ("notices") contained within the Program from any copy of +the Program which they Distribute, provided that Contributors may add +their own appropriate notices. + +4. COMMERCIAL DISTRIBUTION + +Commercial distributors of software may accept certain responsibilities +with respect to end users, business partners and the like. While this +license is intended to facilitate the commercial use of the Program, +the Contributor who includes the Program in a commercial product +offering should do so in a manner which does not create potential +liability for other Contributors. Therefore, if a Contributor includes +the Program in a commercial product offering, such Contributor +("Commercial Contributor") hereby agrees to defend and indemnify every +other Contributor ("Indemnified Contributor") against any losses, +damages and costs (collectively "Losses") arising from claims, lawsuits +and other legal actions brought by a third party against the Indemnified +Contributor to the extent caused by the acts or omissions of such +Commercial Contributor in connection with its distribution of the Program +in a commercial product offering. The obligations in this section do not +apply to any claims or Losses relating to any actual or alleged +intellectual property infringement. In order to qualify, an Indemnified +Contributor must: a) promptly notify the Commercial Contributor in +writing of such claim, and b) allow the Commercial Contributor to control, +and cooperate with the Commercial Contributor in, the defense and any +related settlement negotiations. The Indemnified Contributor may +participate in any such claim at its own expense. + +For example, a Contributor might include the Program in a commercial +product offering, Product X. That Contributor is then a Commercial +Contributor. If that Commercial Contributor then makes performance +claims, or offers warranties related to Product X, those performance +claims and warranties are such Commercial Contributor's responsibility +alone. Under this section, the Commercial Contributor would have to +defend claims against the other Contributors related to those performance +claims and warranties, and if a court requires any other Contributor to +pay any damages as a result, the Commercial Contributor must pay +those damages. + +5. NO WARRANTY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, AND TO THE EXTENT +PERMITTED BY APPLICABLE LAW, THE PROGRAM IS PROVIDED ON AN "AS IS" +BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR +IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF +TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR +PURPOSE. Each Recipient is solely responsible for determining the +appropriateness of using and distributing the Program and assumes all +risks associated with its exercise of rights under this Agreement, +including but not limited to the risks and costs of program errors, +compliance with applicable laws, damage to or loss of data, programs +or equipment, and unavailability or interruption of operations. + +6. DISCLAIMER OF LIABILITY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, AND TO THE EXTENT +PERMITTED BY APPLICABLE LAW, NEITHER RECIPIENT NOR ANY CONTRIBUTORS +SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST +PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE +EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + +7. GENERAL + +If any provision of this Agreement is invalid or unenforceable under +applicable law, it shall not affect the validity or enforceability of +the remainder of the terms of this Agreement, and without further +action by the parties hereto, such provision shall be reformed to the +minimum extent necessary to make such provision valid and enforceable. + +If Recipient institutes patent litigation against any entity +(including a cross-claim or counterclaim in a lawsuit) alleging that the +Program itself (excluding combinations of the Program with other software +or hardware) infringes such Recipient's patent(s), then such Recipient's +rights granted under Section 2(b) shall terminate as of the date such +litigation is filed. + +All Recipient's rights under this Agreement shall terminate if it +fails to comply with any of the material terms or conditions of this +Agreement and does not cure such failure in a reasonable period of +time after becoming aware of such noncompliance. If all Recipient's +rights under this Agreement terminate, Recipient agrees to cease use +and distribution of the Program as soon as reasonably practicable. +However, Recipient's obligations under this Agreement and any licenses +granted by Recipient relating to the Program shall continue and survive. + +Everyone is permitted to copy and distribute copies of this Agreement, +but in order to avoid inconsistency the Agreement is copyrighted and +may only be modified in the following manner. The Agreement Steward +reserves the right to publish new versions (including revisions) of +this Agreement from time to time. No one other than the Agreement +Steward has the right to modify this Agreement. The Eclipse Foundation +is the initial Agreement Steward. The Eclipse Foundation may assign the +responsibility to serve as the Agreement Steward to a suitable separate +entity. Each new version of the Agreement will be given a distinguishing +version number. The Program (including Contributions) may always be +Distributed subject to the version of the Agreement under which it was +received. In addition, after a new version of the Agreement is published, +Contributor may elect to Distribute the Program (including its +Contributions) under the new version. + +Except as expressly stated in Sections 2(a) and 2(b) above, Recipient +receives no rights or licenses to the intellectual property of any +Contributor under this Agreement, whether expressly, by implication, +estoppel or otherwise. All rights in the Program not expressly granted +under this Agreement are reserved. Nothing in this Agreement is intended +to be enforceable by any entity that is not a Contributor or Recipient. +No third-party beneficiary rights are created under this Agreement. + +Exhibit A - Form of Secondary Licenses Notice + +"This Source Code may also be made available under the following +Secondary Licenses when the conditions for such availability set forth +in the Eclipse Public License, v. 2.0 are satisfied: {name license(s), +version(s), and exceptions or additional permissions here}." + + Simply including a copy of this Agreement, including this Exhibit A + is not sufficient to license the Source Code under Secondary Licenses. + + If it is not possible or desirable to put the notice in a particular + file, then You may include the notice in a location (such as a LICENSE + file in a relevant directory) where a recipient would be likely to + look for such a notice. + + You may add additional accurate notices of copyright ownership. + diff --git a/NOTICE.md b/NOTICE.md new file mode 100644 index 0000000..7aff175 --- /dev/null +++ b/NOTICE.md @@ -0,0 +1,40 @@ +# Notices for Eclipse zenoh + +This content is produced and maintained by the Eclipse zenoh project. + + * Project home: https://projects.eclipse.org/projects/iot.zenoh + +## Trademarks + +Eclipse zenoh is trademark of the Eclipse Foundation. +Eclipse, and the Eclipse Logo are registered trademarks of the Eclipse Foundation. + +## Copyright + +All content is the property of the respective authors or their employers. +For more information regarding authorship of content, please consult the +listed source code repository logs. + +## Declared Project Licenses + +This program and the accompanying materials are made available under the +terms of the Eclipse Public License 2.0 which is available at +http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +which is available at https://www.apache.org/licenses/LICENSE-2.0. + +SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 + +## Source Code + +The project maintains the following source code repositories: + + * https://github.com/eclipse-zenoh/zenoh.git + * https://github.com/eclipse-zenoh/zenoh-c.git + * https://github.com/eclipse-zenoh/zenoh-java.git + * https://github.com/eclipse-zenoh/zenoh-go.git + * https://github.com/eclipse-zenoh/zenoh-python.git + +## Third-party Content + + *To be completed...* + diff --git a/README.md b/README.md new file mode 100644 index 0000000..d7beb27 --- /dev/null +++ b/README.md @@ -0,0 +1,116 @@ + + +[![CI](https://github.com/eclipse-zenoh/zenoh-backend-rocksdb/workflows/CI/badge.svg)](https://github.com/eclipse-zenoh/zenoh-backend-rocksdb/actions?query=workflow%3A%22CI%22) +[![Gitter](https://badges.gitter.im/atolab/zenoh.svg)](https://gitter.im/atolab/zenoh?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) +[![License](https://img.shields.io/badge/License-EPL%202.0-blue)](https://choosealicense.com/licenses/epl-2.0/) +[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) + +# RocksDB backend for Eclipse zenoh + +In zenoh a backend is a storage technology (such as DBMS, time-series database, file system...) alowing to store the +keys/values publications made via zenoh and return them on queries. +See the [zenoh documentation](http://zenoh.io/docs/manual/backends/) for more details. + +This backend relies on [RocksDB](https://rocksdb.org/) to implement the storages. +Its library name (without OS specific prefix and extension) that zenoh will rely on to find it and load it is **`zbackend_rocksdb`**. + +:point_right: **Download:** https://download.eclipse.org/zenoh/zenoh-backend-rocksdb/ + +------------------------------- +## **Examples of usage** + +Prerequisites: + - You have a zenoh router running, and the `zbackend_rocksdb` library file is available in `~/.zenoh/lib`. + - Declare the `ZBACKEND_ROCKSDB_ROOT` environment variable to the directory where you want the RocksDB databases + to be stored. If you don't declare it, the `~/.zenoh/zbackend_rocksdb` directory will be used. + +Using `curl` on the zenoh router to add backend and storages: +```bash +# Add a backend that will have all its storages storing data RocksDB databases under the ${ZBACKEND_ROCKSDB_ROOT} directory. +curl -X PUT -H 'content-type:application/properties' http://localhost:8000/@/router/local/plugin/storages/backend/rocksdb + +# Add a storage on /demo/example/** storing data in a ${ZBACKEND_ROCKSDB_ROOT}/test RocksDB database. +# We use 'path_prefix=/demo/example' thus a zenoh path "/demo/example/a/b" will be stored using "a/b" as key in RocksDB +curl -X PUT -H 'content-type:application/properties' -d "path_expr=/demo/example/**;path_prefix=/demo/example;dir=test;create_db" http://localhost:8000/@/router/local/plugin/storages/backend/rocksdb/storage/example + +# Put values that will be stored in the RocksDB database +curl -X PUT -d "TEST-1" http://localhost:8000/demo/example/test-1 +curl -X PUT -d "B" http://localhost:8000/demo/example/a/b + +# Retrive the values +curl http://localhost:8000/demo/example/** +``` + +------------------------------- +## **Properties for Backend creation** + +- **`"lib"`** (optional) : the path to the backend library file. If not speficied, the Backend identifier in admin space must be `rocksdb` (i.e. zenoh will automatically search for a library named `zbackend_rocksdb`). + +------------------------------- +## **Properties for Storage creation** + +- **`"path_expr"`** (**required**) : the Storage's [Path Expression](../abstractions#path-expression) + +- **`"path_prefix"`** (optional) : a prefix of the `"path_expr"` that will be stripped from each path to store. + _Example: with `"path_expr"="/demo/example/**"` and `"path_prefix"="/demo/example/"` the path `"/demo/example/foo/bar"` will be stored as key: `"foo/bar"`. But replying to a get on `"/demo/**"`, the key `"foo/bar"` will be transformed back to the original path (`"/demo/example/foo/bar"`)._ + +- **`"dir"`** (**required**) : The name of directory where the RocksDB database is stored. + The absolute path will be `${ZBACKEND_ROCKSDB_ROOT}/`. + +- **`"create_db"`** (optional) : create the RocksDB database if not already existing. Not set by default. + *(the value doesn't matter, only the property existence is checked)* + +- **`"read_only"`** (optional) : the storage will only answer to GET queries. It will not accept any PUT or DELETE message, and won't put anything in RocksDB database. Not set by default. *(the value doesn't matter, only the property existence is checked)* + +- **`"on_closure"`** (optional) : the strategy to use when the Storage is removed. There are 2 options: + - *unset*: the database remains untouched (this is the default behaviour) + - `"destroy_db"`: the database is destroyed (i.e. removed) + +------------------------------- +## **Behaviour of the backend** + +### Mapping to RocksDB database +Each **storage** will map to a RocksDB database stored in directory: `${ZBACKEND_ROCKSDB_ROOT}/`, where: + * `${ZBACKEND_ROCKSDB_ROOT}` is an environment variable that could be specified before zenoh router startup. + If this variable is not specified `${ZENOH_HOME}/zbackend_rocksdb` will be used + (where the default value of `${ZENOH_HOME}` is `~/.zenoh`). + * `` is the `"dir"` property specified at storage creation. +Each zenoh **path/value** put into the storage will map to 2 **key/values** in the database: + * For both, the key is the zenoh path, stripped from the `"path_prefix"` property specified at storage creation. + * In the `"default"` [Column Family](https://github.com/facebook/rocksdb/wiki/Column-Families) the key is + put with the zenoh encoded value as a value. + * In the `"data_info"` [Column Family](https://github.com/facebook/rocksdb/wiki/Column-Families) the key is + put with a value encoded as following: + - bytes[0]: the "deleted" flag as a u8 (1=true, 0=false) + - bytes[1..9]: the zenoh encoding flag as a u64 + - bytes[9..17]: the timestamp's time as a u64 + - bytes[17..]: the timestamp's ID + +### Behaviour on deletion +On deletion of a path, the corresponding key is removed from the `"default"` Column Family. An entry with the +"deletion" flag set to true and the deletion timestamp is inserted in the `"data-info"` Column Family +(to avoid re-insertion of points with an older timestamp in case of un-ordered messages). +At regular interval, a task cleans-up the `"data-info"` Column Family from entries with old timestamps and +the "deletion" flag set to true + +### Behaviour on GET +On GET operations: + * if the selector is a path (i.e. not containing any `'*'`): the value and its encoding and timestamp + for the corresponding key are directly retrieved from the 2 Column Families using `get` RocksDB operation. + * otherwise: the storage searches for matching keys, leveraging RocksDB's [Prefix Seek](https://github.com/facebook/rocksdb/wiki/Prefix-Seek) if possible to minimize the number of entries to check. + + +------------------------------- +## How to build it + +Install [Cargo and Rust](https://doc.rust-lang.org/cargo/getting-started/installation.html). Currently, zenoh requires a nightly version of Rust, type the following to install it after you have followed the previous instructions: + +```bash +$ rustup default nightly +``` + +And then build the backend with: + +```bash +$ cargo build --release --all-targets +``` diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..9b0946c --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,616 @@ +// +// Copyright (c) 2017, 2020 ADLINK Technology Inc. +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ADLINK zenoh team, +// +#![feature(async_closure)] + +use async_std::sync::{Arc, Mutex}; +use async_trait::async_trait; +use log::{debug, error, trace, warn}; +use rocksdb::{ColumnFamilyDescriptor, IteratorMode, Options, WriteBatch, DB}; +use std::convert::TryFrom; +use std::io::prelude::*; +use std::path::PathBuf; +use std::time::{Duration, SystemTime, UNIX_EPOCH}; +use uhlc::NTP64; +use zenoh::net::utils::resource_name; +use zenoh::net::{DataInfo, RBuf, Sample, ZInt}; +use zenoh::{ + Change, ChangeKind, Properties, Selector, Timestamp, Value, ZError, ZErrorKind, ZResult, +}; +use zenoh_backend_traits::*; +use zenoh_util::collections::{Timed, TimedEvent, Timer}; +use zenoh_util::{zenoh_home, zerror, zerror2}; + +/// The environement variable used to configure the root of all storages managed by this RocksdbBackend. +pub const SCOPE_ENV_VAR: &str = "ZBACKEND_ROCKSDB_ROOT"; + +/// The default root (whithin zenoh's home directory) if the ZBACKEND_ROCKSDB_ROOT environment variable is not specified. +pub const DEFAULT_ROOT_DIR: &str = "zbackend_rocksdb"; + +// Properies used by the Backend +// - None + +// Properies used by the Storage +pub const PROP_STORAGE_DIR: &str = "dir"; +pub const PROP_STORAGE_CREATE_DB: &str = "create_db"; +pub const PROP_STORAGE_READ_ONLY: &str = "read_only"; +pub const PROP_STORAGE_ON_CLOSURE: &str = "on_closure"; + +// Column family names +const CF_PAYLOADS: &str = rocksdb::DEFAULT_COLUMN_FAMILY_NAME; +const CF_DATA_INFO: &str = "data_info"; + +// maximum size of serialized data-info: deleted (u8) + encoding (u64) + timestamp (u64 + ID at max size) +const MAX_VAL_LEN: usize = 1 + 8 + 8 + uhlc::ID::MAX_SIZE; +// minimum size of serialized data-info: deleted (u8) + encoding (u64) + timestamp (u64 + ID at 1 byte) +const MIN_VAL_LEN: usize = 1 + 8 + 8 + uhlc::ID::MAX_SIZE; + +lazy_static::lazy_static! { + static ref GC_PERIOD: Duration = Duration::new(5, 0); + static ref MIN_DELAY_BEFORE_REMOVAL: NTP64 = NTP64::from(Duration::new(5, 0)); +} + +const VERSION: &str = git_version::git_version!(prefix = "v", cargo_prefix = "v"); + +pub(crate) enum OnClosure { + DestroyDB, + DoNothing, +} + +#[no_mangle] +pub fn create_backend(_unused: &Properties) -> ZResult> { + // For some reasons env_logger is sometime not active in a loaded library. + // Try to activate it here, ignoring failures. + let _ = env_logger::try_init(); + debug!("RocksDB backend {}", VERSION); + + let root = if let Some(dir) = std::env::var_os(SCOPE_ENV_VAR) { + PathBuf::from(dir) + } else { + let mut dir = PathBuf::from(zenoh_home()); + dir.push(DEFAULT_ROOT_DIR); + dir + }; + let mut properties = Properties::default(); + properties.insert("root".into(), root.to_string_lossy().into()); + properties.insert("version".into(), VERSION.into()); + + let admin_status = zenoh::utils::properties_to_json_value(&properties); + Ok(Box::new(RocksdbBackend { admin_status, root })) +} + +pub struct RocksdbBackend { + admin_status: Value, + root: PathBuf, +} + +#[async_trait] +impl Backend for RocksdbBackend { + async fn get_admin_status(&self) -> Value { + self.admin_status.clone() + } + + async fn create_storage(&mut self, props: Properties) -> ZResult> { + let path_expr = props.get(PROP_STORAGE_PATH_EXPR).unwrap(); + let path_prefix = props + .get(PROP_STORAGE_PATH_PREFIX) + .ok_or_else(|| { + zerror2!(ZErrorKind::Other { + descr: format!( + r#"Missing required property for File System Storage: "{}""#, + PROP_STORAGE_PATH_PREFIX + ) + }) + })? + .clone(); + if !path_expr.starts_with(&path_prefix) { + return zerror!(ZErrorKind::Other { + descr: format!( + r#"The specified "{}={}" is not a prefix of "{}={}""#, + PROP_STORAGE_PATH_PREFIX, path_prefix, PROP_STORAGE_PATH_EXPR, path_expr + ) + }); + } + + let read_only = props.contains_key(PROP_STORAGE_READ_ONLY); + + let on_closure = match props.get(PROP_STORAGE_ON_CLOSURE) { + Some(s) => { + if s == "destroy_db" { + OnClosure::DestroyDB + } else { + return zerror!(ZErrorKind::Other { + descr: format!("Unsupported value for 'on_closure' property: {}", s) + }); + } + } + None => OnClosure::DoNothing, + }; + + let db_path = props + .get(PROP_STORAGE_DIR) + .map(|dir| { + // prepend db_path with self.root + let mut db_path = self.root.clone(); + for segment in dir.split(std::path::MAIN_SEPARATOR) { + if !segment.is_empty() { + db_path.push(segment); + } + } + db_path + }) + .ok_or_else(|| { + zerror2!(ZErrorKind::Other { + descr: format!( + r#"Missing required property for File System Storage: "{}""#, + PROP_STORAGE_DIR + ) + }) + })?; + + let mut opts = Options::default(); + if props.contains_key(PROP_STORAGE_CREATE_DB) { + opts.create_if_missing(true); + } + opts.create_missing_column_families(true); + let db = if read_only { + DB::open_cf_for_read_only(&opts, &db_path, &[CF_PAYLOADS, CF_DATA_INFO], true) + } else { + let cf_payloads = ColumnFamilyDescriptor::new(CF_PAYLOADS, opts.clone()); + let cf_data_info = ColumnFamilyDescriptor::new(CF_DATA_INFO, opts.clone()); + DB::open_cf_descriptors(&opts, &db_path, vec![cf_payloads, cf_data_info]) + } + .map_err(|e| { + zerror2!(ZErrorKind::Other { + descr: format!( + "Failed to open data-info database from {:?}: {}", + db_path, e + ) + }) + })?; + let db = Arc::new(Mutex::new(db)); + + let timer = if read_only { + None + } else { + // start periodic GC event + let t = Timer::new(); + let gc = TimedEvent::periodic(*GC_PERIOD, GarbageCollectionEvent { db: db.clone() }); + let _ = t.add(gc).await; + Some(t) + }; + + let admin_status = zenoh::utils::properties_to_json_value(&props); + Ok(Box::new(RocksdbStorage { + admin_status, + path_prefix, + on_closure, + read_only, + db, + timer, + })) + } + + fn incoming_data_interceptor(&self) -> Option> { + None + } + + fn outgoing_data_interceptor(&self) -> Option> { + None + } +} + +struct RocksdbStorage { + admin_status: Value, + path_prefix: String, + on_closure: OnClosure, + read_only: bool, + // Note: rocksdb isn't thread-safe. See https://github.com/rust-rocksdb/rust-rocksdb/issues/404 + db: Arc>, + // Note: Timer is kept to not be dropped and keep the GC periodic event running + #[allow(dead_code)] + timer: Option, +} + +#[async_trait] +impl Storage for RocksdbStorage { + async fn get_admin_status(&self) -> Value { + self.admin_status.clone() + } + + // When receiving a Sample (i.e. on PUT or DELETE operations) + async fn on_sample(&mut self, sample: Sample) -> ZResult<()> { + // transform the Sample into a Change to get kind, encoding and timestamp (not decoding => RawValue) + let change = Change::from_sample(sample, false)?; + + // the key in rocksdb is the path stripped from "path_prefix" + let key = change + .path + .as_str() + .strip_prefix(&self.path_prefix) + .ok_or_else(|| { + zerror2!(ZErrorKind::Other { + descr: format!( + "Received a Sample not starting with path_prefix '{}'", + self.path_prefix + ) + }) + })?; + + // Get lock on DB + let db = self.db.lock().await; + + // get latest timestamp for this key (if already exists in db) + // and drop incoming sample if older + if let Some(old_ts) = get_timestamp(&db, key)? { + if change.timestamp < old_ts { + debug!("{} on {} dropped: out-of-date", change.kind, change.path); + return Ok(()); + } + } + + // Store or delete the sample depending the ChangeKind + match change.kind { + ChangeKind::PUT => { + if !self.read_only { + // check that there is a value for this PUT sample + if change.value.is_none() { + return zerror!(ZErrorKind::Other { + descr: format!( + "Received a PUT Sample without value for {}", + change.path + ) + }); + } + + // get the encoding and buffer from the value (RawValue => direct access to inner RBuf) + let (encoding, payload) = change.value.unwrap().encode(); + + // put payload and data_info in DB + put_kv(&db, key, payload, encoding, change.timestamp) + } else { + warn!("Received PUT for read-only DB on {:?} - ignored", key); + Ok(()) + } + } + ChangeKind::DELETE => { + if !self.read_only { + // delete file + delete_kv(&db, key, change.timestamp) + } else { + warn!("Received DELETE for read-only DB on {:?} - ignored", key); + Ok(()) + } + } + ChangeKind::PATCH => { + warn!("Received PATCH for {}: not yet supported", change.path); + Ok(()) + } + } + } + + // When receiving a Query (i.e. on GET operations) + async fn on_query(&mut self, query: Query) -> ZResult<()> { + // get the query's Selector + let selector = Selector::try_from(&query)?; + + // get the list of sub-path expressions that will match the same stored keys than + // the selector, if those keys had the path_prefix. + let path_exprs = utils::get_sub_path_exprs(selector.path_expr.as_str(), &self.path_prefix); + debug!( + "Query on {} with path_prefix={} => sub_path_exprs = {:?}", + selector.path_expr, self.path_prefix, path_exprs + ); + + // Get lock on DB + let db = self.db.lock().await; + + // Get all matching keys/values + let mut kvs: Vec<(String, Vec, ZInt, Timestamp)> = Vec::with_capacity(path_exprs.len()); + for path_expr in path_exprs { + if path_expr.contains('*') { + find_matching_kv(&db, &path_expr, &mut kvs); + } else { + // path_expr correspond to 1 key. Get it. + match get_kv(&db, &path_expr) { + Ok(Some((payload, encoding, timestamp))) => { + kvs.push((path_expr.into(), payload, encoding, timestamp)) + } + Ok(None) => (), // key not found, do nothing + Err(e) => warn!( + "Replying to query on {} : failed get key {} : {}", + query.res_name(), + path_expr, + e + ), + } + } + } + + // Release lock on DB + drop(db); + + // Send replies + for (key, payload, encoding, timestamp) in kvs { + // append path_prefix to the key + let path = concat_str(&self.path_prefix, &key); + let data_info = DataInfo { + source_id: None, + source_sn: None, + first_router_id: None, + first_router_sn: None, + timestamp: Some(timestamp), + kind: None, + encoding: Some(encoding), + }; + query + .reply(Sample { + res_name: path, + payload: payload.into(), + data_info: Some(data_info), + }) + .await; + } + + Ok(()) + } +} + +impl Drop for RocksdbStorage { + fn drop(&mut self) { + async_std::task::block_on(async move { + // Get lock on DB + let db = self.db.lock().await; + let path = db.path(); + + match self.on_closure { + OnClosure::DestroyDB => { + debug!( + "Close Rocksdb storage, destroying database {}", + path.display() + ); + if let Err(err) = DB::destroy(&Options::default(), &path) { + error!( + "Failed to destroy Rocksdb database '{}' : {}", + path.display(), + err + ); + } + } + OnClosure::DoNothing => { + debug!( + "Close Rocksdb storage, keeping database {} as it is", + path.display() + ); + } + } + }); + } +} + +fn put_kv(db: &DB, key: &str, content: RBuf, encoding: ZInt, timestamp: Timestamp) -> ZResult<()> { + trace!("Put key {} in {:?}", key, db); + let data_info = encode_data_info(encoding, timestamp, false)?; + + // Write content and encoding+timestamp in different Column Families + let mut batch = WriteBatch::default(); + batch.put_cf(db.cf_handle(CF_PAYLOADS).unwrap(), key, content.get_vec()); + batch.put_cf(db.cf_handle(CF_DATA_INFO).unwrap(), key, data_info); + db.write(batch).map_err(rocksdb_err_to_zerr) +} + +fn delete_kv(db: &DB, key: &str, timestamp: Timestamp) -> ZResult<()> { + trace!("Delete key {} from {:?}", key, db); + let data_info = encode_data_info(zenoh::net::encoding::NONE, timestamp, true)?; + + // Delete key from CF_PAYLOADS Column Family + // Put deletion timestamp into CF_DATA_INFO Column Family (to avoid re-insertion of older value) + let mut batch = WriteBatch::default(); + batch.delete_cf(db.cf_handle(CF_PAYLOADS).unwrap(), key); + batch.put_cf(db.cf_handle(CF_DATA_INFO).unwrap(), key, data_info); + db.write(batch).map_err(rocksdb_err_to_zerr) +} + +fn get_kv(db: &DB, key: &str) -> ZResult, ZInt, Timestamp)>> { + trace!("Get key {} from {:?}", key, db); + // TODO: use MultiGet when available (see https://github.com/rust-rocksdb/rust-rocksdb/issues/485) + match ( + db.get_cf(db.cf_handle(CF_PAYLOADS).unwrap(), key), + db.get_cf(db.cf_handle(CF_DATA_INFO).unwrap(), key), + ) { + (Ok(Some(payload)), Ok(Some(info))) => { + let (encoding, timestamp, deleted) = decode_data_info(&info)?; + if deleted { + Ok(None) + } else { + Ok(Some((payload, encoding, timestamp))) + } + } + (Ok(Some(payload)), Ok(None)) => { + // Only the payload is present in DB! + // Possibly legacy data. Consider as encoding as NONE and create timestamp from now() + let timestamp = zenoh::utils::new_reception_timestamp(); + Ok(Some((payload, zenoh::net::encoding::NONE, timestamp))) + } + (Ok(None), _) => Ok(None), + (Err(err), _) | (_, Err(err)) => Err(rocksdb_err_to_zerr(err)), + } +} + +fn find_matching_kv( + db: &DB, + path_expr: &str, + results: &mut Vec<(String, Vec, ZInt, Timestamp)>, +) { + // Use Rocksdb prefix seek for faster search + let prefix = &path_expr[..path_expr.find('*').unwrap()]; + trace!( + "Find keys matching {} from {:?} using prefix seek with '{}'", + path_expr, + db, + prefix + ); + + // Iterate over DATA_INFO Column Family to avoid loading payloads possibly for nothing if not matching + for (key, buf) in db.prefix_iterator_cf(db.cf_handle(CF_DATA_INFO).unwrap(), prefix) { + if let Ok(false) = decode_deleted_flag(&buf) { + let key_str = String::from_utf8_lossy(&key); + if resource_name::intersect(&key_str, path_expr) { + match db.get_cf(db.cf_handle(CF_PAYLOADS).unwrap(), &key) { + Ok(Some(payload)) => { + if let Ok((encoding, timestamp, _)) = decode_data_info(&buf) { + results.push((key_str.into_owned(), payload, encoding, timestamp)) + } else { + warn!( + "Replying to query on {} : failed to decode data_info for key {}", + path_expr, key_str + ) + } + } + Ok(None) => (), // data_info exists, but not payload: key was probably deleted + Err(err) => warn!( + "Replying to query on {} : failed get key {} : {}", + path_expr, key_str, err + ), + } + } + } + } +} + +fn get_timestamp(db: &DB, key: &str) -> ZResult> { + match db.get_pinned_cf(db.cf_handle(CF_DATA_INFO).unwrap(), key) { + Ok(Some(pin_val)) => decode_timestamp(pin_val.as_ref()).map(Some), + Ok(None) => { + trace!("timestamp for {:?} not found", key); + Ok(None) + } + Err(e) => zerror!(ZErrorKind::Other { + descr: format!("Failed to get data-info for {:?}: {}", key, e) + }), + } +} + +fn encode_data_info(encoding: ZInt, timestamp: Timestamp, deleted: bool) -> ZResult> { + // Encoding format is the following: + // - bytes[0]: the "deleted" boolean as u8 + // - bytes[1..9]: the encoding as u64 + // - bytes[9..17]: the timestamp's time as u64 + // - bytes[17..]: the timestamp's ID + let mut buf: Vec = Vec::with_capacity(MAX_VAL_LEN); + buf.push(if deleted { 1u8 } else { 0u8 }); + buf.write_all(&encoding.to_ne_bytes()) + .and_then(|()| buf.write_all(×tamp.get_time().as_u64().to_ne_bytes())) + .and_then(|()| buf.write_all(timestamp.get_id().as_slice())) + .map_err(|e| { + zerror2!(ZErrorKind::Other { + descr: format!("Failed to encode data-info for: {}", e) + }) + })?; + Ok(buf) +} + +fn decode_data_info(buf: &[u8]) -> ZResult<(ZInt, Timestamp, bool)> { + if buf.len() < MIN_VAL_LEN { + return zerror!(ZErrorKind::Other { + descr: "Failed to decode data-info (buffer too small)".to_string() + }); + } + let deleted = buf[0] > 0; + let mut encoding_bytes = [0u8; 8]; + encoding_bytes.clone_from_slice(&buf[1..9]); + let encoding = ZInt::from_ne_bytes(encoding_bytes); + let mut time_bytes = [0u8; 8]; + time_bytes.clone_from_slice(&buf[9..17]); + let time = u64::from_ne_bytes(time_bytes); + let id = uhlc::ID::try_from(&buf[17..]).unwrap(); + let timestamp = Timestamp::new(NTP64(time), id); + Ok((encoding, timestamp, deleted)) +} + +// decode the timestamp only +fn decode_timestamp(buf: &[u8]) -> ZResult { + if buf.len() < MIN_VAL_LEN { + return zerror!(ZErrorKind::Other { + descr: "Failed to decode data-info (buffer too small)".to_string() + }); + } + let mut time_bytes = [0u8; 8]; + time_bytes.clone_from_slice(&buf[9..17]); + let time = u64::from_ne_bytes(time_bytes); + let id = uhlc::ID::try_from(&buf[17..]).unwrap(); + Ok(Timestamp::new(NTP64(time), id)) +} + +// decode the deleted flag only +fn decode_deleted_flag(buf: &[u8]) -> ZResult { + if buf.len() < MIN_VAL_LEN { + return zerror!(ZErrorKind::Other { + descr: "Failed to decode data-info (buffer too small)".to_string() + }); + } + Ok(buf[0] > 0) +} + +fn rocksdb_err_to_zerr(err: rocksdb::Error) -> ZError { + zerror2!(ZErrorKind::Other { + descr: format!("Rocksdb error: {}", err.into_string()) + }) +} + +pub(crate) fn concat_str, S2: AsRef>(s1: S1, s2: S2) -> String { + let mut result = String::with_capacity(s1.as_ref().len() + s2.as_ref().len()); + result.push_str(s1.as_ref()); + result.push_str(s2.as_ref()); + result +} + +// Periodic event cleaning-up data info for no-longer existing files +struct GarbageCollectionEvent { + db: Arc>, +} + +#[async_trait] +impl Timed for GarbageCollectionEvent { + async fn run(&mut self) { + trace!("Start garbage collection of obsolete data-infos"); + let time_limit = NTP64::from(SystemTime::now().duration_since(UNIX_EPOCH).unwrap()) + - *MIN_DELAY_BEFORE_REMOVAL; + let db = self.db.lock().await; + let cf_handle = db.cf_handle(CF_DATA_INFO).unwrap(); + let mut batch = WriteBatch::default(); + let mut count = 0; + + // prepare a batch with all keys to delete + for (key, buf) in db.iterator_cf(cf_handle, IteratorMode::Start) { + if let Ok(true) = decode_deleted_flag(&buf) { + if let Ok(timestamp) = decode_timestamp(&buf) { + if timestamp.get_time() < &time_limit { + batch.delete_cf(cf_handle, key); + count += 1; + } + } + } + } + + // write batch + if count > 0 { + trace!("Garbage collect {} old data-info", count); + if let Err(err) = db.write(batch).map_err(rocksdb_err_to_zerr) { + warn!("Failed to clean-up old data-info : {}", err); + } + } + + trace!("End garbage collection of obsolete data-infos"); + } +}