-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpfs2_extension.cpp
More file actions
299 lines (245 loc) · 12.7 KB
/
Copy pathhttpfs2_extension.cpp
File metadata and controls
299 lines (245 loc) · 12.7 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#define DUCKDB_EXTENSION_MAIN
#include "duckdb.hpp"
#include "duckdb/main/setting_info.hpp"
#include "duckdb/planner/expression/bound_function_expression.hpp"
#include "httpfs2_bench.hpp"
#include "httpfs2_cost_model.hpp"
#include "httpfs2_extension.hpp"
#include "httpfs2_fake_s3_filesystem.hpp"
#include "httpfs2_filesystem_wrapper.hpp"
#include "httpfs2_state.hpp"
namespace duckdb {
static uint64_t CeilDivU64(uint64_t num, uint64_t denom) {
return denom == 0 ? 0 : (num + denom - 1) / denom;
}
static uint64_t Httpfs2ChooseFanout(uint64_t bytes, uint64_t max_fanout, uint64_t overhead_ms) {
if (max_fanout == 0) {
throw InvalidInputException("httpfs2_choose_fanout: max_fanout must be > 0");
}
if (bytes == 0) {
return 1;
}
uint64_t best_k = 1;
auto best_cost = static_cast<double>(Httpfs2EstimateS3LatencyMs(bytes));
for (uint64_t k = 2; k <= max_fanout; k++) {
const auto per_req = CeilDivU64(bytes, k);
const auto cost = static_cast<double>(Httpfs2EstimateS3LatencyMs(per_req)) + static_cast<double>((k - 1) * overhead_ms);
if (cost < best_cost) {
best_cost = cost;
best_k = k;
}
}
return best_k;
}
static void Httpfs2EstimateS3LatencyMsScalar(DataChunk &args, ExpressionState &, Vector &result) {
D_ASSERT(args.ColumnCount() == 1);
const auto count = args.size();
UnifiedVectorFormat input_data;
args.data[0].ToUnifiedFormat(count, input_data);
auto input_ptr = UnifiedVectorFormat::GetData<uint64_t>(input_data);
result.SetVectorType(VectorType::FLAT_VECTOR);
auto out_ptr = FlatVector::GetData<uint64_t>(result);
auto &validity = FlatVector::Validity(result);
for (idx_t i = 0; i < count; i++) {
const auto idx = input_data.sel->get_index(i);
if (!input_data.validity.RowIsValid(idx)) {
validity.SetInvalid(i);
continue;
}
out_ptr[i] = Httpfs2EstimateS3LatencyMs(input_ptr[idx]);
}
}
static void Httpfs2ChooseFanoutScalar(DataChunk &args, ExpressionState &, Vector &result) {
D_ASSERT(args.ColumnCount() == 3);
const auto count = args.size();
UnifiedVectorFormat bytes_data;
UnifiedVectorFormat max_fanout_data;
UnifiedVectorFormat overhead_data;
args.data[0].ToUnifiedFormat(count, bytes_data);
args.data[1].ToUnifiedFormat(count, max_fanout_data);
args.data[2].ToUnifiedFormat(count, overhead_data);
auto bytes_ptr = UnifiedVectorFormat::GetData<uint64_t>(bytes_data);
auto max_fanout_ptr = UnifiedVectorFormat::GetData<uint64_t>(max_fanout_data);
auto overhead_ptr = UnifiedVectorFormat::GetData<uint64_t>(overhead_data);
result.SetVectorType(VectorType::FLAT_VECTOR);
auto out_ptr = FlatVector::GetData<uint64_t>(result);
auto &validity = FlatVector::Validity(result);
for (idx_t i = 0; i < count; i++) {
const auto idx0 = bytes_data.sel->get_index(i);
const auto idx1 = max_fanout_data.sel->get_index(i);
const auto idx2 = overhead_data.sel->get_index(i);
if (!bytes_data.validity.RowIsValid(idx0) || !max_fanout_data.validity.RowIsValid(idx1) ||
!overhead_data.validity.RowIsValid(idx2)) {
validity.SetInvalid(i);
continue;
}
out_ptr[i] = Httpfs2ChooseFanout(bytes_ptr[idx0], max_fanout_ptr[idx1], overhead_ptr[idx2]);
}
}
static void Httpfs2WrapFileSystem(DatabaseInstance &instance) {
auto &config = DBConfig::GetConfig(instance);
if (dynamic_cast<Httpfs2FileSystemWrapper *>(config.file_system.get())) {
return;
}
auto state = Httpfs2GetOrCreateState(instance);
config.file_system = make_uniq<Httpfs2FileSystemWrapper>(std::move(config.file_system), std::move(state));
}
static shared_ptr<Httpfs2State> GetStateFromContext(ClientContext &context) {
if (!context.db) {
throw InternalException("httpfs2: missing DatabaseInstance");
}
return Httpfs2GetOrCreateState(*context.db);
}
static void UpdateEnabled(ClientContext &context, SetScope scope, Value ¶meter) {
auto state = GetStateFromContext(context);
state->config.enabled.store(parameter.GetValue<bool>());
}
static void UpdateIOThreads(ClientContext &context, SetScope scope, Value ¶meter) {
auto state = GetStateFromContext(context);
const auto v = parameter.GetValue<uint64_t>();
if (v == 0) {
throw InvalidInputException("httpfs2_parallel_read_io_threads must be > 0");
}
state->SetIOThreads(v);
}
static void UpdateChunkSize(ClientContext &context, SetScope scope, Value ¶meter) {
auto state = GetStateFromContext(context);
const auto v = parameter.GetValue<uint64_t>();
if (v == 0) {
throw InvalidInputException("httpfs2_parallel_read_chunk_size_bytes must be > 0");
}
state->config.chunk_size_bytes.store(v);
}
static void UpdateMaxFanout(ClientContext &context, SetScope scope, Value ¶meter) {
auto state = GetStateFromContext(context);
const auto v = parameter.GetValue<uint64_t>();
if (v == 0) {
throw InvalidInputException("httpfs2_parallel_read_max_fanout must be > 0");
}
state->config.max_fanout.store(v);
}
static void UpdateMinBytes(ClientContext &context, SetScope scope, Value ¶meter) {
auto state = GetStateFromContext(context);
state->config.min_bytes.store(parameter.GetValue<uint64_t>());
}
static void UpdateDedicatedHandles(ClientContext &context, SetScope scope, Value ¶meter) {
auto state = GetStateFromContext(context);
state->config.dedicated_handles.store(parameter.GetValue<bool>());
}
static void UpdateRecommendedPresetEnabled(ClientContext &context, SetScope scope, Value ¶meter) {
auto state = GetStateFromContext(context);
state->config.recommended_preset_enabled.store(parameter.GetValue<bool>());
}
static void UpdateAutoFanoutEnabled(ClientContext &context, SetScope scope, Value ¶meter) {
auto state = GetStateFromContext(context);
state->config.auto_fanout_enabled.store(parameter.GetValue<bool>());
}
static void UpdateAutoFanoutOverheadMs(ClientContext &context, SetScope scope, Value ¶meter) {
auto state = GetStateFromContext(context);
state->config.auto_fanout_overhead_ms.store(parameter.GetValue<uint64_t>());
}
static void UpdateAutoFanoutOverheadAdaptiveEnabled(ClientContext &context, SetScope scope, Value ¶meter) {
auto state = GetStateFromContext(context);
state->config.auto_fanout_overhead_adaptive_enabled.store(parameter.GetValue<bool>());
}
static void UpdateFakeS3LatencyEnabled(ClientContext &, SetScope, Value ¶meter) {
Httpfs2FakeS3FileSystem::SetLatencyEnabled(parameter.GetValue<bool>());
}
static void UpdateFakeS3LatencyScalePercent(ClientContext &, SetScope, Value ¶meter) {
Httpfs2FakeS3FileSystem::SetLatencyScalePercent(parameter.GetValue<uint64_t>());
}
static void UpdateFakeS3LatencyExtraMs(ClientContext &, SetScope, Value ¶meter) {
Httpfs2FakeS3FileSystem::SetLatencyExtraMs(parameter.GetValue<uint64_t>());
}
struct Httpfs2StateBindData : public FunctionData {
shared_ptr<Httpfs2State> state;
unique_ptr<FunctionData> Copy() const override {
auto copy = make_uniq<Httpfs2StateBindData>();
copy->state = state;
return std::move(copy);
}
bool Equals(const FunctionData &other_p) const override {
auto &other = other_p.Cast<Httpfs2StateBindData>();
return state.get() == other.state.get();
}
static Httpfs2StateBindData &GetFrom(ExpressionState &state) {
auto &func_expr = state.expr.Cast<BoundFunctionExpression>();
return func_expr.bind_info->Cast<Httpfs2StateBindData>();
}
};
static unique_ptr<FunctionData> Httpfs2StateBind(ClientContext &context, ScalarFunction &,
vector<unique_ptr<Expression>> &) {
auto bind = make_uniq<Httpfs2StateBindData>();
bind->state = GetStateFromContext(context);
return std::move(bind);
}
static void Httpfs2AutoFanoutObservedOverheadMsScalar(DataChunk &, ExpressionState &state, Vector &result) {
auto &bind = Httpfs2StateBindData::GetFrom(state);
const uint64_t ms = bind.state ? bind.state->observed_overhead_ms.load() : 0;
result.SetVectorType(VectorType::CONSTANT_VECTOR);
ConstantVector::GetData<uint64_t>(result)[0] = ms;
}
void Httpfs2Extension::Load(ExtensionLoader &loader) {
auto &instance = loader.GetDatabaseInstance();
Httpfs2GetOrCreateState(instance);
Httpfs2WrapFileSystem(instance);
auto &config = DBConfig::GetConfig(instance);
config.file_system->RegisterSubSystem(make_uniq<Httpfs2FakeS3FileSystem>());
loader.RegisterFunction(Httpfs2BenchReadFunction());
loader.RegisterFunction(Httpfs2BenchReadManyFunction());
loader.RegisterFunction(Httpfs2BenchReadManyPoolFunction());
loader.RegisterFunction(ScalarFunction("httpfs2_estimate_s3_latency_ms", {LogicalType::UBIGINT}, LogicalType::UBIGINT,
Httpfs2EstimateS3LatencyMsScalar));
loader.RegisterFunction(ScalarFunction("httpfs2_choose_fanout",
{LogicalType::UBIGINT, LogicalType::UBIGINT, LogicalType::UBIGINT},
LogicalType::UBIGINT, Httpfs2ChooseFanoutScalar));
ScalarFunction observed_overhead_fun({}, LogicalType::UBIGINT, Httpfs2AutoFanoutObservedOverheadMsScalar,
Httpfs2StateBind, nullptr, nullptr, nullptr,
LogicalType(LogicalTypeId::INVALID), FunctionStability::VOLATILE);
observed_overhead_fun.name = "httpfs2_auto_fanout_observed_overhead_ms";
loader.RegisterFunction(observed_overhead_fun);
config.AddExtensionOption("httpfs2_parallel_read_enabled", "Enable httpfs2 parallel reads for remote files.",
LogicalTypeId::BOOLEAN, Value::BOOLEAN(false), UpdateEnabled);
config.AddExtensionOption("httpfs2_parallel_read_io_threads",
"Number of httpfs2 IO worker threads (separate from DuckDB threads).",
LogicalTypeId::UBIGINT, Value::UBIGINT(16), UpdateIOThreads);
config.AddExtensionOption("httpfs2_parallel_read_chunk_size_bytes",
"Target chunk size used when splitting a single Read into parallel sub-reads.",
LogicalTypeId::UBIGINT, Value::UBIGINT(16ULL * 1024ULL * 1024ULL), UpdateChunkSize);
config.AddExtensionOption("httpfs2_parallel_read_max_fanout",
"Max number of parallel sub-reads issued for a single Read call.",
LogicalTypeId::UBIGINT, Value::UBIGINT(4), UpdateMaxFanout);
config.AddExtensionOption("httpfs2_parallel_read_min_bytes",
"Minimum Read size (bytes) before httpfs2 starts splitting into parallel sub-reads.",
LogicalTypeId::UBIGINT, Value::UBIGINT(2ULL * 1024ULL * 1024ULL), UpdateMinBytes);
config.AddExtensionOption("httpfs2_parallel_read_dedicated_handles",
"Use multiple underlying file handles per remote file to reduce internal httpfs contention.",
LogicalTypeId::BOOLEAN, Value::BOOLEAN(false), UpdateDedicatedHandles);
config.AddExtensionOption("httpfs2_parallel_read_recommended_preset_enabled",
"If enabled, uses the recommended preset: fanout=2, chunk=16MiB, min_bytes=2MiB, no auto-fanout, no dedicated handles.",
LogicalTypeId::BOOLEAN, Value::BOOLEAN(false), UpdateRecommendedPresetEnabled);
config.AddExtensionOption("httpfs2_parallel_read_auto_fanout_enabled",
"If enabled, httpfs2 chooses a smaller fanout per Read using a simple S3 cost model.",
LogicalTypeId::BOOLEAN, Value::BOOLEAN(false), UpdateAutoFanoutEnabled);
config.AddExtensionOption("httpfs2_parallel_read_auto_fanout_overhead_ms",
"Penalty (ms) per extra subrequest used by auto fanout selection.",
LogicalTypeId::UBIGINT, Value::UBIGINT(500), UpdateAutoFanoutOverheadMs);
config.AddExtensionOption("httpfs2_parallel_read_auto_fanout_overhead_adaptive_enabled",
"If enabled, httpfs2 learns extra per-request overhead from observed timings and uses it in auto-fanout selection.",
LogicalTypeId::BOOLEAN, Value::BOOLEAN(false), UpdateAutoFanoutOverheadAdaptiveEnabled);
config.AddExtensionOption("httpfs2_fake_s3_latency_enabled",
"If enabled, fake_s3:// reads sleep to simulate S3-style latency and throughput.",
LogicalTypeId::BOOLEAN, Value::BOOLEAN(true), UpdateFakeS3LatencyEnabled);
config.AddExtensionOption("httpfs2_fake_s3_latency_scale_percent",
"Scale factor (percent) applied to fake_s3:// simulated latency.",
LogicalTypeId::UBIGINT, Value::UBIGINT(100), UpdateFakeS3LatencyScalePercent);
config.AddExtensionOption("httpfs2_fake_s3_latency_extra_ms",
"Extra constant latency (ms) added to fake_s3:// simulated reads.",
LogicalTypeId::UBIGINT, Value::UBIGINT(0), UpdateFakeS3LatencyExtraMs);
}
} // namespace duckdb
extern "C" {
DUCKDB_CPP_EXTENSION_ENTRY(httpfs2, loader) {
duckdb::Httpfs2Extension().Load(loader);
}
}