use of io.pravega.client.tables.impl.TableSegmentEntry in project pravega by pravega.
the class ControllerMetadataGetEntryCommand method execute.
@Override
public void execute() throws Exception {
Preconditions.checkArgument(getArgCount() >= 3 && getArgCount() < 5, "Incorrect argument count.");
final String tableName = getArg(0);
final String key = getArg(1);
final String segmentStoreHost = getArg(2);
@Cleanup CuratorFramework zkClient = createZKClient();
@Cleanup AdminSegmentHelper adminSegmentHelper = instantiateAdminSegmentHelper(zkClient);
ControllerMetadataSerializer serializer = new ControllerMetadataSerializer(tableName, key);
TableSegmentEntry entry = getTableEntry(tableName, key, segmentStoreHost, adminSegmentHelper);
if (entry == null) {
return;
}
val value = serializer.deserialize(getByteBuffer(entry.getValue()));
output("For the given key: %s", key);
if (getArgCount() == 4) {
final String jsonFile = getArg(3);
ControllerMetadataJsonSerializer jsonSerializer = new ControllerMetadataJsonSerializer();
@Cleanup FileWriter writer = new FileWriter(createFileAndDirectory(jsonFile));
writer.write(jsonSerializer.toJson(value));
writer.flush();
output("Successfully wrote the value to %s in JSON.", jsonFile);
} else {
userFriendlyOutput(value.toString(), serializer.getMetadataType());
}
}
use of io.pravega.client.tables.impl.TableSegmentEntry in project pravega by pravega.
the class ControllerMetadataUpdateEntryCommand method execute.
@Override
public void execute() throws Exception {
ensureArgCount(4);
final String tableName = getArg(0);
final String key = getArg(1);
final String newValueFile = getArg(3);
final String segmentStoreHost = getArg(2);
@Cleanup CuratorFramework zkClient = createZKClient();
@Cleanup AdminSegmentHelper adminSegmentHelper = instantiateAdminSegmentHelper(zkClient);
ControllerMetadataSerializer serializer = new ControllerMetadataSerializer(tableName, key);
ControllerMetadataJsonSerializer jsonSerializer = new ControllerMetadataJsonSerializer();
String jsonValue;
try {
jsonValue = new String(Files.readAllBytes(Paths.get(newValueFile)));
} catch (NoSuchFileException e) {
output("File with new value does not exist: %s", newValueFile);
return;
}
ByteBuffer updatedValue = serializer.serialize(jsonSerializer.fromJson(jsonValue, serializer.getMetadataClass()));
TableSegmentEntry currentEntry = getTableEntry(tableName, key, segmentStoreHost, adminSegmentHelper);
if (currentEntry == null) {
return;
}
long currentVersion = currentEntry.getKey().getVersion().getSegmentVersion();
TableSegmentKeyVersion newVersion = updateTableEntry(tableName, key, updatedValue, currentVersion, segmentStoreHost, adminSegmentHelper);
if (newVersion == null) {
return;
}
output("Successfully updated the key %s in table %s with version %s", key, tableName, newVersion.getSegmentVersion());
}
use of io.pravega.client.tables.impl.TableSegmentEntry in project pravega by pravega.
the class PravegaTablesStoreHelper method updateEntry.
/**
* Method to update a single entry.
* @param tableName tablename
* @param key key
* @param toBytes to bytes function
* @param val value
* @param ver previous key version
* @param <T> Type of value to be added
* @param requestId request id
* @return CompletableFuture which when completed will indicate that the value is updated in the table.
*/
public <T> CompletableFuture<Version> updateEntry(String tableName, String key, T val, Function<T, byte[]> toBytes, Version ver, long requestId) {
long version = ver.asLongVersion().getLongValue();
log.trace(requestId, "updateEntry entry called for : {} key : {} version {}", tableName, key, version);
byte[] value = toBytes.apply(val);
long time = System.currentTimeMillis();
List<TableSegmentEntry> entries = Collections.singletonList(TableSegmentEntry.versioned(key.getBytes(Charsets.UTF_8), value, version));
return withRetries(() -> segmentHelper.updateTableEntries(tableName, entries, authToken.get(), requestId), () -> String.format("updateEntry: key: %s table: %s", key, tableName), true, requestId).thenApplyAsync(x -> {
TableSegmentKeyVersion first = x.get(0);
log.debug(requestId, "entry for key {} updated to table {} with new version {}", key, tableName, first.getSegmentVersion());
Version newVersion = new Version.LongVersion(first.getSegmentVersion());
putInCache(tableName, key, new VersionedMetadata<>(val, newVersion), time);
return newVersion;
}, executor).exceptionally(e -> {
invalidateCache(tableName, key);
throw new CompletionException(e);
}).whenComplete((r, ex) -> releaseEntries(entries));
}
use of io.pravega.client.tables.impl.TableSegmentEntry in project pravega by pravega.
the class PravegaTablesStoreHelper method addNewEntriesIfAbsent.
/**
* Method to add a batch of entries if absent. Table implementation on segment store guarantees that either all or none of
* the entries are added.
* If segment store responds with success, then it is guaranteed that all entries are added to the store.
* However, it is important to note that the segment store could respond with Data Exists even if one of the entries exists.
* In such case, this method will ignore data exist and respond with success for the entire batch. It does not verify
* if all entries existed or one of the entries existed.
* Callers should use this only if they are guaranteed to never create the requested entries outside of the requested batch.
*
* @param tableName table name
* @param toAdd map of keys and values to add.
* @param toBytes function to serialize individual values in the list of items to add.
* @param requestId request id
* @param <T> type of values.
* @return CompletableFuture which when completed successfully will indicate that all entries have been added successfully.
*/
public <T> CompletableFuture<Void> addNewEntriesIfAbsent(String tableName, List<Map.Entry<String, T>> toAdd, Function<T, byte[]> toBytes, long requestId) {
List<TableSegmentEntry> entries = toAdd.stream().map(x -> TableSegmentEntry.notExists(x.getKey().getBytes(Charsets.UTF_8), toBytes.apply(x.getValue()))).collect(Collectors.toList());
Supplier<String> errorMessage = () -> String.format("addNewEntriesIfAbsent: table: %s", tableName);
long time = System.currentTimeMillis();
return expectingDataExists(withRetries(() -> segmentHelper.updateTableEntries(tableName, entries, authToken.get(), requestId), errorMessage, requestId).handle((r, e) -> {
releaseEntries(entries);
if (e != null) {
Throwable unwrap = Exceptions.unwrap(e);
toAdd.forEach(entry -> invalidateCache(tableName, entry.getKey()));
if (unwrap instanceof StoreException.WriteConflictException) {
throw StoreException.create(StoreException.Type.DATA_EXISTS, errorMessage.get());
} else {
log.debug(requestId, "add new entries to {} threw exception {} {}", tableName, unwrap.getClass(), unwrap.getMessage());
throw new CompletionException(e);
}
} else {
log.debug(requestId, "entries added {} to table {}", toAdd, tableName);
for (int i = 0; i < r.size(); i++) {
putInCache(tableName, toAdd.get(i).getKey(), new VersionedMetadata<>(toAdd.get(i).getValue(), new Version.LongVersion(r.get(i).getSegmentVersion())), time);
}
return null;
}
}), null);
}
Aggregations