| 1164 | } |
| 1165 | |
| 1166 | uint64_t Table::CleanObsoleteFile() { |
| 1167 | leveldb::Env* env = io::LeveldbBaseEnv(); |
| 1168 | std::string table_path = FLAGS_tera_tabletnode_path_prefix + "/" + name_; |
| 1169 | uint64_t delete_file_num = 0; |
| 1170 | int64_t start_ts = get_micros(); |
| 1171 | |
| 1172 | MutexLock l(&mutex_); |
| 1173 | while (!obsolete_inh_files_.empty()) { |
| 1174 | TabletFile file = obsolete_inh_files_.front(); |
| 1175 | mutex_.Unlock(); |
| 1176 | |
| 1177 | if (GetStatus() == kTableDeleting) { |
| 1178 | LOG(INFO) << "[gc] [" << name_ << "] table deleted, give up clean"; |
| 1179 | mutex_.Lock(); |
| 1180 | break; |
| 1181 | } |
| 1182 | |
| 1183 | std::string path; |
| 1184 | leveldb::Status s; |
| 1185 | if (file.lg_id == 0 && file.file_id == 0) { |
| 1186 | std::string path = leveldb::BuildTabletPath(table_path, file.tablet_id); |
| 1187 | leveldb::FileLock* file_lock = nullptr; |
| 1188 | // NEVER remove the trailing character '/', otherwise you will lock the |
| 1189 | // parent directory |
| 1190 | s = env->LockFile(path + "/", &file_lock); |
| 1191 | if (!s.ok()) { |
| 1192 | LOG(WARNING) << "lock path failed, path: " << path << ", status: " << s.ToString(); |
| 1193 | } |
| 1194 | delete file_lock; |
| 1195 | |
| 1196 | LOG(INFO) << "[gc] [" << name_ << "] delete dir " << path; |
| 1197 | s = io::DeleteEnvDir(path); // safely delete dir and all file in it |
| 1198 | } else { |
| 1199 | std::string lg_path = leveldb::BuildTabletLgPath(table_path, file.tablet_id, file.lg_id); |
| 1200 | leveldb::FileLock* file_lock = nullptr; |
| 1201 | // NEVER remove the trailing character '/', otherwise you will lock the |
| 1202 | // parent directory |
| 1203 | s = env->LockFile(lg_path + "/", &file_lock); |
| 1204 | if (!s.ok()) { |
| 1205 | LOG(WARNING) << "lock path failed, path: " << lg_path << ", status: " << s.ToString(); |
| 1206 | } |
| 1207 | delete file_lock; |
| 1208 | |
| 1209 | std::string path = |
| 1210 | leveldb::BuildTableFilePath(table_path, file.tablet_id, file.lg_id, file.file_id); |
| 1211 | if (FLAGS_tera_master_gc_trash_enabled) { |
| 1212 | LOG(INFO) << "[gc] [" << name_ << "] move file to trash, file: " << file |
| 1213 | << ", path: " << path; |
| 1214 | // move sst to trackable gc trash instead of deleting it directly |
| 1215 | s = io::MoveSstToTrackableGcTrash(name_, file.tablet_id, file.lg_id, file.file_id); |
| 1216 | } else { |
| 1217 | LOG(INFO) << "[gc] [" << name_ << "] delete file " << file << " path " << path; |
| 1218 | s = env->DeleteFile(path); |
| 1219 | } |
| 1220 | } |
| 1221 | mutex_.Lock(); |
| 1222 | if (s.ok()) { |
| 1223 | delete_file_num++; |