use of io.pravega.segmentstore.contracts.tables.TableEntry in project pravega by pravega.
the class TableBasedMetadataStore method writeAll.
/**
* Writes transaction data from a given list to the metadata store.
*
* @param dataList List of transaction data to write.
*/
@Override
protected CompletableFuture<Void> writeAll(Collection<TransactionData> dataList) {
val toUpdate = new ArrayList<TableEntry>();
val entryToTxnDataMap = new HashMap<TableEntry, TransactionData>();
val deletedKeyToTxnDataMap = new HashMap<TableKey, TransactionData>();
val keysToDelete = new ArrayList<TableKey>();
val t = new Timer();
return ensureInitialized().thenRunAsync(() -> {
for (TransactionData txnData : dataList) {
Preconditions.checkState(null != txnData.getDbObject(), "Missing tracking object");
val version = (Long) txnData.getDbObject();
if (null == txnData.getValue()) {
val toDelete = TableKey.unversioned(new ByteArraySegment(txnData.getKey().getBytes(Charsets.UTF_8)));
keysToDelete.add(toDelete);
deletedKeyToTxnDataMap.put(toDelete, txnData);
}
try {
val arraySegment = SERIALIZER.serialize(txnData);
TableEntry tableEntry = TableEntry.versioned(new ByteArraySegment(txnData.getKey().getBytes(Charsets.UTF_8)), arraySegment, version);
entryToTxnDataMap.put(tableEntry, txnData);
toUpdate.add(tableEntry);
} catch (Exception e) {
throw new CompletionException(handleException(e));
}
}
}, getExecutor()).thenComposeAsync(v -> {
// toUpdate includes both modified keys as well updates to deleted keys to mark them as deleted.
return this.tableStore.put(tableName, toUpdate, timeout).thenComposeAsync(ret -> {
// Update versions.
int i = 0;
for (TableEntry tableEntry : toUpdate) {
entryToTxnDataMap.get(tableEntry).setDbObject(ret.get(i));
i++;
}
// This next step will just remove them from table store.
return this.tableStore.remove(tableName, keysToDelete, timeout).handleAsync((v1, ex) -> {
// keys are already persisted successfully in earlier step.
if (ex == null) {
deletedKeyToTxnDataMap.values().stream().forEach(txnData -> txnData.setDbObject(TableKey.NOT_EXISTS));
} else {
log.warn("Error while deleting keys from table segment {}.", tableName, ex);
}
TABLE_WRITE_LATENCY.reportSuccessEvent(t.getElapsed());
return v1;
}, getExecutor());
}, getExecutor());
}, getExecutor()).exceptionally(e -> {
val ex = Exceptions.unwrap(e);
throw new CompletionException(handleException(ex));
});
}
use of io.pravega.segmentstore.contracts.tables.TableEntry in project pravega by pravega.
the class TableBasedMetadataStoreMockTests method testRandomExceptionDuringRead.
@Test
public void testRandomExceptionDuringRead() {
TableStore mockTableStore = mock(TableStore.class);
@Cleanup TableBasedMetadataStore tableBasedMetadataStore = new TableBasedMetadataStore("test", mockTableStore, ChunkedSegmentStorageConfig.DEFAULT_CONFIG, executorService());
when(mockTableStore.createSegment(any(), any(), any())).thenReturn(Futures.failedFuture(new CompletionException(new StreamSegmentExistsException("test"))));
// Throw random exception
Exception e = new ArithmeticException();
val f = new CompletableFuture<List<TableEntry>>();
f.completeExceptionally(e);
when(mockTableStore.get(anyString(), any(), any())).thenReturn(f);
AssertExtensions.assertFutureThrows("read should throw an exception", tableBasedMetadataStore.read("test"), ex -> ex instanceof StorageMetadataException && ex.getCause() == e);
}
use of io.pravega.segmentstore.contracts.tables.TableEntry in project pravega by pravega.
the class InMemoryTableStore method entryIterator.
@Override
public CompletableFuture<AsyncIterator<IteratorItem<TableEntry>>> entryIterator(String segmentName, IteratorArgs args) {
Collection<TableEntry> tableEntries = getTableEntries(segmentName);
val item = new IteratorItemImpl<>(args.getContinuationToken(), tableEntries);
return CompletableFuture.completedFuture(AsyncIterator.singleton(item));
}
use of io.pravega.segmentstore.contracts.tables.TableEntry in project pravega by pravega.
the class InMemoryTableStore method keyIterator.
@Override
public CompletableFuture<AsyncIterator<IteratorItem<TableKey>>> keyIterator(String segmentName, IteratorArgs args) {
Collection<TableEntry> tableEntries = getTableEntries(segmentName);
val item = new IteratorItemImpl<>(args.getContinuationToken(), tableEntries.stream().map(TableEntry::getKey).collect(Collectors.toList()));
return CompletableFuture.completedFuture(AsyncIterator.singleton(item));
}
use of io.pravega.segmentstore.contracts.tables.TableEntry in project pravega by pravega.
the class PravegaRequestProcessor method updateTableEntries.
@Override
public void updateTableEntries(final WireCommands.UpdateTableEntries updateTableEntries) {
String segment = updateTableEntries.getSegment();
final String operation = "updateTableEntries";
if (!verifyToken(segment, updateTableEntries.getRequestId(), updateTableEntries.getDelegationToken(), operation)) {
updateTableEntries.release();
return;
}
log.debug(updateTableEntries.getRequestId(), "Update Table Segment Entries: Segment={}, Offset={}, Count={}.", updateTableEntries.getSegment(), updateTableEntries.getTableSegmentOffset(), updateTableEntries.getTableEntries().getEntries().size());
val entries = new ArrayList<TableEntry>(updateTableEntries.getTableEntries().getEntries().size());
val conditional = new AtomicBoolean(false);
val size = new AtomicInteger(0);
for (val e : updateTableEntries.getTableEntries().getEntries()) {
val v = TableEntry.versioned(new ByteBufWrapper(e.getKey().getData()), new ByteBufWrapper(e.getValue().getData()), e.getKey().getKeyVersion());
entries.add(v);
size.addAndGet(v.getKey().getKey().getLength() + v.getValue().getLength());
if (v.getKey().hasVersion()) {
conditional.set(true);
}
}
val timer = new Timer();
this.connection.adjustOutstandingBytes(size.get());
tableStore.put(segment, entries, updateTableEntries.getTableSegmentOffset(), TIMEOUT).thenAccept(versions -> {
connection.send(new WireCommands.TableEntriesUpdated(updateTableEntries.getRequestId(), versions));
this.tableStatsRecorder.updateEntries(updateTableEntries.getSegment(), entries.size(), conditional.get(), timer.getElapsed());
}).exceptionally(e -> handleException(updateTableEntries.getRequestId(), segment, updateTableEntries.getTableSegmentOffset(), operation, e)).whenComplete((r, ex) -> {
this.connection.adjustOutstandingBytes(-size.get());
updateTableEntries.release();
});
}
Aggregations