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