1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
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  */
15 #ifndef OHOS_CLOUD_SYNC_SERVICE_CLOUD_FILE_FAULT_EVENT_H
16 #define OHOS_CLOUD_SYNC_SERVICE_CLOUD_FILE_FAULT_EVENT_H
17 
18 #include "hisysevent.h"
19 
20 #define CLOUD_SYNC_FAULT_REPORT(...) \
21     CloudFile::CloudFileFaultEvent::CloudSyncFaultReport(__FUNCTION__, __LINE__, ##__VA_ARGS__)
22 #define CLOUD_FILE_FAULT_REPORT(...) \
23     CloudFile::CloudFileFaultEvent::CloudFileFaultReport(__FUNCTION__, __LINE__, ##__VA_ARGS__)
24 
25 namespace OHOS {
26 namespace FileManagement {
27 namespace CloudFile {
28 
29 #ifndef CLOUD_SYNC_SYS_EVENT
30 #define CLOUD_SYNC_SYS_EVENT(eventName, type, ...)                                  \
31     HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, eventName, type, \
32                     ##__VA_ARGS__)
33 #endif
34 
35 #define MINIMUM_FAULT_REPORT_INTERVAL 3600
36 
37 enum class FaultScenarioCode {
38     CLOUD_FULL_SYNC = 100,
39     CLOUD_INC_SYNC = 200,
40     CLOUD_CHECK_SYNC = 300,
41     CLOUD_SWITCH_CLOSE = 400,
42     CLOUD_LOAD_VIDEO_CACHE = 500,
43     CLOUD_DOWNLOAD_FILE = 600,
44     CLOUD_DOWNLOAD_THUM = 700,
45     CLOUD_OPTIMMIZE_STORAGE = 800,
46     CLOUD_PROCESS_EXIT_START = 900,
47     CLOUD_ACCOUNT_EXIT = 1000
48 };
49 
50 enum class FaultOperation {
51     LOOKUP = 100,
52     FORGET = 200,
53     GETATTR = 300,
54     OPEN = 400,
55     READ = 500,
56     READDIR = 600,
57     RELEASE = 700,
58     FORGETMULTI = 800,
59     IOCTL = 900,
60     UNLINK = 1000
61 };
62 
63 enum class FaultType {
64     /* file fault type */
65     FILE = 10000000,
66     DENTRY_FILE = 10000001,
67     INODE_FILE = 10000002,
68 
69     /* driverkit fault type */
70     DRIVERKIT = 20000000,
71     DRIVERKIT_NETWORK = 20000001,
72     DRIVERKIT_SERVER = 20000002,
73     DRIVERKIT_LOCAL = 20000003,
74     DRIVERKIT_DATABASE = 20000004,
75     DRIVERKIT_READSESSION = 20000005,
76 
77     /* timeout fault type */
78     TIMEOUT = 30000000,
79     OPEN_CLOUD_FILE_TIMEOUT = 30000001,
80     CLOUD_READ_FILE_TIMEOUT = 30000002,
81     CLOUD_FILE_LOOKUP_TIMEOUT = 30000003,
82     CLOUD_FILE_FORGET_TIMEOUT = 30000004,
83 
84     /* temperature fault type */
85     TEMPERATURE = 40000000,
86 
87     /* database fault type */
88     DATABASE = 50000000,
89     QUERY_DATABASE = 50000001,
90     INSERT_DATABASE = 50000002,
91     DELETE_DATABASE = 50000003,
92     MODIFY_DATABASE = 50000004,
93 
94     /* consistency fault type */
95     CONSISTENCY = 60000000,
96     FILE_CONSISTENCY = 60000001,
97     META_CONSISTENCY = 60000002,
98 
99     /* process fault type */
100     PROCESS = 70000000,
101 
102     /* cloud sync error */
103     CLOUD_SYNC_ERROR = 80000000,
104 
105     /* inner error */
106     INNER_ERROR = 90000000
107 };
108 
109 struct CloudSyncFaultInfo {
110     std::string bundleName_;
111     FaultScenarioCode faultScenario_;
112     FaultType faultType_;
113     int32_t faultErrorCode_;
114     std::string message_;
115 };
116 
117 struct CloudFileFaultInfo {
118     std::string bundleName_;
119     FaultOperation faultOperation_;
120     FaultType faultType_;
121     int32_t faultErrorCode_;
122     std::string message_;
123 };
124 
125 const std::vector<FaultType> PERIODIC_REPORT_FAULT_TYPE = { FaultType::OPEN_CLOUD_FILE_TIMEOUT,
126                                                             FaultType::CLOUD_READ_FILE_TIMEOUT,
127                                                             FaultType::CLOUD_FILE_LOOKUP_TIMEOUT,
128                                                             FaultType::CLOUD_FILE_FORGET_TIMEOUT };
129 
130 class CloudFaultReportStatus {
131 public:
132     static CloudFaultReportStatus &GetInstance();
133 
134     bool IsAllowToReport(const FaultType &faultType);
135 
136 private:
137     CloudFaultReportStatus();
138 
139     CloudFaultReportStatus(const CloudFaultReportStatus&) = delete;
140 
141     CloudFaultReportStatus& operator=(const CloudFaultReportStatus&) = delete;
142 
143     std::mutex timeMapLock_;
144     std::unordered_map<FaultType, int64_t> lastTimeMap_;
145 } ;
146 
147 class CloudFileFaultEvent {
148 public:
149     static int32_t CloudSyncFaultReport(const std::string &funcName,
150                                         const int lineNum,
151                                         const CloudSyncFaultInfo &event);
152 
153     static int32_t CloudFileFaultReport(const std::string &funcName,
154                                         const int lineNum,
155                                         const CloudFileFaultInfo &event);
156 };
157 } // namespace CloudFile
158 } // namespace FileManagement
159 } // namespace OHOS
160 
161 #endif // OHOS_CLOUD_SYNC_SERVICE_CLOUD_FILE_FAULT_EVENT_H