aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/core/tx/data_events/columnshard_splitter.h
blob: 264697973ad00856fed26ceeefd66fa8c7c16670 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#pragma once

#include "shards_splitter.h"

#include <ydb/core/tx/sharding/sharding.h>
#include <ydb/core/tx/columnshard/columnshard.h>
#include <ydb/core/formats/arrow/size_calcer.h>
#include <ydb/core/formats/arrow/arrow_helpers.h>
#include <ydb/core/scheme/scheme_types_proto.h>


namespace NKikimr::NEvWrite {

class TColumnShardShardsSplitter : public IShardsSplitter {
    class TShardInfo : public IShardInfo {
    private:
        const TString SchemaData;
        const TString Data;
        const ui32 RowsCount;
    public:
        TShardInfo(const TString& schemaData, const TString& data, const ui32 rowsCount)
            : SchemaData(schemaData)
            , Data(data)
            , RowsCount(rowsCount)
        {}

        ui64 GetBytes() const override {
            return Data.size();
        }

        ui32 GetRowsCount() const override {
            return RowsCount;
        }

        void Serialize(TEvWrite& evWrite) const override {
            evWrite.SetArrowData(SchemaData, Data);
        }
    };

private:
    TYdbConclusionStatus DoSplitData(const NSchemeCache::TSchemeCacheNavigate::TEntry& schemeEntry, const IEvWriteDataAccessor& data) override {
        if (schemeEntry.Kind != NSchemeCache::TSchemeCacheNavigate::KindColumnTable) {
            return TYdbConclusionStatus::Fail(Ydb::StatusIds::SCHEME_ERROR, "The specified path is not an column table");
        }

        if (!schemeEntry.ColumnTableInfo) {
            return TYdbConclusionStatus::Fail(Ydb::StatusIds::SCHEME_ERROR, "Column table expected");
        }

        const auto& description = schemeEntry.ColumnTableInfo->Description;
        if (!description.HasSharding()) {
            return TYdbConclusionStatus::Fail(Ydb::StatusIds::SCHEME_ERROR, "Unknown sharding method is not supported");
        }
        if (!description.HasSchema()) {
            return TYdbConclusionStatus::Fail(Ydb::StatusIds::SCHEME_ERROR, "Unknown schema for column table");
        }

        const auto& scheme = description.GetSchema();
        const auto& sharding = description.GetSharding();

        if (sharding.ColumnShardsSize() == 0) {
            return TYdbConclusionStatus::Fail(Ydb::StatusIds::SCHEME_ERROR, "No shards to write to");
        }
        if (!scheme.HasEngine() || scheme.GetEngine() == NKikimrSchemeOp::COLUMN_ENGINE_NONE) {
            return TYdbConclusionStatus::Fail(Ydb::StatusIds::SCHEME_ERROR, "Wrong column table configuration");
        }

        if (sharding.HasRandomSharding()) {
            return SplitRandom(data, scheme, sharding);
        } else if (sharding.HasHashSharding()) {
            return SplitByHash(data, scheme, sharding);
        }
        return TYdbConclusionStatus::Fail(Ydb::StatusIds::SCHEME_ERROR, "Sharding method is not supported");
    }

private:
    TYdbConclusionStatus SplitByHash(const IEvWriteDataAccessor& data, const NKikimrSchemeOp::TColumnTableSchema& scheme, const NKikimrSchemeOp::TColumnTableSharding& sharding) {
        if (scheme.GetEngine() != NKikimrSchemeOp::COLUMN_ENGINE_REPLACING_TIMESERIES) {
            return TYdbConclusionStatus::Fail(Ydb::StatusIds::SCHEME_ERROR, "Wrong column table configuration fro hash sharding");
        }

        if (data.HasDeserializedBatch()) {
            return SplitByHashImpl(data.GetDeserializedBatch(), sharding);
        }

        std::shared_ptr<arrow::Schema> arrowScheme = ExtractArrowSchema(scheme);
        std::shared_ptr<arrow::RecordBatch> batch = NArrow::DeserializeBatch(data.GetSerializedData(), arrowScheme);
        if (!batch) {
            return TYdbConclusionStatus::Fail(Ydb::StatusIds::SCHEME_ERROR, TString("cannot deserialize batch with schema ") + arrowScheme->ToString());
        }

        auto res = batch->ValidateFull();
        if (!res.ok()) {
            return TYdbConclusionStatus::Fail(Ydb::StatusIds::SCHEME_ERROR, TString("deserialize batch is not valid: ") + res.ToString());
        }
        return SplitByHashImpl(batch, sharding);
    }

