datafile.c 20 KB

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