Search in sources :

Example 56 with NamespaceDescriptor

use of org.apache.hadoop.hbase.NamespaceDescriptor in project hbase by apache.

the class TestNamespacesInstanceResource method testGetNamespaceTablesAndCannotDeleteNamespace.

@Test
public void testGetNamespaceTablesAndCannotDeleteNamespace() throws IOException, JAXBException {
    Admin admin = TEST_UTIL.getAdmin();
    String nsName = "TestNamespacesInstanceResource5";
    Response response;
    // Create namespace via admin.
    NamespaceDescriptor.Builder nsBuilder = NamespaceDescriptor.create(nsName);
    NamespaceDescriptor nsd = nsBuilder.build();
    nsd.setConfiguration("key1", "value1");
    admin.createNamespace(nsd);
    // Create two tables via admin.
    TableName tn1 = TableName.valueOf(nsName + ":table1");
    TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tn1);
    ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("cf1")).build();
    tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
    admin.createTable(tableDescriptorBuilder.build());
    TableName tn2 = TableName.valueOf(nsName + ":table2");
    tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tn2);
    tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
    admin.createTable(tableDescriptorBuilder.build());
    Map<String, String> nsProperties = new HashMap<>();
    nsProperties.put("key1", "value1");
    List<String> nsTables = Arrays.asList("table1", "table2");
    // Check get namespace properties as XML, JSON and Protobuf.
    String namespacePath = "/namespaces/" + nsName;
    response = client.get(namespacePath);
    assertEquals(200, response.getCode());
    response = client.get(namespacePath, Constants.MIMETYPE_XML);
    assertEquals(200, response.getCode());
    NamespacesInstanceModel model = fromXML(response.getBody());
    checkNamespaceProperties(model.getProperties(), nsProperties);
    response = client.get(namespacePath, Constants.MIMETYPE_JSON);
    assertEquals(200, response.getCode());
    model = jsonMapper.readValue(response.getBody(), NamespacesInstanceModel.class);
    checkNamespaceProperties(model.getProperties(), nsProperties);
    response = client.get(namespacePath, Constants.MIMETYPE_PROTOBUF);
    assertEquals(200, response.getCode());
    model.getObjectFromMessage(response.getBody());
    checkNamespaceProperties(model.getProperties(), nsProperties);
    // Check get namespace tables as XML, JSON and Protobuf.
    namespacePath = "/namespaces/" + nsName + "/tables";
    response = client.get(namespacePath);
    assertEquals(200, response.getCode());
    response = client.get(namespacePath, Constants.MIMETYPE_XML);
    assertEquals(200, response.getCode());
    TableListModel tablemodel = fromXML(response.getBody());
    checkNamespaceTables(tablemodel.getTables(), nsTables);
    response = client.get(namespacePath, Constants.MIMETYPE_JSON);
    assertEquals(200, response.getCode());
    tablemodel = jsonMapper.readValue(response.getBody(), TableListModel.class);
    checkNamespaceTables(tablemodel.getTables(), nsTables);
    response = client.get(namespacePath, Constants.MIMETYPE_PROTOBUF);
    assertEquals(200, response.getCode());
    tablemodel.setTables(new ArrayList<>());
    tablemodel.getObjectFromMessage(response.getBody());
    checkNamespaceTables(tablemodel.getTables(), nsTables);
    // Check cannot delete namespace via REST because it contains tables.
    response = client.delete(namespacePath);
    namespacePath = "/namespaces/" + nsName;
    assertEquals(503, response.getCode());
}
Also used : TableListModel(org.apache.hadoop.hbase.rest.model.TableListModel) HashMap(java.util.HashMap) TableDescriptorBuilder(org.apache.hadoop.hbase.client.TableDescriptorBuilder) Admin(org.apache.hadoop.hbase.client.Admin) ColumnFamilyDescriptor(org.apache.hadoop.hbase.client.ColumnFamilyDescriptor) Response(org.apache.hadoop.hbase.rest.client.Response) TableName(org.apache.hadoop.hbase.TableName) TestNamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.TestNamespacesInstanceModel) NamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.NamespacesInstanceModel) NamespaceDescriptor(org.apache.hadoop.hbase.NamespaceDescriptor) Test(org.junit.Test)