    TYdbConclusionStatus SplitRandom(const IEvWriteDataAccessor& data, const NKikimrSchemeOp::TColumnTableSchema& scheme, const NKikimrSchemeOp::TColumnTableSharding& sharding) {
        const ui64 shardId = sharding.GetColumnShards(RandomNumber<ui32>(sharding.ColumnShardsSize()));
        FullSplitData.emplace(sharding.ColumnShardsSize());
        if (data.HasDeserializedBatch()) {
            FullSplitData->AddShardInfo(shardId, std::make_shared<TShardInfo>(NArrow::SerializeSchema(*data.GetDeserializedBatch()->schema()), data.GetSerializedData(), 0));
        } else {
            FullSplitData->AddShardInfo(shardId, std::make_shared<TShardInfo>(NArrow::SerializeSchema(*ExtractArrowSchema(scheme)), data.GetSerializedData(), 0));
        }
        return TYdbConclusionStatus::Success();
    }

    TYdbConclusionStatus SplitByHashImpl(const std::shared_ptr<arrow::RecordBatch>& batch, const NKikimrSchemeOp::TColumnTableSharding& descSharding) {
        Y_ABORT_UNLESS(descSharding.HasHashSharding());
        Y_ABORT_UNLESS(batch);

        TVector<ui64> tabletIds(descSharding.GetColumnShards().begin(), descSharding.GetColumnShards().end());
        const ui32 numShards = tabletIds.size();
        Y_ABORT_UNLESS(numShards);

        TFullSplitData result(numShards);
        if (numShards == 1) {
            NArrow::TBatchSplitttingContext context(NColumnShard::TLimits::GetMaxBlobSize() * 0.875);
            NArrow::TSplitBlobResult blobsSplitted = NArrow::SplitByBlobSize(batch, context);
            if (!blobsSplitted) {
                return TYdbConclusionStatus::Fail(Ydb::StatusIds::SCHEME_ERROR, "cannot split batch in according to limits: " + blobsSplitted.GetErrorMessage());
            }
            for (auto&& b : blobsSplitted.GetResult()) {
                result.AddShardInfo(tabletIds.front(), std::make_shared<TShardInfo>(b.GetSchemaData(), b.GetData(), b.GetRowsCount()));
            }
            FullSplitData = result;
            return TYdbConclusionStatus::Success();
        }

        auto sharding = NSharding::TShardingBase::BuildShardingOperator(descSharding);
        std::vector<ui32> rowSharding;
        if (sharding) {
            rowSharding = sharding->MakeSharding(batch);
        }
        if (rowSharding.empty()) {
            return TYdbConclusionStatus::Fail(Ydb::StatusIds::SCHEME_ERROR, "empty "
                + NKikimrSchemeOp::TColumnTableSharding::THashSharding::EHashFunction_Name(descSharding.GetHashSharding().GetFunction())
                + " sharding (" + (sharding ? sharding->DebugString() : "no sharding object") + ")");
        }

        std::vector<std::shared_ptr<arrow::RecordBatch>> sharded = NArrow::ShardingSplit(batch, rowSharding, numShards);
        Y_ABORT_UNLESS(sharded.size() == numShards);

        THashMap<ui64, TString> out;
        for (size_t i = 0; i < sharded.size(); ++i) {
            if (sharded[i]) {
                NArrow::TBatchSplitttingContext context(NColumnShard::TLimits::GetMaxBlobSize() * 0.875);
                NArrow::TSplitBlobResult blobsSplitted = NArrow::SplitByBlobSize(sharded[i], context);
                if (!blobsSplitted) {
                    return TYdbConclusionStatus::Fail(Ydb::StatusIds::SCHEME_ERROR, "cannot split batch in according to limits: " + blobsSplitted.GetErrorMessage());
                }
                for (auto&& b : blobsSplitted.GetResult()) {
                    result.AddShardInfo(tabletIds[i], std::make_shared<TShardInfo>(b.GetSchemaData(), b.GetData(), b.GetRowsCount()));
                }
            }
        }

        Y_ABORT_UNLESS(result.GetShardsInfo().size());
        FullSplitData = result;
        return TYdbConclusionStatus::Success();
    }

    std::shared_ptr<arrow::Schema> ExtractArrowSchema(const NKikimrSchemeOp::TColumnTableSchema& schema) {
        TVector<std::pair<TString, NScheme::TTypeInfo>> columns;
        for (auto& col : schema.GetColumns()) {
            Y_ABORT_UNLESS(col.HasTypeId());
            auto typeInfoMod = NScheme::TypeInfoModFromProtoColumnType(col.GetTypeId(),
                col.HasTypeInfo() ? &col.GetTypeInfo() : nullptr);
            columns.emplace_back(col.GetName(), typeInfoMod.TypeInfo);
        }
        return NArrow::MakeArrowSchema(columns);
    }
};
}