OpenTSDBソース分析のTSDB-UIDテーブル操作

6039 ワード

[root@centos build]# ./tsdb mkmetric t1 t2
java -enableassertions -enablesystemassertions -classpath.:/usr/hadoop/jdk1.6.0_26/lib:/usr/hadoop/jdk1.6.0_26/jre/lib::/usr/hadoop/opentsdb-2.0.0/build/../third_party/hbase/asynchbase-1.4.1.jar:/usr/hadoop/opentsdb-2.0.0/build/../third_party/guava/guava-13.0.1.jar:/usr/hadoop/opentsdb-2.0.0/build/../third_party/slf4j/log4j-over-slf4j-1.7.2.jar:/usr/hadoop/opentsdb-2.0.0/build/../third_party/logback/logback-classic-1.0.9.jar:/usr/hadoop/opentsdb-2.0.0/build/../third_party/logback/logback-core-1.0.9.jar:/usr/hadoop/opentsdb-2.0.0/build/../third_party/jackson/jackson-annotations-2.1.4.jar:/usr/hadoop/opentsdb-2.0.0/build/../third_party/jackson/jackson-core-2.1.4.jar:/usr/hadoop/opentsdb-2.0.0/build/../third_party/jackson/jackson-databind-2.1.4.jar:/usr/hadoop/opentsdb-2.0.0/build/../third_party/netty/netty-3.6.2.Final.jar:/usr/hadoop/opentsdb-2.0.0/build/../third_party/slf4j/slf4j-api-1.7.2.jar:/usr/hadoop/opentsdb-2.0.0/build/../third_party/suasync/suasync-1.4.0.jar:/usr/hadoop/opentsdb-2.0.0/build/../third_party/zookeeper/zookeeper-3.3.6.jar:/usr/hadoop/opentsdb-2.0.0/build/tsdb-2.0.0.jar:/usr/hadoop/opentsdb-2.0.0/build/../srcnet.opentsdb.tools.UidManagerassign metrics t1 t2
metrics t1: [0, 0, 3]
metrics t2: [0, 0, 4]
[root@centos build]#
tsdbファイルおよびnetを分析する.opentsdb.tools.UidManagerのrunCommandメソッド:
case $1 in
  (fsck)
    MAINCLASS=Fsck
    ;;
  (import)
    MAINCLASS=TextImporter
    ;;
  (mkmetric)
    shift
    set uid assign metrics "$@"
    MAINCLASS=UidManager
    ;;
  (query)
    MAINCLASS=CliQuery
    ;;
  (tsd)
    MAINCLASS=TSDMain
    ;;
  (scan)
    MAINCLASS=DumpSeries
    ;;
  (uid)
    MAINCLASS=UidManager
    ;;
  (*)
    echo >&2 "$me: error: unknown command '$1'"
    usage
    ;;
esac
shift


private static int runCommand(final TSDB tsdb, final byte[] table, final short idwidth, 
                 final boolean ignorecase, final String[] args) {
		final int nargs = args.length;
		
		/**
		 *  
		 * grep t
		 * grep metrics t11
		 */
		if (args[0].equals("grep")) {
			if (2 <= nargs && nargs <= 3) {
				try {
					return grep(tsdb.getClient(), table, ignorecase, args);
				} catch (HBaseException e) {
					return 3;
				}
			} else {
				usage("Wrong number of arguments");
				return 2;
			}
			
			
			/**
			 *  
			 * assign metrics t1
			 * assign metrics t1 t2 t3
			 */
		} else if (args[0].equals("assign")) {
			if (nargs < 3) {
				usage("Wrong number of arguments");
				return 2;
			}
			return assign(tsdb.getClient(), table, idwidth, args);
			
			
			/**
			 *  
			 * rename metrics t1 t11
			 */
		} else if (args[0].equals("rename")) {
			if (nargs != 4) {
				usage("Wrong number of arguments");
				return 2;
			}
			return rename(tsdb.getClient(), table, idwidth, args);
			
			/**
			 *  
			 */
		} else if (args[0].equals("fsck")) {
			return fsck(tsdb.getClient(), table);
			
			
			
		} else if (args[0].equals("metasync")) {
			// check for the data table existence and initialize our plugins
			// so that update meta data can be pushed to search engines
			try {
				tsdb.getClient().ensureTableExists(tsdb.getConfig().getString("tsd.storage.hbase.data_table"))
                                            .joinUninterruptibly();
				tsdb.initializePlugins(false);
				return metaSync(tsdb);
			} catch (Exception e) {
				LOG.error("Unexpected exception", e);
				return 3;
			}
		} else if (args[0].equals("metapurge")) {
			// check for the data table existence and initialize our plugins
			// so that update meta data can be pushed to search engines
			try {
				tsdb.getClient().ensureTableExists(tsdb.getConfig().getString("tsd.storage.hbase.uid_table"))
                                                  .joinUninterruptibly();
				return metaPurge(tsdb);
			} catch (Exception e) {
				LOG.error("Unexpected exception", e);
				return 3;
			}
		} else if (args[0].equals("treesync")) {
			// check for the UID table existence
			try {
				tsdb.getClient().ensureTableExists(tsdb.getConfig().getString("tsd.storage.hbase.uid_table"))
                                               .joinUninterruptibly();
				if (!tsdb.getConfig().enable_tree_processing()) {
					LOG.warn("Tree processing is disabled");
					return 0;
				}
				return treeSync(tsdb);
			} catch (Exception e) {
				LOG.error("Unexpected exception", e);
				return 3;
			}
		} else if (args[0].equals("treepurge")) {
			if (nargs < 2) {
				usage("Wrong number of arguments");
				return 2;
			}
			try {
				tsdb.getClient().ensureTableExists(tsdb.getConfig().getString("tsd.storage.hbase.uid_table"))
                                           .joinUninterruptibly();
				final int tree_id = Integer.parseInt(args[1]);
				final boolean delete_definitions;
				if (nargs < 3) {
					delete_definitions = false;
				} else {
					final String delete_all = args[2];
					if (delete_all.toLowerCase().equals("true")) {
						delete_definitions = true;
					} else {
						delete_definitions = false;
					}
				}
				return purgeTree(tsdb, tree_id, delete_definitions);
			} catch (Exception e) {
				LOG.error("Unexpected exception", e);
				return 3;
			}
		} else {
			if (1 <= nargs && nargs <= 2) {
				final String kind = nargs == 2 ? args[0] : null;
				try {
					final long id = Long.parseLong(args[nargs - 1]);
					return lookupId(tsdb.getClient(), table, idwidth, id, kind);
				} catch (NumberFormatException e) {
					return lookupName(tsdb.getClient(), table, idwidth, args[nargs - 1], kind);
				}
			} else {
				usage("Wrong number of arguments");
				return 2;
			}
		}
	}

shellコマンドラインでは、次のような操作も実行できます.
./tsdb uid grep metrics tクエリー
./tsdb uid rename metrics t 1 t 11改名
./tsdb uid fsckテーブルデータチェック