2022-10-26 14:24:49 +00:00
|
|
|
use std::{collections::BTreeSet, iter, string::String, sync::Arc};
|
|
|
|
|
2022-02-18 14:04:39 +00:00
|
|
|
use assert_matches::assert_matches;
|
2022-10-27 08:14:20 +00:00
|
|
|
use data_types::{
|
|
|
|
ColumnType, PartitionTemplate, QueryPoolId, ShardIndex, TableId, TemplatePart, TopicId,
|
|
|
|
};
|
2022-02-18 14:04:39 +00:00
|
|
|
use dml::DmlOperation;
|
2022-10-28 10:37:05 +00:00
|
|
|
use futures::{stream::FuturesUnordered, StreamExt};
|
2022-02-18 14:04:39 +00:00
|
|
|
use hashbrown::HashMap;
|
|
|
|
use hyper::{Body, Request, StatusCode};
|
2022-02-25 15:10:23 +00:00
|
|
|
use iox_catalog::{interface::Catalog, mem::MemCatalog};
|
2022-11-17 20:55:58 +00:00
|
|
|
use iox_time::{SystemProvider, TimeProvider};
|
2022-06-09 17:07:45 +00:00
|
|
|
use metric::{Attributes, DurationHistogram, Metric, Registry, U64Counter};
|
2022-02-18 14:04:39 +00:00
|
|
|
use mutable_batch::MutableBatch;
|
2022-05-06 18:51:52 +00:00
|
|
|
use router::{
|
2022-02-18 14:04:39 +00:00
|
|
|
dml_handlers::{
|
2022-10-26 14:33:01 +00:00
|
|
|
Chain, DmlError, DmlHandlerChainExt, FanOutAdaptor, InstrumentationDecorator, Partitioned,
|
2022-11-17 20:55:58 +00:00
|
|
|
Partitioner, RetentionError, RetentionValidator, SchemaError, SchemaValidator,
|
|
|
|
ShardedWriteBuffer, WriteSummaryAdapter,
|
2022-02-18 14:04:39 +00:00
|
|
|
},
|
|
|
|
namespace_cache::{MemoryNamespaceCache, ShardedCache},
|
2022-12-07 16:12:00 +00:00
|
|
|
namespace_resolver::{MissingNamespaceAction, NamespaceAutocreation, NamespaceSchemaResolver},
|
2022-02-18 14:04:39 +00:00
|
|
|
server::http::HttpDelegate,
|
2022-08-19 21:19:28 +00:00
|
|
|
shard::Shard,
|
2022-02-18 14:04:39 +00:00
|
|
|
};
|
2022-06-09 19:10:16 +00:00
|
|
|
use sharder::JumpHash;
|
2022-02-25 15:10:23 +00:00
|
|
|
use write_buffer::{
|
|
|
|
core::WriteBufferWriting,
|
|
|
|
mock::{MockBufferForWriting, MockBufferSharedState},
|
|
|
|
};
|
2022-02-18 14:04:39 +00:00
|
|
|
|
2022-08-25 20:17:15 +00:00
|
|
|
/// The topic catalog ID assigned by the namespace auto-creator in the
|
2022-02-18 14:04:39 +00:00
|
|
|
/// handler stack for namespaces it has not yet observed.
|
2022-08-25 20:17:15 +00:00
|
|
|
const TEST_TOPIC_ID: i64 = 1;
|
2022-02-18 14:04:39 +00:00
|
|
|
|
|
|
|
/// The query pool catalog ID assigned by the namespace auto-creator in the
|
|
|
|
/// handler stack for namespaces it has not yet observed.
|
2022-04-25 16:49:34 +00:00
|
|
|
const TEST_QUERY_POOL_ID: i64 = 1;
|
2022-02-18 14:04:39 +00:00
|
|
|
|
2022-11-18 13:02:12 +00:00
|
|
|
/// Common retention period value we'll use in tests
|
|
|
|
const TEST_RETENTION_PERIOD_NS: Option<i64> = Some(3_600 * 1_000_000_000);
|
2022-11-17 20:55:58 +00:00
|
|
|
|
2022-02-18 14:04:39 +00:00
|
|
|
pub struct TestContext {
|
|
|
|
delegate: HttpDelegateStack,
|
|
|
|
catalog: Arc<dyn Catalog>,
|
|
|
|
write_buffer_state: Arc<MockBufferSharedState>,
|
|
|
|
metrics: Arc<Registry>,
|
|
|
|
}
|
|
|
|
|
|
|
|
// This mass of words is certainly a downside of chained handlers.
|
|
|
|
//
|
|
|
|
// Fortunately the compiler errors are very descriptive and updating this is
|
|
|
|
// relatively easy when something changes!
|
|
|
|
type HttpDelegateStack = HttpDelegate<
|
|
|
|
InstrumentationDecorator<
|
|
|
|
Chain<
|
2022-11-17 20:55:58 +00:00
|
|
|
Chain<
|
|
|
|
Chain<
|
|
|
|
RetentionValidator<Arc<ShardedCache<Arc<MemoryNamespaceCache>>>>,
|
|
|
|
SchemaValidator<Arc<ShardedCache<Arc<MemoryNamespaceCache>>>>,
|
|
|
|
>,
|
|
|
|
Partitioner,
|
|
|
|
>,
|
2022-04-02 10:34:51 +00:00
|
|
|
WriteSummaryAdapter<
|
|
|
|
FanOutAdaptor<
|
2022-08-19 21:19:28 +00:00
|
|
|
ShardedWriteBuffer<JumpHash<Arc<Shard>>>,
|
2022-10-27 08:14:20 +00:00
|
|
|
Vec<Partitioned<HashMap<TableId, (String, MutableBatch)>>>,
|
2022-04-02 10:34:51 +00:00
|
|
|
>,
|
2022-02-18 14:04:39 +00:00
|
|
|
>,
|
|
|
|
>,
|
|
|
|
>,
|
2022-10-26 14:33:01 +00:00
|
|
|
NamespaceAutocreation<
|
|
|
|
Arc<ShardedCache<Arc<MemoryNamespaceCache>>>,
|
|
|
|
NamespaceSchemaResolver<Arc<ShardedCache<Arc<MemoryNamespaceCache>>>>,
|
|
|
|
>,
|
2022-02-18 14:04:39 +00:00
|
|
|
>;
|
|
|
|
|
2022-05-06 18:51:52 +00:00
|
|
|
/// A [`router`] stack configured with the various DML handlers using mock
|
2022-02-18 14:04:39 +00:00
|
|
|
/// catalog / write buffer backends.
|
|
|
|
impl TestContext {
|
2022-12-07 16:12:00 +00:00
|
|
|
pub fn new(autocreate_ns: bool, ns_autocreate_retention_period_ns: Option<i64>) -> Self {
|
2022-02-18 14:04:39 +00:00
|
|
|
let metrics = Arc::new(metric::Registry::default());
|
2022-11-14 13:34:09 +00:00
|
|
|
let time = iox_time::MockProvider::new(
|
|
|
|
iox_time::Time::from_timestamp_millis(668563200000).unwrap(),
|
|
|
|
);
|
2022-02-18 14:04:39 +00:00
|
|
|
|
|
|
|
let write_buffer = MockBufferForWriting::new(
|
2022-08-19 21:19:28 +00:00
|
|
|
MockBufferSharedState::empty_with_n_shards(1.try_into().unwrap()),
|
2022-02-18 14:04:39 +00:00
|
|
|
None,
|
|
|
|
Arc::new(time),
|
|
|
|
)
|
|
|
|
.expect("failed to init mock write buffer");
|
|
|
|
let write_buffer_state = write_buffer.state();
|
|
|
|
let write_buffer: Arc<dyn WriteBufferWriting> = Arc::new(write_buffer);
|
|
|
|
|
2022-08-19 21:19:28 +00:00
|
|
|
let shards: BTreeSet<_> = write_buffer.shard_indexes();
|
2022-08-24 11:16:04 +00:00
|
|
|
let sharded_write_buffer = ShardedWriteBuffer::new(JumpHash::new(
|
|
|
|
shards
|
|
|
|
.into_iter()
|
2022-08-19 21:19:28 +00:00
|
|
|
.map(|shard_index| Shard::new(shard_index, Arc::clone(&write_buffer), &metrics))
|
2022-08-24 11:16:04 +00:00
|
|
|
.map(Arc::new),
|
|
|
|
));
|
2022-02-18 14:04:39 +00:00
|
|
|
|
2022-03-01 11:33:54 +00:00
|
|
|
let catalog: Arc<dyn Catalog> = Arc::new(MemCatalog::new(Arc::clone(&metrics)));
|
2022-08-24 11:16:04 +00:00
|
|
|
let ns_cache = Arc::new(ShardedCache::new(
|
|
|
|
iter::repeat_with(|| Arc::new(MemoryNamespaceCache::default())).take(10),
|
|
|
|
));
|
2022-02-18 14:04:39 +00:00
|
|
|
|
2022-11-17 20:55:58 +00:00
|
|
|
let retention_validator =
|
|
|
|
RetentionValidator::new(Arc::clone(&catalog), Arc::clone(&ns_cache));
|
2022-10-26 14:33:01 +00:00
|
|
|
let schema_validator =
|
2022-11-04 15:38:51 +00:00
|
|
|
SchemaValidator::new(Arc::clone(&catalog), Arc::clone(&ns_cache), &metrics);
|
2022-02-18 14:04:39 +00:00
|
|
|
let partitioner = Partitioner::new(PartitionTemplate {
|
|
|
|
parts: vec![TemplatePart::TimeFormat("%Y-%m-%d".to_owned())],
|
|
|
|
});
|
|
|
|
|
2022-11-17 20:55:58 +00:00
|
|
|
let handler_stack = retention_validator
|
|
|
|
.and_then(schema_validator)
|
|
|
|
.and_then(partitioner)
|
|
|
|
.and_then(WriteSummaryAdapter::new(FanOutAdaptor::new(
|
|
|
|
sharded_write_buffer,
|
|
|
|
)));
|
2022-02-18 14:04:39 +00:00
|
|
|
|
2022-11-04 15:38:51 +00:00
|
|
|
let handler_stack = InstrumentationDecorator::new("request", &metrics, handler_stack);
|
2022-02-18 14:04:39 +00:00
|
|
|
|
2022-10-26 14:33:01 +00:00
|
|
|
let namespace_resolver =
|
|
|
|
NamespaceSchemaResolver::new(Arc::clone(&catalog), Arc::clone(&ns_cache));
|
|
|
|
let namespace_resolver = NamespaceAutocreation::new(
|
|
|
|
namespace_resolver,
|
|
|
|
Arc::clone(&ns_cache),
|
|
|
|
Arc::clone(&catalog),
|
|
|
|
TopicId::new(TEST_TOPIC_ID),
|
|
|
|
QueryPoolId::new(TEST_QUERY_POOL_ID),
|
2022-12-07 16:12:00 +00:00
|
|
|
{
|
|
|
|
if autocreate_ns {
|
|
|
|
MissingNamespaceAction::AutoCreate(ns_autocreate_retention_period_ns)
|
|
|
|
} else {
|
|
|
|
MissingNamespaceAction::Reject
|
|
|
|
}
|
|
|
|
},
|
2022-10-26 14:33:01 +00:00
|
|
|
);
|
|
|
|
|
2022-10-26 14:38:52 +00:00
|
|
|
let delegate = HttpDelegate::new(1024, 100, namespace_resolver, handler_stack, &metrics);
|
2022-02-18 14:04:39 +00:00
|
|
|
|
|
|
|
Self {
|
|
|
|
delegate,
|
|
|
|
catalog,
|
|
|
|
write_buffer_state,
|
|
|
|
metrics,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a reference to the test context's delegate.
|
|
|
|
pub fn delegate(&self) -> &HttpDelegateStack {
|
|
|
|
&self.delegate
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a reference to the test context's catalog.
|
2022-10-28 10:37:05 +00:00
|
|
|
pub fn catalog(&self) -> Arc<dyn Catalog> {
|
|
|
|
Arc::clone(&self.catalog)
|
2022-02-18 14:04:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a reference to the test context's write buffer state.
|
|
|
|
pub fn write_buffer_state(&self) -> &Arc<MockBufferSharedState> {
|
|
|
|
&self.write_buffer_state
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a reference to the test context's metrics.
|
|
|
|
pub fn metrics(&self) -> &Registry {
|
|
|
|
self.metrics.as_ref()
|
|
|
|
}
|
2022-11-16 16:43:23 +00:00
|
|
|
|
|
|
|
/// Return the [`TableId`] in the catalog for `name` in `namespace`, or panic.
|
|
|
|
pub async fn table_id(&self, namespace: &str, name: &str) -> TableId {
|
|
|
|
let mut repos = self.catalog.repositories().await;
|
|
|
|
let namespace_id = repos
|
|
|
|
.namespaces()
|
|
|
|
.get_by_name(namespace)
|
|
|
|
.await
|
|
|
|
.expect("query failed")
|
|
|
|
.expect("namespace does not exist")
|
|
|
|
.id;
|
|
|
|
|
|
|
|
repos
|
|
|
|
.tables()
|
|
|
|
.get_by_namespace_and_name(namespace_id, name)
|
|
|
|
.await
|
|
|
|
.expect("query failed")
|
|
|
|
.expect("no table entry for the specified namespace/table name pair")
|
|
|
|
.id
|
|
|
|
}
|
2022-02-18 14:04:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TestContext {
|
|
|
|
fn default() -> Self {
|
2022-12-07 16:12:00 +00:00
|
|
|
Self::new(true, None)
|
2022-02-18 14:04:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_write_ok() {
|
2022-12-07 16:12:00 +00:00
|
|
|
let ctx = TestContext::new(true, None);
|
2022-02-18 14:04:39 +00:00
|
|
|
|
2022-11-17 20:55:58 +00:00
|
|
|
// Write data inside retention period
|
|
|
|
let now = SystemProvider::default()
|
|
|
|
.now()
|
|
|
|
.timestamp_nanos()
|
|
|
|
.to_string();
|
|
|
|
let lp = "platanos,tag1=A,tag2=B val=42i ".to_string() + &now;
|
|
|
|
|
2022-02-18 14:04:39 +00:00
|
|
|
let request = Request::builder()
|
|
|
|
.uri("https://bananas.example/api/v2/write?org=bananas&bucket=test")
|
|
|
|
.method("POST")
|
2022-11-17 20:55:58 +00:00
|
|
|
.body(Body::from(lp))
|
2022-02-18 14:04:39 +00:00
|
|
|
.expect("failed to construct HTTP request");
|
|
|
|
|
|
|
|
let response = ctx
|
|
|
|
.delegate()
|
|
|
|
.route(request)
|
|
|
|
.await
|
|
|
|
.expect("LP write request failed");
|
|
|
|
|
|
|
|
assert_eq!(response.status(), StatusCode::NO_CONTENT);
|
|
|
|
|
|
|
|
// Check the write buffer observed the correct write.
|
2022-08-19 21:19:28 +00:00
|
|
|
let writes = ctx.write_buffer_state().get_messages(ShardIndex::new(0));
|
2022-02-18 14:04:39 +00:00
|
|
|
assert_eq!(writes.len(), 1);
|
|
|
|
assert_matches!(writes.as_slice(), [Ok(DmlOperation::Write(w))] => {
|
2022-11-16 16:43:23 +00:00
|
|
|
let table_id = ctx.table_id("bananas_test", "platanos").await;
|
|
|
|
assert!(w.table(&table_id).is_some());
|
2022-02-18 14:04:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Ensure the catalog saw the namespace creation
|
|
|
|
let ns = ctx
|
|
|
|
.catalog()
|
|
|
|
.repositories()
|
|
|
|
.await
|
|
|
|
.namespaces()
|
|
|
|
.get_by_name("bananas_test")
|
|
|
|
.await
|
|
|
|
.expect("query should succeed")
|
|
|
|
.expect("namespace not found");
|
|
|
|
assert_eq!(ns.name, "bananas_test");
|
2022-08-25 20:17:15 +00:00
|
|
|
assert_eq!(ns.topic_id, TopicId::new(TEST_TOPIC_ID));
|
2022-02-18 14:04:39 +00:00
|
|
|
assert_eq!(ns.query_pool_id, QueryPoolId::new(TEST_QUERY_POOL_ID));
|
2022-11-18 13:02:12 +00:00
|
|
|
assert_eq!(ns.retention_period_ns, None);
|
2022-02-18 14:04:39 +00:00
|
|
|
|
|
|
|
// Ensure the metric instrumentation was hit
|
|
|
|
let histogram = ctx
|
|
|
|
.metrics()
|
2022-06-09 17:07:45 +00:00
|
|
|
.get_instrument::<Metric<DurationHistogram>>("dml_handler_write_duration")
|
2022-02-18 14:04:39 +00:00
|
|
|
.expect("failed to read metric")
|
|
|
|
.get_observer(&Attributes::from(&[
|
|
|
|
("handler", "request"),
|
|
|
|
("result", "success"),
|
|
|
|
]))
|
|
|
|
.expect("failed to get observer")
|
|
|
|
.fetch();
|
2022-06-14 17:58:19 +00:00
|
|
|
let hit_count = histogram.sample_count();
|
2022-02-18 14:04:39 +00:00
|
|
|
assert_eq!(hit_count, 1);
|
2022-03-02 12:42:31 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
ctx.metrics()
|
2022-11-07 10:04:31 +00:00
|
|
|
.get_instrument::<Metric<U64Counter>>("http_write_lines")
|
2022-03-02 12:42:31 +00:00
|
|
|
.expect("failed to read metric")
|
|
|
|
.get_observer(&Attributes::from(&[]))
|
|
|
|
.expect("failed to get observer")
|
|
|
|
.fetch(),
|
|
|
|
1
|
|
|
|
);
|
2022-03-03 16:24:51 +00:00
|
|
|
|
|
|
|
let histogram = ctx
|
|
|
|
.metrics()
|
2022-08-19 21:19:28 +00:00
|
|
|
.get_instrument::<Metric<DurationHistogram>>("shard_enqueue_duration")
|
2022-03-03 16:24:51 +00:00
|
|
|
.expect("failed to read metric")
|
|
|
|
.get_observer(&Attributes::from(&[
|
2022-05-23 19:11:58 +00:00
|
|
|
("kafka_partition", "0"),
|
2022-03-03 16:24:51 +00:00
|
|
|
("result", "success"),
|
|
|
|
]))
|
|
|
|
.expect("failed to get observer")
|
|
|
|
.fetch();
|
2022-06-14 17:58:19 +00:00
|
|
|
let hit_count = histogram.sample_count();
|
2022-03-03 16:24:51 +00:00
|
|
|
assert_eq!(hit_count, 1);
|
2022-02-18 14:04:39 +00:00
|
|
|
}
|
2022-03-25 10:39:30 +00:00
|
|
|
|
2022-11-17 20:55:58 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_write_outside_retention_period() {
|
2022-12-07 16:12:00 +00:00
|
|
|
let ctx = TestContext::new(true, TEST_RETENTION_PERIOD_NS);
|
2022-11-17 20:55:58 +00:00
|
|
|
|
|
|
|
// Write data outside retention period into a new table
|
|
|
|
let two_hours_ago =
|
|
|
|
(SystemProvider::default().now().timestamp_nanos() - 2 * 3_600 * 1_000_000_000).to_string();
|
|
|
|
let lp = "apple,tag1=AAA,tag2=BBB val=422i ".to_string() + &two_hours_ago;
|
|
|
|
|
|
|
|
let request = Request::builder()
|
|
|
|
.uri("https://bananas.example/api/v2/write?org=bananas&bucket=test")
|
|
|
|
.method("POST")
|
|
|
|
.body(Body::from(lp))
|
|
|
|
.expect("failed to construct HTTP request");
|
|
|
|
|
|
|
|
let err = ctx
|
|
|
|
.delegate()
|
|
|
|
.route(request)
|
|
|
|
.await
|
|
|
|
.expect_err("LP write request should fail");
|
|
|
|
|
|
|
|
assert_matches!(
|
|
|
|
&err,
|
|
|
|
router::server::http::Error::DmlHandler(
|
|
|
|
DmlError::Retention(
|
|
|
|
RetentionError::OutsideRetention(e))
|
|
|
|
) => {
|
|
|
|
assert_eq!(e, "apple");
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(err.as_status_code(), StatusCode::FORBIDDEN);
|
|
|
|
}
|
|
|
|
|
2022-03-25 10:39:30 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_schema_conflict() {
|
2022-12-07 16:12:00 +00:00
|
|
|
let ctx = TestContext::new(true, None);
|
2022-03-25 10:39:30 +00:00
|
|
|
|
2022-11-17 20:55:58 +00:00
|
|
|
// data inside the retention period
|
|
|
|
let now = SystemProvider::default()
|
|
|
|
.now()
|
|
|
|
.timestamp_nanos()
|
|
|
|
.to_string();
|
|
|
|
let lp = "platanos,tag1=A,tag2=B val=42i ".to_string() + &now;
|
|
|
|
|
2022-03-25 10:39:30 +00:00
|
|
|
let request = Request::builder()
|
|
|
|
.uri("https://bananas.example/api/v2/write?org=bananas&bucket=test")
|
|
|
|
.method("POST")
|
2022-11-17 20:55:58 +00:00
|
|
|
.body(Body::from(lp))
|
2022-03-25 10:39:30 +00:00
|
|
|
.expect("failed to construct HTTP request");
|
|
|
|
|
|
|
|
let response = ctx
|
|
|
|
.delegate()
|
|
|
|
.route(request)
|
|
|
|
.await
|
|
|
|
.expect("LP write request failed");
|
|
|
|
|
|
|
|
assert_eq!(response.status(), StatusCode::NO_CONTENT);
|
|
|
|
|
2022-11-17 20:55:58 +00:00
|
|
|
let now = SystemProvider::default()
|
|
|
|
.now()
|
|
|
|
.timestamp_nanos()
|
|
|
|
.to_string();
|
|
|
|
let lp = "platanos,tag1=A,tag2=B val=42.0 ".to_string() + &now;
|
|
|
|
|
2022-03-25 10:39:30 +00:00
|
|
|
let request = Request::builder()
|
|
|
|
.uri("https://bananas.example/api/v2/write?org=bananas&bucket=test")
|
|
|
|
.method("POST")
|
2022-11-17 20:55:58 +00:00
|
|
|
.body(Body::from(lp))
|
2022-03-25 10:39:30 +00:00
|
|
|
.expect("failed to construct HTTP request");
|
|
|
|
|
|
|
|
let err = ctx
|
|
|
|
.delegate()
|
|
|
|
.route(request)
|
|
|
|
.await
|
|
|
|
.expect_err("LP write request should fail");
|
|
|
|
|
|
|
|
assert_matches!(
|
|
|
|
&err,
|
2022-05-06 18:51:52 +00:00
|
|
|
router::server::http::Error::DmlHandler(
|
2022-03-25 10:39:30 +00:00
|
|
|
DmlError::Schema(
|
2022-03-25 11:04:35 +00:00
|
|
|
SchemaError::Conflict(
|
2022-08-16 16:48:15 +00:00
|
|
|
e
|
2022-03-25 10:39:30 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
) => {
|
2022-08-16 16:48:15 +00:00
|
|
|
assert_matches!(e.err(), iox_catalog::interface::Error::ColumnTypeMismatch {
|
|
|
|
name,
|
|
|
|
existing,
|
|
|
|
new,
|
|
|
|
} => {
|
|
|
|
assert_eq!(name, "val");
|
2022-09-12 21:24:30 +00:00
|
|
|
assert_eq!(*existing, ColumnType::I64);
|
|
|
|
assert_eq!(*new, ColumnType::F64);
|
2022-08-16 16:48:15 +00:00
|
|
|
});
|
2022-03-25 10:39:30 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(err.as_status_code(), StatusCode::BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
2022-12-07 16:12:00 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_rejected_ns() {
|
|
|
|
let ctx = TestContext::new(false, None);
|
|
|
|
|
|
|
|
let now = SystemProvider::default()
|
|
|
|
.now()
|
|
|
|
.timestamp_nanos()
|
|
|
|
.to_string();
|
|
|
|
let lp = "platanos,tag1=A,tag2=B val=42i ".to_string() + &now;
|
|
|
|
|
|
|
|
let request = Request::builder()
|
|
|
|
.uri("https://bananas.example/api/v2/write?org=bananas&bucket=test")
|
|
|
|
.method("POST")
|
|
|
|
.body(Body::from(lp))
|
|
|
|
.expect("failed to construct HTTP request");
|
|
|
|
|
|
|
|
let err = ctx
|
|
|
|
.delegate()
|
|
|
|
.route(request)
|
|
|
|
.await
|
|
|
|
.expect_err("should error");
|
|
|
|
assert_matches!(
|
|
|
|
err,
|
|
|
|
router::server::http::Error::NamespaceResolver(
|
|
|
|
// can't check the type of the create error without making ns_autocreation public, but
|
|
|
|
// not worth it just for this test, as the correct error is asserted in unit tests in
|
|
|
|
// that module. here it's just important that the write fails.
|
|
|
|
router::namespace_resolver::Error::Create(_)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
assert_eq!(err.as_status_code(), StatusCode::BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
2022-03-25 10:39:30 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_schema_limit() {
|
2022-12-07 16:12:00 +00:00
|
|
|
let ctx = TestContext::new(true, None);
|
2022-03-25 10:39:30 +00:00
|
|
|
|
2022-11-17 20:55:58 +00:00
|
|
|
let now = SystemProvider::default()
|
|
|
|
.now()
|
|
|
|
.timestamp_nanos()
|
|
|
|
.to_string();
|
|
|
|
let lp = "platanos,tag1=A,tag2=B val=42i ".to_string() + &now;
|
|
|
|
|
2022-03-25 10:39:30 +00:00
|
|
|
// Drive the creation of the namespace
|
|
|
|
let request = Request::builder()
|
|
|
|
.uri("https://bananas.example/api/v2/write?org=bananas&bucket=test")
|
|
|
|
.method("POST")
|
2022-11-17 20:55:58 +00:00
|
|
|
.body(Body::from(lp))
|
2022-03-25 10:39:30 +00:00
|
|
|
.expect("failed to construct HTTP request");
|
|
|
|
let response = ctx
|
|
|
|
.delegate()
|
|
|
|
.route(request)
|
|
|
|
.await
|
|
|
|
.expect("LP write request failed");
|
|
|
|
assert_eq!(response.status(), StatusCode::NO_CONTENT);
|
|
|
|
|
|
|
|
// Update the table limit
|
|
|
|
ctx.catalog()
|
|
|
|
.repositories()
|
|
|
|
.await
|
|
|
|
.namespaces()
|
|
|
|
.update_table_limit("bananas_test", 1)
|
|
|
|
.await
|
|
|
|
.expect("failed to update table limit");
|
|
|
|
|
|
|
|
// Attempt to create another table
|
2022-11-17 20:55:58 +00:00
|
|
|
let now = SystemProvider::default()
|
|
|
|
.now()
|
|
|
|
.timestamp_nanos()
|
|
|
|
.to_string();
|
|
|
|
let lp = "platanos2,tag1=A,tag2=B val=42i ".to_string() + &now;
|
|
|
|
|
2022-03-25 10:39:30 +00:00
|
|
|
let request = Request::builder()
|
|
|
|
.uri("https://bananas.example/api/v2/write?org=bananas&bucket=test")
|
|
|
|
.method("POST")
|
2022-11-17 20:55:58 +00:00
|
|
|
.body(Body::from(lp))
|
2022-03-25 10:39:30 +00:00
|
|
|
.expect("failed to construct HTTP request");
|
|
|
|
let err = ctx
|
|
|
|
.delegate()
|
|
|
|
.route(request)
|
|
|
|
.await
|
|
|
|
.expect_err("LP write request should fail");
|
|
|
|
|
|
|
|
assert_matches!(
|
|
|
|
&err,
|
2022-05-06 18:51:52 +00:00
|
|
|
router::server::http::Error::DmlHandler(
|
2022-03-25 10:39:30 +00:00
|
|
|
DmlError::Schema(
|
2022-10-14 11:34:17 +00:00
|
|
|
SchemaError::ServiceLimit(e)
|
2022-03-25 10:39:30 +00:00
|
|
|
)
|
|
|
|
) => {
|
2022-10-14 11:34:17 +00:00
|
|
|
assert_eq!(
|
|
|
|
e.to_string(),
|
|
|
|
"couldn't create table platanos2; limit reached on namespace 1"
|
|
|
|
);
|
2022-03-25 10:39:30 +00:00
|
|
|
}
|
|
|
|
);
|
2022-10-14 14:12:56 +00:00
|
|
|
assert_eq!(err.as_status_code(), StatusCode::BAD_REQUEST);
|
2022-03-25 10:39:30 +00:00
|
|
|
}
|
2022-10-28 10:37:05 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_write_propagate_ids() {
|
2022-12-07 16:12:00 +00:00
|
|
|
let ctx = TestContext::new(true, None);
|
2022-10-28 10:37:05 +00:00
|
|
|
|
|
|
|
// Create the namespace and a set of tables.
|
|
|
|
let ns = ctx
|
|
|
|
.catalog()
|
|
|
|
.repositories()
|
|
|
|
.await
|
|
|
|
.namespaces()
|
|
|
|
.create(
|
|
|
|
"bananas_test",
|
2022-11-18 13:02:12 +00:00
|
|
|
None,
|
2022-10-28 10:37:05 +00:00
|
|
|
TopicId::new(TEST_TOPIC_ID),
|
|
|
|
QueryPoolId::new(TEST_QUERY_POOL_ID),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.expect("failed to update table limit");
|
|
|
|
|
|
|
|
let catalog = ctx.catalog();
|
|
|
|
let ids = ["another", "test", "table", "platanos"]
|
|
|
|
.iter()
|
|
|
|
.map(|t| {
|
|
|
|
let catalog = Arc::clone(&catalog);
|
|
|
|
async move {
|
|
|
|
let table = catalog
|
|
|
|
.repositories()
|
|
|
|
.await
|
|
|
|
.tables()
|
|
|
|
.create_or_get(t, ns.id)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
(*t, table.id)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<FuturesUnordered<_>>()
|
|
|
|
.collect::<HashMap<_, _>>()
|
|
|
|
.await;
|
|
|
|
|
2022-11-17 20:55:58 +00:00
|
|
|
// data inside the retention period
|
|
|
|
let now = SystemProvider::default()
|
|
|
|
.now()
|
|
|
|
.timestamp_nanos()
|
|
|
|
.to_string();
|
|
|
|
let lp = format! {
|
|
|
|
"
|
|
|
|
platanos,tag1=A,tag2=B val=42i {}\n\
|
|
|
|
another,tag1=A,tag2=B val=42i {}\n\
|
|
|
|
test,tag1=A,tag2=B val=42i {}\n\
|
|
|
|
platanos,tag1=A,tag2=B val=42i {}\n\
|
|
|
|
table,tag1=A,tag2=B val=42i {}\n\
|
|
|
|
", now, now, now, now, now
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2022-10-28 10:37:05 +00:00
|
|
|
let request = Request::builder()
|
|
|
|
.uri("https://bananas.example/api/v2/write?org=bananas&bucket=test")
|
|
|
|
.method("POST")
|
2022-11-17 20:55:58 +00:00
|
|
|
.body(Body::from(lp))
|
2022-10-28 10:37:05 +00:00
|
|
|
.expect("failed to construct HTTP request");
|
|
|
|
|
|
|
|
let response = ctx
|
|
|
|
.delegate()
|
|
|
|
.route(request)
|
|
|
|
.await
|
|
|
|
.expect("LP write request failed");
|
|
|
|
|
|
|
|
assert_eq!(response.status(), StatusCode::NO_CONTENT);
|
|
|
|
|
|
|
|
// Check the write buffer observed the correct write.
|
|
|
|
let writes = ctx.write_buffer_state().get_messages(ShardIndex::new(0));
|
|
|
|
assert_eq!(writes.len(), 1);
|
|
|
|
assert_matches!(writes.as_slice(), [Ok(DmlOperation::Write(w))] => {
|
2022-11-03 10:05:51 +00:00
|
|
|
assert_eq!(w.namespace_id(), ns.id);
|
2022-10-28 10:37:05 +00:00
|
|
|
|
2022-11-16 16:43:23 +00:00
|
|
|
for id in ids.values() {
|
|
|
|
assert!(w.table(id).is_some());
|
2022-10-28 10:37:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_delete_propagate_ids() {
|
2022-12-07 16:12:00 +00:00
|
|
|
let ctx = TestContext::new(true, None);
|
2022-10-28 10:37:05 +00:00
|
|
|
|
|
|
|
// Create the namespace and a set of tables.
|
|
|
|
let ns = ctx
|
|
|
|
.catalog()
|
|
|
|
.repositories()
|
|
|
|
.await
|
|
|
|
.namespaces()
|
|
|
|
.create(
|
|
|
|
"bananas_test",
|
2022-11-18 13:02:12 +00:00
|
|
|
None,
|
2022-10-28 10:37:05 +00:00
|
|
|
TopicId::new(TEST_TOPIC_ID),
|
|
|
|
QueryPoolId::new(TEST_QUERY_POOL_ID),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.expect("failed to update table limit");
|
|
|
|
|
|
|
|
let request = Request::builder()
|
|
|
|
.uri("https://bananas.example/api/v2/delete?org=bananas&bucket=test")
|
|
|
|
.method("POST")
|
|
|
|
.body(Body::from(
|
|
|
|
r#"{
|
|
|
|
"predicate": "_measurement=bananas",
|
|
|
|
"start": "1970-01-01T00:00:00Z",
|
|
|
|
"stop": "2070-01-02T00:00:00Z"
|
|
|
|
}"#,
|
|
|
|
))
|
|
|
|
.expect("failed to construct HTTP request");
|
|
|
|
|
|
|
|
let response = ctx
|
|
|
|
.delegate()
|
|
|
|
.route(request)
|
|
|
|
.await
|
|
|
|
.expect("delete request failed");
|
|
|
|
|
|
|
|
assert_eq!(response.status(), StatusCode::NO_CONTENT);
|
|
|
|
|
|
|
|
// Check the write buffer observed the correct write.
|
|
|
|
let writes = ctx.write_buffer_state().get_messages(ShardIndex::new(0));
|
|
|
|
assert_eq!(writes.len(), 1);
|
|
|
|
assert_matches!(writes.as_slice(), [Ok(DmlOperation::Delete(w))] => {
|
2022-11-03 10:05:51 +00:00
|
|
|
assert_eq!(w.namespace_id(), ns.id);
|
2022-10-28 10:37:05 +00:00
|
|
|
});
|
|
|
|
}
|