org.cloudata.core.client.CTableManager.java Source code

Java tutorial

Introduction

Here is the source code for org.cloudata.core.client.CTableManager.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cloudata.core.client;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.zookeeper.ZooKeeper;
import org.cloudata.core.common.Constants;
import org.cloudata.core.common.CloudataLock;
import org.cloudata.core.common.conf.CloudataConf;
import org.cloudata.core.common.ipc.CRPC;
import org.cloudata.core.common.ipc.CRemoteException;
import org.cloudata.core.common.lock.LockUtil;
import org.cloudata.core.common.util.NetworkUtil;
import org.cloudata.core.master.CloudataMaster;
import org.cloudata.core.master.TableManagerProtocol;
import org.cloudata.core.tablet.TableSchema;
import org.cloudata.core.tablet.TabletInfo;
import org.cloudata.core.tabletserver.DataServiceProtocol;
import org.cloudata.core.tabletserver.TabletManagerProtocol;

/**
 * ?? ? ? , drop  ? DDL(data definit ion language) ? ? .
 * @author babokim
 *
 */
public class CTableManager implements Constants {
    private static final Log LOG = LogFactory.getLog(CTableManager.class.getName());

    //tabletServerHostName -> DataServiceProtocol
    static Map<String, DataServiceProtocol> tabletServers = new HashMap<String, DataServiceProtocol>();
    static final CloudataLock lock = new CloudataLock();
    static String hostName;
    //public static LockEventManager LOCK_EVENT_MANAGER = new LockEventManager();
    static ZooKeeper zk;

    protected static TabletManagerProtocol connectTabletManager(String tabletServerHostName, CloudataConf conf)
            throws IOException {
        try {
            return (TabletManagerProtocol) CRPC.getProxy(TabletManagerProtocol.class,
                    TabletManagerProtocol.versionID, NetworkUtil.getAddress(tabletServerHostName), conf);
        } catch (Exception e) {
            throw new IOException(e.getMessage());
        }
    }

    public static DataServiceProtocol connectTabletServer(TabletInfo tabletInfo, CloudataConf conf)
            throws IOException {
        if (tabletInfo == null) {
            LOG.warn("Cannot connect to tablet server since tabletInfo is null.");
            return null;
        }
        return connectTabletServer(tabletInfo.getAssignedHostName(), conf);
    }

    public static DataServiceProtocol connectTabletServer(String hostName, CloudataConf conf) throws IOException {
        if (hostName == null) {
            return null;
        }
        DataServiceProtocol tabletServer = (DataServiceProtocol) CRPC.getProxy(DataServiceProtocol.class,
                DataServiceProtocol.versionID, NetworkUtil.getAddress(hostName), conf);

        return tabletServer;
    }

    public static IOException makeIOException(Exception e) {
        if (e instanceof CRemoteException) {
            CRemoteException re = (CRemoteException) e;
            IOException resultException = null;
            try {
                if (re.getClassName() == null) {
                    throw re;
                }
                Constructor cn = IOException.class.getConstructor(String.class);
                cn.setAccessible(true);
                IOException ex = (IOException) cn.newInstance(e.getMessage());
                ex.initCause(e);
                return ex;
                //resultException = (IOException)Class.forName(re.getClassName()).newInstance();
                //resultException.initCause(e);
                //return resultException;
            } catch (Exception e1) {
                e1.printStackTrace();
                LOG.error(e1.getMessage());
                //        return null;
                return re;
            }
        } else if (e instanceof IOException) {
            return (IOException) e;
        } else {
            IOException err = new IOException(e.getMessage());
            err.initCause(e);
            return err;
        }
    }

    /**
     * CloudataMaster? .
     * @param conf
     * @return
     * @throws IOException
     */
    public static TableManagerProtocol getMasterServer(CloudataConf conf) throws IOException {
        ZooKeeper zk = getZooKeeper(conf);
        TableManagerProtocol masterServer = (TableManagerProtocol) CRPC.getProxy(TableManagerProtocol.class,
                TableManagerProtocol.versionID,
                NetworkUtil.getAddress(CloudataMaster.getMasterServerHostName(conf, zk)), conf);
        return masterServer;
    }

    public static synchronized ZooKeeper getZooKeeper(CloudataConf conf) throws IOException {
        if (hostName == null) {
            hostName = InetAddress.getLocalHost().getHostName();
        }
        if (zk == null) {
            zk = LockUtil.getZooKeeper(conf, "CloudataClient_" + hostName + "_" + System.currentTimeMillis(), null);
        }
        return zk;
    }

    /**
     * ?? ?
     * @param conf
     * @param table
     * @throws IOException
     */
    protected static void createTable(CloudataConf conf, TableSchema table) throws IOException {
        createTable(conf, table, null);
    }

    /**
     * ?? ?. ? ?  ? Row.Key  ? tablet? .
     * @param conf
     * @param table
     * @param endRowKeys
     * @throws IOException
     */
    protected static void createTable(CloudataConf conf, TableSchema table, Row.Key[] endRowKeys)
            throws IOException {
        checkReservedTable(table);
        try {
            TableManagerProtocol masterServer = getMasterServer(conf);
            masterServer.createTable(table, endRowKeys);
        } catch (IOException e) {
            //      IOException err = RemoteExceptionHandler.decodeRemoteException(e);
            //      if(!(err instanceof SocketTimeoutException)) {
            //        throw err;
            //      }

            throw e;
        }
    }

    /**
     * ??  ?.
     * @param conf
     * @param tableName
     * @return  true,   false
     * @throws IOException
     */
    protected static boolean existsTable(CloudataConf conf, String tableName) throws IOException {
        TableSchema tableSchema = TableSchema.loadTableSchema(conf, getZooKeeper(conf), tableName);
        return tableSchema != null;
    }

    /**
     * ROOT, META ?  ? ?  ?? ?.
     * @param table
     * @throws IllegalArgumentException
     */
    private static void checkReservedTable(TableSchema table) throws IllegalArgumentException {
        if (TABLE_NAME_ROOT.equals(table.getTableName()) || TABLE_NAME_META.equals(table.getTableName())) {
            throw new IllegalArgumentException("Table [" + table.getTableName() + "] is reserved table");
        }
    }

    /**
     *  ? ?? .
     * @param conf
     * @return TableInfo[]
     * @throws IOException
     */
    protected static TableSchema[] listTables(CloudataConf conf) throws IOException {
        TableSchema[] tables = getMasterServer(conf).listTables();
        if (tables == null || tables.length == 0) {
            return tables;
        }

        List<TableSchema> result = new ArrayList<TableSchema>();
        for (TableSchema eachTable : tables) {
            result.add(eachTable);
        }
        Collections.sort(result);
        return result.toArray(new TableSchema[result.size()]);
    }

    /**
     * ?? drop.
     * @param conf
     * @param tableName
     * @throws IOException
     */
    public static void dropTable(CloudataConf conf, String tableName) throws IOException {
        getMasterServer(conf).dropTable(tableName);
    }

    public static void clear() {
        zk = null;
        tabletServers.clear();
    }
}