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