G - Type of a GroupK - Type of a KeyT - Type of a Valuepublic abstract class TaskBatchingStrategy<G,K,T> extends BatchingStrategy<G,K,T>
Example below shows how to build a ParSeq client for a key-value store that provides transparent batching given existing Task-based API. Let's assume that we have an implementation of the following key-value store interface:
interface KVStore {
Task <String> get(Long key);
Task <Map<Long, Try<String>>> batchGet(Collection <Long> keys);
}
We can then implement a TaskBatchingStrategy in the following way (for the sake
of simplicity we assume that all keys can be grouped into one batch thus we implement
SimpleTaskBatchingStrategy):
public static class BatchingKVStoreClient extends SimpleTaskBatchingStrategy<Long, String>{ private final KVStore _store; public BatchingKVStoreClient(KVStore store) { _store = store; }@Overridepublic void executeBatch(Integer group, Batch<Long, String>batch) { Map<Long, String>batchResult = _store.batchGet(batch.keys()); batch.foreach((key, promise)->promise.done(batchResult.get(key))); }@OverridepublicTask<Map<Long, Try<String>>>taskForBatch(Set<Long>keys) { return _store.batchGet(keys); } }
taskForBatch method returns a task that computes a map that for every key contains
either a success with a value or a failure. If returned map does not contain results for
some keys the tasks for which results are missing will fail.BatchingStrategy,
SimpleTaskBatchingStrategyDEFAULT_MAX_BATCH_SIZE| Constructor and Description |
|---|
TaskBatchingStrategy() |
| Modifier and Type | Method and Description |
|---|---|
void |
executeBatch(G group,
Batch<K,T> batch)
This method will be called for every
Batch. |
protected void |
executeBatchWithContext(G group,
Batch<K,T> batch,
com.linkedin.parseq.Context ctx) |
String |
getBatchName(G group,
Batch<K,T> batch)
Overriding this method allows providing custom name for a batch.
|
String |
getBatchName(G group,
Set<K> keys)
Overriding this method allows providing custom name for a batch.
|
abstract com.linkedin.parseq.Task<Map<K,com.linkedin.parseq.function.Try<T>>> |
taskForBatch(G group,
Set<K> keys)
This method will be called for every batch.
|
batchable, batchable, classify, getBatchAggregationTimeMetric, getBatchSizeMetric, keySize, maxBatchSizeForGrouppublic final void executeBatch(G group, Batch<K,T> batch)
BatchingStrategyBatch.
Implementation of this method must make sure that all SettablePromise contained in the Batch
will eventually be resolved - typically asynchronously. Failing to eventually resolve any
of the promises may lead to plan that never completes i.e. appears to hung and may lead to
a memory leak.executeBatch in class BatchingStrategy<G,K,T>group - group that represents the batchbatch - batch contains collection of SettablePromise that eventually need to be resolved - typically asynchronouslyprotected void executeBatchWithContext(G group, Batch<K,T> batch, com.linkedin.parseq.Context ctx)
executeBatchWithContext in class BatchingStrategy<G,K,T>public final String getBatchName(G group, Batch<K,T> batch)
BatchingStrategygetBatchName in class BatchingStrategy<G,K,T>group - group to be describedbatch - batch to be describedpublic String getBatchName(G group, Set<K> keys)
keys - set of keys belonging to the batch that needs to be describedgroup - group to be describedpublic abstract com.linkedin.parseq.Task<Map<K,com.linkedin.parseq.function.Try<T>>> taskForBatch(G group, Set<K> keys)
group - group that represents the batchkeys - set of keys belonging to the batchCopyright © 2018. All rights reserved.