datafile.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "rrdengine.h"
  3. void datafile_list_insert(struct rrdengine_instance *ctx, struct rrdengine_datafile *datafile, bool having_lock)
  4. {
  5. if(!having_lock)
  6. uv_rwlock_wrlock(&ctx->datafiles.rwlock);
  7. DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(ctx->datafiles.first, datafile, prev, next);
  8. if(!having_lock)
  9. uv_rwlock_wrunlock(&ctx->datafiles.rwlock);
  10. }
  11. void datafile_list_delete_unsafe(struct rrdengine_instance *ctx, struct rrdengine_datafile *datafile)
  12. {
  13. DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(ctx->datafiles.first, datafile, prev, next);
  14. }
  15. static struct rrdengine_datafile *datafile_alloc_and_init(struct rrdengine_instance *ctx, unsigned tier, unsigned fileno)
  16. {
  17. fatal_assert(tier == 1);
  18. struct rrdengine_datafile *datafile = callocz(1, sizeof(struct rrdengine_datafile));
  19. datafile->tier = tier;
  20. datafile->fileno = fileno;
  21. fatal_assert(0 == uv_rwlock_init(&datafile->extent_rwlock));
  22. datafile->ctx = ctx;
  23. datafile->users.available = true;
  24. spinlock_init(&datafile->users.spinlock);
  25. spinlock_init(&datafile->writers.spinlock);
  26. spinlock_init(&datafile->extent_queries.spinlock);
  27. return datafile;
  28. }
  29. bool datafile_acquire(struct rrdengine_datafile *df, DATAFILE_ACQUIRE_REASONS reason) {
  30. bool ret;
  31. spinlock_lock(&df->users.spinlock);
  32. if(df->users.available) {
  33. ret = true;
  34. df->users.lockers++;
  35. df->users.lockers_by_reason[reason]++;
  36. }
  37. else
  38. ret = false;
  39. spinlock_unlock(&df->users.spinlock);
  40. return ret;
  41. }
  42. void datafile_release(struct rrdengine_datafile *df, DATAFILE_ACQUIRE_REASONS reason) {
  43. spinlock_lock(&df->users.spinlock);
  44. if(!df->users.lockers)
  45. fatal("DBENGINE DATAFILE: cannot release a datafile that is not acquired");
  46. df->users.lockers--;
  47. df->users.lockers_by_reason[reason]--;
  48. spinlock_unlock(&df->users.spinlock);
  49. }
  50. bool datafile_acquire_for_deletion(struct rrdengine_datafile *df) {
  51. bool can_be_deleted = false;
  52. spinlock_lock(&df->users.spinlock);
  53. df->users.available = false;
  54. if(!df->users.lockers)
  55. can_be_deleted = true;
  56. else {
  57. // there are lockers
  58. // evict any pages referencing this in the open cache
  59. spinlock_unlock(&df->users.spinlock);
  60. pgc_open_evict_clean_pages_of_datafile(open_cache, df);
  61. spinlock_lock(&df->users.spinlock);
  62. if(!df->users.lockers)
  63. can_be_deleted = true;
  64. else {
  65. // there are lockers still
  66. // count the number of pages referencing this in the open cache
  67. spinlock_unlock(&df->users.spinlock);
  68. usec_t time_to_scan_ut = now_monotonic_usec();
  69. size_t clean_pages_in_open_cache = pgc_count_clean_pages_having_data_ptr(open_cache, (Word_t)df->ctx, df);
  70. size_t hot_pages_in_open_cache = pgc_count_hot_pages_having_data_ptr(open_cache, (Word_t)df->ctx, df);
  71. time_to_scan_ut = now_monotonic_usec() - time_to_scan_ut;
  72. spinlock_lock(&df->users.spinlock);
  73. if(!df->users.lockers)
  74. can_be_deleted = true;
  75. else if(!clean_pages_in_open_cache && !hot_pages_in_open_cache) {
  76. // no pages in the open cache related to this datafile
  77. time_t now_s = now_monotonic_sec();
  78. if(!df->users.time_to_evict) {
  79. // first time we did the above
  80. df->users.time_to_evict = now_s + 120;
  81. internal_error(true, "DBENGINE: datafile %u of tier %d is not used by any open cache pages, "
  82. "but it has %u lockers (oc:%u, pd:%u), "
  83. "%zu clean and %zu hot open cache pages "
  84. "- will be deleted shortly "
  85. "(scanned open cache in %"PRIu64" usecs)",
  86. df->fileno, df->ctx->config.tier,
  87. df->users.lockers,
  88. df->users.lockers_by_reason[DATAFILE_ACQUIRE_OPEN_CACHE],
  89. df->users.lockers_by_reason[DATAFILE_ACQUIRE_PAGE_DETAILS],
  90. clean_pages_in_open_cache,
  91. hot_pages_in_open_cache,
  92. time_to_scan_ut);
  93. }
  94. else if(now_s > df->users.time_to_evict) {
  95. // time expired, lets remove it
  96. can_be_deleted = true;
  97. internal_error(true, "DBENGINE: datafile %u of tier %d is not used by any open cache pages, "
  98. "but it has %u lockers (oc:%u, pd:%u), "
  99. "%zu clean and %zu hot open cache pages "
  100. "- will be deleted now "
  101. "(scanned open cache in %"PRIu64" usecs)",
  102. df->fileno, df->ctx->config.tier,
  103. df->users.lockers,
  104. df->users.lockers_by_reason[DATAFILE_ACQUIRE_OPEN_CACHE],
  105. df->users.lockers_by_reason[DATAFILE_ACQUIRE_PAGE_DETAILS],
  106. clean_pages_in_open_cache,
  107. hot_pages_in_open_cache,
  108. time_to_scan_ut);
  109. }
  110. }
  111. else
  112. internal_error(true, "DBENGINE: datafile %u of tier %d "
  113. "has %u lockers (oc:%u, pd:%u), "
  114. "%zu clean and %zu hot open cache pages "
  115. "(scanned open cache in %"PRIu64" usecs)",
  116. df->fileno, df->ctx->config.tier,
  117. df->users.lockers,
  118. df->users.lockers_by_reason[DATAFILE_ACQUIRE_OPEN_CACHE],
  119. df->users.lockers_by_reason[DATAFILE_ACQUIRE_PAGE_DETAILS],
  120. clean_pages_in_open_cache,
  121. hot_pages_in_open_cache,
  122. time_to_scan_ut);
  123. }
  124. }
  125. spinlock_unlock(&df->users.spinlock);
  126. return can_be_deleted;
  127. }
  128. void generate_datafilepath(struct rrdengine_datafile *datafile, char *str, size_t maxlen)
  129. {
  130. (void) snprintfz(str, maxlen, "%s/" DATAFILE_PREFIX RRDENG_FILE_NUMBER_PRINT_TMPL DATAFILE_EXTENSION,
  131. datafile->ctx->config.dbfiles_path, datafile->tier, datafile->fileno);
  132. }
  133. int close_data_file(struct rrdengine_datafile *datafile)
  134. {
  135. struct rrdengine_instance *ctx = datafile->ctx;
  136. uv_fs_t req;
  137. int ret;
  138. char path[RRDENG_PATH_MAX];
  139. generate_datafilepath(datafile, path, sizeof(path));
  140. ret = uv_fs_close(NULL, &req, datafile->file, NULL);
  141. if (ret < 0) {
  142. netdata_log_error("DBENGINE: uv_fs_close(%s): %s", path, uv_strerror(ret));
  143. ctx_fs_error(ctx);
  144. }
  145. uv_fs_req_cleanup(&req);
  146. return ret;
  147. }
  148. int unlink_data_file(struct rrdengine_datafile *datafile)
  149. {
  150. struct rrdengine_instance *ctx = datafile->ctx;
  151. uv_fs_t req;
  152. int ret;
  153. char path[RRDENG_PATH_MAX];
  154. generate_datafilepath(datafile, path, sizeof(path));
  155. ret = uv_fs_unlink(NULL, &req, path, NULL);
  156. if (ret < 0) {
  157. netdata_log_error("DBENGINE: uv_fs_fsunlink(%s): %s", path, uv_strerror(ret));
  158. ctx_fs_error(ctx);
  159. }
  160. uv_fs_req_cleanup(&req);
  161. __atomic_add_fetch(&ctx->stats.datafile_deletions, 1, __ATOMIC_RELAXED);
  162. return ret;
  163. }
  164. int destroy_data_file_unsafe(struct rrdengine_datafile *datafile)
  165. {
  166. struct rrdengine_instance *ctx = datafile->ctx;
  167. uv_fs_t req;
  168. int ret;
  169. char path[RRDENG_PATH_MAX];
  170. generate_datafilepath(datafile, path, sizeof(path));
  171. ret = uv_fs_ftruncate(NULL, &req, datafile->file, 0, NULL);
  172. if (ret < 0) {
  173. netdata_log_error("DBENGINE: uv_fs_ftruncate(%s): %s", path, uv_strerror(ret));
  174. ctx_fs_error(ctx);
  175. }
  176. uv_fs_req_cleanup(&req);
  177. ret = uv_fs_close(NULL, &req, datafile->file, NULL);
  178. if (ret < 0) {
  179. netdata_log_error("DBENGINE: uv_fs_close(%s): %s", path, uv_strerror(ret));
  180. ctx_fs_error(ctx);
  181. }
  182. uv_fs_req_cleanup(&req);
  183. ret = uv_fs_unlink(NULL, &req, path, NULL);
  184. if (ret < 0) {
  185. netdata_log_error("DBENGINE: uv_fs_fsunlink(%s): %s", path, uv_strerror(ret));
  186. ctx_fs_error(ctx);
  187. }
  188. uv_fs_req_cleanup(&req);
  189. __atomic_add_fetch(&ctx->stats.datafile_deletions, 1, __ATOMIC_RELAXED);
  190. return ret;
  191. }
  192. int create_data_file(struct rrdengine_datafile *datafile)
  193. {
  194. struct rrdengine_instance *ctx = datafile->ctx;
  195. uv_fs_t req;
  196. uv_file file;
  197. int ret, fd;
  198. struct rrdeng_df_sb *superblock;
  199. uv_buf_t iov;
  200. char path[RRDENG_PATH_MAX];
  201. generate_datafilepath(datafile, path, sizeof(path));
  202. fd = open_file_for_io(path, O_CREAT | O_RDWR | O_TRUNC, &file, use_direct_io);
  203. if (fd < 0) {
  204. ctx_fs_error(ctx);
  205. return fd;
  206. }
  207. datafile->file = file;
  208. __atomic_add_fetch(&ctx->stats.datafile_creations, 1, __ATOMIC_RELAXED);
  209. ret = posix_memalign((void *)&superblock, RRDFILE_ALIGNMENT, sizeof(*superblock));
  210. if (unlikely(ret)) {
  211. fatal("DBENGINE: posix_memalign:%s", strerror(ret));
  212. }
  213. memset(superblock, 0, sizeof(*superblock));
  214. (void) strncpy(superblock->magic_number, RRDENG_DF_MAGIC, RRDENG_MAGIC_SZ);
  215. (void) strncpy(superblock->version, RRDENG_DF_VER, RRDENG_VER_SZ);
  216. superblock->tier = 1;
  217. iov = uv_buf_init((void *)superblock, sizeof(*superblock));
  218. ret = uv_fs_write(NULL, &req, file, &iov, 1, 0, NULL);
  219. if (ret < 0) {
  220. fatal_assert(req.result < 0);
  221. netdata_log_error("DBENGINE: uv_fs_write: %s", uv_strerror(ret));
  222. ctx_io_error(ctx);
  223. }
  224. uv_fs_req_cleanup(&req);
  225. posix_memfree(superblock);
  226. if (ret < 0) {
  227. destroy_data_file_unsafe(datafile);
  228. return ret;
  229. }
  230. datafile->pos = sizeof(*superblock);
  231. ctx_io_write_op_bytes(ctx, sizeof(*superblock));
  232. return 0;
  233. }
  234. static int check_data_file_superblock(uv_file file)
  235. {
  236. int ret;
  237. struct rrdeng_df_sb *superblock;
  238. uv_buf_t iov;
  239. uv_fs_t req;
  240. ret = posix_memalign((void *)&superblock, RRDFILE_ALIGNMENT, sizeof(*superblock));
  241. if (unlikely(ret)) {
  242. fatal("DBENGINE: posix_memalign:%s", strerror(ret));
  243. }
  244. iov = uv_buf_init((void *)superblock, sizeof(*superblock));
  245. ret = uv_fs_read(NULL, &req, file, &iov, 1, 0, NULL);
  246. if (ret < 0) {
  247. netdata_log_error("DBENGINE: uv_fs_read: %s", uv_strerror(ret));
  248. uv_fs_req_cleanup(&req);
  249. goto error;
  250. }
  251. fatal_assert(req.result >= 0);
  252. uv_fs_req_cleanup(&req);
  253. if (strncmp(superblock->magic_number, RRDENG_DF_MAGIC, RRDENG_MAGIC_SZ) ||
  254. strncmp(superblock->version, RRDENG_DF_VER, RRDENG_VER_SZ) ||
  255. superblock->tier != 1) {
  256. netdata_log_error("DBENGINE: file has invalid superblock.");
  257. ret = UV_EINVAL;
  258. } else {
  259. ret = 0;
  260. }
  261. error:
  262. posix_memfree(superblock);
  263. return ret;
  264. }
  265. static int load_data_file(struct rrdengine_datafile *datafile)
  266. {
  267. struct rrdengine_instance *ctx = datafile->ctx;
  268. uv_fs_t req;
  269. uv_file file;
  270. int ret, fd, error;
  271. uint64_t file_size;
  272. char path[RRDENG_PATH_MAX];
  273. generate_datafilepath(datafile, path, sizeof(path));
  274. fd = open_file_for_io(path, O_RDWR, &file, use_direct_io);
  275. if (fd < 0) {
  276. ctx_fs_error(ctx);
  277. return fd;
  278. }
  279. netdata_log_info("DBENGINE: initializing data file \"%s\".", path);
  280. ret = check_file_properties(file, &file_size, sizeof(struct rrdeng_df_sb));
  281. if (ret)
  282. goto error;
  283. file_size = ALIGN_BYTES_CEILING(file_size);
  284. ret = check_data_file_superblock(file);
  285. if (ret)
  286. goto error;
  287. ctx_io_read_op_bytes(ctx, sizeof(struct rrdeng_df_sb));
  288. datafile->file = file;
  289. datafile->pos = file_size;
  290. netdata_log_info("DBENGINE: data file \"%s\" initialized (size:%"PRIu64").", path, file_size);
  291. return 0;
  292. error:
  293. error = ret;
  294. ret = uv_fs_close(NULL, &req, file, NULL);
  295. if (ret < 0) {
  296. netdata_log_error("DBENGINE: uv_fs_close(%s): %s", path, uv_strerror(ret));
  297. ctx_fs_error(ctx);
  298. }
  299. uv_fs_req_cleanup(&req);
  300. return error;
  301. }
  302. static int scan_data_files_cmp(const void *a, const void *b)
  303. {
  304. struct rrdengine_datafile *file1, *file2;
  305. char path1[RRDENG_PATH_MAX], path2[RRDENG_PATH_MAX];
  306. file1 = *(struct rrdengine_datafile **)a;
  307. file2 = *(struct rrdengine_datafile **)b;
  308. generate_datafilepath(file1, path1, sizeof(path1));
  309. generate_datafilepath(file2, path2, sizeof(path2));
  310. return strcmp(path1, path2);
  311. }
  312. /* Returns number of datafiles that were loaded or < 0 on error */
  313. static int scan_data_files(struct rrdengine_instance *ctx)
  314. {
  315. int ret, matched_files, failed_to_load, i;
  316. unsigned tier, no;
  317. uv_fs_t req;
  318. uv_dirent_t dent;
  319. struct rrdengine_datafile **datafiles, *datafile;
  320. struct rrdengine_journalfile *journalfile;
  321. ret = uv_fs_scandir(NULL, &req, ctx->config.dbfiles_path, 0, NULL);
  322. if (ret < 0) {
  323. fatal_assert(req.result < 0);
  324. uv_fs_req_cleanup(&req);
  325. netdata_log_error("DBENGINE: uv_fs_scandir(%s): %s", ctx->config.dbfiles_path, uv_strerror(ret));
  326. ctx_fs_error(ctx);
  327. return ret;
  328. }
  329. netdata_log_info("DBENGINE: found %d files in path %s", ret, ctx->config.dbfiles_path);
  330. datafiles = callocz(MIN(ret, MAX_DATAFILES), sizeof(*datafiles));
  331. for (matched_files = 0 ; UV_EOF != uv_fs_scandir_next(&req, &dent) && matched_files < MAX_DATAFILES ; ) {
  332. ret = sscanf(dent.name, DATAFILE_PREFIX RRDENG_FILE_NUMBER_SCAN_TMPL DATAFILE_EXTENSION, &tier, &no);
  333. if (2 == ret) {
  334. datafile = datafile_alloc_and_init(ctx, tier, no);
  335. datafiles[matched_files++] = datafile;
  336. }
  337. }
  338. uv_fs_req_cleanup(&req);
  339. if (0 == matched_files) {
  340. freez(datafiles);
  341. return 0;
  342. }
  343. if (matched_files == MAX_DATAFILES)
  344. netdata_log_error("DBENGINE: warning: hit maximum database engine file limit of %d files", MAX_DATAFILES);
  345. qsort(datafiles, matched_files, sizeof(*datafiles), scan_data_files_cmp);
  346. ctx->atomic.last_fileno = datafiles[matched_files - 1]->fileno;
  347. for (failed_to_load = 0, i = 0 ; i < matched_files ; ++i) {
  348. uint8_t must_delete_pair = 0;
  349. datafile = datafiles[i];
  350. ret = load_data_file(datafile);
  351. if (0 != ret)
  352. must_delete_pair = 1;
  353. journalfile = journalfile_alloc_and_init(datafile);
  354. ret = journalfile_load(ctx, journalfile, datafile);
  355. if (0 != ret) {
  356. if (!must_delete_pair) /* If datafile is still open close it */
  357. close_data_file(datafile);
  358. must_delete_pair = 1;
  359. }
  360. if (must_delete_pair) {
  361. char path[RRDENG_PATH_MAX];
  362. netdata_log_error("DBENGINE: deleting invalid data and journal file pair.");
  363. ret = journalfile_unlink(journalfile);
  364. if (!ret) {
  365. journalfile_v1_generate_path(datafile, path, sizeof(path));
  366. netdata_log_info("DBENGINE: deleted journal file \"%s\".", path);
  367. }
  368. ret = unlink_data_file(datafile);
  369. if (!ret) {
  370. generate_datafilepath(datafile, path, sizeof(path));
  371. netdata_log_info("DBENGINE: deleted data file \"%s\".", path);
  372. }
  373. freez(journalfile);
  374. freez(datafile);
  375. ++failed_to_load;
  376. continue;
  377. }
  378. ctx_current_disk_space_increase(ctx, datafile->pos + journalfile->unsafe.pos);
  379. datafile_list_insert(ctx, datafile, false);
  380. }
  381. matched_files -= failed_to_load;
  382. freez(datafiles);
  383. return matched_files;
  384. }
  385. /* Creates a datafile and a journalfile pair */
  386. int create_new_datafile_pair(struct rrdengine_instance *ctx, bool having_lock)
  387. {
  388. __atomic_add_fetch(&rrdeng_cache_efficiency_stats.datafile_creation_started, 1, __ATOMIC_RELAXED);
  389. struct rrdengine_datafile *datafile;
  390. struct rrdengine_journalfile *journalfile;
  391. unsigned fileno = ctx_last_fileno_get(ctx) + 1;
  392. int ret;
  393. char path[RRDENG_PATH_MAX];
  394. netdata_log_info("DBENGINE: creating new data and journal files in path %s", ctx->config.dbfiles_path);
  395. datafile = datafile_alloc_and_init(ctx, 1, fileno);
  396. ret = create_data_file(datafile);
  397. if(ret)
  398. goto error_after_datafile;
  399. generate_datafilepath(datafile, path, sizeof(path));
  400. netdata_log_info("DBENGINE: created data file \"%s\".", path);
  401. journalfile = journalfile_alloc_and_init(datafile);
  402. ret = journalfile_create(journalfile, datafile);
  403. if (ret)
  404. goto error_after_journalfile;
  405. journalfile_v1_generate_path(datafile, path, sizeof(path));
  406. netdata_log_info("DBENGINE: created journal file \"%s\".", path);
  407. ctx_current_disk_space_increase(ctx, datafile->pos + journalfile->unsafe.pos);
  408. datafile_list_insert(ctx, datafile, having_lock);
  409. ctx_last_fileno_increment(ctx);
  410. return 0;
  411. error_after_journalfile:
  412. destroy_data_file_unsafe(datafile);
  413. freez(journalfile);
  414. error_after_datafile:
  415. freez(datafile);
  416. return ret;
  417. }
  418. /* Page cache must already be initialized.
  419. * Return 0 on success.
  420. */
  421. int init_data_files(struct rrdengine_instance *ctx)
  422. {
  423. int ret;
  424. fatal_assert(0 == uv_rwlock_init(&ctx->datafiles.rwlock));
  425. ret = scan_data_files(ctx);
  426. if (ret < 0) {
  427. netdata_log_error("DBENGINE: failed to scan path \"%s\".", ctx->config.dbfiles_path);
  428. return ret;
  429. } else if (0 == ret) {
  430. netdata_log_info("DBENGINE: data files not found, creating in path \"%s\".", ctx->config.dbfiles_path);
  431. ctx->atomic.last_fileno = 0;
  432. ret = create_new_datafile_pair(ctx, false);
  433. if (ret) {
  434. netdata_log_error("DBENGINE: failed to create data and journal files in path \"%s\".", ctx->config.dbfiles_path);
  435. return ret;
  436. }
  437. }
  438. else {
  439. if (ctx->loading.create_new_datafile_pair)
  440. create_new_datafile_pair(ctx, false);
  441. while(rrdeng_ctx_exceeded_disk_quota(ctx))
  442. datafile_delete(ctx, ctx->datafiles.first, false, false);
  443. }
  444. pgc_reset_hot_max(open_cache);
  445. ctx->loading.create_new_datafile_pair = false;
  446. return 0;
  447. }
  448. void finalize_data_files(struct rrdengine_instance *ctx)
  449. {
  450. bool logged = false;
  451. logged = false;
  452. while(__atomic_load_n(&ctx->atomic.extents_currently_being_flushed, __ATOMIC_RELAXED)) {
  453. if(!logged) {
  454. netdata_log_info("Waiting for inflight flush to finish on tier %d...", ctx->config.tier);
  455. logged = true;
  456. }
  457. sleep_usec(100 * USEC_PER_MS);
  458. }
  459. do {
  460. struct rrdengine_datafile *datafile = ctx->datafiles.first;
  461. struct rrdengine_journalfile *journalfile = datafile->journalfile;
  462. logged = false;
  463. size_t iterations = 100;
  464. while(!datafile_acquire_for_deletion(datafile) && datafile != ctx->datafiles.first->prev && --iterations > 0) {
  465. if(!logged) {
  466. netdata_log_info("Waiting to acquire data file %u of tier %d to close it...", datafile->fileno, ctx->config.tier);
  467. logged = true;
  468. }
  469. sleep_usec(100 * USEC_PER_MS);
  470. }
  471. logged = false;
  472. bool available = false;
  473. do {
  474. uv_rwlock_wrlock(&ctx->datafiles.rwlock);
  475. spinlock_lock(&datafile->writers.spinlock);
  476. available = (datafile->writers.running || datafile->writers.flushed_to_open_running) ? false : true;
  477. if(!available) {
  478. spinlock_unlock(&datafile->writers.spinlock);
  479. uv_rwlock_wrunlock(&ctx->datafiles.rwlock);
  480. if(!logged) {
  481. netdata_log_info("Waiting for writers to data file %u of tier %d to finish...", datafile->fileno, ctx->config.tier);
  482. logged = true;
  483. }
  484. sleep_usec(100 * USEC_PER_MS);
  485. }
  486. } while(!available);
  487. journalfile_close(journalfile, datafile);
  488. close_data_file(datafile);
  489. datafile_list_delete_unsafe(ctx, datafile);
  490. spinlock_unlock(&datafile->writers.spinlock);
  491. uv_rwlock_wrunlock(&ctx->datafiles.rwlock);
  492. freez(journalfile);
  493. freez(datafile);
  494. } while(ctx->datafiles.first);
  495. }