Example 57 with NamespaceDescriptor

use of org.apache.hadoop.hbase.NamespaceDescriptor in project hbase by apache.

the class TestNamespacesInstanceResource method testInvalidNamespacePostsAndPuts.

@Ignore("HBASE-19210")
@Test
public void testInvalidNamespacePostsAndPuts() throws IOException, JAXBException {
    String namespacePath1 = "/namespaces/" + NAMESPACE1;
    String namespacePath2 = "/namespaces/" + NAMESPACE2;
    String namespacePath3 = "/namespaces/" + NAMESPACE3;
    NamespacesInstanceModel model1;
    NamespacesInstanceModel model2;
    NamespacesInstanceModel model3;
    Response response;
    // Check that namespaces don't exist via non-REST call.
    Admin admin = TEST_UTIL.getAdmin();
    assertNull(findNamespace(admin, NAMESPACE1));
    assertNull(findNamespace(admin, NAMESPACE2));
    assertNull(findNamespace(admin, NAMESPACE3));
    model1 = testNamespacesInstanceModel.buildTestModel(NAMESPACE1, NAMESPACE1_PROPS);
    testNamespacesInstanceModel.checkModel(model1, NAMESPACE1, NAMESPACE1_PROPS);
    model2 = testNamespacesInstanceModel.buildTestModel(NAMESPACE2, NAMESPACE2_PROPS);
    testNamespacesInstanceModel.checkModel(model2, NAMESPACE2, NAMESPACE2_PROPS);
    model3 = testNamespacesInstanceModel.buildTestModel(NAMESPACE3, NAMESPACE3_PROPS);
    testNamespacesInstanceModel.checkModel(model3, NAMESPACE3, NAMESPACE3_PROPS);
    // Try REST post and puts with invalid content.
    response = client.post(namespacePath1, Constants.MIMETYPE_JSON, toXML(model1));
    assertEquals(500, response.getCode());
    String jsonString = jsonMapper.writeValueAsString(model2);
    response = client.put(namespacePath2, Constants.MIMETYPE_XML, Bytes.toBytes(jsonString));
    assertEquals(400, response.getCode());
    response = client.post(namespacePath3, Constants.MIMETYPE_PROTOBUF, toXML(model3));
    assertEquals(500, response.getCode());
    NamespaceDescriptor nd1 = findNamespace(admin, NAMESPACE1);
    NamespaceDescriptor nd2 = findNamespace(admin, NAMESPACE2);
    NamespaceDescriptor nd3 = findNamespace(admin, NAMESPACE3);
    assertNull(nd1);
    assertNull(nd2);
    assertNull(nd3);
}
Also used : Response(org.apache.hadoop.hbase.rest.client.Response) TestNamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.TestNamespacesInstanceModel) NamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.NamespacesInstanceModel) NamespaceDescriptor(org.apache.hadoop.hbase.NamespaceDescriptor) Admin(org.apache.hadoop.hbase.client.Admin) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 58 with NamespaceDescriptor

use of org.apache.hadoop.hbase.NamespaceDescriptor in project hbase by apache.

the class TestNamespacesInstanceResource method testNamespaceCreateAndDeletePBAndNoBody.

