Browse Source
PostgreSQL ts_kv export now retries transient query failures from the same cursor, fails fast on non-transient errors, and cleans up time-series files on latest-read failures. Cassandra export reads partitions in ordered pages through retried queries. The importer stops reading immediately after a failed insert, abort deletes files even when close fails, and startup failures force JVM exit.pull/16897/head
13 changed files with 458 additions and 72 deletions
@ -0,0 +1,139 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
package org.thingsboard.migrator.service.tenant.exporting; |
|||
|
|||
import org.junit.jupiter.api.BeforeEach; |
|||
import org.junit.jupiter.api.Test; |
|||
import org.junit.jupiter.api.io.TempDir; |
|||
import org.mockito.Mockito; |
|||
import org.springframework.test.util.ReflectionTestUtils; |
|||
import org.thingsboard.migrator.utils.CassandraService; |
|||
import org.thingsboard.migrator.utils.Storage; |
|||
import org.thingsboard.migrator.utils.TestStorage; |
|||
import org.thingsboard.migrator.utils.TsKvFiles; |
|||
import org.thingsboard.migrator.utils.TsKvWriterFactory; |
|||
|
|||
import java.io.IOException; |
|||
import java.io.Writer; |
|||
import java.nio.file.Path; |
|||
import java.util.ArrayList; |
|||
import java.util.LinkedHashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.Queue; |
|||
import java.util.TreeMap; |
|||
import java.util.UUID; |
|||
import java.util.concurrent.ConcurrentLinkedQueue; |
|||
import java.util.stream.LongStream; |
|||
|
|||
import static org.assertj.core.api.Assertions.assertThat; |
|||
|
|||
class CassandraTsKvExporterTest { |
|||
|
|||
private static final UUID DEVICE = UUID.randomUUID(); |
|||
|
|||
@TempDir |
|||
Path workingDir; |
|||
|
|||
private Storage storage; |
|||
// ts of the rows by partition, as in ts_kv_cf
|
|||
private final Map<Long, List<Long>> partitions = new TreeMap<>(); |
|||
private final Queue<String> rowQueries = new ConcurrentLinkedQueue<>(); |
|||
private CassandraService cassandraService; |
|||
|
|||
@BeforeEach |
|||
void setUp() throws IOException { |
|||
storage = TestStorage.create(workingDir, "TENANT_DATA_EXPORT"); |
|||
cassandraService = Mockito.mock(CassandraService.class, invocation -> { |
|||
String query = invocation.getArgument(0); |
|||
Object[] args = (Object[]) invocation.getRawArguments()[2]; |
|||
if (query.contains(" FROM ts_kv_partitions_cf ")) { |
|||
return List.copyOf(partitions.keySet()); |
|||
} |
|||
if (query.contains(" FROM ts_kv_cf ")) { |
|||
rowQueries.add(query); |
|||
return page(query, args); |
|||
} |
|||
return null; |
|||
}); |
|||
storage.newFile(CassandraTsKvExporter.LATEST_KV); |
|||
try (Writer writer = storage.newWriter(CassandraTsKvExporter.LATEST_KV)) { |
|||
storage.addToFile(writer, new LinkedHashMap<>(Map.of("table_name", "DEVICE", "entity_id", DEVICE, "key_name", "temperature"))); |
|||
} |
|||
} |
|||
|
|||
@Test |
|||
void readsPartitionsInPages() throws Exception { |
|||
partitions.put(0L, List.of(1L, 2L, 3L, 4L, 5L)); |
|||
partitions.put(1000L, List.of(1001L, 1002L, 1003L, 1004L)); |
|||
partitions.put(2000L, List.of()); |
|||
|
|||
exporter(null, null).run(); |
|||
|
|||
assertThat(exportedTs()).containsExactly(1L, 2L, 3L, 4L, 5L, 1001L, 1002L, 1003L, 1004L); |
|||
// 3 + 3 (the last page is empty) + 1
|
|||
assertThat(rowQueries).hasSize(7).allMatch(query -> query.endsWith(" ORDER BY ts LIMIT ?")); |
|||
} |
|||
|
|||
@Test |
|||
void appliesFilterBoundsToAllPages() throws Exception { |
|||
partitions.put(0L, LongStream.rangeClosed(1, 10).boxed().toList()); |
|||
|
|||
exporter(3L, 8L).run(); |
|||
|
|||
assertThat(exportedTs()).containsExactly(3L, 4L, 5L, 6L, 7L, 8L); |
|||
} |
|||
|
|||
private CassandraTsKvExporter exporter(Long startTs, Long endTs) { |
|||
TsKvWriterFactory writerFactory = new TsKvWriterFactory(storage); |
|||
ReflectionTestUtils.setField(writerFactory, "maxFileSizeMb", 1L); |
|||
CassandraTsKvExporter exporter = new CassandraTsKvExporter(storage, cassandraService, writerFactory); |
|||
ReflectionTestUtils.setField(exporter, "parallelismLevel", 4); |
|||
ReflectionTestUtils.setField(exporter, "statsPrintInterval", 1_000); |
|||
ReflectionTestUtils.setField(exporter, "startTs", startTs); |
|||
ReflectionTestUtils.setField(exporter, "endTs", endTs); |
|||
ReflectionTestUtils.setField(exporter, "partitionSizeMs", 1000L); |
|||
ReflectionTestUtils.setField(exporter, "pageSize", 2); |
|||
ReflectionTestUtils.invokeMethod(exporter, "init"); |
|||
ReflectionTestUtils.invokeMethod(exporter, "validate"); |
|||
return exporter; |
|||
} |
|||
|
|||
// Executes the row query against the partitions map
|
|||
private List<Map<String, Object>> page(String query, Object[] args) { |
|||
long partition = (Long) args[3]; |
|||
int i = 4; |
|||
long lowerBound = Long.MIN_VALUE; |
|||
if (query.contains(" ts > ?")) { |
|||
lowerBound = (Long) args[i++] + 1; |
|||
} else if (query.contains(" ts >= ?")) { |
|||
lowerBound = (Long) args[i++]; |
|||
} |
|||
long upperBound = query.contains(" ts <= ?") ? (Long) args[i++] : Long.MAX_VALUE; |
|||
int limit = (Integer) args[i]; |
|||
long from = lowerBound; |
|||
return partitions.get(partition).stream() |
|||
.filter(ts -> ts >= from && ts <= upperBound) |
|||
.limit(limit) |
|||
.map(ts -> { |
|||
Map<String, Object> row = new LinkedHashMap<>(); |
|||
row.put("entity_type", "DEVICE"); |
|||
row.put("entity_id", DEVICE); |
|||
row.put("partition", partition); |
|||
row.put("key", "temperature"); |
|||
row.put("ts", ts); |
|||
row.put("long_v", ts); |
|||
return row; |
|||
}) |
|||
.toList(); |
|||
} |
|||
|
|||
private List<Long> exportedTs() throws IOException { |
|||
List<Long> ts = new ArrayList<>(); |
|||
for (TsKvFiles.Part part : TsKvFiles.listForImport(storage)) { |
|||
storage.readAndProcess(part.name(), row -> ts.add(((Number) row.get("ts")).longValue())); |
|||
} |
|||
return ts; |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue