001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import java.io.IOException;
021import java.util.concurrent.CompletableFuture;
022import java.util.concurrent.CountDownLatch;
023import java.util.concurrent.atomic.AtomicLong;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.client.backoff.ClientBackoffPolicy;
026import org.apache.hadoop.hbase.testclassification.ClientTests;
027import org.apache.hadoop.hbase.testclassification.LargeTests;
028import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
029import org.apache.hadoop.hbase.util.FutureUtils;
030import org.junit.After;
031import org.junit.Before;
032import org.junit.ClassRule;
033import org.junit.experimental.categories.Category;
034
035import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
036
037@Category({ LargeTests.class, ClientTests.class })
038public class TestAsyncClientPushback extends ClientPushbackTestBase {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestAsyncClientPushback.class);
043
044  private AsyncConnectionImpl conn;
045
046  private AsyncBufferedMutator mutator;
047
048  @Before
049  public void setUp() throws Exception {
050    conn =
051      (AsyncConnectionImpl) ConnectionFactory.createAsyncConnection(UTIL.getConfiguration()).get();
052    mutator = conn.getBufferedMutator(tableName);
053  }
054
055  @After
056  public void tearDown() throws IOException {
057    Closeables.close(mutator, true);
058    Closeables.close(conn, true);
059  }
060
061  @Override
062  protected ClientBackoffPolicy getBackoffPolicy() throws IOException {
063    return conn.getBackoffPolicy();
064  }
065
066  @Override
067  protected ServerStatisticTracker getStatisticsTracker() throws IOException {
068    return conn.getStatisticsTracker().get();
069  }
070
071  @Override
072  protected MetricsConnection getConnectionMetrics() throws IOException {
073    return conn.getConnectionMetrics().get();
074  }
075
076  @Override
077  protected void mutate(Put put) throws IOException {
078    CompletableFuture<?> future = mutator.mutate(put);
079    mutator.flush();
080    future.join();
081  }
082
083  @Override
084  protected void mutate(Put put, AtomicLong endTime, CountDownLatch latch) throws IOException {
085    FutureUtils.addListener(mutator.mutate(put), (r, e) -> {
086      endTime.set(EnvironmentEdgeManager.currentTime());
087      latch.countDown();
088    });
089    mutator.flush();
090  }
091
092  @Override
093  protected void mutateRow(RowMutations mutations) throws IOException {
094    conn.getTable(tableName).mutateRow(mutations).join();
095  }
096}