• Home
Name Date Size #Lines LOC

..--

conf/12-May-2024-111108

figures/12-May-2024-

services/distributeddataservice/12-May-2024-73,41648,818

test/12-May-2024-539366

.gitattributesD12-May-2024631 1615

BUILD.gnD12-May-20241.3 KiB3733

LICENSED12-May-20249.9 KiB176150

OAT.xmlD12-May-20244 KiB7016

README.mdD12-May-20249.1 KiB12872

README_zh.mdD12-May-20248.8 KiB12569

bundle.jsonD12-May-20243.2 KiB103102

datamgr_service.gniD12-May-20241,015 2825

hisysevent.yamlD12-May-20244.8 KiB10758

README.md

1# distributeddatamgr\_distributeddatamgr<a name="EN-US_TOPIC_0000001124232845"></a>
2
3-   [Introduction](#section11660541593)
4-   [Directory Structure](#section161941989596)
5-   [Constraints](#section119744591305)
6-   [Description](#section1312121216216)
7-   [Repositories Involved](#section1371113476307)
8
9## Introduction<a name="section11660541593"></a>
10
11Distributed Data Service \(DDS\) provides the capability to store data in the databases of different devices. DDS isolates data based on a triplet of the account, app, and database. DDS synchronizes data between trusted devices to provide users with consistent data access experience on different devices.
12
13DDS manages data of OpenHarmony in a distributed manner. It consists of the following parts:
14
15-   **Service interfaces**
16
17    DDS provides APIs for other modules to create databases, access data, and subscribe to data. Supporting the KV data model and common data types, the APIs are highly compatible and easy to use, and can be released.
18
19-   **Service component**
20
21    The service component manages metadata, permissions, backup and restoration, and multiple users. It also initializes the storage and synchronization components and communication adaptation layer of the distributed database.
22
23-   **Storage component**
24
25    The storage component provides data access, data reduction, transactions, snapshots, data combination, and conflict resolution.
26
27-   **Synchronization component**
28
29    The synchronization component connects the storage and communication components. It maintains data consistency between online devices by synchronizing data generated on the local device to other devices and merging data received from other devices into the local device.
30
31-   **Communication adaptation layer**
32
33    The communication adaptation layer invokes interfaces of the public communication layer to create and connect to communication channels, receive device online and offline messages, maintain metadata of the connected and disconnected devices, send device online and offline messages to the synchronization component, synchronize the list of devices connected to and maintained by components, and invoke interfaces to encapsulate and send data to the connected devices.
34
35
36You call APIs of DDS to create, access, and subscribe to distributed databases. The service interfaces store data to the storage component based on the capabilities provided by the service component. The storage component invokes the synchronization component to synchronize data. The synchronization component uses the communication adaptation layer to synchronize data to remote devices, which update the data in the storage component.
37
38**Figure  1**  How DDS works<a name="fig371116145419"></a>
39
40
41![](figures/distributed-datamgr-subsystem-architecture.png)
42
43## Directory Structure<a name="section161941989596"></a>
44
45```
46/foundation/distributeddatamgr/datamgr_service
47├── interfaces                    # APIs
48│   └── innerkits                 # Native APIs
49│   └── jskits                    # JavaScript APIs
50├── services                      # Service code
51│   └── distributeddataservice    # DDS implementation
52└── test                          # Test case resources
53```
54
55## Constraints<a name="section119744591305"></a>
56
57-   To use all functions of DDS, you need to obtain the  **ohos.permission.DISTRIBUTED\_DATASYNC**  permission.
58-   DDS supports the KV data model, but not foreign keys or triggers of the relational database.
59-   DDS supports the following KV data model specifications:
60    -   For the device KV store, the maximum size of a key is 896 bytes, and that of a value is 4 MB.
61    -   For the single KV store, the maximum size of a key is 1 KB, and that of a value is 4 MB.
62    -   Each app can open a maximum of 16 databases simultaneously.
63
64-   DDS cannot completely replace the database in the service sandbox for storing data, because the storage types supported by them are not completely the same. You need to determine the data to be synchronized in distributed mode and store the data in DDS.
65-   Currently, DDS does not allow customization of conflict resolution policies.
66-   DDS supports a maximum of 1000  **KvStore**  API calls per second, and 10,000 per minute. It supports a maximum of 50  **KvManager**  API calls per second, and 500 per minute.
67
68## Description<a name="section1312121216216"></a>
69
70Some basic concepts related to the DDS are as follows:
71
72-   **KV data model**
73
74    KV is short for key-value. The KV database is a type of NoSQL database. Data in this type of database is organized, indexed, and stored in the form of key-value pairs.
75
76    The KV data model is suitable for storing service data that does not involve too many data or service relationships. It provides better read and write performance than the SQL database. The KV data model is widely used in distributed scenarios because it handles conflict more easily in database version compatibility and data synchronization. The distributed database is based on the KV data model and provides KV-based access interfaces.
77
78-   **Distributed database transactions**
79
80    Distributed database transactions include local transactions \(same as the transactions of traditional databases\) and synchronization transactions. Synchronization transactions refer to data synchronization between devices in the unit of local transaction. Synchronization of a local transaction modification either succeeds or fails on multiple devices.
81
82-   **Distributed database consistency**
83
84    In a distributed scenario where multiple devices are used in the same network, distributed database consistency means that data is consistent on these devices. This consistency can be classified into strong, weak, and eventual consistency.
85
86    -   **Strong consistency**: After data is inserted, deleted, or updated on a device, other devices in the same network will obtain the updated data.
87    -   **Weak consistency**: After data is inserted, deleted, or updated on a device, other devices in the same network may or may not obtain the updated data. The time when all devices have the same data is uncertain.
88    -   **Eventual consistency**: After data is inserted, deleted, or updated on a device, other devices in the same network may not obtain the updated data immediately. However, data on all the devices will become consistent after some time.
89
90    Strong consistency has high requirements on distributed data management and may be used in distributed server scenarios. DDS supports only ultimate consistency because mobile devices are not always online and there is no center.
91
92-   **Distributed database synchronization**
93
94    After discovering and authenticating a device, the bottom-layer communication component notifies the DDS that the device goes online. After receiving the notification, DDS establishes an encrypted transmission channel to synchronize data between the two devices.
95
96    DDS provides both manual and automatic synchronization. In manual synchronization, you can specify the list of target devices and the synchronization mode \(PULL, PUSH, or PULL\_PUSH\). In automatic synchronization, the distributed database synchronizes data \(when devices go online or data is modified\), and you are unaware of the synchronization.
97
98-   **Single KV store**
99
100    Data is saved locally in the unit of a single KV entry. Only one entry is saved for each key. When users modify data locally, the corresponding KV entry is modified, regardless of whether the data has been synchronized. As for synchronization, the latest modification is synchronized to remote devices.
101
102-   **Device KV store**
103
104    The device KV store is based on the single KV store and adds device IDs before the keys of key-value pairs. This ensures that the data generated by each device is isolated from each other and is centrally managed by the system. With the device KV store, you can query data of a specified device, but cannot modify the data synchronized from remote devices.
105
106-   **Conflict resolution policy for the distributed database**
107
108    A data conflict occurs when multiple devices modify the same data and commit the modification to the database. In this case, the default conflict resolution policy is used, that is, the data modification committed later takes effect. Currently, the conflict resolution policy cannot be customized.
109
110-   **Schema-based database management and data query based on predicates**
111
112    When creating or opening a single KV store, you can specify a schema. The database detects the value format of key-value pairs based on the schema and checks the value structure. In addition, the database provides index creation and predicate-based query functions based on the fields in the values.
113
114-   **Distributed database backup capability**
115
116    DDS provides the database backup capability. By setting  **backup**  to  **true**, you can trigger daily database backup. If a distributed database is damaged, DDS deletes the database and restores the most recent data from the backup database. If no backup database is available, DDS creates one. DDS can also back up encrypted databases.
117
118
119## Repositories Involved<a name="section1371113476307"></a>
120
121Distributed Data Management subsystem
122
123distributeddatamgr\_datamgr\_service
124
125third\_party\_sqlite
126
127
128

README_zh.md

1# 分布式数据服务组件<a name="ZH-CN_TOPIC_0000001124232845"></a>
2
3-   [简介](#section11660541593)
4-   [目录](#section161941989596)
5-   [约束](#section119744591305)
6-   [说明](#section1312121216216)
7-   [相关仓](#section1371113476307)
8
9## 简介<a name="section11660541593"></a>
10
11分布式数据服务(Distributed Data Service,DDS) 提供不同设备间数据库数据分布式的能力。通过结合帐号、应用和数据库三元组,分布式数据服务对数据进行隔离。在通过可信认证的设备间,分布式数据服务支持数据相互同步,为用户提供在多种终端设备上一致的数据访问体验。
12
13分布式数据服务支撑OpenHarmony系统上分布式管理数据,包含五部分:
14
15-   **服务接口**
16
17    分布式数据服务提供专门的数据库创建、数据访问、数据订阅等接口给内部其他部件调用,接口支持KV数据模型,支持常用的数据类型,同时确保接口的兼容性、易用性和可发布性。
18
19-   **服务组件**
20
21    服务组件负责服务内元数据管理、权限管理、备份和恢复管理以及多用户管理等、同时负责初始化底层分布式DB的存储组件、同步组件和通信适配层。
22
23-   **存储组件**
24
25    存储组件负责数据的访问、数据的缩减、事务、快照,以及数据合并和冲突解决等特性。
26
27-   **同步组件**
28
29    同步组件连结了存储组件与通信组件,其目标是保持在线设备间的数据库数据一致性,包括将本地产生的未同步数据同步给其他设备,接收来自其他设备发送过来的数据,并合并到本地设备中。
30
31-   **通信适配层**
32
33    通信适配层负责调用底层公共通信层的接口完成通信管道的创建、连接,接收设备上下线消息,维护已连接和断开设备列表的元数据,同时将设备上下线信息发送给上层同步组件,同步组件维护连接的设备列表,同步数据时根据该列表,调用通信适配层的接口将数据封装并发送给连接的设备。
34
35
36通过调用分布式数据服务接口实现分布式数据库创建、访问、订阅功能,服务接口通过操作服务组件提供的能力,将数据存储至存储组件,存储组件调用同步组件实现将数据同步,同步组件使用通信适配层将数据同步至远端设备,远端设备通过同步组件接收数据,并更新至本端存储组件。
37
38**图 1**  数据分布式运作示意图<a name="fig371116145419"></a>
39
40
41![](figures/distributed_datamgr_subsystem_architecture.png)
42
43## 目录<a name="section161941989596"></a>
44
45```
46/foundation/distributeddatamgr/datamgr_service
47├── services                      # 服务层代码
48│   └── distributeddataservice    # 分布式数据服务实现
49└── test                          # 测试用例资源
50```
51
52## 约束<a name="section119744591305"></a>
53
54-   如需使用分布式数据服务完整功能,需要申请ohos.permission.DISTRIBUTED\_DATASYNC权限。
55-   分布式数据服务的数据模型仅支持KV数据模型,不支持外键、触发器等关系型数据库中的技术点。
56-   分布式数据服务支持的KV数据模型规格:
57    -   设备协同数据库,Key最大支持896Byte,Value最大支持4MB。
58    -   单版本数据库,Key最大支持1KB,Value最大支持4MB。
59    -   每个程序最多支持同时打开16个DB。
60
61-   由于支持的存储类型不完全相同等原因,分布式数据服务无法完全代替业务沙箱内数据库数据的存储功能,开发人员需要确定要做分布式同步的数据,把这些数据保存到分布式数据服务中。
62-   分布式数据服务当前不支持自定义冲突解决策略。
63-   分布式数据服务当前流控机制针对KvStore的接口1秒最大访问1000次,1分钟最大访问10000次。KvManager的接口1秒最大访问50次,1分钟最大访问500次。
64
65## 说明<a name="section1312121216216"></a>
66
67分布式数据服务几个基本概念:
68
69-   **KV数据模型**
70
71    “KV数据模型”是“Key-Value数据模型”的简称,“Key-Value”即“键-值”。它是一种NoSQL类型数据库,其数据以键值对的形式进行组织、索引和存储。
72
73    KV数据模型适合不涉及过多数据关系和业务关系的业务数据存储,比SQL数据库存储拥有更好的读写性能,同时因在分布式场景中降低了数据库版本兼容和数据同步过程中冲突解决的复杂度而被广泛使用。分布式数据库也是基于KV数据模型,对外提供KV类型的访问接口。
74
75-   **分布式数据库事务性**
76
77    分布式数据库事务支持本地事务(和传统数据库的事务概念一致)和同步事务,同步事务是指在设备之间同步数据时,是以本地事务为单位进行同步,一次本地事务的修改要么都同步成功,要么都同步失败。
78
79-   **分布式数据库一致性**
80
81    在分布式场景中一般会涉及多个设备,组网内设备之间看到的数据是否一致称为分布式数据库的一致性。分布式数据库一致性可以分为**强一致性**、**弱一致性**和**最终一致性**。
82
83    -   **强一致性**:是指某一设备成功增、删、改数据后,组网内设备对该数据的读取操作都将得到更新后的值。
84    -   **弱一致性**:是指某一设备成功增、删、改数据后,组网内设备可能能读取到本次更新数据,也可能读取不到,不能保证在多长时间后每个设备的数据一定是一致的。
85    -   **最终一致性**:是指某一设备成功增、删、改数据后,组网内设备可能读取不到本次更新数据,但在某个时间窗口之后组网内设备的数据能够达到一致状态。
86
87    强一致性对分布式数据的管理要求非常高,在服务器的分布式场景可能会遇到。因为移动终端设备的不常在线、以及无中心的特性,分布式数据服务不支持强一致,只支持最终一致性。
88
89-   **分布式数据库同步**
90
91    底层通信组件完成设备发现和认证,会通知分布式数据服务设备上线。收到设备上线的消息后分布式数据服务可以在两个设备之间建立加密的数据传输通道,利用该通道在两个设备之间进行数据同步。
92
93    分布式数据服务提供了两种同步模式:**手动同步**和**自动同步模式**。**手动同步模式**支持指定同步的设备列表和同步模式(PULL、PUSH和PULL\_PUSH三种同步模式)。**自动同步模式**由分布式数据库来完成数据同步(同步时机包括设备上线、修改数据等),业务不感知同步操作。
94
95-   **单版本分布式数据库**
96
97    单版本是指数据在本地保存是以单个KV条目为单位的方式保存,对每个Key最多只保存一个条目项,当数据在本地被用户修改时,不管它是否已经被同步出去,均直接在这个条目上进行修改。同步也以此为基础,按照它在本地被写入或更改的顺序将当前最新一次修改逐条同步至远端设备。
98
99-   **设备协同分布式数据库**
100
101    设备协同分布式数据库建立在单版本分布式数据库之上,对于存入的KV数据中的Key前面拼接了本设备的DeviceID标识符,这样能保证每个设备产生的数据严格隔离,底层按照设备的维度管理这些数据,设备协同分布式数据库支持以设备的维度查询分布式数据,但是不支持修改远端设备同步过来的数据。
102
103-   **分布式数据库冲突解决策略**
104
105    分布式数据库多设备提交冲突场景,在给提交冲突做合并的过程中,如果多个设备同时修改了同一数据,则称这种场景为数据冲突。数据冲突采用默认冲突解决策略,基于提交时间戳,取时间戳较大的提交数据,当前不支持定制冲突解决策略。
106
107-   **数据库Schema化管理与谓词查询**
108
109    单版本数据库支持在创建和打开数据库时指定Schema,数据库根据Schema定义感知KV记录的Value格式,以实现对Value值结构的检查,并基于Value中的字段实现索引建立和支持谓词查询。
110
111-   **分布式数据库备份能力**
112
113    提供分布式数据库备份能力,业务通过设置backup属性为true,可以触发分布式数据服务每日备份。当分布式数据库发生损坏,分布式数据服务会删除损坏数据库,并且从备份数据库中恢复上次备份的数据。如果不存在备份数据库,则创建一个新的数据库。同时支持加密数据库的备份能力。
114
115
116## 相关仓<a name="section1371113476307"></a>
117
118分布式数据管理子系统
119
120[distributeddatamgr\_datamgr\_service](https://gitee.com/openharmony/distributeddatamgr_datamgr_service)
121
122[third\_party\_sqlite](https://gitee.com/openharmony/third_party_sqlite)
123
124
125