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 ioe) {
063      notifyFailed(ioe);
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)
088      throws IOException {
089    return submitProcedure(new CreateNamespaceProcedure(
090      this.masterServices.getMasterProcedureExecutor().getEnvironment(), namespaceDescriptor, latch),
091        nonceKey);
092  }
093
094  @Override
095  public long modifyNamespace(NamespaceDescriptor namespaceDescriptor, final NonceKey nonceKey,
096      final ProcedurePrepareLatch latch) throws IOException {
097    return submitProcedure(new ModifyNamespaceProcedure(
098      this.masterServices.getMasterProcedureExecutor().getEnvironment(), namespaceDescriptor, latch),
099        nonceKey);
100  }
101
102  @Override
103  public long deleteNamespace(String name, final NonceKey nonceKey, final ProcedurePrepareLatch latch)
104      throws IOException {
105    return submitProcedure(new DeleteNamespaceProcedure(
106      this.masterServices.getMasterProcedureExecutor().getEnvironment(), name, latch),
107      nonceKey);
108  }
109
110  @Override
111  public NamespaceDescriptor getNamespace(String name) throws IOException {
112    NamespaceDescriptor nsd = getTableNamespaceManager().get(name);
113    if (nsd == null) throw new NamespaceNotFoundException(name);
114    return nsd;
115  }
116
117  @Override
118  public List<NamespaceDescriptor> getNamespaces() throws IOException {
119    checkIsRunning();
120    return getTableNamespaceManager().list().stream()
121      .sorted(NamespaceDescriptor.NAMESPACE_DESCRIPTOR_COMPARATOR)
122      .collect(ImmutableList.toImmutableList());
123  }
124}