|
| 1 | +#include <library/cpp/testing/hook/hook.h> |
| 2 | +#include <library/cpp/testing/unittest/registar.h> |
| 3 | + |
| 4 | +#include <contrib/libs/apache/arrow/cpp/src/arrow/array/builder_binary.h> |
| 5 | +#include <contrib/libs/apache/arrow/cpp/src/arrow/result.h> |
| 6 | + |
| 7 | +#include <ydb/library/testlib/s3_recipe_helper/s3_recipe_helper.h> |
| 8 | + |
| 9 | +#include <ydb/apps/ydbd/export/export.h> |
| 10 | +#include <ydb/core/protos/flat_scheme_op.pb.h> |
| 11 | +#include <ydb/core/protos/s3_settings.pb.h> |
| 12 | +#include <ydb/core/testlib/basics/runtime.h> |
| 13 | +#include <ydb/core/testlib/tablet_helpers.h> |
| 14 | +#include <ydb/core/tx/columnshard/backup/async_jobs/import_downloader.h> |
| 15 | +#include <ydb/core/tx/columnshard/backup/iscan/iscan.h> |
| 16 | +#include <ydb/core/tx/columnshard/columnshard_private_events.h> |
| 17 | + |
| 18 | + |
| 19 | +namespace NKikimr { |
| 20 | + |
| 21 | +namespace { |
| 22 | + |
| 23 | +using TRuntimePtr = std::shared_ptr<TTestActorRuntime>; |
| 24 | + |
| 25 | +std::shared_ptr<arrow::RecordBatch> TestRecordBatch() { |
| 26 | + std::vector<std::string> keys = {"foo", "bar", "baz"}; |
| 27 | + std::vector<std::string> values = {"one", "two", "three"}; |
| 28 | + |
| 29 | + arrow::StringBuilder key_builder; |
| 30 | + for (const auto& k : keys) { |
| 31 | + Y_UNUSED(key_builder.Append(k)); |
| 32 | + } |
| 33 | + std::shared_ptr<arrow::Array> key_array; |
| 34 | + Y_UNUSED(key_builder.Finish(&key_array)); |
| 35 | + |
| 36 | + arrow::StringBuilder value_builder; |
| 37 | + for (const auto& v : values) { |
| 38 | + Y_UNUSED(value_builder.Append(v)); |
| 39 | + } |
| 40 | + std::shared_ptr<arrow::Array> value_array; |
| 41 | + Y_UNUSED(value_builder.Finish(&value_array)); |
| 42 | + |
| 43 | + auto schema = arrow::schema({ |
| 44 | + arrow::field("key", arrow::binary()), |
| 45 | + arrow::field("value", arrow::binary()) |
| 46 | + }); |
| 47 | + |
| 48 | + return arrow::RecordBatch::Make(schema, keys.size(), {key_array, value_array}); |
| 49 | +} |
| 50 | + |
| 51 | +NDataShard::IExport::TTableColumns MakeYdbColumns() { |
| 52 | + NDataShard::IExport::TTableColumns columns; |
| 53 | + columns[0] = NDataShard::TUserTable::TUserColumn(NScheme::TTypeInfo(NScheme::NTypeIds::String), "", "key", true); |
| 54 | + columns[1] = NDataShard::TUserTable::TUserColumn(NScheme::TTypeInfo(NScheme::NTypeIds::String), "", "value", false); |
| 55 | + return columns; |
| 56 | +} |
| 57 | + |
| 58 | +NKikimrSchemeOp::TBackupTask MakeBackupTask(const TString& bucketName) { |
| 59 | + NKikimrSchemeOp::TBackupTask backupTask; |
| 60 | + backupTask.SetEnablePermissions(true); |
| 61 | + auto& s3Settings = *backupTask.MutableS3Settings(); |
| 62 | + s3Settings.SetBucket(bucketName); |
| 63 | + s3Settings.SetEndpoint(GetEnv("S3_ENDPOINT")); |
| 64 | + auto& table = *backupTask.MutableTable(); |
| 65 | + auto& tableDescription = *table.MutableColumnTableDescription(); |
| 66 | + tableDescription.SetColumnShardCount(4); |
| 67 | + auto& col1 = *tableDescription.MutableSchema()->MutableColumns()->Add(); |
| 68 | + col1.SetName("key"); |
| 69 | + col1.SetType("String"); |
| 70 | + |
| 71 | + auto& col2 = *tableDescription.MutableSchema()->MutableColumns()->Add(); |
| 72 | + col2.SetName("value"); |
| 73 | + col2.SetType("String"); |
| 74 | + table.MutableSelf(); |
| 75 | + return backupTask; |
| 76 | +} |
| 77 | + |
| 78 | +NKikimrSchemeOp::TRestoreTask MakeRestoreTask(const TString& bucketName) { |
| 79 | + NKikimrSchemeOp::TRestoreTask restoreTask; |
| 80 | + auto& s3Settings = *restoreTask.MutableS3Settings(); |
| 81 | + s3Settings.SetBucket(bucketName); |
| 82 | + s3Settings.SetEndpoint(GetEnv("S3_ENDPOINT")); |
| 83 | + auto& description = *restoreTask.MutableTableDescription(); |
| 84 | + auto& col1 = *description.AddColumns(); |
| 85 | + col1.SetName("key"); |
| 86 | + col1.SetType("String"); |
| 87 | + col1.SetId(1); |
| 88 | + col1.SetTypeId(NScheme::NTypeIds::String); |
| 89 | + auto& col2 = *description.AddColumns(); |
| 90 | + col2.SetName("value"); |
| 91 | + col2.SetType("String"); |
| 92 | + col2.SetId(2); |
| 93 | + col2.SetTypeId(NScheme::NTypeIds::String); |
| 94 | + description.AddKeyColumnNames("key"); |
| 95 | + description.AddKeyColumnIds(1); |
| 96 | + return restoreTask; |
| 97 | +} |
| 98 | + |
| 99 | +} |
| 100 | + |
| 101 | +using namespace NColumnShard; |
| 102 | + |
| 103 | +Y_UNIT_TEST_SUITE(IScan) { |
| 104 | + |
| 105 | + Y_UNIT_TEST(MultiExport) { |
| 106 | + Aws::S3::S3Client s3Client = NTestUtils::MakeS3Client(); |
| 107 | + NTestUtils::CreateBucket("test2", s3Client); |
| 108 | + |
| 109 | + TRuntimePtr runtime(new TTestBasicRuntime()); |
| 110 | + runtime->SetLogPriority(NKikimrServices::DATASHARD_BACKUP, NActors::NLog::PRI_DEBUG); |
| 111 | + runtime->SetLogPriority(NKikimrServices::DATASHARD_RESTORE, NActors::NLog::PRI_DEBUG); |
| 112 | + SetupTabletServices(*runtime); |
| 113 | + |
| 114 | + const auto edge = runtime->AllocateEdgeActor(0); |
| 115 | + auto exportFactory = std::make_shared<TDataShardExportFactory>(); |
| 116 | + auto actor = NKikimr::NColumnShard::NBackup::CreateExportUploaderActor(edge, MakeBackupTask("test2"), exportFactory.get(), MakeYdbColumns(), 0); |
| 117 | + auto exporter = runtime->Register(actor.release()); |
| 118 | + |
| 119 | + TAutoPtr<IEventHandle> handle; |
| 120 | + runtime->DispatchEvents({}, TDuration::Seconds(1)); |
| 121 | + runtime->Send(new IEventHandle(exporter, edge, new NColumnShard::TEvPrivate::TEvBackupExportRecordBatch(TestRecordBatch(), false))); |
| 122 | + runtime->Send(new IEventHandle(exporter, edge, new NColumnShard::TEvPrivate::TEvBackupExportRecordBatch(TestRecordBatch(), true))); |
| 123 | + auto event1 = runtime->GrabEdgeEvent<NColumnShard::TEvPrivate::TEvBackupExportRecordBatchResult>(handle); |
| 124 | + UNIT_ASSERT(!event1->IsFinish); |
| 125 | + auto event2 = runtime->GrabEdgeEvent<NColumnShard::TEvPrivate::TEvBackupExportRecordBatchResult>(handle); |
| 126 | + UNIT_ASSERT(event2->IsFinish); |
| 127 | + |
| 128 | + runtime->DispatchEvents({}, TDuration::Seconds(5)); |
| 129 | + std::vector<TString> result = NTestUtils::GetObjectKeys("test2", s3Client); |
| 130 | + UNIT_ASSERT_VALUES_EQUAL(NTestUtils::GetUncommittedUploadsCount("test2", s3Client), 0); |
| 131 | + UNIT_ASSERT_VALUES_EQUAL(JoinSeq(",", result), "data_00.csv,metadata.json,permissions.pb,scheme.pb"); |
| 132 | + auto scheme = NTestUtils::GetObject("test2", "scheme.pb", s3Client); |
| 133 | + UNIT_ASSERT_VALUES_EQUAL(scheme, "columns {\n name: \"key\"\n type {\n optional_type {\n item {\n type_id: STRING\n }\n }\n }\n}\ncolumns {\n name: \"value\"\n type {\n optional_type {\n item {\n type_id: STRING\n }\n }\n }\n}\npartitioning_settings {\n min_partitions_count: 4\n}\nstore_type: STORE_TYPE_COLUMN\n"); |
| 134 | + auto metadata = NTestUtils::GetObject("test2", "metadata.json", s3Client); |
| 135 | + UNIT_ASSERT_VALUES_EQUAL(metadata, "{\"version\":0,\"full_backups\":[{\"snapshot_vts\":[0,0]}],\"permissions\":1,\"changefeeds\":[]}"); |
| 136 | + auto data = NTestUtils::GetObject("test2", "data_00.csv", s3Client); |
| 137 | + UNIT_ASSERT_VALUES_EQUAL(data, "\"foo\",\"one\"\n\"bar\",\"two\"\n\"baz\",\"three\"\n\"foo\",\"one\"\n\"bar\",\"two\"\n\"baz\",\"three\"\n"); |
| 138 | + |
| 139 | + |
| 140 | + auto restoreTask = MakeRestoreTask("test2"); |
| 141 | + auto userTable = MakeIntrusiveConst<NDataShard::TUserTable>(ui32(0), restoreTask.GetTableDescription(), ui32(0)); |
| 142 | + |
| 143 | + auto importActor = NKikimr::NColumnShard::NBackup::CreateImportDownloaderImport(edge, 0, restoreTask, NKikimr::NDataShard::TTableInfo{0, userTable}); |
| 144 | + runtime->Register(importActor.release()); |
| 145 | + runtime->DispatchEvents({}, TDuration::Seconds(1)); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +} // namespace NKikimr |
0 commit comments