add flag to make CSV upload wait until entries are flushed

To make it easier/possible to write stable unit test the CSV upload
can optionally wait until all entries have been flushed to disk.
This is necessary for tests that ingest data and then read the data.
This commit is contained in:
2019-12-13 18:05:20 +01:00
parent 07ad62ddd9
commit 550d7ba44e
8 changed files with 79 additions and 16 deletions

View File

@@ -5,7 +5,24 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* Wrapper for chunk of {@link Entry}s.
* <p>
* This class is supposed to be provided to the queue returned by
* PerformanceDb.getQueue(). Processing {@link Entry}s in chunks is more
* efficient than processing each one individually.
* <p>
* Optionally, you can request that the entries will be flushed to disk by
* calling {@link #forceFlush()} before adding it to the queue.
* <p>
* Optionally, this class can act like a future. This is useful if you have to
* wait until the entries have been processed. Use {@link #forceFlush()} and
* {@link #waitUntilFlushed(long, TimeUnit)}.
*/
public class Entries implements Iterable<Entry> { public class Entries implements Iterable<Entry> {
/** /**
* A special {@link Entries} instance that can be used as poison object for * A special {@link Entries} instance that can be used as poison object for
@@ -15,6 +32,10 @@ public class Entries implements Iterable<Entry> {
private final List<Entry> entries; private final List<Entry> entries;
private boolean forceFlush = false;
private CountDownLatch flushLatch = null;
public Entries(final int initialSize) { public Entries(final int initialSize) {
entries = new ArrayList<>(initialSize); entries = new ArrayList<>(initialSize);
} }
@@ -39,4 +60,25 @@ public class Entries implements Iterable<Entry> {
public int size() { public int size() {
return entries.size(); return entries.size();
} }
public boolean isForceFlush() {
return forceFlush;
}
public void forceFlush() {
forceFlush = true;
flushLatch = new CountDownLatch(1);
}
public void waitUntilFlushed(final long timeout, final TimeUnit unit)
throws InterruptedException, TimeoutException {
final boolean finished = flushLatch.await(timeout, unit);
if (!finished) {
throw new TimeoutException();
}
}
public void notifyFlushed() {
flushLatch.countDown();
}
} }

View File

@@ -4,6 +4,8 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.lucares.collections.IntList; import org.lucares.collections.IntList;
import org.lucares.pdb.api.Entries; import org.lucares.pdb.api.Entries;
@@ -26,7 +28,7 @@ class CsvToEntryTransformer {
this.settings = settings; this.settings = settings;
} }
void readCSV(final InputStream in) throws IOException, InterruptedException { void readCSV(final InputStream in) throws IOException, InterruptedException, TimeoutException {
final int chunksize = 1000; final int chunksize = 1000;
Entries entries = new Entries(chunksize); Entries entries = new Entries(chunksize);
@@ -93,7 +95,9 @@ class CsvToEntryTransformer {
if (entry != null) { if (entry != null) {
entries.add(entry); entries.add(entry);
} }
entries.forceFlush();
queue.put(entries); queue.put(entries);
entries.waitUntilFlushed(5, TimeUnit.MINUTES);
} }
private int[] handleCsvHeaderLine(final byte[] line, final int bytesInLine, final IntList separatorPositions) { private int[] handleCsvHeaderLine(final byte[] line, final int bytesInLine, final IntList separatorPositions) {

View File

@@ -9,9 +9,12 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.lucares.pdb.api.Entries; import org.lucares.pdb.api.Entries;
import org.lucares.performance.db.PerformanceDb; import org.lucares.performance.db.PerformanceDb;
@@ -41,8 +44,9 @@ public class CsvUploadHandler implements PropertyKeys, DisposableBean {
this.performanceDb = performanceDb; this.performanceDb = performanceDb;
} }
public void ingest(final List<MultipartFile> files, final CsvReaderSettings settings) public void ingest(final List<MultipartFile> files, final CsvReaderSettings settings,
throws IllegalStateException, IOException { final boolean waitUntilFinished)
throws IllegalStateException, IOException, InterruptedException, ExecutionException, TimeoutException {
final List<Path> tmpFiles = new ArrayList<Path>(); final List<Path> tmpFiles = new ArrayList<Path>();
@@ -58,7 +62,7 @@ public class CsvUploadHandler implements PropertyKeys, DisposableBean {
throw e; throw e;
} }
threadPool.submit(() -> { final Future<?> future = threadPool.submit(() -> {
final ArrayBlockingQueue<Entries> queue = performanceDb.getQueue(); final ArrayBlockingQueue<Entries> queue = performanceDb.getQueue();
for (final Path tmpFile : tmpFiles) { for (final Path tmpFile : tmpFiles) {
try { try {
@@ -74,8 +78,10 @@ public class CsvUploadHandler implements PropertyKeys, DisposableBean {
LOGGER.error("csv ingestion failed", e); LOGGER.error("csv ingestion failed", e);
} }
} }
queue.add(Entries.POISON);
}); });
if (waitUntilFinished) {
future.get(1, TimeUnit.HOURS);
}
} }
@Override @Override

View File

@@ -12,6 +12,7 @@ import java.nio.charset.StandardCharsets;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.TimeoutException;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import org.lucares.pdb.api.Entries; import org.lucares.pdb.api.Entries;
@@ -50,7 +51,7 @@ public final class IngestionHandler implements Callable<Void> {
return null; return null;
} }
private void handleInputStream(final InputStream in) throws IOException, InterruptedException { private void handleInputStream(final InputStream in) throws IOException, InterruptedException, TimeoutException {
in.mark(1); in.mark(1);
final byte firstByte = (byte) in.read(); final byte firstByte = (byte) in.read();
if (firstByte == '{') { if (firstByte == '{') {

View File

@@ -12,7 +12,9 @@ import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -327,10 +329,11 @@ public class PdbController implements HardcodedValues, PropertyKeys {
@ResponseBody @ResponseBody
@ResponseStatus(code = HttpStatus.CREATED) @ResponseStatus(code = HttpStatus.CREATED)
public String handleCsvFileUpload(@RequestParam("file") final MultipartFile[] files, public String handleCsvFileUpload(@RequestParam("file") final MultipartFile[] files,
@RequestPart("settings") final CsvReaderSettings csvReaderSettings) @RequestPart("settings") final CsvReaderSettings csvReaderSettings,
throws IllegalStateException, IOException { @RequestParam(name = "waitUntilFinished", defaultValue = "false") final boolean waitUntilFinished)
throws IllegalStateException, IOException, InterruptedException, ExecutionException, TimeoutException {
csvUploadHandler.ingest(List.of(files), csvReaderSettings); csvUploadHandler.ingest(List.of(files), csvReaderSettings, waitUntilFinished);
return ""; // return value might become a job id that can be used to cancel, or observe return ""; // return value might become a job id that can be used to cancel, or observe
// status // status
} }

View File

@@ -10,8 +10,10 @@ import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.List; import java.util.List;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeoutException;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.lucares.collections.LongList; import org.lucares.collections.LongList;
@@ -19,7 +21,6 @@ import org.lucares.pdb.api.DateTimeRange;
import org.lucares.pdb.api.Entries; import org.lucares.pdb.api.Entries;
import org.lucares.pdb.api.Query; import org.lucares.pdb.api.Query;
import org.lucares.performance.db.PerformanceDb; import org.lucares.performance.db.PerformanceDb;
import org.junit.jupiter.api.Assertions;
import org.lucares.utils.file.FileUtils; import org.lucares.utils.file.FileUtils;
public class CsvToEntryTransformerTest { public class CsvToEntryTransformerTest {
@@ -37,7 +38,7 @@ public class CsvToEntryTransformerTest {
} }
@Test @Test
public void testIngest() throws IOException, InterruptedException { public void testIngest() throws IOException, InterruptedException, TimeoutException {
final OffsetDateTime dateA = OffsetDateTime.now(); final OffsetDateTime dateA = OffsetDateTime.now();
final OffsetDateTime dateB = OffsetDateTime.now(); final OffsetDateTime dateB = OffsetDateTime.now();
@@ -75,9 +76,10 @@ public class CsvToEntryTransformerTest {
* *
* @throws IOException * @throws IOException
* @throws InterruptedException * @throws InterruptedException
* @throws TimeoutException
*/ */
@Test @Test
public void testIgnoreColumns() throws IOException, InterruptedException { public void testIgnoreColumns() throws IOException, InterruptedException, TimeoutException {
try (final PerformanceDb db = new PerformanceDb(dataDirectory)) { try (final PerformanceDb db = new PerformanceDb(dataDirectory)) {

View File

@@ -4,10 +4,10 @@ import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator; import org.apache.logging.log4j.core.config.Configurator;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.lucares.collections.LongList; import org.lucares.collections.LongList;
@@ -15,7 +15,6 @@ import org.lucares.pdb.api.DateTimeRange;
import org.lucares.pdb.api.GroupResult; import org.lucares.pdb.api.GroupResult;
import org.lucares.pdb.api.Query; import org.lucares.pdb.api.Query;
import org.lucares.performance.db.PerformanceDb; import org.lucares.performance.db.PerformanceDb;
import org.junit.jupiter.api.Assertions;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
@@ -60,7 +59,6 @@ public class PdbControllerTest {
final CsvReaderSettings settings = CsvReaderSettings.create(timeColumn, ',', ignoredColumn); final CsvReaderSettings settings = CsvReaderSettings.create(timeColumn, ',', ignoredColumn);
uploadCsv(settings, csv); uploadCsv(settings, csv);
TimeUnit.SECONDS.sleep(1);
{ {
final GroupResult groupResult = performanceDb.get(new Query("tag=tagValue", DateTimeRange.ofDay(dateA))) final GroupResult groupResult = performanceDb.get(new Query("tag=tagValue", DateTimeRange.ofDay(dateA)))
.singleGroup(); .singleGroup();
@@ -96,7 +94,8 @@ public class PdbControllerTest {
final HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<MultiValueMap<String, Object>>( final HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<MultiValueMap<String, Object>>(
parameters, headers); parameters, headers);
final ResponseEntity<String> response = rest.exchange("/data", HttpMethod.POST, entity, String.class); final ResponseEntity<String> response = rest.exchange("/data?waitUntilFinished=true", HttpMethod.POST, entity,
String.class);
Assertions.assertEquals(response.getStatusCode(), HttpStatus.CREATED, "response status"); Assertions.assertEquals(response.getStatusCode(), HttpStatus.CREATED, "response status");
} }

View File

@@ -134,6 +134,12 @@ public class PerformanceDb implements AutoCloseable {
LOGGER.info("", e); LOGGER.info("", e);
} }
} }
if (entries.isForceFlush()) {
LOGGER.info("flush triggered via entries.isForceFlush()");
dataStore.flush();
entries.notifyFlushed();
}
} }
} catch (final RuntimeException e) { } catch (final RuntimeException e) {