builder.cc 7.4 KB
Newer Older
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2 3 4 5
//  This source code is licensed under the BSD-style license found in the
//  LICENSE file in the root directory of this source tree. An additional grant
//  of patent rights can be found in the PATENTS file in the same directory.
//
J
jorlow@chromium.org 已提交
6 7 8 9 10 11
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#include "db/builder.h"

I
Igor Canadi 已提交
12
#include <algorithm>
13
#include <deque>
14
#include <vector>
15

16
#include "db/compaction_iterator.h"
J
jorlow@chromium.org 已提交
17
#include "db/dbformat.h"
18
#include "db/event_helpers.h"
K
kailiu 已提交
19
#include "db/filename.h"
20
#include "db/internal_stats.h"
21
#include "db/merge_helper.h"
J
jorlow@chromium.org 已提交
22 23
#include "db/table_cache.h"
#include "db/version_edit.h"
24 25 26
#include "rocksdb/db.h"
#include "rocksdb/env.h"
#include "rocksdb/iterator.h"
S
Siying Dong 已提交
27
#include "rocksdb/options.h"
K
kailiu 已提交
28
#include "rocksdb/table.h"
S
Siying Dong 已提交
29
#include "table/block_based_table_builder.h"
S
sdong 已提交
30
#include "table/internal_iterator.h"
31
#include "util/file_reader_writer.h"
32
#include "util/iostats_context_imp.h"
33
#include "util/stop_watch.h"
34
#include "util/thread_status_util.h"
J
jorlow@chromium.org 已提交
35

36
namespace rocksdb {
J
jorlow@chromium.org 已提交
37

S
Siying Dong 已提交
38 39
class TableFactory;

40 41 42 43 44
TableBuilder* NewTableBuilder(
    const ImmutableCFOptions& ioptions,
    const InternalKeyComparator& internal_comparator,
    const std::vector<std::unique_ptr<IntTblPropCollectorFactory>>*
        int_tbl_prop_collector_factories,
45 46
    uint32_t column_family_id, const std::string& column_family_name,
    WritableFileWriter* file, const CompressionType compression_type,
47 48
    const CompressionOptions& compression_opts,
    const std::string* compression_dict, const bool skip_filters) {
49 50 51
  assert((column_family_id ==
          TablePropertiesCollectorFactory::Context::kUnknownColumnFamily) ==
         column_family_name.empty());
52 53 54
  return ioptions.table_factory->NewTableBuilder(
      TableBuilderOptions(ioptions, internal_comparator,
                          int_tbl_prop_collector_factories, compression_type,
55
                          compression_opts, compression_dict, skip_filters,
56
                          column_family_name),
57
      column_family_id, file);
S
Siying Dong 已提交
58 59
}

60 61
Status BuildTable(
    const std::string& dbname, Env* env, const ImmutableCFOptions& ioptions,
A
Aaron Gao 已提交
62 63
    const MutableCFOptions& mutable_cf_options, const EnvOptions& env_options,
    TableCache* table_cache, InternalIterator* iter, FileMetaData* meta,
S
sdong 已提交
64
    const InternalKeyComparator& internal_comparator,
65 66
    const std::vector<std::unique_ptr<IntTblPropCollectorFactory>>*
        int_tbl_prop_collector_factories,
67 68
    uint32_t column_family_id, const std::string& column_family_name,
    std::vector<SequenceNumber> snapshots,
69
    SequenceNumber earliest_write_conflict_snapshot,
70
    const CompressionType compression,
71
    const CompressionOptions& compression_opts, bool paranoid_file_checks,
72 73
    InternalStats* internal_stats, TableFileCreationReason reason,
    EventLogger* event_logger, int job_id, const Env::IOPriority io_priority,
74
    TableProperties* table_properties, int level) {
75 76 77
  assert((column_family_id ==
          TablePropertiesCollectorFactory::Context::kUnknownColumnFamily) ==
         column_family_name.empty());
78 79
  // Reports the IOStats for flush for every following bytes.
  const size_t kReportFlushIOStatsEvery = 1048576;
J
jorlow@chromium.org 已提交
80
  Status s;
81
  meta->fd.file_size = 0;
J
jorlow@chromium.org 已提交
82 83
  iter->SeekToFirst();

L
Lei Jin 已提交
84
  std::string fname = TableFileName(ioptions.db_paths, meta->fd.GetNumber(),
85
                                    meta->fd.GetPathId());
86 87 88 89 90 91
#ifndef ROCKSDB_LITE
  EventHelpers::NotifyTableFileCreationStarted(
      ioptions.listeners, dbname, column_family_name, fname, job_id, reason);
#endif  // !ROCKSDB_LITE
  TableProperties tp;

J
jorlow@chromium.org 已提交
92
  if (iter->Valid()) {
93 94 95 96
    TableBuilder* builder;
    unique_ptr<WritableFileWriter> file_writer;
    {
      unique_ptr<WritableFile> file;
S
sdong 已提交
97
      s = NewWritableFile(env, fname, &file, env_options);
98
      if (!s.ok()) {
99 100 101
        EventHelpers::LogAndNotifyTableFileCreationFinished(
            event_logger, ioptions.listeners, dbname, column_family_name, fname,
            job_id, meta->fd, tp, reason, s);
102 103 104
        return s;
      }
      file->SetIOPriority(io_priority);
S
Siying Dong 已提交
105

106 107 108 109
      file_writer.reset(new WritableFileWriter(std::move(file), env_options));

      builder = NewTableBuilder(
          ioptions, internal_comparator, int_tbl_prop_collector_factories,
110 111
          column_family_id, column_family_name, file_writer.get(), compression,
          compression_opts);
112
    }
113

I
Igor Canadi 已提交
114 115
    MergeHelper merge(env, internal_comparator.user_comparator(),
                      ioptions.merge_operator, nullptr, ioptions.info_log,
A
Aaron Gao 已提交
116
                      mutable_cf_options.min_partial_merge_operands,
I
Igor Canadi 已提交
117 118
                      true /* internal key corruption is not ok */,
                      snapshots.empty() ? 0 : snapshots.back());
119

120
    CompactionIterator c_iter(iter, internal_comparator.user_comparator(),
121
                              &merge, kMaxSequenceNumber, &snapshots,
122
                              earliest_write_conflict_snapshot, env,
123 124 125 126 127 128 129 130 131
                              true /* internal key corruption is not ok */);
    c_iter.SeekToFirst();
    for (; c_iter.Valid(); c_iter.Next()) {
      const Slice& key = c_iter.key();
      const Slice& value = c_iter.value();
      builder->Add(key, value);
      meta->UpdateBoundaries(key, c_iter.ikey().sequence);

      // TODO(noetzli): Update stats after flush, too.
I
Igor Canadi 已提交
132 133
      if (io_priority == Env::IO_HIGH &&
          IOSTATS(bytes_written) >= kReportFlushIOStatsEvery) {
134
        ThreadStatusUtil::SetThreadOperationProperty(
I
Igor Canadi 已提交
135
            ThreadStatus::FLUSH_BYTES_WRITTEN, IOSTATS(bytes_written));
136
      }
J
jorlow@chromium.org 已提交
137 138 139
    }

    // Finish and check for builder errors
A
Andres Noetzli 已提交
140
    bool empty = builder->NumEntries() == 0;
141
    s = c_iter.status();
A
Andres Noetzli 已提交
142
    if (!s.ok() || empty) {
J
jorlow@chromium.org 已提交
143
      builder->Abandon();
A
Andres Noetzli 已提交
144 145
    } else {
      s = builder->Finish();
J
jorlow@chromium.org 已提交
146
    }
A
Andres Noetzli 已提交
147 148

    if (s.ok() && !empty) {
149 150
      uint64_t file_size = builder->FileSize();
      meta->fd.file_size = file_size;
151
      meta->marked_for_compaction = builder->NeedCompact();
152
      assert(meta->fd.GetFileSize() > 0);
153
      tp = builder->GetTableProperties();
154
      if (table_properties) {
155
        *table_properties = tp;
156 157
      }
    }
J
jorlow@chromium.org 已提交
158 159 160
    delete builder;

    // Finish and check for file errors
A
Andres Noetzli 已提交
161
    if (s.ok() && !empty && !ioptions.disable_data_sync) {
162 163
      StopWatch sw(env, ioptions.statistics, TABLE_SYNC_MICROS);
      file_writer->Sync(ioptions.use_fsync);
J
jorlow@chromium.org 已提交
164
    }
A
Andres Noetzli 已提交
165
    if (s.ok() && !empty) {
166
      s = file_writer->Close();
J
jorlow@chromium.org 已提交
167 168
    }

A
Andres Noetzli 已提交
169
    if (s.ok() && !empty) {
J
jorlow@chromium.org 已提交
170
      // Verify that the table is usable
S
sdong 已提交
171
      std::unique_ptr<InternalIterator> it(table_cache->NewIterator(
172 173 174
          ReadOptions(), env_options, internal_comparator, meta->fd, nullptr,
          (internal_stats == nullptr) ? nullptr
                                      : internal_stats->GetFileReadHist(0),
175 176
          false /* for_compaction */, nullptr /* arena */,
          false /* skip_filter */, level));
J
jorlow@chromium.org 已提交
177
      s = it->status();
178
      if (s.ok() && paranoid_file_checks) {
A
Andres Noetzli 已提交
179 180
        for (it->SeekToFirst(); it->Valid(); it->Next()) {
        }
181 182
        s = it->status();
      }
J
jorlow@chromium.org 已提交
183 184 185 186 187 188 189 190
    }
  }

  // Check for input iterator errors
  if (!iter->status().ok()) {
    s = iter->status();
  }

A
Andres Noetzli 已提交
191
  if (!s.ok() || meta->fd.GetFileSize() == 0) {
J
jorlow@chromium.org 已提交
192 193
    env->DeleteFile(fname);
  }
194 195 196 197 198 199

  // Output to event logger and fire events.
  EventHelpers::LogAndNotifyTableFileCreationFinished(
      event_logger, ioptions.listeners, dbname, column_family_name, fname,
      job_id, meta->fd, tp, reason, s);

J
jorlow@chromium.org 已提交
200 201 202
  return s;
}

203
}  // namespace rocksdb