| 678 | } |
| 679 | |
| 680 | Status DataCache::Partition::Init() { |
| 681 | std::unique_lock<SpinLock> partition_lock(lock_); |
| 682 | |
| 683 | RETURN_IF_ERROR(meta_cache_->Init()); |
| 684 | |
| 685 | // Trace replay does not require further initialization, as it is only doing |
| 686 | // metadata operations and does not do filesystem operations. |
| 687 | if (trace_replay_) return Status::OK(); |
| 688 | |
| 689 | // Verify the validity of the path specified. |
| 690 | if (!FileSystemUtil::IsCanonicalPath(path_)) { |
| 691 | return Status(Substitute("$0 is not a canonical path", path_)); |
| 692 | } |
| 693 | RETURN_IF_ERROR(FileSystemUtil::VerifyIsDirectory(path_)); |
| 694 | |
| 695 | // Make sure hole punching is supported for the caching directory. |
| 696 | RETURN_IF_ERROR(FileSystemUtil::CheckHolePunch(path_)); |
| 697 | |
| 698 | if (FLAGS_data_cache_enable_tracing) { |
| 699 | if (FLAGS_data_cache_trace_percentage > 100 || |
| 700 | FLAGS_data_cache_trace_percentage < 0) { |
| 701 | return Status(Substitute("Misconfigured data_cache_trace_percentage: $0." |
| 702 | "Must be between 0 and 100.", FLAGS_data_cache_trace_percentage)); |
| 703 | } |
| 704 | |
| 705 | // If unspecified, use a directory under the "log_dir". |
| 706 | if (FLAGS_data_cache_trace_dir.empty()) { |
| 707 | stringstream default_trace_dir; |
| 708 | default_trace_dir << FLAGS_log_dir << "/data_cache_traces/"; |
| 709 | FLAGS_data_cache_trace_dir = default_trace_dir.str(); |
| 710 | } |
| 711 | // To avoid mixing trace files from different partitions, give each partition |
| 712 | // its own subdirectory. |
| 713 | stringstream trace_dir; |
| 714 | trace_dir << FLAGS_data_cache_trace_dir << Substitute("/partition-$0/", index_); |
| 715 | tracer_.reset(new trace::Tracer(trace_dir.str(), |
| 716 | FLAGS_max_data_cache_trace_file_size, FLAGS_max_data_cache_trace_files, |
| 717 | FLAGS_data_cache_anonymize_trace)); |
| 718 | RETURN_IF_ERROR(tracer_->Init()); |
| 719 | } |
| 720 | |
| 721 | data_cache_write_concurrency_ = DeviceWriteConcurrency(path_); |
| 722 | |
| 723 | // Create metrics for this partition |
| 724 | InitMetrics(); |
| 725 | |
| 726 | // Only if loading is not enabled, or if loading is enabled but fails, do we need to |
| 727 | // create new cache file. |
| 728 | if (!(FLAGS_data_cache_keep_across_restarts && Load().ok())) { |
| 729 | // Create a backing file for the partition. |
| 730 | RETURN_IF_ERROR(CreateCacheFile()); |
| 731 | oldest_opened_file_ = 0; |
| 732 | } |
| 733 | |
| 734 | // Delete all cache files that are not listed in cache_files_ (including dumped metadata |
| 735 | // file). These files were left over from previous runs and are now no longer needed. |
| 736 | RETURN_IF_ERROR(DeleteUntrackedFiles()); |
| 737 | |