Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,22 @@ public void tearDown() throws Exception {

@Test
public void testAsynchronousDeletionProgress() throws Exception {
final String path = "root.async_progress.d.s";
final String sql = "DELETE TIMESERIES " + path;
final String device = "root.async_progress.d";
final String firstPath = device + ".s1";
final String secondPath = device + ".s2";
final String sql = "DELETE TIMESERIES " + firstPath + ", " + secondPath;
try (Connection connection = EnvFactory.getEnv().getConnection();
Statement statement = TestUtils.createSingleNodeStatement(connection)) {
statement.execute("INSERT INTO root.async_progress.d(time,s) VALUES(1,1)");
statement.execute("INSERT INTO " + device + "(time,s1,s2) VALUES(1,1,2)");
statement.execute("FLUSH");
statement.execute(sql);
boolean found = false;
long procedureId = -1;
try (ResultSet result = statement.executeQuery("SHOW DELETE TIMESERIES PROGRESS")) {
Assert.assertEquals(6, result.getMetaData().getColumnCount());
Assert.assertEquals(8, result.getMetaData().getColumnCount());
while (result.next()) {
if (("[" + path + "]").equals(result.getString("Paths"))) {
found = true;
Assert.assertTrue(result.getLong("ProcedureId") >= 0);
final String paths = result.getString("Paths");
if (paths.contains(firstPath) && paths.contains(secondPath)) {
procedureId = result.getLong("ProcedureId");
Assert.assertNotNull(result.getString("QueryId"));
Assert.assertNotNull(result.getString("Progress"));
Assert.assertTrue(
Expand All @@ -89,9 +92,23 @@ public void testAsynchronousDeletionProgress() throws Exception {
}
}
}
Assert.assertTrue("Accepted deletion must be visible immediately", found);
Assert.assertTrue("Accepted deletion must be visible immediately", procedureId >= 0);
TestUtils.waitForTimeSeriesDeletion(statement, sql);
try (ResultSet result = statement.executeQuery("SHOW TIMESERIES " + path)) {
boolean foundCompleted = false;
try (ResultSet result = statement.executeQuery("SHOW DELETE TIMESERIES PROGRESS")) {
while (result.next()) {
if (result.getLong("ProcedureId") == procedureId) {
foundCompleted = true;
Assert.assertEquals("SUCCESS", result.getString("State"));
Assert.assertEquals(1, result.getLong("WrittenModFileCount"));
Assert.assertEquals(0, result.getLong("RemainingModFileCount"));
Assert.assertFalse(result.wasNull());
break;
}
}
}
Assert.assertTrue("Completed deletion must retain its mod file counts", foundCompleted);
try (ResultSet result = statement.executeQuery("SHOW TIMESERIES " + device + ".*")) {
Assert.assertFalse(result.next());
}
// A nonexistent path is accepted too; the error belongs to the asynchronous task.
Expand Down
120 changes: 105 additions & 15 deletions iotdb-client/client-cpp/test/cpp/sessionCIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,104 @@

#include "catch.hpp"
#include "SessionC.h"
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <thread>
#include <vector>

extern CSession* g_session;

static std::string lastErrorMessage() {
const char* message = ts_get_last_error();
return message == nullptr ? "unknown error" : message;
}

static int64_t latestDeleteTimeSeriesProcedureId(CSession* session) {
CSessionDataSet* rawDataSet = nullptr;
if (ts_session_execute_query(session, "SHOW DELETE TIMESERIES PROGRESS", &rawDataSet) != TS_OK) {
throw std::runtime_error(lastErrorMessage());
}
if (rawDataSet == nullptr) {
throw std::runtime_error("SHOW DELETE TIMESERIES PROGRESS returned no dataset");
}
std::unique_ptr<CSessionDataSet, decltype(&ts_dataset_destroy)> dataSet(rawDataSet,
ts_dataset_destroy);
int64_t latestId = -1;
while (ts_dataset_has_next(dataSet.get())) {
std::unique_ptr<CRowRecord, decltype(&ts_row_record_destroy)> record(
ts_dataset_next(dataSet.get()), ts_row_record_destroy);
if (!record) {
throw std::runtime_error(lastErrorMessage());
}
latestId = std::max(latestId, ts_row_record_get_int64(record.get(), 0));
}
return latestId;
}

static void waitForDeleteTimeSeriesProcedure(CSession* session, int64_t previousId) {
const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(60);
do {
CSessionDataSet* rawDataSet = nullptr;
if (ts_session_execute_query(session, "SHOW DELETE TIMESERIES PROGRESS", &rawDataSet) !=
TS_OK) {
throw std::runtime_error(lastErrorMessage());
}
if (rawDataSet == nullptr) {
throw std::runtime_error("SHOW DELETE TIMESERIES PROGRESS returned no dataset");
}
std::unique_ptr<CSessionDataSet, decltype(&ts_dataset_destroy)> dataSet(rawDataSet,
ts_dataset_destroy);
while (ts_dataset_has_next(dataSet.get())) {
std::unique_ptr<CRowRecord, decltype(&ts_row_record_destroy)> record(
ts_dataset_next(dataSet.get()), ts_row_record_destroy);
if (!record) {
throw std::runtime_error(lastErrorMessage());
}
if (ts_row_record_get_int64(record.get(), 0) <= previousId) {
continue;
}
const char* stateValue = ts_row_record_get_string(record.get(), 3);
if (stateValue == nullptr) {
throw std::runtime_error("Delete timeseries progress returned a null state");
}
const std::string state(stateValue);
if (state == "ROLLEDBACK") {
const char* errorValue = ts_row_record_get_string(record.get(), 5);
throw std::runtime_error(std::string("Asynchronous timeseries deletion rolled back: ") +
(errorValue == nullptr ? "unknown error" : errorValue));
}
if (state == "SUCCESS") {
return;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(20));
} while (std::chrono::steady_clock::now() < deadline);
throw std::runtime_error("Timed out waiting for asynchronous timeseries deletion");
}

static void deleteTimeseriesAndWait(CSession* session, const char* path) {
const int64_t previousId = latestDeleteTimeSeriesProcedureId(session);
if (ts_session_delete_timeseries(session, path) != TS_OK) {
throw std::runtime_error(lastErrorMessage());
}
waitForDeleteTimeSeriesProcedure(session, previousId);
}

static void deleteTimeseriesBatchAndWait(CSession* session, int pathCount,
const char* const* paths) {
const int64_t previousId = latestDeleteTimeSeriesProcedureId(session);
if (ts_session_delete_timeseries_batch(session, paths, pathCount) != TS_OK) {
throw std::runtime_error(lastErrorMessage());
}
waitForDeleteTimeSeriesProcedure(session, previousId);
}

static int global_test_id = 0;

class CaseReporter {
Expand All @@ -51,7 +141,7 @@ static void dropTimeseriesIfExists(CSession* session, const char* path) {
bool exists = false;
REQUIRE(ts_session_check_timeseries_exists(session, path, &exists) == TS_OK);
if (exists) {
REQUIRE(ts_session_delete_timeseries(session, path) == TS_OK);
deleteTimeseriesAndWait(session, path);
}
}

Expand Down Expand Up @@ -100,14 +190,14 @@ TEST_CASE("C API - Create timeseries", "[c_createTimeseries]") {
bool exists = false;
REQUIRE(ts_session_check_timeseries_exists(g_session, path, &exists) == TS_OK);
REQUIRE(exists);
REQUIRE(ts_session_delete_timeseries(g_session, path) == TS_OK);
deleteTimeseriesAndWait(g_session, path);
}

TEST_CASE("C API - Delete timeseries", "[c_deleteTimeseries]") {
CaseReporter cr("c_deleteTimeseries");
const char* path = "root.ctest.d1.s1";
ensureTimeseries(g_session, path, TS_TYPE_INT64, TS_ENCODING_RLE, TS_COMPRESSION_SNAPPY);
REQUIRE(ts_session_delete_timeseries(g_session, path) == TS_OK);
deleteTimeseriesAndWait(g_session, path);
bool exists = true;
REQUIRE(ts_session_check_timeseries_exists(g_session, path, &exists) == TS_OK);
REQUIRE_FALSE(exists);
Expand Down Expand Up @@ -384,7 +474,7 @@ TEST_CASE("C API - Multi-node session", "[c_multiNode]") {
bool exists = false;
REQUIRE(ts_session_check_timeseries_exists(localSession, path, &exists) == TS_OK);
REQUIRE(exists);
REQUIRE(ts_session_delete_timeseries(localSession, path) == TS_OK);
deleteTimeseriesAndWait(localSession, path);

ts_session_close(localSession);
ts_session_destroy(localSession);
Expand Down Expand Up @@ -478,8 +568,8 @@ TEST_CASE("C API - Database and extended timeseries APIs", "[c_dbTimeseries]") {
dropTimeseriesIfExists(g_session, pathsM[i]);
}
REQUIRE(ts_session_create_multi_timeseries(g_session, 2, pathsM, tsM, encM, compM) == TS_OK);
REQUIRE(ts_session_delete_timeseries_batch(g_session, pathsM, 2) == TS_OK);
REQUIRE(ts_session_delete_timeseries(g_session, pathEx) == TS_OK);
deleteTimeseriesBatchAndWait(g_session, 2, pathsM);
deleteTimeseriesAndWait(g_session, pathEx);
REQUIRE(ts_session_delete_database(g_session, sgEx) == TS_OK);
}

Expand Down Expand Up @@ -782,14 +872,14 @@ TEST_CASE("C API - RowRecord and delete data APIs", "[c_rowDelete]") {
const char* delPaths[] = {pi, pf};
REQUIRE(ts_session_delete_data_range(g_session, 2, delPaths, 400LL, 600LL) == TS_OK);

REQUIRE(ts_session_delete_timeseries(g_session, pb) == TS_OK);
REQUIRE(ts_session_delete_timeseries(g_session, pi) == TS_OK);
REQUIRE(ts_session_delete_timeseries(g_session, pf) == TS_OK);
REQUIRE(ts_session_delete_timeseries(g_session, pd) == TS_OK);
REQUIRE(ts_session_delete_timeseries(g_session, pt) == TS_OK);
REQUIRE(ts_session_delete_timeseries(g_session, pstr) == TS_OK);
REQUIRE(ts_session_delete_timeseries(g_session, pdate) == TS_OK);
REQUIRE(ts_session_delete_timeseries(g_session, pblob) == TS_OK);
deleteTimeseriesAndWait(g_session, pb);
deleteTimeseriesAndWait(g_session, pi);
deleteTimeseriesAndWait(g_session, pf);
deleteTimeseriesAndWait(g_session, pd);
deleteTimeseriesAndWait(g_session, pt);
deleteTimeseriesAndWait(g_session, pstr);
deleteTimeseriesAndWait(g_session, pdate);
deleteTimeseriesAndWait(g_session, pblob);
REQUIRE(ts_session_delete_database(g_session, sg) == TS_OK);
}

Expand Down Expand Up @@ -820,5 +910,5 @@ TEST_CASE("C API - Query DATE 1000-01-01", "[c_dateMinYear]") {
ts_row_record_destroy(record);
ts_dataset_destroy(dataSet);

REQUIRE(ts_session_delete_timeseries(g_session, path) == TS_OK);
deleteTimeseriesAndWait(g_session, path);
}
Loading
Loading