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.master; 019 020import java.io.IOException; 021import java.util.List; 022import org.apache.hadoop.hbase.NamespaceDescriptor; 023import org.apache.hadoop.hbase.NamespaceNotFoundException; 024import org.apache.hadoop.hbase.ServiceNotRunningException; 025import org.apache.hadoop.hbase.master.procedure.CreateNamespaceProcedure; 026import org.apache.hadoop.hbase.master.procedure.DeleteNamespaceProcedure; 027import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv; 028import org.apache.hadoop.hbase.master.procedure.ModifyNamespaceProcedure; 029import org.apache.hadoop.hbase.master.procedure.ProcedurePrepareLatch; 030import org.apache.hadoop.hbase.procedure2.Procedure; 031import org.apache.hadoop.hbase.procedure2.ProcedureExecutor; 032import org.apache.hadoop.hbase.util.NonceKey; 033import org.apache.yetus.audience.InterfaceAudience; 034 035import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList; 036import org.apache.hbase.thirdparty.com.google.common.util.concurrent.AbstractService; 037 038@InterfaceAudience.Private 039class ClusterSchemaServiceImpl extends AbstractService implements ClusterSchemaService { 040 041 private final TableNamespaceManager tableNamespaceManager; 042 private final MasterServices masterServices; 043 044 ClusterSchemaServiceImpl(final MasterServices masterServices) { 045 this.masterServices = masterServices; 046 this.tableNamespaceManager = new TableNamespaceManager(masterServices); 047 } 048 049 // All below are synchronized so consistent view on whether running or not. 050 051 private synchronized void checkIsRunning() throws ServiceNotRunningException { 052 if (!isRunning()) { 053 throw new ServiceNotRunningException(); 054 } 055 } 056 057 @Override 058 protected synchronized void doStart() { 059 try { 060 notifyStarted(); 061 this.tableNamespaceManager.start(); 062 } catch (IOException | InterruptedException e) { 063 notifyFailed(e); 064 } 065 } 066 067 @Override 068 protected void doStop() { 069 // This is no stop for the table manager. 070 notifyStopped(); 071 } 072 073 @Override 074 public TableNamespaceManager getTableNamespaceManager() { 075 return this.tableNamespaceManager; 076 } 077 078 private long submitProcedure(final Procedure<MasterProcedureEnv> procedure, 079 final NonceKey nonceKey) throws ServiceNotRunningException { 080 checkIsRunning(); 081 ProcedureExecutor<MasterProcedureEnv> pe = this.masterServices.getMasterProcedureExecutor(); 082 return pe.submitProcedure(procedure, nonceKey); 083 } 084 085 @Override 086 public long createNamespace(NamespaceDescriptor namespaceDescriptor, final NonceKey nonceKey, 087 final ProcedurePrepareLatch latch) throws IOException { 088 return submitProcedure(new CreateNamespaceProcedure( 089 this.masterServices.getMasterProcedureExecutor().getEnvironment(), namespaceDescriptor, 090 latch), nonceKey); 091 } 092 093 @Override 094 public long modifyNamespace(NamespaceDescriptor namespaceDescriptor, final NonceKey nonceKey, 095 final ProcedurePrepareLatch latch) throws IOException { 096 return submitProcedure(new ModifyNamespaceProcedure( 097 this.masterServices.getMasterProcedureExecutor().getEnvironment(), namespaceDescriptor, 098 latch), nonceKey); 099 } 100 101 @Override 102 public long deleteNamespace(String name, final NonceKey nonceKey, 103 final ProcedurePrepareLatch latch) throws IOException { 104 return submitProcedure(new DeleteNamespaceProcedure( 105 this.masterServices.getMasterProcedureExecutor().getEnvironment(), name, latch), nonceKey); 106 } 107 108 @Override 109 public NamespaceDescriptor getNamespace(String name) throws IOException { 110 NamespaceDescriptor nsd = getTableNamespaceManager().get(name); 111 if (nsd == null) throw new NamespaceNotFoundException(name); 112 return nsd; 113 } 114 115 @Override 116 public List<NamespaceDescriptor> getNamespaces() throws IOException { 117 checkIsRunning(); 118 return getTableNamespaceManager().list().stream() 119 .sorted(NamespaceDescriptor.NAMESPACE_DESCRIPTOR_COMPARATOR) 120 .collect(ImmutableList.toImmutableList()); 121 } 122}