Skip to content
4 changes: 4 additions & 0 deletions cpp/src/common/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ struct MeasurementSchemaGroup {
TimeChunkWriter* time_chunk_writer_ = nullptr;
int64_t last_time_ = INT64_MIN;

// Transferred to each non-aligned chunk writer on its first use after
// recovery.
std::map<std::string, int64_t> recovered_series_end_times_;

void update_last_time(int64_t timestamp) {
last_time_ = std::max(last_time_, timestamp);
has_last_time_ = true;
Expand Down
8 changes: 5 additions & 3 deletions cpp/src/file/restorable_tsfile_io_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ void RestorableTsFileIOWriter::close() {
cgm->device_id_.reset();
}
self_check_recovered_cgm_.clear();
aligned_devices_.clear();
self_check_arena_.destroy();
}

Expand Down Expand Up @@ -813,7 +814,7 @@ int RestorableTsFileIOWriter::self_check(bool truncate_corrupted) {
// For aligned series, a time chunk implies this device
// uses aligned layout. Record it so recovered writer state
// can keep alignment behavior consistent.
aligned_devices_.insert(cur_device_id->get_table_name());
aligned_devices_.insert(cur_device_id);
}
}
continue;
Expand Down Expand Up @@ -871,8 +872,9 @@ int RestorableTsFileIOWriter::self_check(bool truncate_corrupted) {
}

bool RestorableTsFileIOWriter::is_device_aligned(
const std::string& device) const {
return aligned_devices_.find(device) != aligned_devices_.end();
const std::shared_ptr<IDeviceID>& device) const {
return device != nullptr &&
aligned_devices_.find(device) != aligned_devices_.end();
}