@Test
public void testNamespaceCreateAndDeletePBAndNoBody() throws IOException {
    String namespacePath3 = "/namespaces/" + NAMESPACE3;
    String namespacePath4 = "/namespaces/" + NAMESPACE4;
    NamespacesInstanceModel model3;
    NamespacesInstanceModel model4;
    Response response;
    // Check that namespaces don't exist via non-REST call.
    Admin admin = TEST_UTIL.getAdmin();
    assertNull(findNamespace(admin, NAMESPACE3));
    assertNull(findNamespace(admin, NAMESPACE4));
    model3 = testNamespacesInstanceModel.buildTestModel(NAMESPACE3, NAMESPACE3_PROPS);
    testNamespacesInstanceModel.checkModel(model3, NAMESPACE3, NAMESPACE3_PROPS);
    model4 = testNamespacesInstanceModel.buildTestModel(NAMESPACE4, NAMESPACE4_PROPS);
    testNamespacesInstanceModel.checkModel(model4, NAMESPACE4, NAMESPACE4_PROPS);
    // Defines null headers for use in tests where no body content is provided, so that we set
    // no content-type in the request
    Header[] nullHeaders = null;
    // Test cannot PUT (alter) non-existent namespace.
    response = client.put(namespacePath3, nullHeaders, new byte[] {});
    assertEquals(403, response.getCode());
    response = client.put(namespacePath4, Constants.MIMETYPE_PROTOBUF, model4.createProtobufOutput());
    assertEquals(403, response.getCode());
    // Test cannot create tables when in read only mode.
    conf.set("hbase.rest.readonly", "true");
    response = client.post(namespacePath3, nullHeaders, new byte[] {});
    assertEquals(403, response.getCode());
    response = client.put(namespacePath4, Constants.MIMETYPE_PROTOBUF, model4.createProtobufOutput());
    assertEquals(403, response.getCode());
    NamespaceDescriptor nd3 = findNamespace(admin, NAMESPACE3);
    NamespaceDescriptor nd4 = findNamespace(admin, NAMESPACE4);
    assertNull(nd3);
    assertNull(nd4);
    conf.set("hbase.rest.readonly", "false");
    // Create namespace with no body and binary content type.
    response = client.post(namespacePath3, nullHeaders, new byte[] {});
    assertEquals(201, response.getCode());
    // Create namespace with protobuf content-type.
    response = client.post(namespacePath4, Constants.MIMETYPE_PROTOBUF, model4.createProtobufOutput());
    assertEquals(201, response.getCode());
    // check setting unsupported content-type returns 415
    response = client.post(namespacePath3, Constants.MIMETYPE_BINARY, new byte[] {});
    assertEquals(415, response.getCode());
    // Check that created namespaces correctly.
    nd3 = findNamespace(admin, NAMESPACE3);
    nd4 = findNamespace(admin, NAMESPACE4);
    assertNotNull(nd3);
    assertNotNull(nd4);
    checkNamespaceProperties(nd3, new HashMap<>());
    checkNamespaceProperties(nd4, NAMESPACE4_PROPS);
    // Check cannot post tables that already exist.
    response = client.post(namespacePath3, nullHeaders, new byte[] {});
    assertEquals(403, response.getCode());
    response = client.post(namespacePath4, Constants.MIMETYPE_PROTOBUF, model4.createProtobufOutput());
    assertEquals(403, response.getCode());
    // Check cannot post tables when in read only mode.
    conf.set("hbase.rest.readonly", "true");
    response = client.delete(namespacePath3);
    assertEquals(403, response.getCode());
    response = client.delete(namespacePath4);
    assertEquals(403, response.getCode());
    nd3 = findNamespace(admin, NAMESPACE3);
    nd4 = findNamespace(admin, NAMESPACE4);
    assertNotNull(nd3);
    assertNotNull(nd4);
    conf.set("hbase.rest.readonly", "false");
    // Delete namespaces via XML and JSON.
    response = client.delete(namespacePath3);
    assertEquals(200, response.getCode());
    response = client.delete(namespacePath4);
    assertEquals(200, response.getCode());
    nd3 = findNamespace(admin, NAMESPACE3);
    nd4 = findNamespace(admin, NAMESPACE4);
    assertNull(nd3);
    assertNull(nd4);
}
Also used : Response(org.apache.hadoop.hbase.rest.client.Response) TestNamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.TestNamespacesInstanceModel) NamespacesInstanceModel(org.apache.hadoop.hbase.rest.model.NamespacesInstanceModel) Header(org.apache.http.Header) NamespaceDescriptor(org.apache.hadoop.hbase.NamespaceDescriptor) Admin(org.apache.hadoop.hbase.client.Admin) Test(org.junit.Test)

Example 59 with NamespaceDescriptor

use of org.apache.hadoop.hbase.NamespaceDescriptor in project hbase by apache.

the class TestNamespacesResource method createNamespaceViaAdmin.

private void createNamespaceViaAdmin(Admin admin, String name) throws IOException {
    NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(name);
    NamespaceDescriptor nsd = builder.build();
    admin.createNamespace(nsd);
}
Also used : NamespaceDescriptor(org.apache.hadoop.hbase.NamespaceDescriptor)

