[ 
https://issues.apache.org/jira/browse/BEAM-4049?focusedWorklogId=92090&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-92090
 ]

ASF GitHub Bot logged work on BEAM-4049:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 18/Apr/18 11:51
            Start Date: 18/Apr/18 11:51
    Worklog Time Spent: 10m 
      Work Description: echauchot commented on a change in pull request #5112: 
[BEAM-4049] Improve CassandraIO write throughput by performing async queries
URL: https://github.com/apache/beam/pull/5112#discussion_r182354003
 
 

 ##########
 File path: 
sdks/java/io/cassandra/src/main/java/org/apache/beam/sdk/io/cassandra/CassandraServiceImpl.java
 ##########
 @@ -351,33 +354,54 @@ public TokenRange(
    */
   protected class WriterImpl<T> implements Writer<T> {
 
+    /**
+     * The threshold of 100 concurrent async queries is a heuristic commonly 
used
+     * by the Apache Cassandra community. There is no real gain to expect in 
tuning this value.
+     */
+    private static final int CONCURRENT_ASYNC_QUERIES = 100;
+
     private final CassandraIO.Write<T> spec;
 
     private final Cluster cluster;
     private final Session session;
     private final MappingManager mappingManager;
+    private List<ListenableFuture<Void>> writeFutures;
 
     public WriterImpl(CassandraIO.Write<T> spec) {
       this.spec = spec;
       this.cluster = getCluster(spec.hosts(), spec.port(), spec.username(), 
spec.password(),
           spec.localDc(), spec.consistencyLevel());
       this.session = cluster.connect(spec.keyspace());
       this.mappingManager = new MappingManager(session);
+      this.writeFutures = Lists.newArrayList();
     }
 
     /**
      * Write the entity to the Cassandra instance, using {@link Mapper} 
obtained with the
-     * {@link MappingManager}. This method use {@link Mapper#save(Object)} 
method, which is
-     * synchronous. It means the entity is guaranteed to be reliably committed 
to Cassandra.
+     * {@link MappingManager}. This method use {@link 
Mapper#saveAsync(Object)} method, which is
+     * asynchronous. Beam will wait for all futures to complete, to guarantee 
all writes have
+     * succeeded.
      */
     @Override
-    public void write(T entity) {
+    public void write(T entity) throws ExecutionException, 
InterruptedException {
       Mapper<T> mapper = (Mapper<T>) mappingManager.mapper(entity.getClass());
-      mapper.save(entity);
+      this.writeFutures.add(mapper.saveAsync(entity));
+      if (this.writeFutures.size() % CONCURRENT_ASYNC_QUERIES == 0) {
+        // We reached the max number of allowed in flight queries
+        LOG.info("Waiting for a batch of {} Cassandra writes to be 
executed...",
+            CONCURRENT_ASYNC_QUERIES);
+        waitForFuturesToFinish();
 
 Review comment:
   Add a comment like "as stated in the CassandraService interface, write 
methods needs to be synchronous, so blocks until all the asynchronous queries 
are issued"

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 92090)
    Time Spent: 4h 40m  (was: 4.5h)

> Improve write throughput of CassandraIO
> ---------------------------------------
>
>                 Key: BEAM-4049
>                 URL: https://issues.apache.org/jira/browse/BEAM-4049
>             Project: Beam
>          Issue Type: Improvement
>          Components: io-java-cassandra
>    Affects Versions: 2.4.0
>            Reporter: Alexander Dejanovski
>            Assignee: Alexander Dejanovski
>            Priority: Major
>              Labels: performance
>          Time Spent: 4h 40m
>  Remaining Estimate: 0h
>
> The CassandraIO currently uses the mapper to perform writes in a synchronous 
> fashion. 
> This implies that writes are serialized and is a very suboptimal way of 
> writing to Cassandra.
> The IO should use the saveAsync() method instead of save() and should wait 
> for completion each time 100 queries are in flight, in order to avoid 
> overwhelming clusters.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to