TsFileIOWriter* RestorableTsFileIOWriter::get_tsfile_io_writer() {
Expand Down
6 changes: 3 additions & 3 deletions cpp/src/file/restorable_tsfile_io_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ class RestorableTsFileIOWriter : public TsFileIOWriter {
int64_t get_truncated_size() const { return truncated_size_; }
std::shared_ptr<Schema> get_known_schema() { return get_schema(); }

/** True if the device was recovered as aligned (has time column). */
bool is_device_aligned(const std::string& device) const;
/** True if this device was recovered with an aligned time chunk. */
bool is_device_aligned(const std::shared_ptr<IDeviceID>& device) const;

/**
* Recovered chunk group metas from self_check (actual device_id and chunk
Expand Down Expand Up @@ -122,7 +122,7 @@ class RestorableTsFileIOWriter : public TsFileIOWriter {
bool crashed_;
bool can_write_;

std::set<std::string> aligned_devices_;
std::set<std::shared_ptr<IDeviceID>, IDeviceIDComparator> aligned_devices_;
common::PageArena self_check_arena_;
/** ChunkGroupMeta* allocated from self_check_arena_; reset device_id before
* arena destroy to avoid leak. */
Expand Down
1 change: 1 addition & 0 deletions cpp/src/writer/chunk_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ int ChunkWriter::init(const ColumnSchema& col_schema) {
int ChunkWriter::init(const std::string& measurement_name, TSDataType data_type,
TSEncoding encoding, CompressionType compression_type) {
int ret = E_OK;
has_last_timestamp_ = false;
chunk_statistic_ = StatisticFactory::alloc_statistic(data_type);
if (chunk_statistic_ == nullptr) {
return E_OOM;
Expand Down
11 changes: 11 additions & 0 deletions cpp/src/writer/chunk_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ namespace storage {
if (RET_FAIL(page_writer_.write(timestamp, value))) { \
return ret; \
} \
last_timestamp_ = timestamp; \
has_last_timestamp_ = true; \
if (RET_FAIL(seal_cur_page_if_full())) { \
return ret; \
} \
Expand Down Expand Up @@ -128,6 +130,8 @@ class ChunkWriter {
return ret;
}
offset += batch_size;
last_timestamp_ = timestamps[offset - 1];
has_last_timestamp_ = true;
if (RET_FAIL(seal_cur_page_if_full())) {
return ret;
}
Expand Down Expand Up @@ -165,6 +169,8 @@ class ChunkWriter {
return ret;
}
offset += batch_size;
last_timestamp_ = timestamps[offset - 1];
has_last_timestamp_ = true;
if (RET_FAIL(seal_cur_page_if_full())) {
return ret;
}
Expand Down Expand Up @@ -227,6 +233,11 @@ class ChunkWriter {

ChunkHeader chunk_header_;
int32_t num_of_pages_;

// Tree writes compare with the last point of this series across flushes.
friend class TsFileWriter;
int64_t last_timestamp_ = 0;
bool has_last_timestamp_ = false;
};

} // end namespace storage
Expand Down
104 changes: 60 additions & 44 deletions cpp/src/writer/tsfile_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <map>
#include <new>
#include <set>
#include <unordered_set>

#include "chunk_writer.h"
#include "common/config/config.h"
Expand Down Expand Up @@ -217,8 +218,7 @@ int TsFileWriter::init(RestorableTsFileIOWriter* rw) {
group = it->second;
} else {
group = new MeasurementSchemaGroup;
group->is_aligned_ =
rw->is_device_aligned(device_id->get_table_name());
group->is_aligned_ = rw->is_device_aligned(device_id);
schemas_.insert(std::make_pair(device_id, group));
}

Expand All @@ -229,12 +229,18 @@ int TsFileWriter::init(RestorableTsFileIOWriter* rw) {
if (cm == nullptr) {
continue;
}
// Track the highest end_time across recovered chunks so that
// appending writes can refuse out-of-order timestamps.
std::string mname = cm->measurement_name_.to_std_string();
if (cm->statistic_ != nullptr && cm->statistic_->count_ > 0) {
group->update_last_time(cm->statistic_->end_time_);
const int64_t end_time = cm->statistic_->end_time_;
if (group->is_aligned_) {
group->update_last_time(end_time);
} else {
auto entry = group->recovered_series_end_times_
.emplace(mname, end_time)
.first;
entry->second = std::max(entry->second, end_time);
}
}
std::string mname = cm->measurement_name_.to_std_string();
if (mname.empty()) {
continue;
}
Expand Down Expand Up @@ -576,6 +582,15 @@ int TsFileWriter::do_check_schema(
ms->data_type_, ms->encoding_,
ms->compression_type_);
if (IS_SUCC(ret)) {
if (enforce_recovered_last_time_order_) {
auto& ends = device_schema->recovered_series_end_times_;
auto it = ends.find(ms->measurement_name_);
if (it != ends.end()) {
ms->chunk_writer_->last_timestamp_ = it->second;
ms->chunk_writer_->has_last_timestamp_ = true;
ends.erase(it);
}
}
chunk_writers.push_back(ms->chunk_writer_);
} else {
for (size_t chunk_writer_idx = 0;
Expand Down Expand Up @@ -845,14 +860,14 @@ bool TsFileWriter::recovered_last_time_order_violated(
int TsFileWriter::write_record(const TsRecord& record) {
std::lock_guard<std::recursive_mutex> write_lock(write_mutex_);
if (UNLIKELY(unrecoverable_)) return E_DATA_INCONSISTENCY;
std::unordered_set<std::string> measurements;
for (const auto& point : record.points_) {
if (!measurements.insert(point.measurement_name_).second) {
return E_INVALID_ARG;
}
}
int ret = E_OK;
auto device_id = std::make_shared<StringArrayDeviceID>(record.device_id_);
// After recovery, refuse writes whose timestamp would land at or before
// any already-flushed chunk's end_time for this device.
if (recovered_last_time_order_violated(device_id, record.timestamp_)) {
return E_OUT_OF_ORDER;
}
// std::vector<ChunkWriter*> chunk_writers;
SimpleVector<ChunkWriter*> chunk_writers;
SimpleVector<common::TSDataType> data_types;
MeasurementNamesFromRecord mnames_getter(record);
Expand All @@ -862,6 +877,15 @@ int TsFileWriter::write_record(const TsRecord& record) {
}

ASSERT(chunk_writers.size() == record.points_.size());
for (uint32_t c = 0; c < chunk_writers.size(); ++c) {
ChunkWriter* writer = chunk_writers[c];
if (writer == nullptr) continue;
if (writer->has_last_timestamp_ &&
TimePageWriter::is_out_of_order(record.timestamp_,
writer->last_timestamp_)) {
return E_OUT_OF_ORDER;
}
}
for (uint32_t c = 0; c < chunk_writers.size(); c++) {
ChunkWriter* chunk_writer = chunk_writers[c];
if (IS_NULL(chunk_writer)) {
Expand All @@ -872,12 +896,6 @@ int TsFileWriter::write_record(const TsRecord& record) {
record.points_[c]);
}

if (enforce_recovered_last_time_order_) {
auto schema_it = schemas_.find(device_id);
if (schema_it != schemas_.end() && schema_it->second != nullptr) {
schema_it->second->update_last_time(record.timestamp_);
}
}
record_count_since_last_flush_++;
ret = check_memory_size_and_may_flush_chunks();
return ret;
Expand All @@ -886,6 +904,12 @@ int TsFileWriter::write_record(const TsRecord& record) {
int TsFileWriter::write_record_aligned(const TsRecord& record) {
std::lock_guard<std::recursive_mutex> write_lock(write_mutex_);
if (UNLIKELY(unrecoverable_)) return E_DATA_INCONSISTENCY;
std::unordered_set<std::string> measurements;
for (const auto& point : record.points_) {
if (!measurements.insert(point.measurement_name_).second) {
return E_INVALID_ARG;
}
}
int ret = E_OK;
auto device_id = std::make_shared<StringArrayDeviceID>(record.device_id_);
if (recovered_last_time_order_violated(device_id, record.timestamp_)) {
Expand All @@ -908,13 +932,6 @@ int TsFileWriter::write_record_aligned(const TsRecord& record) {
return E_DEVICE_NOT_EXIST;
}
MeasurementSchemaGroup* device_schema = dev_it->second;
// Index of the point that carries each measurement of this row. A
// duplicate point for the same measurement keeps the last one, so that
// every value column advances exactly one row per timestamp.
std::map<std::string, uint32_t> row_point_index;
for (uint32_t c = 0; c < record.points_.size(); c++) {
row_point_index[record.points_[c].measurement_name_] = c;
}
// A record may only carry a subset of the device's measurements. The
// measurements it does not mention still have to advance one row (as a
// NULL) so that every value column's not-null bitmap stays aligned with
Expand All @@ -931,7 +948,7 @@ int TsFileWriter::write_record_aligned(const TsRecord& record) {
MeasurementSchemaMap& msm = device_schema->measurement_schema_map_;
for (MeasurementSchemaMapIter ms_iter = msm.begin(); ms_iter != msm.end();
ms_iter++) {
if (row_point_index.find(ms_iter->first) != row_point_index.end()) {
if (measurements.find(ms_iter->first) != measurements.end()) {
continue;
}
MeasurementSchema* ms = ms_iter->second;
Expand Down Expand Up @@ -959,14 +976,8 @@ int TsFileWriter::write_record_aligned(const TsRecord& record) {
if (IS_NULL(value_chunk_writer)) {
continue;
}
const DataPoint& point = record.points_[c];
if (row_point_index[point.measurement_name_] != c) {
// Duplicate point for this measurement: skipped, the last one is
// written below (one row per column per timestamp).
continue;
}
if (RET_FAIL(write_point_aligned(value_chunk_writer, record.timestamp_,
data_types[c], point))) {
data_types[c], record.points_[c]))) {
// Time wrote the row but at least one value column failed
// mid-record; the per-column row counts no longer agree.
// Mark the writer unrecoverable so flush/close refuses to
Expand Down Expand Up @@ -1252,8 +1263,10 @@ int TsFileWriter::write_tablet(const Tablet& tablet) {
// and would let uninitialized timestamps/values past the live range leak
// into the chunk.
const uint32_t total_rows = tablet.get_cur_row_size();
if (total_rows > 0 && tablet.timestamps_ != nullptr &&
recovered_last_time_order_violated(device_id, tablet.timestamps_[0])) {
if (tablet.err_code_ != E_OK) return tablet.err_code_;
if (total_rows > tablet.max_row_num_) return E_OUT_OF_RANGE;
if (total_rows > 0 && tablet.timestamps_ == nullptr) return E_INVALID_ARG;
if (!TimePageWriter::is_batch_in_order(tablet.timestamps_, total_rows)) {
return E_OUT_OF_ORDER;
}
SimpleVector<ChunkWriter*> chunk_writers;
Expand All @@ -1264,15 +1277,26 @@ int TsFileWriter::write_tablet(const Tablet& tablet) {
return ret;
}
ASSERT(data_types.size() == tablet.get_column_count());
ASSERT(chunk_writers.size() == tablet.get_column_count());
for (uint32_t c = 0; c < data_types.size(); c++) {
if (data_types[c] == common::NULL_TYPE) {
continue;
}
if (data_types[c] != tablet.schema_vec_->at(c).data_type_) {
return E_TYPE_NOT_MATCH;
}
ChunkWriter* writer = chunk_writers[c];
BitMap& nulls = tablet.bitmaps_[c];
uint32_t first = 0;
while (first < total_rows && nulls.test(first)) ++first;
if (first == total_rows) continue;
const int64_t first_time = tablet.timestamps_[first];
if (writer->has_last_timestamp_ &&
TimePageWriter::is_out_of_order(first_time,
writer->last_timestamp_)) {
return E_OUT_OF_ORDER;
}
}
ASSERT(chunk_writers.size() == tablet.get_column_count());
uint32_t columns_written = 0;
for (uint32_t c = 0; c < chunk_writers.size(); c++) {
ChunkWriter* chunk_writer = chunk_writers[c];
Expand All @@ -1291,14 +1315,6 @@ int TsFileWriter::write_tablet(const Tablet& tablet) {
columns_written++;
}

if (enforce_recovered_last_time_order_ && total_rows > 0 &&
tablet.timestamps_ != nullptr) {
auto schema_it = schemas_.find(device_id);
if (schema_it != schemas_.end() && schema_it->second != nullptr) {
schema_it->second->update_last_time(
tablet.timestamps_[total_rows - 1]);
}
}
record_count_since_last_flush_ += total_rows;
ret = check_memory_size_and_may_flush_chunks();
return ret;
Expand Down
Loading
Loading