| 1 | /* |
| 2 | * Copyright (C) 2012-2015 DataStax Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.datastax.driver.stress; |
| 17 | |
| 18 | import com.datastax.driver.core.ResultSet; |
| 19 | import com.datastax.driver.core.ResultSetFuture; |
| 20 | import com.datastax.driver.core.Session; |
| 21 | import com.google.common.util.concurrent.FutureCallback; |
| 22 | import com.google.common.util.concurrent.Futures; |
| 23 | |
| 24 | import java.util.concurrent.CountDownLatch; |
| 25 | import java.util.concurrent.ExecutorService; |
| 26 | import java.util.concurrent.Executors; |
| 27 | |
| 28 | import static com.google.common.util.concurrent.Uninterruptibles.awaitUninterruptibly; |
| 29 | |
| 30 | public class AsynchronousConsumer implements Consumer { |
| 31 | |
| 32 | private static final ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); |
| 33 | |
| 34 | private final CountDownLatch shutdownLatch = new CountDownLatch(1); |
| 35 | |
| 36 | private final Session session; |
| 37 | private final QueryGenerator requests; |
| 38 | private final Reporter reporter; |
| 39 | |
| 40 | public AsynchronousConsumer(Session session, |
| 41 | QueryGenerator requests, |
| 42 | Reporter reporter) { |
| 43 | this.session = session; |
| 44 | this.requests = requests; |
| 45 | this.reporter = reporter; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public void start() { |
| 50 | executorService.execute(new Runnable() { |
| 51 | @Override |
| 52 | public void run() { |
| 53 | request(); |
| 54 | } |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | private void request() { |
| 59 | |
| 60 | if (!requests.hasNext()) { |
| 61 | shutdown(); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | handle(requests.next()); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void join() { |
| 70 | awaitUninterruptibly(shutdownLatch); |
| 71 | } |
| 72 | |
| 73 | protected void handle(QueryGenerator.Request request) { |
| 74 | |
| 75 | final Reporter.Context ctx = reporter.newRequest(); |
| 76 | |
| 77 | ResultSetFuture resultSetFuture = request.executeAsync(session); |
| 78 | Futures.addCallback(resultSetFuture, new FutureCallback<ResultSet>() { |
| 79 | @Override |
| 80 | public void onSuccess(final ResultSet result) { |
| 81 | ctx.done(); |
| 82 | request(); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void onFailure(final Throwable t) { |
| 87 | // Could do better I suppose |
| 88 | System.err.println("Error during request: " + t); |
| 89 | ctx.done(); |
| 90 | request(); |
| 91 | } |
| 92 | }, executorService); |
| 93 | } |
| 94 | |
| 95 | protected void shutdown() { |
| 96 | shutdownLatch.countDown(); |
| 97 | } |
| 98 | } |