Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 7CF5F200C53 for ; Tue, 11 Apr 2017 19:31:12 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 7B9A0160BB0; Tue, 11 Apr 2017 17:31:12 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id A2EBD160B9B for ; Tue, 11 Apr 2017 19:31:11 +0200 (CEST) Received: (qmail 33156 invoked by uid 500); 11 Apr 2017 17:31:07 -0000 Mailing-List: contact commits-help@hbase.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@hbase.apache.org Delivered-To: mailing list commits@hbase.apache.org Received: (qmail 32382 invoked by uid 99); 11 Apr 2017 17:31:06 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 11 Apr 2017 17:31:06 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id D2FB9E024D; Tue, 11 Apr 2017 17:31:06 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: elserj@apache.org To: commits@hbase.apache.org Date: Tue, 11 Apr 2017 17:31:31 -0000 Message-Id: <7830b41961f7491ca4ba60237fbe3b28@git.apache.org> In-Reply-To: <358888fa9a6c44adb1f92a0995815315@git.apache.org> References: <358888fa9a6c44adb1f92a0995815315@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [26/54] [abbrv] hbase git commit: HBASE-16999 Implement master and regionserver synchronization of quota state archived-at: Tue, 11 Apr 2017 17:31:12 -0000 http://git-wip-us.apache.org/repos/asf/hbase/blob/4a715450/hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/TestTableSpaceQuotaViolationNotifier.java ---------------------------------------------------------------------- diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/TestTableSpaceQuotaViolationNotifier.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/TestTableSpaceQuotaViolationNotifier.java new file mode 100644 index 0000000..4a7000c --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/quotas/TestTableSpaceQuotaViolationNotifier.java @@ -0,0 +1,144 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.quotas; + +import static org.mockito.Matchers.argThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.List; +import java.util.Map.Entry; +import java.util.NavigableMap; +import java.util.Objects; + +import org.apache.hadoop.hbase.Cell; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Connection; +import org.apache.hadoop.hbase.client.Delete; +import org.apache.hadoop.hbase.client.Mutation; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; +import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos.SpaceQuota; +import org.apache.hadoop.hbase.testclassification.SmallTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.mockito.ArgumentMatcher; + +/** + * Test case for {@link TableSpaceQuotaViolationNotifier}. + */ +@Category(SmallTests.class) +public class TestTableSpaceQuotaViolationNotifier { + + private TableSpaceQuotaViolationNotifier notifier; + private Connection conn; + + @Before + public void setup() throws Exception { + notifier = new TableSpaceQuotaViolationNotifier(); + conn = mock(Connection.class); + notifier.initialize(conn); + } + + @Test + public void testToViolation() throws Exception { + final TableName tn = TableName.valueOf("inviolation"); + final SpaceViolationPolicy policy = SpaceViolationPolicy.NO_INSERTS; + final Table quotaTable = mock(Table.class); + when(conn.getTable(QuotaTableUtil.QUOTA_TABLE_NAME)).thenReturn(quotaTable); + + final Put expectedPut = new Put(Bytes.toBytes("t." + tn.getNameAsString())); + final SpaceQuota protoQuota = SpaceQuota.newBuilder() + .setViolationPolicy(ProtobufUtil.toProtoViolationPolicy(policy)) + .build(); + expectedPut.addColumn(Bytes.toBytes("u"), Bytes.toBytes("v"), protoQuota.toByteArray()); + + notifier.transitionTableToViolation(tn, policy); + + verify(quotaTable).put(argThat(new SingleCellPutMatcher(expectedPut))); + } + + @Test + public void testToObservance() throws Exception { + final TableName tn = TableName.valueOf("notinviolation"); + final Table quotaTable = mock(Table.class); + when(conn.getTable(QuotaTableUtil.QUOTA_TABLE_NAME)).thenReturn(quotaTable); + + final Delete expectedDelete = new Delete(Bytes.toBytes("t." + tn.getNameAsString())); + expectedDelete.addColumn(Bytes.toBytes("u"), Bytes.toBytes("v")); + + notifier.transitionTableToObservance(tn); + + verify(quotaTable).delete(argThat(new SingleCellDeleteMatcher(expectedDelete))); + } + + /** + * Parameterized for Puts. + */ + private static class SingleCellPutMatcher extends SingleCellMutationMatcher { + private SingleCellPutMatcher(Put expected) { + super(expected); + } + } + + /** + * Parameterized for Deletes. + */ + private static class SingleCellDeleteMatcher extends SingleCellMutationMatcher { + private SingleCellDeleteMatcher(Delete expected) { + super(expected); + } + } + + /** + * Quick hack to verify a Mutation with one column. + */ + private static class SingleCellMutationMatcher extends ArgumentMatcher { + private final Mutation expected; + + private SingleCellMutationMatcher(Mutation expected) { + this.expected = expected; + } + + @Override + public boolean matches(Object argument) { + if (!expected.getClass().isAssignableFrom(argument.getClass())) { + return false; + } + Mutation actual = (Mutation) argument; + if (!Arrays.equals(expected.getRow(), actual.getRow())) { + return false; + } + if (expected.size() != actual.size()) { + return false; + } + NavigableMap> expectedCells = expected.getFamilyCellMap(); + NavigableMap> actualCells = actual.getFamilyCellMap(); + Entry> expectedEntry = expectedCells.entrySet().iterator().next(); + Entry> actualEntry = actualCells.entrySet().iterator().next(); + if (!Arrays.equals(expectedEntry.getKey(), actualEntry.getKey())) { + return false; + } + return Objects.equals(expectedEntry.getValue(), actualEntry.getValue()); + } + } +}