Example 60 with NamespaceDescriptor

use of org.apache.hadoop.hbase.NamespaceDescriptor in project hbase by apache.

the class HMaster method modifyNamespace.

/**
 * Modify an existing Namespace.
 * @param nonceGroup Identifier for the source of the request, a client or process.
 * @param nonce A unique identifier for this operation from the client or process identified by
 * <code>nonceGroup</code> (the source must ensure each operation gets a unique id).
 * @return procedure id
 */
long modifyNamespace(final NamespaceDescriptor newNsDescriptor, final long nonceGroup, final long nonce) throws IOException {
    checkInitialized();
    TableName.isLegalNamespaceName(Bytes.toBytes(newNsDescriptor.getName()));
    return MasterProcedureUtil.submitProcedure(new MasterProcedureUtil.NonceProcedureRunnable(this, nonceGroup, nonce) {

        @Override
        protected void run() throws IOException {
            NamespaceDescriptor oldNsDescriptor = getNamespace(newNsDescriptor.getName());
            getMaster().getMasterCoprocessorHost().preModifyNamespace(oldNsDescriptor, newNsDescriptor);
            // We need to wait for the procedure to potentially fail due to "prepare" sanity
            // checks. This will block only the beginning of the procedure. See HBASE-19953.
            ProcedurePrepareLatch latch = ProcedurePrepareLatch.createBlockingLatch();
            LOG.info(getClientIdAuditPrefix() + " modify " + newNsDescriptor);
            // Execute the operation synchronously - wait for the operation to complete before
            // continuing.
            setProcId(getClusterSchema().modifyNamespace(newNsDescriptor, getNonceKey(), latch));
            latch.await();
            getMaster().getMasterCoprocessorHost().postModifyNamespace(oldNsDescriptor, newNsDescriptor);
        }

        @Override
        protected String getDescription() {
            return "ModifyNamespaceProcedure";
        }
    });
}
Also used : NamespaceDescriptor(org.apache.hadoop.hbase.NamespaceDescriptor) ProcedurePrepareLatch(org.apache.hadoop.hbase.master.procedure.ProcedurePrepareLatch) IOException(java.io.IOException) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) HBaseIOException(org.apache.hadoop.hbase.HBaseIOException) InterruptedIOException(java.io.InterruptedIOException) NonceProcedureRunnable(org.apache.hadoop.hbase.master.procedure.MasterProcedureUtil.NonceProcedureRunnable) MasterProcedureUtil(org.apache.hadoop.hbase.master.procedure.MasterProcedureUtil)

Aggregations

NamespaceDescriptor (org.apache.hadoop.hbase.NamespaceDescriptor)97 Test (org.junit.Test)51 TableName (org.apache.hadoop.hbase.TableName)26 IOException (java.io.IOException)17 Admin (org.apache.hadoop.hbase.client.Admin)15 TableDescriptor (org.apache.hadoop.hbase.client.TableDescriptor)13 ColumnFamilyDescriptor (org.apache.hadoop.hbase.client.ColumnFamilyDescriptor)11 TableDescriptorBuilder (org.apache.hadoop.hbase.client.TableDescriptorBuilder)11 QuotaExceededException (org.apache.hadoop.hbase.quotas.QuotaExceededException)9 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)8 Table (org.apache.hadoop.hbase.client.Table)8 DoNotRetryIOException (org.apache.hadoop.hbase.DoNotRetryIOException)7 NamespaceNotFoundException (org.apache.hadoop.hbase.NamespaceNotFoundException)7 Connection (org.apache.hadoop.hbase.client.Connection)7 ConstraintException (org.apache.hadoop.hbase.constraint.ConstraintException)7 RestoreSnapshotException (org.apache.hadoop.hbase.snapshot.RestoreSnapshotException)7 KeeperException (org.apache.zookeeper.KeeperException)7 ArrayList (java.util.ArrayList)6 ExecutionException (java.util.concurrent.ExecutionException)5 NamespaceExistException (org.apache.hadoop.hbase.NamespaceExistException)5