Search in sources :

Example 1 with AbstractDocument

use of com.couchbase.client.java.document.AbstractDocument in project incubator-gobblin by apache.

the class CouchbaseWriterTest method writeRecordsWithAsyncWriter.

/**
 * Uses the {@link AsyncWriterManager} to write records through a couchbase writer
 * It keeps a copy of the key, value combinations written and checks after all the writes have gone through.
 * @param recordIterator
 * @throws IOException
 */
private void writeRecordsWithAsyncWriter(Iterator<AbstractDocument> recordIterator) throws IOException {
    boolean verbose = false;
    Properties props = new Properties();
    props.setProperty(CouchbaseWriterConfigurationKeys.BUCKET, "default");
    Config config = ConfigFactory.parseProperties(props);
    CouchbaseWriter writer = new CouchbaseWriter(_couchbaseEnvironment, config);
    try {
        AsyncWriterManager asyncWriterManager = AsyncWriterManager.builder().asyncDataWriter(writer).maxOutstandingWrites(100000).retriesEnabled(true).numRetries(5).build();
        if (verbose) {
            // Create a reporter for metrics. This reporter will write metrics to STDOUT.
            OutputStreamReporter.Factory.newBuilder().build(new Properties());
            // Start all metric reporters.
            RootMetricContext.get().startReporting();
        }
        Verifier verifier = new Verifier();
        while (recordIterator.hasNext()) {
            AbstractDocument doc = recordIterator.next();
            verifier.onWrite(doc);
            asyncWriterManager.write(doc);
        }
        asyncWriterManager.commit();
        verifier.verify(writer.getBucket());
    } finally {
        writer.close();
    }
}
Also used : AbstractDocument(com.couchbase.client.java.document.AbstractDocument) Config(com.typesafe.config.Config) AsyncWriterManager(org.apache.gobblin.writer.AsyncWriterManager) Properties(java.util.Properties)

Example 2 with AbstractDocument

use of com.couchbase.client.java.document.AbstractDocument in project incubator-gobblin by apache.

the class CouchbaseWriterTest method writeRecords.

private List<Pair<AbstractDocument, Future>> writeRecords(Iterator<AbstractDocument> recordIterator, CouchbaseWriter writer, int outstandingRequests, long kvTimeout, TimeUnit kvTimeoutUnit) throws DataConversionException, UnsupportedEncodingException {
    final BlockingQueue<Pair<AbstractDocument, Future>> outstandingCallQueue = new LinkedBlockingDeque<>(outstandingRequests);
    final List<Pair<AbstractDocument, Future>> failedFutures = new ArrayList<>(outstandingRequests);
    int index = 0;
    long runTime = 0;
    final AtomicInteger callbackSuccesses = new AtomicInteger(0);
    final AtomicInteger callbackFailures = new AtomicInteger(0);
    final ConcurrentLinkedDeque<Throwable> callbackExceptions = new ConcurrentLinkedDeque<>();
    Verifier verifier = new Verifier();
    while (recordIterator.hasNext()) {
        AbstractDocument doc = recordIterator.next();
        index++;
        verifier.onWrite(doc);
        final long startTime = System.nanoTime();
        Future callFuture = writer.write(doc, new WriteCallback<TupleDocument>() {

            @Override
            public void onSuccess(WriteResponse<TupleDocument> writeResponse) {
                callbackSuccesses.incrementAndGet();
            }

            @Override
            public void onFailure(Throwable throwable) {
                callbackFailures.incrementAndGet();
                callbackExceptions.add(throwable);
            }
        });
        drainQueue(outstandingCallQueue, 1, kvTimeout, kvTimeoutUnit, failedFutures);
        outstandingCallQueue.add(new Pair<>(doc, callFuture));
        runTime += System.nanoTime() - startTime;
    }
    int failedWrites = 0;
    long responseStartTime = System.nanoTime();
    drainQueue(outstandingCallQueue, outstandingRequests, kvTimeout, kvTimeoutUnit, failedFutures);
    runTime += System.nanoTime() - responseStartTime;
    for (Throwable failure : callbackExceptions) {
        System.out.println(failure.getClass() + " : " + failure.getMessage());
    }
    failedWrites += failedFutures.size();
    System.out.println("Total time to send " + index + " records = " + runTime / 1000000.0 + "ms, " + "Failed writes = " + failedWrites + " Callback Successes = " + callbackSuccesses.get() + "Callback Failures = " + callbackFailures.get());
    verifier.verify(writer.getBucket());
    return failedFutures;
}
Also used : LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ArrayList(java.util.ArrayList) TupleDocument(org.apache.gobblin.couchbase.common.TupleDocument) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) AbstractDocument(com.couchbase.client.java.document.AbstractDocument) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Future(java.util.concurrent.Future) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) Pair(org.apache.commons.math3.util.Pair)

Aggregations

AbstractDocument (com.couchbase.client.java.document.AbstractDocument)2 Config (com.typesafe.config.Config)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 ConcurrentLinkedDeque (java.util.concurrent.ConcurrentLinkedDeque)1 Future (java.util.concurrent.Future)1 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Pair (org.apache.commons.math3.util.Pair)1 TupleDocument (org.apache.gobblin.couchbase.common.TupleDocument)1 AsyncWriterManager (org.apache.gobblin.writer.AsyncWriterManager)1 NameValuePair (org.apache.http.NameValuePair)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1