http://git-wip-us.apache.org/repos/asf/hbase-site/blob/8bb348c6/devapidocs/src-html/org/apache/hadoop/hbase/tool/Canary.RegionServerTask.html ---------------------------------------------------------------------- diff --git a/devapidocs/src-html/org/apache/hadoop/hbase/tool/Canary.RegionServerTask.html b/devapidocs/src-html/org/apache/hadoop/hbase/tool/Canary.RegionServerTask.html index d3abe39..c51826f 100644 --- a/devapidocs/src-html/org/apache/hadoop/hbase/tool/Canary.RegionServerTask.html +++ b/devapidocs/src-html/org/apache/hadoop/hbase/tool/Canary.RegionServerTask.html @@ -615,9 +615,9 @@ <span class="sourceLineNo">607</span> if (this.failOnError && monitor.hasError()) {<a name="line.607"></a> <span class="sourceLineNo">608</span> monitorThread.interrupt();<a name="line.608"></a> <span class="sourceLineNo">609</span> if (monitor.initialized) {<a name="line.609"></a> -<span class="sourceLineNo">610</span> System.exit(monitor.errorCode);<a name="line.610"></a> +<span class="sourceLineNo">610</span> return monitor.errorCode;<a name="line.610"></a> <span class="sourceLineNo">611</span> } else {<a name="line.611"></a> -<span class="sourceLineNo">612</span> System.exit(INIT_ERROR_EXIT_CODE);<a name="line.612"></a> +<span class="sourceLineNo">612</span> return INIT_ERROR_EXIT_CODE;<a name="line.612"></a> <span class="sourceLineNo">613</span> }<a name="line.613"></a> <span class="sourceLineNo">614</span> }<a name="line.614"></a> <span class="sourceLineNo">615</span> currentTimeLength = System.currentTimeMillis() - startTime;<a name="line.615"></a> @@ -626,630 +626,629 @@ <span class="sourceLineNo">618</span> + ") after timeout limit:" + this.timeout<a name="line.618"></a> <span class="sourceLineNo">619</span> + " will be killed itself !!");<a name="line.619"></a> <span class="sourceLineNo">620</span> if (monitor.initialized) {<a name="line.620"></a> -<span class="sourceLineNo">621</span> System.exit(TIMEOUT_ERROR_EXIT_CODE);<a name="line.621"></a> +<span class="sourceLineNo">621</span> return TIMEOUT_ERROR_EXIT_CODE;<a name="line.621"></a> <span class="sourceLineNo">622</span> } else {<a name="line.622"></a> -<span class="sourceLineNo">623</span> System.exit(INIT_ERROR_EXIT_CODE);<a name="line.623"></a> +<span class="sourceLineNo">623</span> return INIT_ERROR_EXIT_CODE;<a name="line.623"></a> <span class="sourceLineNo">624</span> }<a name="line.624"></a> -<span class="sourceLineNo">625</span> break;<a name="line.625"></a> -<span class="sourceLineNo">626</span> }<a name="line.626"></a> -<span class="sourceLineNo">627</span> }<a name="line.627"></a> -<span class="sourceLineNo">628</span><a name="line.628"></a> -<span class="sourceLineNo">629</span> if (this.failOnError && monitor.finalCheckForErrors()) {<a name="line.629"></a> -<span class="sourceLineNo">630</span> monitorThread.interrupt();<a name="line.630"></a> -<span class="sourceLineNo">631</span> System.exit(monitor.errorCode);<a name="line.631"></a> -<span class="sourceLineNo">632</span> }<a name="line.632"></a> -<span class="sourceLineNo">633</span> } finally {<a name="line.633"></a> -<span class="sourceLineNo">634</span> if (monitor != null) monitor.close();<a name="line.634"></a> -<span class="sourceLineNo">635</span> }<a name="line.635"></a> -<span class="sourceLineNo">636</span><a name="line.636"></a> -<span class="sourceLineNo">637</span> Thread.sleep(interval);<a name="line.637"></a> -<span class="sourceLineNo">638</span> } while (interval > 0);<a name="line.638"></a> -<span class="sourceLineNo">639</span> } // try-with-resources close<a name="line.639"></a> -<span class="sourceLineNo">640</span><a name="line.640"></a> -<span class="sourceLineNo">641</span> if (choreService != null) {<a name="line.641"></a> -<span class="sourceLineNo">642</span> choreService.shutdown();<a name="line.642"></a> -<span class="sourceLineNo">643</span> }<a name="line.643"></a> -<span class="sourceLineNo">644</span> return(monitor.errorCode);<a name="line.644"></a> -<span class="sourceLineNo">645</span> }<a name="line.645"></a> -<span class="sourceLineNo">646</span><a name="line.646"></a> -<span class="sourceLineNo">647</span> private void printUsageAndExit() {<a name="line.647"></a> -<span class="sourceLineNo">648</span> System.err.printf(<a name="line.648"></a> -<span class="sourceLineNo">649</span> "Usage: bin/hbase %s [opts] [table1 [table2]...] | [regionserver1 [regionserver2]..]%n",<a name="line.649"></a> -<span class="sourceLineNo">650</span> getClass().getName());<a name="line.650"></a> -<span class="sourceLineNo">651</span> System.err.println(" where [opts] are:");<a name="line.651"></a> -<span class="sourceLineNo">652</span> System.err.println(" -help Show this help and exit.");<a name="line.652"></a> -<span class="sourceLineNo">653</span> System.err.println(" -regionserver replace the table argument to regionserver,");<a name="line.653"></a> -<span class="sourceLineNo">654</span> System.err.println(" which means to enable regionserver mode");<a name="line.654"></a> -<span class="sourceLineNo">655</span> System.err.println(" -allRegions Tries all regions on a regionserver,");<a name="line.655"></a> -<span class="sourceLineNo">656</span> System.err.println(" only works in regionserver mode.");<a name="line.656"></a> -<span class="sourceLineNo">657</span> System.err.println(" -daemon Continuous check at defined intervals.");<a name="line.657"></a> -<span class="sourceLineNo">658</span> System.err.println(" -interval <N> Interval between checks (sec)");<a name="line.658"></a> -<span class="sourceLineNo">659</span> System.err.println(" -e Use table/regionserver as regular expression");<a name="line.659"></a> -<span class="sourceLineNo">660</span> System.err.println(" which means the table/regionserver is regular expression pattern");<a name="line.660"></a> -<span class="sourceLineNo">661</span> System.err.println(" -f <B> stop whole program if first error occurs," +<a name="line.661"></a> -<span class="sourceLineNo">662</span> " default is true");<a name="line.662"></a> -<span class="sourceLineNo">663</span> System.err.println(" -t <N> timeout for a check, default is 600000 (milisecs)");<a name="line.663"></a> -<span class="sourceLineNo">664</span> System.err.println(" -writeSniffing enable the write sniffing in canary");<a name="line.664"></a> -<span class="sourceLineNo">665</span> System.err.println(" -treatFailureAsError treats read / write failure as error");<a name="line.665"></a> -<span class="sourceLineNo">666</span> System.err.println(" -writeTable The table used for write sniffing."<a name="line.666"></a> -<span class="sourceLineNo">667</span> + " Default is hbase:canary");<a name="line.667"></a> -<span class="sourceLineNo">668</span> System.err<a name="line.668"></a> -<span class="sourceLineNo">669</span> .println(" -D<configProperty>=<value> assigning or override the configuration params");<a name="line.669"></a> -<span class="sourceLineNo">670</span> System.exit(USAGE_EXIT_CODE);<a name="line.670"></a> -<span class="sourceLineNo">671</span> }<a name="line.671"></a> -<span class="sourceLineNo">672</span><a name="line.672"></a> -<span class="sourceLineNo">673</span> /**<a name="line.673"></a> -<span class="sourceLineNo">674</span> * A Factory method for {@link Monitor}.<a name="line.674"></a> -<span class="sourceLineNo">675</span> * Can be overridden by user.<a name="line.675"></a> -<span class="sourceLineNo">676</span> * @param index a start index for monitor target<a name="line.676"></a> -<span class="sourceLineNo">677</span> * @param args args passed from user<a name="line.677"></a> -<span class="sourceLineNo">678</span> * @return a Monitor instance<a name="line.678"></a> -<span class="sourceLineNo">679</span> */<a name="line.679"></a> -<span class="sourceLineNo">680</span> public Monitor newMonitor(final Connection connection, int index, String[] args) {<a name="line.680"></a> -<span class="sourceLineNo">681</span> Monitor monitor = null;<a name="line.681"></a> -<span class="sourceLineNo">682</span> String[] monitorTargets = null;<a name="line.682"></a> -<span class="sourceLineNo">683</span><a name="line.683"></a> -<span class="sourceLineNo">684</span> if(index >= 0) {<a name="line.684"></a> -<span class="sourceLineNo">685</span> int length = args.length - index;<a name="line.685"></a> -<span class="sourceLineNo">686</span> monitorTargets = new String[length];<a name="line.686"></a> -<span class="sourceLineNo">687</span> System.arraycopy(args, index, monitorTargets, 0, length);<a name="line.687"></a> -<span class="sourceLineNo">688</span> }<a name="line.688"></a> -<span class="sourceLineNo">689</span><a name="line.689"></a> -<span class="sourceLineNo">690</span> if (this.regionServerMode) {<a name="line.690"></a> -<span class="sourceLineNo">691</span> monitor =<a name="line.691"></a> -<span class="sourceLineNo">692</span> new RegionServerMonitor(connection, monitorTargets, this.useRegExp,<a name="line.692"></a> -<span class="sourceLineNo">693</span> (ExtendedSink) this.sink, this.executor, this.regionServerAllRegions,<a name="line.693"></a> -<span class="sourceLineNo">694</span> this.treatFailureAsError);<a name="line.694"></a> -<span class="sourceLineNo">695</span> } else {<a name="line.695"></a> -<span class="sourceLineNo">696</span> monitor =<a name="line.696"></a> -<span class="sourceLineNo">697</span> new RegionMonitor(connection, monitorTargets, this.useRegExp, this.sink, this.executor,<a name="line.697"></a> -<span class="sourceLineNo">698</span> this.writeSniffing, this.writeTableName, this.treatFailureAsError);<a name="line.698"></a> -<span class="sourceLineNo">699</span> }<a name="line.699"></a> -<span class="sourceLineNo">700</span> return monitor;<a name="line.700"></a> -<span class="sourceLineNo">701</span> }<a name="line.701"></a> -<span class="sourceLineNo">702</span><a name="line.702"></a> -<span class="sourceLineNo">703</span> // a Monitor super-class can be extended by users<a name="line.703"></a> -<span class="sourceLineNo">704</span> public static abstract class Monitor implements Runnable, Closeable {<a name="line.704"></a> -<span class="sourceLineNo">705</span><a name="line.705"></a> -<span class="sourceLineNo">706</span> protected Connection connection;<a name="line.706"></a> -<span class="sourceLineNo">707</span> protected Admin admin;<a name="line.707"></a> -<span class="sourceLineNo">708</span> protected String[] targets;<a name="line.708"></a> -<span class="sourceLineNo">709</span> protected boolean useRegExp;<a name="line.709"></a> -<span class="sourceLineNo">710</span> protected boolean treatFailureAsError;<a name="line.710"></a> -<span class="sourceLineNo">711</span> protected boolean initialized = false;<a name="line.711"></a> -<span class="sourceLineNo">712</span><a name="line.712"></a> -<span class="sourceLineNo">713</span> protected boolean done = false;<a name="line.713"></a> -<span class="sourceLineNo">714</span> protected int errorCode = 0;<a name="line.714"></a> -<span class="sourceLineNo">715</span> protected Sink sink;<a name="line.715"></a> -<span class="sourceLineNo">716</span> protected ExecutorService executor;<a name="line.716"></a> -<span class="sourceLineNo">717</span><a name="line.717"></a> -<span class="sourceLineNo">718</span> public boolean isDone() {<a name="line.718"></a> -<span class="sourceLineNo">719</span> return done;<a name="line.719"></a> -<span class="sourceLineNo">720</span> }<a name="line.720"></a> -<span class="sourceLineNo">721</span><a name="line.721"></a> -<span class="sourceLineNo">722</span> public boolean hasError() {<a name="line.722"></a> -<span class="sourceLineNo">723</span> return errorCode != 0;<a name="line.723"></a> -<span class="sourceLineNo">724</span> }<a name="line.724"></a> -<span class="sourceLineNo">725</span><a name="line.725"></a> -<span class="sourceLineNo">726</span> public boolean finalCheckForErrors() {<a name="line.726"></a> -<span class="sourceLineNo">727</span> if (errorCode != 0) {<a name="line.727"></a> -<span class="sourceLineNo">728</span> return true;<a name="line.728"></a> -<span class="sourceLineNo">729</span> }<a name="line.729"></a> -<span class="sourceLineNo">730</span> return treatFailureAsError &&<a name="line.730"></a> -<span class="sourceLineNo">731</span> (sink.getReadFailureCount() > 0 || sink.getWriteFailureCount() > 0);<a name="line.731"></a> -<span class="sourceLineNo">732</span> }<a name="line.732"></a> -<span class="sourceLineNo">733</span><a name="line.733"></a> -<span class="sourceLineNo">734</span> @Override<a name="line.734"></a> -<span class="sourceLineNo">735</span> public void close() throws IOException {<a name="line.735"></a> -<span class="sourceLineNo">736</span> if (this.admin != null) this.admin.close();<a name="line.736"></a> -<span class="sourceLineNo">737</span> }<a name="line.737"></a> -<span class="sourceLineNo">738</span><a name="line.738"></a> -<span class="sourceLineNo">739</span> protected Monitor(Connection connection, String[] monitorTargets, boolean useRegExp, Sink sink,<a name="line.739"></a> -<span class="sourceLineNo">740</span> ExecutorService executor, boolean treatFailureAsError) {<a name="line.740"></a> -<span class="sourceLineNo">741</span> if (null == connection) throw new IllegalArgumentException("connection shall not be null");<a name="line.741"></a> -<span class="sourceLineNo">742</span><a name="line.742"></a> -<span class="sourceLineNo">743</span> this.connection = connection;<a name="line.743"></a> -<span class="sourceLineNo">744</span> this.targets = monitorTargets;<a name="line.744"></a> -<span class="sourceLineNo">745</span> this.useRegExp = useRegExp;<a name="line.745"></a> -<span class="sourceLineNo">746</span> this.treatFailureAsError = treatFailureAsError;<a name="line.746"></a> -<span class="sourceLineNo">747</span> this.sink = sink;<a name="line.747"></a> -<span class="sourceLineNo">748</span> this.executor = executor;<a name="line.748"></a> -<span class="sourceLineNo">749</span> }<a name="line.749"></a> -<span class="sourceLineNo">750</span><a name="line.750"></a> -<span class="sourceLineNo">751</span> @Override<a name="line.751"></a> -<span class="sourceLineNo">752</span> public abstract void run();<a name="line.752"></a> -<span class="sourceLineNo">753</span><a name="line.753"></a> -<span class="sourceLineNo">754</span> protected boolean initAdmin() {<a name="line.754"></a> -<span class="sourceLineNo">755</span> if (null == this.admin) {<a name="line.755"></a> -<span class="sourceLineNo">756</span> try {<a name="line.756"></a> -<span class="sourceLineNo">757</span> this.admin = this.connection.getAdmin();<a name="line.757"></a> -<span class="sourceLineNo">758</span> } catch (Exception e) {<a name="line.758"></a> -<span class="sourceLineNo">759</span> LOG.error("Initial HBaseAdmin failed...", e);<a name="line.759"></a> -<span class="sourceLineNo">760</span> this.errorCode = INIT_ERROR_EXIT_CODE;<a name="line.760"></a> -<span class="sourceLineNo">761</span> }<a name="line.761"></a> -<span class="sourceLineNo">762</span> } else if (admin.isAborted()) {<a name="line.762"></a> -<span class="sourceLineNo">763</span> LOG.error("HBaseAdmin aborted");<a name="line.763"></a> -<span class="sourceLineNo">764</span> this.errorCode = INIT_ERROR_EXIT_CODE;<a name="line.764"></a> -<span class="sourceLineNo">765</span> }<a name="line.765"></a> -<span class="sourceLineNo">766</span> return !this.hasError();<a name="line.766"></a> -<span class="sourceLineNo">767</span> }<a name="line.767"></a> -<span class="sourceLineNo">768</span> }<a name="line.768"></a> -<span class="sourceLineNo">769</span><a name="line.769"></a> -<span class="sourceLineNo">770</span> // a monitor for region mode<a name="line.770"></a> -<span class="sourceLineNo">771</span> private static class RegionMonitor extends Monitor {<a name="line.771"></a> -<span class="sourceLineNo">772</span> // 10 minutes<a name="line.772"></a> -<span class="sourceLineNo">773</span> private static final int DEFAULT_WRITE_TABLE_CHECK_PERIOD = 10 * 60 * 1000;<a name="line.773"></a> -<span class="sourceLineNo">774</span> // 1 days<a name="line.774"></a> -<span class="sourceLineNo">775</span> private static final int DEFAULT_WRITE_DATA_TTL = 24 * 60 * 60;<a name="line.775"></a> -<span class="sourceLineNo">776</span><a name="line.776"></a> -<span class="sourceLineNo">777</span> private long lastCheckTime = -1;<a name="line.777"></a> -<span class="sourceLineNo">778</span> private boolean writeSniffing;<a name="line.778"></a> -<span class="sourceLineNo">779</span> private TableName writeTableName;<a name="line.779"></a> -<span class="sourceLineNo">780</span> private int writeDataTTL;<a name="line.780"></a> -<span class="sourceLineNo">781</span> private float regionsLowerLimit;<a name="line.781"></a> -<span class="sourceLineNo">782</span> private float regionsUpperLimit;<a name="line.782"></a> -<span class="sourceLineNo">783</span> private int checkPeriod;<a name="line.783"></a> -<span class="sourceLineNo">784</span><a name="line.784"></a> -<span class="sourceLineNo">785</span> public RegionMonitor(Connection connection, String[] monitorTargets, boolean useRegExp,<a name="line.785"></a> -<span class="sourceLineNo">786</span> Sink sink, ExecutorService executor, boolean writeSniffing, TableName writeTableName,<a name="line.786"></a> -<span class="sourceLineNo">787</span> boolean treatFailureAsError) {<a name="line.787"></a> -<span class="sourceLineNo">788</span> super(connection, monitorTargets, useRegExp, sink, executor, treatFailureAsError);<a name="line.788"></a> -<span class="sourceLineNo">789</span> Configuration conf = connection.getConfiguration();<a name="line.789"></a> -<span class="sourceLineNo">790</span> this.writeSniffing = writeSniffing;<a name="line.790"></a> -<span class="sourceLineNo">791</span> this.writeTableName = writeTableName;<a name="line.791"></a> -<span class="sourceLineNo">792</span> this.writeDataTTL =<a name="line.792"></a> -<span class="sourceLineNo">793</span> conf.getInt(HConstants.HBASE_CANARY_WRITE_DATA_TTL_KEY, DEFAULT_WRITE_DATA_TTL);<a name="line.793"></a> -<span class="sourceLineNo">794</span> this.regionsLowerLimit =<a name="line.794"></a> -<span class="sourceLineNo">795</span> conf.getFloat(HConstants.HBASE_CANARY_WRITE_PERSERVER_REGIONS_LOWERLIMIT_KEY, 1.0f);<a name="line.795"></a> -<span class="sourceLineNo">796</span> this.regionsUpperLimit =<a name="line.796"></a> -<span class="sourceLineNo">797</span> conf.getFloat(HConstants.HBASE_CANARY_WRITE_PERSERVER_REGIONS_UPPERLIMIT_KEY, 1.5f);<a name="line.797"></a> -<span class="sourceLineNo">798</span> this.checkPeriod =<a name="line.798"></a> -<span class="sourceLineNo">799</span> conf.getInt(HConstants.HBASE_CANARY_WRITE_TABLE_CHECK_PERIOD_KEY,<a name="line.799"></a> -<span class="sourceLineNo">800</span> DEFAULT_WRITE_TABLE_CHECK_PERIOD);<a name="line.800"></a> -<span class="sourceLineNo">801</span> }<a name="line.801"></a> -<span class="sourceLineNo">802</span><a name="line.802"></a> -<span class="sourceLineNo">803</span> @Override<a name="line.803"></a> -<span class="sourceLineNo">804</span> public void run() {<a name="line.804"></a> -<span class="sourceLineNo">805</span> if (this.initAdmin()) {<a name="line.805"></a> -<span class="sourceLineNo">806</span> try {<a name="line.806"></a> -<span class="sourceLineNo">807</span> List<Future<Void>> taskFutures = new LinkedList<Future<Void>>();<a name="line.807"></a> -<span class="sourceLineNo">808</span> if (this.targets != null && this.targets.length > 0) {<a name="line.808"></a> -<span class="sourceLineNo">809</span> String[] tables = generateMonitorTables(this.targets);<a name="line.809"></a> -<span class="sourceLineNo">810</span> this.initialized = true;<a name="line.810"></a> -<span class="sourceLineNo">811</span> for (String table : tables) {<a name="line.811"></a> -<span class="sourceLineNo">812</span> taskFutures.addAll(Canary.sniff(admin, sink, table, executor, TaskType.READ));<a name="line.812"></a> -<span class="sourceLineNo">813</span> }<a name="line.813"></a> -<span class="sourceLineNo">814</span> } else {<a name="line.814"></a> -<span class="sourceLineNo">815</span> taskFutures.addAll(sniff(TaskType.READ));<a name="line.815"></a> -<span class="sourceLineNo">816</span> }<a name="line.816"></a> -<span class="sourceLineNo">817</span><a name="line.817"></a> -<span class="sourceLineNo">818</span> if (writeSniffing) {<a name="line.818"></a> -<span class="sourceLineNo">819</span> if (EnvironmentEdgeManager.currentTime() - lastCheckTime > checkPeriod) {<a name="line.819"></a> -<span class="sourceLineNo">820</span> try {<a name="line.820"></a> -<span class="sourceLineNo">821</span> checkWriteTableDistribution();<a name="line.821"></a> -<span class="sourceLineNo">822</span> } catch (IOException e) {<a name="line.822"></a> -<span class="sourceLineNo">823</span> LOG.error("Check canary table distribution failed!", e);<a name="line.823"></a> -<span class="sourceLineNo">824</span> }<a name="line.824"></a> -<span class="sourceLineNo">825</span> lastCheckTime = EnvironmentEdgeManager.currentTime();<a name="line.825"></a> -<span class="sourceLineNo">826</span> }<a name="line.826"></a> -<span class="sourceLineNo">827</span> // sniff canary table with write operation<a name="line.827"></a> -<span class="sourceLineNo">828</span> taskFutures.addAll(Canary.sniff(admin, sink,<a name="line.828"></a> -<span class="sourceLineNo">829</span> admin.getTableDescriptor(writeTableName), executor, TaskType.WRITE));<a name="line.829"></a> -<span class="sourceLineNo">830</span> }<a name="line.830"></a> -<span class="sourceLineNo">831</span><a name="line.831"></a> -<span class="sourceLineNo">832</span> for (Future<Void> future : taskFutures) {<a name="line.832"></a> -<span class="sourceLineNo">833</span> try {<a name="line.833"></a> -<span class="sourceLineNo">834</span> future.get();<a name="line.834"></a> -<span class="sourceLineNo">835</span> } catch (ExecutionException e) {<a name="line.835"></a> -<span class="sourceLineNo">836</span> LOG.error("Sniff region failed!", e);<a name="line.836"></a> -<span class="sourceLineNo">837</span> }<a name="line.837"></a> -<span class="sourceLineNo">838</span> }<a name="line.838"></a> -<span class="sourceLineNo">839</span> } catch (Exception e) {<a name="line.839"></a> -<span class="sourceLineNo">840</span> LOG.error("Run regionMonitor failed", e);<a name="line.840"></a> -<span class="sourceLineNo">841</span> this.errorCode = ERROR_EXIT_CODE;<a name="line.841"></a> -<span class="sourceLineNo">842</span> }<a name="line.842"></a> -<span class="sourceLineNo">843</span> }<a name="line.843"></a> -<span class="sourceLineNo">844</span> this.done = true;<a name="line.844"></a> -<span class="sourceLineNo">845</span> }<a name="line.845"></a> -<span class="sourceLineNo">846</span><a name="line.846"></a> -<span class="sourceLineNo">847</span> private String[] generateMonitorTables(String[] monitorTargets) throws IOException {<a name="line.847"></a> -<span class="sourceLineNo">848</span> String[] returnTables = null;<a name="line.848"></a> -<span class="sourceLineNo">849</span><a name="line.849"></a> -<span class="sourceLineNo">850</span> if (this.useRegExp) {<a name="line.850"></a> -<span class="sourceLineNo">851</span> Pattern pattern = null;<a name="line.851"></a> -<span class="sourceLineNo">852</span> HTableDescriptor[] tds = null;<a name="line.852"></a> -<span class="sourceLineNo">853</span> Set<String> tmpTables = new TreeSet<String>();<a name="line.853"></a> -<span class="sourceLineNo">854</span> try {<a name="line.854"></a> -<span class="sourceLineNo">855</span> if (LOG.isDebugEnabled()) {<a name="line.855"></a> -<span class="sourceLineNo">856</span> LOG.debug(String.format("reading list of tables"));<a name="line.856"></a> -<span class="sourceLineNo">857</span> }<a name="line.857"></a> -<span class="sourceLineNo">858</span> tds = this.admin.listTables(pattern);<a name="line.858"></a> -<span class="sourceLineNo">859</span> if (tds == null) {<a name="line.859"></a> -<span class="sourceLineNo">860</span> tds = new HTableDescriptor[0];<a name="line.860"></a> -<span class="sourceLineNo">861</span> }<a name="line.861"></a> -<span class="sourceLineNo">862</span> for (String monitorTarget : monitorTargets) {<a name="line.862"></a> -<span class="sourceLineNo">863</span> pattern = Pattern.compile(monitorTarget);<a name="line.863"></a> -<span class="sourceLineNo">864</span> for (HTableDescriptor td : tds) {<a name="line.864"></a> -<span class="sourceLineNo">865</span> if (pattern.matcher(td.getNameAsString()).matches()) {<a name="line.865"></a> -<span class="sourceLineNo">866</span> tmpTables.add(td.getNameAsString());<a name="line.866"></a> -<span class="sourceLineNo">867</span> }<a name="line.867"></a> -<span class="sourceLineNo">868</span> }<a name="line.868"></a> -<span class="sourceLineNo">869</span> }<a name="line.869"></a> -<span class="sourceLineNo">870</span> } catch (IOException e) {<a name="line.870"></a> -<span class="sourceLineNo">871</span> LOG.error("Communicate with admin failed", e);<a name="line.871"></a> -<span class="sourceLineNo">872</span> throw e;<a name="line.872"></a> -<span class="sourceLineNo">873</span> }<a name="line.873"></a> -<span class="sourceLineNo">874</span><a name="line.874"></a> -<span class="sourceLineNo">875</span> if (tmpTables.size() > 0) {<a name="line.875"></a> -<span class="sourceLineNo">876</span> returnTables = tmpTables.toArray(new String[tmpTables.size()]);<a name="line.876"></a> -<span class="sourceLineNo">877</span> } else {<a name="line.877"></a> -<span class="sourceLineNo">878</span> String msg = "No HTable found, tablePattern:" + Arrays.toString(monitorTargets);<a name="line.878"></a> -<span class="sourceLineNo">879</span> LOG.error(msg);<a name="line.879"></a> -<span class="sourceLineNo">880</span> this.errorCode = INIT_ERROR_EXIT_CODE;<a name="line.880"></a> -<span class="sourceLineNo">881</span> throw new TableNotFoundException(msg);<a name="line.881"></a> -<span class="sourceLineNo">882</span> }<a name="line.882"></a> -<span class="sourceLineNo">883</span> } else {<a name="line.883"></a> -<span class="sourceLineNo">884</span> returnTables = monitorTargets;<a name="line.884"></a> -<span class="sourceLineNo">885</span> }<a name="line.885"></a> -<span class="sourceLineNo">886</span><a name="line.886"></a> -<span class="sourceLineNo">887</span> return returnTables;<a name="line.887"></a> -<span class="sourceLineNo">888</span> }<a name="line.888"></a> -<span class="sourceLineNo">889</span><a name="line.889"></a> -<span class="sourceLineNo">890</span> /*<a name="line.890"></a> -<span class="sourceLineNo">891</span> * canary entry point to monitor all the tables.<a name="line.891"></a> -<span class="sourceLineNo">892</span> */<a name="line.892"></a> -<span class="sourceLineNo">893</span> private List<Future<Void>> sniff(TaskType taskType) throws Exception {<a name="line.893"></a> -<span class="sourceLineNo">894</span> if (LOG.isDebugEnabled()) {<a name="line.894"></a> -<span class="sourceLineNo">895</span> LOG.debug(String.format("reading list of tables"));<a name="line.895"></a> -<span class="sourceLineNo">896</span> }<a name="line.896"></a> -<span class="sourceLineNo">897</span> List<Future<Void>> taskFutures = new LinkedList<Future<Void>>();<a name="line.897"></a> -<span class="sourceLineNo">898</span> for (HTableDescriptor table : admin.listTables()) {<a name="line.898"></a> -<span class="sourceLineNo">899</span> if (admin.isTableEnabled(table.getTableName())<a name="line.899"></a> -<span class="sourceLineNo">900</span> && (!table.getTableName().equals(writeTableName))) {<a name="line.900"></a> -<span class="sourceLineNo">901</span> taskFutures.addAll(Canary.sniff(admin, sink, table, executor, taskType));<a name="line.901"></a> -<span class="sourceLineNo">902</span> }<a name="line.902"></a> -<span class="sourceLineNo">903</span> }<a name="line.903"></a> -<span class="sourceLineNo">904</span> return taskFutures;<a name="line.904"></a> -<span class="sourceLineNo">905</span> }<a name="line.905"></a> -<span class="sourceLineNo">906</span><a name="line.906"></a> -<span class="sourceLineNo">907</span> private void checkWriteTableDistribution() throws IOException {<a name="line.907"></a> -<span class="sourceLineNo">908</span> if (!admin.tableExists(writeTableName)) {<a name="line.908"></a> -<span class="sourceLineNo">909</span> int numberOfServers = admin.getClusterStatus().getServers().size();<a name="line.909"></a> -<span class="sourceLineNo">910</span> if (numberOfServers == 0) {<a name="line.910"></a> -<span class="sourceLineNo">911</span> throw new IllegalStateException("No live regionservers");<a name="line.911"></a> -<span class="sourceLineNo">912</span> }<a name="line.912"></a> -<span class="sourceLineNo">913</span> createWriteTable(numberOfServers);<a name="line.913"></a> -<span class="sourceLineNo">914</span> }<a name="line.914"></a> -<span class="sourceLineNo">915</span><a name="line.915"></a> -<span class="sourceLineNo">916</span> if (!admin.isTableEnabled(writeTableName)) {<a name="line.916"></a> -<span class="sourceLineNo">917</span> admin.enableTable(writeTableName);<a name="line.917"></a> -<span class="sourceLineNo">918</span> }<a name="line.918"></a> -<span class="sourceLineNo">919</span><a name="line.919"></a> -<span class="sourceLineNo">920</span> int numberOfServers = admin.getClusterStatus().getServers().size();<a name="line.920"></a> -<span class="sourceLineNo">921</span> List<Pair<HRegionInfo, ServerName>> pairs =<a name="line.921"></a> -<span class="sourceLineNo">922</span> MetaTableAccessor.getTableRegionsAndLocations(connection, writeTableName);<a name="line.922"></a> -<span class="sourceLineNo">923</span> int numberOfRegions = pairs.size();<a name="line.923"></a> -<span class="sourceLineNo">924</span> if (numberOfRegions < numberOfServers * regionsLowerLimit<a name="line.924"></a> -<span class="sourceLineNo">925</span> || numberOfRegions > numberOfServers * regionsUpperLimit) {<a name="line.925"></a> -<span class="sourceLineNo">926</span> admin.disableTable(writeTableName);<a name="line.926"></a> -<span class="sourceLineNo">927</span> admin.deleteTable(writeTableName);<a name="line.927"></a> -<span class="sourceLineNo">928</span> createWriteTable(numberOfServers);<a name="line.928"></a> -<span class="sourceLineNo">929</span> }<a name="line.929"></a> -<span class="sourceLineNo">930</span> HashSet<ServerName> serverSet = new HashSet<ServerName>();<a name="line.930"></a> -<span class="sourceLineNo">931</span> for (Pair<HRegionInfo, ServerName> pair : pairs) {<a name="line.931"></a> -<span class="sourceLineNo">932</span> serverSet.add(pair.getSecond());<a name="line.932"></a> -<span class="sourceLineNo">933</span> }<a name="line.933"></a> -<span class="sourceLineNo">934</span> int numberOfCoveredServers = serverSet.size();<a name="line.934"></a> -<span class="sourceLineNo">935</span> if (numberOfCoveredServers < numberOfServers) {<a name="line.935"></a> -<span class="sourceLineNo">936</span> admin.balancer();<a name="line.936"></a> -<span class="sourceLineNo">937</span> }<a name="line.937"></a> -<span class="sourceLineNo">938</span> }<a name="line.938"></a> -<span class="sourceLineNo">939</span><a name="line.939"></a> -<span class="sourceLineNo">940</span> private void createWriteTable(int numberOfServers) throws IOException {<a name="line.940"></a> -<span class="sourceLineNo">941</span> int numberOfRegions = (int)(numberOfServers * regionsLowerLimit);<a name="line.941"></a> -<span class="sourceLineNo">942</span> LOG.info("Number of live regionservers: " + numberOfServers + ", "<a name="line.942"></a> -<span class="sourceLineNo">943</span> + "pre-splitting the canary table into " + numberOfRegions + " regions "<a name="line.943"></a> -<span class="sourceLineNo">944</span> + "(current lower limit of regions per server is " + regionsLowerLimit<a name="line.944"></a> -<span class="sourceLineNo">945</span> + " and you can change it by config: "<a name="line.945"></a> -<span class="sourceLineNo">946</span> + HConstants.HBASE_CANARY_WRITE_PERSERVER_REGIONS_LOWERLIMIT_KEY + " )");<a name="line.946"></a> -<span class="sourceLineNo">947</span> HTableDescriptor desc = new HTableDescriptor(writeTableName);<a name="line.947"></a> -<span class="sourceLineNo">948</span> HColumnDescriptor family = new HColumnDescriptor(CANARY_TABLE_FAMILY_NAME);<a name="line.948"></a> -<span class="sourceLineNo">949</span> family.setMaxVersions(1);<a name="line.949"></a> -<span class="sourceLineNo">950</span> family.setTimeToLive(writeDataTTL);<a name="line.950"></a> -<span class="sourceLineNo">951</span><a name="line.951"></a> -<span class="sourceLineNo">952</span> desc.addFamily(family);<a name="line.952"></a> -<span class="sourceLineNo">953</span> byte[][] splits = new RegionSplitter.HexStringSplit().split(numberOfRegions);<a name="line.953"></a> -<span class="sourceLineNo">954</span> admin.createTable(desc, splits);<a name="line.954"></a> -<span class="sourceLineNo">955</span> }<a name="line.955"></a> -<span class="sourceLineNo">956</span> }<a name="line.956"></a> -<span class="sourceLineNo">957</span><a name="line.957"></a> -<span class="sourceLineNo">958</span> /**<a name="line.958"></a> -<span class="sourceLineNo">959</span> * Canary entry point for specified table.<a name="line.959"></a> -<span class="sourceLineNo">960</span> * @throws Exception<a name="line.960"></a> -<span class="sourceLineNo">961</span> */<a name="line.961"></a> -<span class="sourceLineNo">962</span> public static void sniff(final Admin admin, TableName tableName)<a name="line.962"></a> -<span class="sourceLineNo">963</span> throws Exception {<a name="line.963"></a> -<span class="sourceLineNo">964</span> sniff(admin, tableName, TaskType.READ);<a name="line.964"></a> -<span class="sourceLineNo">965</span> }<a name="line.965"></a> -<span class="sourceLineNo">966</span><a name="line.966"></a> -<span class="sourceLineNo">967</span> /**<a name="line.967"></a> -<span class="sourceLineNo">968</span> * Canary entry point for specified table with task type(read/write)<a name="line.968"></a> -<span class="sourceLineNo">969</span> * @throws Exception<a name="line.969"></a> -<span class="sourceLineNo">970</span> */<a name="line.970"></a> -<span class="sourceLineNo">971</span> public static void sniff(final Admin admin, TableName tableName, TaskType taskType)<a name="line.971"></a> -<span class="sourceLineNo">972</span> throws Exception {<a name="line.972"></a> -<span class="sourceLineNo">973</span> List<Future<Void>> taskFutures =<a name="line.973"></a> -<span class="sourceLineNo">974</span> Canary.sniff(admin, new StdOutSink(), tableName.getNameAsString(),<a name="line.974"></a> -<span class="sourceLineNo">975</span> new ScheduledThreadPoolExecutor(1), taskType);<a name="line.975"></a> -<span class="sourceLineNo">976</span> for (Future<Void> future : taskFutures) {<a name="line.976"></a> -<span class="sourceLineNo">977</span> future.get();<a name="line.977"></a> -<span class="sourceLineNo">978</span> }<a name="line.978"></a> -<span class="sourceLineNo">979</span> }<a name="line.979"></a> -<span class="sourceLineNo">980</span><a name="line.980"></a> -<span class="sourceLineNo">981</span> /**<a name="line.981"></a> -<span class="sourceLineNo">982</span> * Canary entry point for specified table.<a name="line.982"></a> -<span class="sourceLineNo">983</span> * @throws Exception<a name="line.983"></a> -<span class="sourceLineNo">984</span> */<a name="line.984"></a> -<span class="sourceLineNo">985</span> private static List<Future<Void>> sniff(final Admin admin, final Sink sink, String tableName,<a name="line.985"></a> -<span class="sourceLineNo">986</span> ExecutorService executor, TaskType taskType) throws Exception {<a name="line.986"></a> -<span class="sourceLineNo">987</span> if (LOG.isDebugEnabled()) {<a name="line.987"></a> -<span class="sourceLineNo">988</span> LOG.debug(String.format("checking table is enabled and getting table descriptor for table %s",<a name="line.988"></a> -<span class="sourceLineNo">989</span> tableName));<a name="line.989"></a> -<span class="sourceLineNo">990</span> }<a name="line.990"></a> -<span class="sourceLineNo">991</span> if (admin.isTableEnabled(TableName.valueOf(tableName))) {<a name="line.991"></a> -<span class="sourceLineNo">992</span> return Canary.sniff(admin, sink, admin.getTableDescriptor(TableName.valueOf(tableName)),<a name="line.992"></a> -<span class="sourceLineNo">993</span> executor, taskType);<a name="line.993"></a> -<span class="sourceLineNo">994</span> } else {<a name="line.994"></a> -<span class="sourceLineNo">995</span> LOG.warn(String.format("Table %s is not enabled", tableName));<a name="line.995"></a> -<span class="sourceLineNo">996</span> }<a name="line.996"></a> -<span class="sourceLineNo">997</span> return new LinkedList<Future<Void>>();<a name="line.997"></a> -<span class="sourceLineNo">998</span> }<a name="line.998"></a> -<span class="sourceLineNo">999</span><a name="line.999"></a> -<span class="sourceLineNo">1000</span> /*<a name="line.1000"></a> -<span class="sourceLineNo">1001</span> * Loops over regions that owns this table, and output some information abouts the state.<a name="line.1001"></a> -<span class="sourceLineNo">1002</span> */<a name="line.1002"></a> -<span class="sourceLineNo">1003</span> private static List<Future<Void>> sniff(final Admin admin, final Sink sink,<a name="line.1003"></a> -<span class="sourceLineNo">1004</span> HTableDescriptor tableDesc, ExecutorService executor, TaskType taskType) throws Exception {<a name="line.1004"></a> -<span class="sourceLineNo">1005</span><a name="line.1005"></a> -<span class="sourceLineNo">1006</span> if (LOG.isDebugEnabled()) {<a name="line.1006"></a> -<span class="sourceLineNo">1007</span> LOG.debug(String.format("reading list of regions for table %s", tableDesc.getTableName()));<a name="line.1007"></a> -<span class="sourceLineNo">1008</span> }<a name="line.1008"></a> -<span class="sourceLineNo">1009</span><a name="line.1009"></a> -<span class="sourceLineNo">1010</span> Table table = null;<a name="line.1010"></a> -<span class="sourceLineNo">1011</span> try {<a name="line.1011"></a> -<span class="sourceLineNo">1012</span> table = admin.getConnection().getTable(tableDesc.getTableName());<a name="line.1012"></a> -<span class="sourceLineNo">1013</span> } catch (TableNotFoundException e) {<a name="line.1013"></a> -<span class="sourceLineNo">1014</span> return new ArrayList<Future<Void>>();<a name="line.1014"></a> -<span class="sourceLineNo">1015</span> }<a name="line.1015"></a> -<span class="sourceLineNo">1016</span> List<RegionTask> tasks = new ArrayList<RegionTask>();<a name="line.1016"></a> -<span class="sourceLineNo">1017</span> try {<a name="line.1017"></a> -<span class="sourceLineNo">1018</span> for (HRegionInfo region : admin.getTableRegions(tableDesc.getTableName())) {<a name="line.1018"></a> -<span class="sourceLineNo">1019</span> tasks.add(new RegionTask(admin.getConnection(), region, sink, taskType));<a name="line.1019"></a> -<span class="sourceLineNo">1020</span> }<a name="line.1020"></a> -<span class="sourceLineNo">1021</span> } finally {<a name="line.1021"></a> -<span class="sourceLineNo">1022</span> table.close();<a name="line.1022"></a> -<span class="sourceLineNo">1023</span> }<a name="line.1023"></a> -<span class="sourceLineNo">1024</span> return executor.invokeAll(tasks);<a name="line.1024"></a> -<span class="sourceLineNo">1025</span> }<a name="line.1025"></a> -<span class="sourceLineNo">1026</span> // a monitor for regionserver mode<a name="line.1026"></a> -<span class="sourceLineNo">1027</span> private static class RegionServerMonitor extends Monitor {<a name="line.1027"></a> -<span class="sourceLineNo">1028</span><a name="line.1028"></a> -<span class="sourceLineNo">1029</span> private boolean allRegions;<a name="line.1029"></a> -<span class="sourceLineNo">1030</span><a name="line.1030"></a> -<span class="sourceLineNo">1031</span> public RegionServerMonitor(Connection connection, String[] monitorTargets, boolean useRegExp,<a name="line.1031"></a> -<span class="sourceLineNo">1032</span> ExtendedSink sink, ExecutorService executor, boolean allRegions,<a name="line.1032"></a> -<span class="sourceLineNo">1033</span> boolean treatFailureAsError) {<a name="line.1033"></a> -<span class="sourceLineNo">1034</span> super(connection, monitorTargets, useRegExp, sink, executor, treatFailureAsError);<a name="line.1034"></a> -<span class="sourceLineNo">1035</span> this.allRegions = allRegions;<a name="line.1035"></a> -<span class="sourceLineNo">1036</span> }<a name="line.1036"></a> -<span class="sourceLineNo">1037</span><a name="line.1037"></a> -<span class="sourceLineNo">1038</span> private ExtendedSink getSink() {<a name="line.1038"></a> -<span class="sourceLineNo">1039</span> return (ExtendedSink) this.sink;<a name="line.1039"></a> -<span class="sourceLineNo">1040</span> }<a name="line.1040"></a> -<span class="sourceLineNo">1041</span><a name="line.1041"></a> -<span class="sourceLineNo">1042</span> @Override<a name="line.1042"></a> -<span class="sourceLineNo">1043</span> public void run() {<a name="line.1043"></a> -<span class="sourceLineNo">1044</span> if (this.initAdmin() && this.checkNoTableNames()) {<a name="line.1044"></a> -<span class="sourceLineNo">1045</span> Map<String, List<HRegionInfo>> rsAndRMap = this.filterRegionServerByName();<a name="line.1045"></a> -<span class="sourceLineNo">1046</span> this.initialized = true;<a name="line.1046"></a> -<span class="sourceLineNo">1047</span> this.monitorRegionServers(rsAndRMap);<a name="line.1047"></a> -<span class="sourceLineNo">1048</span> }<a name="line.1048"></a> -<span class="sourceLineNo">1049</span> this.done = true;<a name="line.1049"></a> -<span class="sourceLineNo">1050</span> }<a name="line.1050"></a> -<span class="sourceLineNo">1051</span><a name="line.1051"></a> -<span class="sourceLineNo">1052</span> private boolean checkNoTableNames() {<a name="line.1052"></a> -<span class="sourceLineNo">1053</span> List<String> foundTableNames = new ArrayList<String>();<a name="line.1053"></a> -<span class="sourceLineNo">1054</span> TableName[] tableNames = null;<a name="line.1054"></a> -<span class="sourceLineNo">1055</span><a name="line.1055"></a> -<span class="sourceLineNo">1056</span> if (LOG.isDebugEnabled()) {<a name="line.1056"></a> -<span class="sourceLineNo">1057</span> LOG.debug(String.format("reading list of tables"));<a name="line.1057"></a> -<span class="sourceLineNo">1058</span> }<a name="line.1058"></a> -<span class="sourceLineNo">1059</span> try {<a name="line.1059"></a> -<span class="sourceLineNo">1060</span> tableNames = this.admin.listTableNames();<a name="line.1060"></a> -<span class="sourceLineNo">1061</span> } catch (IOException e) {<a name="line.1061"></a> -<span class="sourceLineNo">1062</span> LOG.error("Get listTableNames failed", e);<a name="line.1062"></a> -<span class="sourceLineNo">1063</span> this.errorCode = INIT_ERROR_EXIT_CODE;<a name="line.1063"></a> -<span class="sourceLineNo">1064</span> return false;<a name="line.1064"></a> -<span class="sourceLineNo">1065</span> }<a name="line.1065"></a> -<span class="sourceLineNo">1066</span><a name="line.1066"></a> -<span class="sourceLineNo">1067</span> if (this.targets == null || this.targets.length == 0) return true;<a name="line.1067"></a> -<span class="sourceLineNo">1068</span><a name="line.1068"></a> -<span class="sourceLineNo">1069</span> for (String target : this.targets) {<a name="line.1069"></a> -<span class="sourceLineNo">1070</span> for (TableName tableName : tableNames) {<a name="line.1070"></a> -<span class="sourceLineNo">1071</span> if (target.equals(tableName.getNameAsString())) {<a name="line.1071"></a> -<span class="sourceLineNo">1072</span> foundTableNames.add(target);<a name="line.1072"></a> -<span class="sourceLineNo">1073</span> }<a name="line.1073"></a> -<span class="sourceLineNo">1074</span> }<a name="line.1074"></a> -<span class="sourceLineNo">1075</span> }<a name="line.1075"></a> -<span class="sourceLineNo">1076</span><a name="line.1076"></a> -<span class="sourceLineNo">1077</span> if (foundTableNames.size() > 0) {<a name="line.1077"></a> -<span class="sourceLineNo">1078</span> System.err.println("Cannot pass a tablename when using the -regionserver " +<a name="line.1078"></a> -<span class="sourceLineNo">1079</span> "option, tablenames:" + foundTableNames.toString());<a name="line.1079"></a> -<span class="sourceLineNo">1080</span> this.errorCode = USAGE_EXIT_CODE;<a name="line.1080"></a> -<span class="sourceLineNo">1081</span> }<a name="line.1081"></a> -<span class="sourceLineNo">1082</span> return foundTableNames.size() == 0;<a name="line.1082"></a> -<span class="sourceLineNo">1083</span> }<a name="line.1083"></a> -<span class="sourceLineNo">1084</span><a name="line.1084"></a> -<span class="sourceLineNo">1085</span> private void monitorRegionServers(Map<String, List<HRegionInfo>> rsAndRMap) {<a name="line.1085"></a> -<span class="sourceLineNo">1086</span> List<RegionServerTask> tasks = new ArrayList<RegionServerTask>();<a name="line.1086"></a> -<span class="sourceLineNo">1087</span> Map<String, AtomicLong> successMap = new HashMap<String, AtomicLong>();<a name="line.1087"></a> -<span class="sourceLineNo">1088</span> Random rand = new Random();<a name="line.1088"></a> -<span class="sourceLineNo">1089</span> for (Map.Entry<String, List<HRegionInfo>> entry : rsAndRMap.entrySet()) {<a name="line.1089"></a> -<span class="sourceLineNo">1090</span> String serverName = entry.getKey();<a name="line.1090"></a> -<span class="sourceLineNo">1091</span> AtomicLong successes = new AtomicLong(0);<a name="line.1091"></a> -<span class="sourceLineNo">1092</span> successMap.put(serverName, successes);<a name="line.1092"></a> -<span class="sourceLineNo">1093</span> if (this.allRegions) {<a name="line.1093"></a> -<span class="sourceLineNo">1094</span> for (HRegionInfo region : entry.getValue()) {<a name="line.1094"></a> -<span class="sourceLineNo">1095</span> tasks.add(new RegionServerTask(this.connection,<a name="line.1095"></a> -<span class="sourceLineNo">1096</span> serverName,<a name="line.1096"></a> -<span class="sourceLineNo">1097</span> region,<a name="line.1097"></a> -<span class="sourceLineNo">1098</span> getSink(),<a name="line.1098"></a> -<span class="sourceLineNo">1099</span> successes));<a name="line.1099"></a> -<span class="sourceLineNo">1100</span> }<a name="line.1100"></a> -<span class="sourceLineNo">1101</span> } else {<a name="line.1101"></a> -<span class="sourceLineNo">1102</span> // random select a region if flag not set<a name="line.1102"></a> -<span class="sourceLineNo">1103</span> HRegionInfo region = entry.getValue().get(rand.nextInt(entry.getValue().size()));<a name="line.1103"></a> -<span class="sourceLineNo">1104</span> tasks.add(new RegionServerTask(this.connection,<a name="line.1104"></a> -<span class="sourceLineNo">1105</span> serverName,<a name="line.1105"></a> -<span class="sourceLineNo">1106</span> region,<a name="line.1106"></a> -<span class="sourceLineNo">1107</span> getSink(),<a name="line.1107"></a> -<span class="sourceLineNo">1108</span> successes));<a name="line.1108"></a> -<span class="sourceLineNo">1109</span> }<a name="line.1109"></a> -<span class="sourceLineNo">1110</span> }<a name="line.1110"></a> -<span class="sourceLineNo">1111</span> try {<a name="line.1111"></a> -<span class="sourceLineNo">1112</span> for (Future<Void> future : this.executor.invokeAll(tasks)) {<a name="line.1112"></a> -<span class="sourceLineNo">1113</span> try {<a name="line.1113"></a> -<span class="sourceLineNo">1114</span> future.get();<a name="line.1114"></a> -<span class="sourceLineNo">1115</span> } catch (ExecutionException e) {<a name="line.1115"></a> -<span class="sourceLineNo">1116</span> LOG.error("Sniff regionserver failed!", e);<a name="line.1116"></a> -<span class="sourceLineNo">1117</span> this.errorCode = ERROR_EXIT_CODE;<a name="line.1117"></a> -<span class="sourceLineNo">1118</span> }<a name="line.1118"></a> -<span class="sourceLineNo">1119</span> }<a name="line.1119"></a> -<span class="sourceLineNo">1120</span> if (this.allRegions) {<a name="line.1120"></a> -<span class="sourceLineNo">1121</span> for (Map.Entry<String, List<HRegionInfo>> entry : rsAndRMap.entrySet()) {<a name="line.1121"></a> -<span class="sourceLineNo">1122</span> String serverName = entry.getKey();<a name="line.1122"></a> -<span class="sourceLineNo">1123</span> LOG.info("Successfully read " + successMap.get(serverName) + " regions out of "<a name="line.1123"></a> -<span class="sourceLineNo">1124</span> + entry.getValue().size() + " on regionserver:" + serverName);<a name="line.1124"></a> -<span class="sourceLineNo">1125</span> }<a name="line.1125"></a> -<span class="sourceLineNo">1126</span> }<a name="line.1126"></a> -<span class="sourceLineNo">1127</span> } catch (InterruptedException e) {<a name="line.1127"></a> -<span class="sourceLineNo">1128</span> this.errorCode = ERROR_EXIT_CODE;<a name="line.1128"></a> -<span class="sourceLineNo">1129</span> LOG.error("Sniff regionserver interrupted!", e);<a name="line.1129"></a> -<span class="sourceLineNo">1130</span> }<a name="line.1130"></a> -<span class="sourceLineNo">1131</span> }<a name="line.1131"></a> -<span class="sourceLineNo">1132</span><a name="line.1132"></a> -<span class="sourceLineNo">1133</span> private Map<String, List<HRegionInfo>> filterRegionServerByName() {<a name="line.1133"></a> -<span class="sourceLineNo">1134</span> Map<String, List<HRegionInfo>> regionServerAndRegionsMap = this.getAllRegionServerByName();<a name="line.1134"></a> -<span class="sourceLineNo">1135</span> regionServerAndRegionsMap = this.doFilterRegionServerByName(regionServerAndRegionsMap);<a name="line.1135"></a> -<span class="sourceLineNo">1136</span> return regionServerAndRegionsMap;<a name="line.1136"></a> -<span class="sourceLineNo">1137</span> }<a name="line.1137"></a> -<span class="sourceLineNo">1138</span><a name="line.1138"></a> -<span class="sourceLineNo">1139</span> private Map<String, List<HRegionInfo>> getAllRegionServerByName() {<a name="line.1139"></a> -<span class="sourceLineNo">1140</span> Map<String, List<HRegionInfo>> rsAndRMap = new HashMap<String, List<HRegionInfo>>();<a name="line.1140"></a> -<span class="sourceLineNo">1141</span> Table table = null;<a name="line.1141"></a> -<span class="sourceLineNo">1142</span> RegionLocator regionLocator = null;<a name="line.1142"></a> -<span class="sourceLineNo">1143</span> try {<a name="line.1143"></a> -<span class="sourceLineNo">1144</span> if (LOG.isDebugEnabled()) {<a name="line.1144"></a> -<span class="sourceLineNo">1145</span> LOG.debug(String.format("reading list of tables and locations"));<a name="line.1145"></a> -<span class="sourceLineNo">1146</span> }<a name="line.1146"></a> -<span class="sourceLineNo">1147</span> HTableDescriptor[] tableDescs = this.admin.listTables();<a name="line.1147"></a> -<span class="sourceLineNo">1148</span> List<HRegionInfo> regions = null;<a name="line.1148"></a> -<span class="sourceLineNo">1149</span> for (HTableDescriptor tableDesc : tableDescs) {<a name="line.1149"></a> -<span class="sourceLineNo">1150</span> table = this.admin.getConnection().getTable(tableDesc.getTableName());<a name="line.1150"></a> -<span class="sourceLineNo">1151</span> regionLocator = this.admin.getConnection().getRegionLocator(tableDesc.getTableName());<a name="line.1151"></a> -<span class="sourceLineNo">1152</span><a name="line.1152"></a> -<span class="sourceLineNo">1153</span> for (HRegionLocation location : regionLocator.getAllRegionLocations()) {<a name="line.1153"></a> -<span class="sourceLineNo">1154</span> ServerName rs = location.getServerName();<a name="line.1154"></a> -<span class="sourceLineNo">1155</span> String rsName = rs.getHostname();<a name="line.1155"></a> -<span class="sourceLineNo">1156</span> HRegionInfo r = location.getRegionInfo();<a name="line.1156"></a> -<span class="sourceLineNo">1157</span><a name="line.1157"></a> -<span class="sourceLineNo">1158</span> if (rsAndRMap.containsKey(rsName)) {<a name="line.1158"></a> -<span class="sourceLineNo">1159</span> regions = rsAndRMap.get(rsName);<a name="line.1159"></a> -<span class="sourceLineNo">1160</span> } else {<a name="line.1160"></a> -<span class="sourceLineNo">1161</span> regions = new ArrayList<HRegionInfo>();<a name="line.1161"></a> -<span class="sourceLineNo">1162</span> rsAndRMap.put(rsName, regions);<a name="line.1162"></a> -<span class="sourceLineNo">1163</span> }<a name="line.1163"></a> -<span class="sourceLineNo">1164</span> regions.add(r);<a name="line.1164"></a> -<span class="sourceLineNo">1165</span> }<a name="line.1165"></a> -<span class="sourceLineNo">1166</span> table.close();<a name="line.1166"></a> -<span class="sourceLineNo">1167</span> }<a name="line.1167"></a> -<span class="sourceLineNo">1168</span><a name="line.1168"></a> -<span class="sourceLineNo">1169</span> } catch (IOException e) {<a name="line.1169"></a> -<span class="sourceLineNo">1170</span> String msg = "Get HTables info failed";<a name="line.1170"></a> -<span class="sourceLineNo">1171</span> LOG.error(msg, e);<a name="line.1171"></a> -<span class="sourceLineNo">1172</span> this.errorCode = INIT_ERROR_EXIT_CODE;<a name="line.1172"></a> -<span class="sourceLineNo">1173</span> } finally {<a name="line.1173"></a> -<span class="sourceLineNo">1174</span> if (table != null) {<a name="line.1174"></a> -<span class="sourceLineNo">1175</span> try {<a name="line.1175"></a> -<span class="sourceLineNo">1176</span> table.close();<a name="line.1176"></a> -<span class="sourceLineNo">1177</span> } catch (IOException e) {<a name="line.1177"></a> -<span class="sourceLineNo">1178</span> LOG.warn("Close table failed", e);<a name="line.1178"></a> -<span class="sourceLineNo">1179</span> }<a name="line.1179"></a> -<span class="sourceLineNo">1180</span> }<a name="line.1180"></a> -<span class="sourceLineNo">1181</span> }<a name="line.1181"></a> -<span class="sourceLineNo">1182</span><a name="line.1182"></a> -<span class="sourceLineNo">1183</span> return rsAndRMap;<a name="line.1183"></a> -<span class="sourceLineNo">1184</span> }<a name="line.1184"></a> -<span class="sourceLineNo">1185</span><a name="line.1185"></a> -<span class="sourceLineNo">1186</span> private Map<String, List<HRegionInfo>> doFilterRegionServerByName(<a name="line.1186"></a> -<span class="sourceLineNo">1187</span> Map<String, List<HRegionInfo>> fullRsAndRMap) {<a name="line.1187"></a> -<span class="sourceLineNo">1188</span><a name="line.1188"></a> -<span class="sourceLineNo">1189</span> Map<String, List<HRegionInfo>> filteredRsAndRMap = null;<a name="line.1189"></a> -<span class="sourceLineNo">1190</span><a name="line.1190"></a> -<span class="sourceLineNo">1191</span> if (this.targets != null && this.targets.length > 0) {<a name="line.1191"></a> -<span class="sourceLineNo">1192</span> filteredRsAndRMap = new HashMap<String, List<HRegionInfo>>();<a name="line.1192"></a> -<span class="sourceLineNo">1193</span> Pattern pattern = null;<a name="line.1193"></a> -<span class="sourceLineNo">1194</span> Matcher matcher = null;<a name="line.1194"></a> -<span class="sourceLineNo">1195</span> boolean regExpFound = false;<a name="line.1195"></a> -<span class="sourceLineNo">1196</span> for (String rsName : this.targets) {<a name="line.1196"></a> -<span class="sourceLineNo">1197</span> if (this.useRegExp) {<a name="line.1197"></a> -<span class="sourceLineNo">1198</span> regExpFound = false;<a name="line.1198"></a> -<span class="sourceLineNo">1199</span> pattern = Pattern.compile(rsName);<a name="line.1199"></a> -<span class="sourceLineNo">1200</span> for (Map.Entry<String, List<HRegionInfo>> entry : fullRsAndRMap.entrySet()) {<a name="line.1200"></a> -<span class="sourceLineNo">1201</span> matcher = pattern.matcher(entry.getKey());<a name="line.1201"></a> -<span class="sourceLineNo">1202</span> if (matcher.matches()) {<a name="line.1202"></a> -<span class="sourceLineNo">1203</span> filteredRsAndRMap.put(entry.getKey(), entry.getValue());<a name="line.1203"></a> -<span class="sourceLineNo">1204</span> regExpFound = true;<a name="line.1204"></a> -<span class="sourceLineNo">1205</span> }<a name="line.1205"></a> -<span class="sourceLineNo">1206</span> }<a name="line.1206"></a> -<span class="sourceLineNo">1207</span> if (!regExpFound) {<a name="line.1207"></a> -<span class="sourceLineNo">1208</span> LOG.info("No RegionServerInfo found, regionServerPattern:" + rsName);<a name="line.1208"></a> -<span class="sourceLineNo">1209</span> }<a name="line.1209"></a> -<span class="sourceLineNo">1210</span> } else {<a name="line.1210"></a> -<span class="sourceLineNo">1211</span> if (fullRsAndRMap.containsKey(rsName)) {<a name="line.1211"></a> -<span class="sourceLineNo">1212</span> filteredRsAndRMap.put(rsName, fullRsAndRMap.get(rsName));<a name="line.1212"></a> -<span class="sourceLineNo">1213</span> } else {<a name="line.1213"></a> -<span class="sourceLineNo">1214</span> LOG.info("No RegionServerInfo found, regionServerName:" + rsName);<a name="line.1214"></a> -<span class="sourceLineNo">1215</span> }<a name="line.1215"></a> -<span class="sourceLineNo">1216</span> }<a name="line.1216"></a> -<span class="sourceLineNo">1217</span> }<a name="line.1217"></a> -<span class="sourceLineNo">1218</span> } else {<a name="line.1218"></a> -<span class="sourceLineNo">1219</span> filteredRsAndRMap = fullRsAndRMap;<a name="line.1219"></a> -<span class="sourceLineNo">1220</span> }<a name="line.1220"></a> -<span class="sourceLineNo">1221</span> return filteredRsAndRMap;<a name="line.1221"></a> -<span class="sourceLineNo">1222</span> }<a name="line.1222"></a> -<span class="sourceLineNo">1223</span> }<a name="line.1223"></a> -<span class="sourceLineNo">1224</span><a name="line.1224"></a> -<span class="sourceLineNo">1225</span> public static void main(String[] args) throws Exception {<a name="line.1225"></a> -<span class="sourceLineNo">1226</span> final Configuration conf = HBaseConfiguration.create();<a name="line.1226"></a> -<span class="sourceLineNo">1227</span><a name="line.1227"></a> -<span class="sourceLineNo">1228</span> // loading the generic options to conf<a name="line.1228"></a> -<span class="sourceLineNo">1229</span> new GenericOptionsParser(conf, args);<a name="line.1229"></a> -<span class="sourceLineNo">1230</span><a name="line.1230"></a> -<span class="sourceLineNo">1231</span> int numThreads = conf.getInt("hbase.canary.threads.num", MAX_THREADS_NUM);<a name="line.1231"></a> -<span class="sourceLineNo">1232</span> LOG.info("Number of exection threads " + numThreads);<a name="line.1232"></a> -<span class="sourceLineNo">1233</span><a name="line.1233"></a> -<span class="sourceLineNo">1234</span> ExecutorService executor = new ScheduledThreadPoolExecutor(numThreads);<a name="line.1234"></a> -<span class="sourceLineNo">1235</span><a name="line.1235"></a> -<span class="sourceLineNo">1236</span> Class<? extends Sink> sinkClass =<a name="line.1236"></a> -<span class="sourceLineNo">1237</span> conf.getClass("hbase.canary.sink.class", RegionServerStdOutSink.class, Sink.class);<a name="line.1237"></a> -<span class="sourceLineNo">1238</span> Sink sink = ReflectionUtils.newInstance(sinkClass);<a name="line.1238"></a> -<span class="sourceLineNo">1239</span><a name="line.1239"></a> -<span class="sourceLineNo">1240</span> int exitCode = ToolRunner.run(conf, new Canary(executor, sink), args);<a name="line.1240"></a> -<span class="sourceLineNo">1241</span> executor.shutdown();<a name="line.1241"></a> -<span class="sourceLineNo">1242</span> System.exit(exitCode);<a name="line.1242"></a> -<span class="sourceLineNo">1243</span> }<a name="line.1243"></a> -<span class="sourceLineNo">1244</span>}<a name="line.1244"></a> +<span class="sourceLineNo">625</span> }<a name="line.625"></a> +<span class="sourceLineNo">626</span> }<a name="line.626"></a> +<span class="sourceLineNo">627</span><a name="line.627"></a> +<span class="sourceLineNo">628</span> if (this.failOnError && monitor.finalCheckForErrors()) {<a name="line.628"></a> +<span class="sourceLineNo">629</span> monitorThread.interrupt();<a name="line.629"></a> +<span class="sourceLineNo">630</span> return monitor.errorCode;<a name="line.630"></a> +<span class="sourceLineNo">631</span> }<a name="line.631"></a> +<span class="sourceLineNo">632</span> } finally {<a name="line.632"></a> +<span class="sourceLineNo">633</span> if (monitor != null) monitor.close();<a name="line.633"></a> +<span class="sourceLineNo">634</span> }<a name="line.634"></a> +<span class="sourceLineNo">635</span><a name="line.635"></a> +<span class="sourceLineNo">636</span> Thread.sleep(interval);<a name="line.636"></a> +<span class="sourceLineNo">637</span> } while (interval > 0);<a name="line.637"></a> +<span class="sourceLineNo">638</span> } // try-with-resources close<a name="line.638"></a> +<span class="sourceLineNo">639</span><a name="line.639"></a> +<span class="sourceLineNo">640</span> if (choreService != null) {<a name="line.640"></a> +<span class="sourceLineNo">641</span> choreService.shutdown();<a name="line.641"></a> +<span class="sourceLineNo">642</span> }<a name="line.642"></a> +<span class="sourceLineNo">643</span> return monitor.errorCode;<a name="line.643"></a> +<span class="sourceLineNo">644</span> }<a name="line.644"></a> +<span class="sourceLineNo">645</span><a name="line.645"></a> +<span class="sourceLineNo">646</span> private void printUsageAndExit() {<a name="line.646"></a> +<span class="sourceLineNo">647</span> System.err.printf(<a name="line.647"></a> +<span class="sourceLineNo">648</span> "Usage: bin/hbase %s [opts] [table1 [table2]...] | [regionserver1 [regionserver2]..]%n",<a name="line.648"></a> +<span class="sourceLineNo">649</span> getClass().getName());<a name="line.649"></a> +<span class="sourceLineNo">650</span> System.err.println(" where [opts] are:");<a name="line.650"></a> +<span class="sourceLineNo">651</span> System.err.println(" -help Show this help and exit.");<a name="line.651"></a> +<span class="sourceLineNo">652</span> System.err.println(" -regionserver replace the table argument to regionserver,");<a name="line.652"></a> +<span class="sourceLineNo">653</span> System.err.println(" which means to enable regionserver mode");<a name="line.653"></a> +<span class="sourceLineNo">654</span> System.err.println(" -allRegions Tries all regions on a regionserver,");<a name="line.654"></a> +<span class="sourceLineNo">655</span> System.err.println(" only works in regionserver mode.");<a name="line.655"></a> +<span class="sourceLineNo">656</span> System.err.println(" -daemon Continuous check at defined intervals.");<a name="line.656"></a> +<span class="sourceLineNo">657</span> System.err.println(" -interval <N> Interval between checks (sec)");<a name="line.657"></a> +<span class="sourceLineNo">658</span> System.err.println(" -e Use table/regionserver as regular expression");<a name="line.658"></a> +<span class="sourceLineNo">659</span> System.err.println(" which means the table/regionserver is regular expression pattern");<a name="line.659"></a> +<span class="sourceLineNo">660</span> System.err.println(" -f <B> stop whole program if first error occurs," +<a name="line.660"></a> +<span class="sourceLineNo">661</span> " default is true");<a name="line.661"></a> +<span class="sourceLineNo">662</span> System.err.println(" -t <N> timeout for a check, default is 600000 (milisecs)");<a name="line.662"></a> +<span class="sourceLineNo">663</span> System.err.println(" -writeSniffing enable the write sniffing in canary");<a name="line.663"></a> +<span class="sourceLineNo">664</span> System.err.println(" -treatFailureAsError treats read / write failure as error");<a name="line.664"></a> +<span class="sourceLineNo">665</span> System.err.println(" -writeTable The table used for write sniffing."<a name="line.665"></a> +<span class="sourceLineNo">666</span> + " Default is hbase:canary");<a name="line.666"></a> +<span class="sourceLineNo">667</span> System.err<a name="line.667"></a> +<span class="sourceLineNo">668</span> .println(" -D<configProperty>=<value> assigning or override the configuration params");<a name="line.668"></a> +<span class="sourceLineNo">669</span> System.exit(USAGE_EXIT_CODE);<a name="line.669"></a> +<span class="sourceLineNo">670</span> }<a name="line.670"></a> +<span class="sourceLineNo">671</span><a name="line.671"></a> +<span class="sourceLineNo">672</span> /**<a name="line.672"></a> +<span class="sourceLineNo">673</span> * A Factory method for {@link Monitor}.<a name="line.673"></a> +<span class="sourceLineNo">674</span> * Can be overridden by user.<a name="line.674"></a> +<span class="sourceLineNo">675</span> * @param index a start index for monitor target<a name="line.675"></a> +<span class="sourceLineNo">676</span> * @param args args passed from user<a name="line.676"></a> +<span class="sourceLineNo">677</span> * @return a Monitor instance<a name="line.677"></a> +<span class="sourceLineNo">678</span> */<a name="line.678"></a> +<span class="sourceLineNo">679</span> public Monitor newMonitor(final Connection connection, int index, String[] args) {<a name="line.679"></a> +<span class="sourceLineNo">680</span> Monitor monitor = null;<a name="line.680"></a> +<span class="sourceLineNo">681</span> String[] monitorTargets = null;<a name="line.681"></a> +<span class="sourceLineNo">682</span><a name="line.682"></a> +<span class="sourceLineNo">683</span> if(index >= 0) {<a name="line.683"></a> +<span class="sourceLineNo">684</span> int length = args.length - index;<a name="line.684"></a> +<span class="sourceLineNo">685</span> monitorTargets = new String[length];<a name="line.685"></a> +<span class="sourceLineNo">686</span> System.arraycopy(args, index, monitorTargets, 0, length);<a name="line.686"></a> +<span class="sourceLineNo">687</span> }<a name="line.687"></a> +<span class="sourceLineNo">688</span><a name="line.688"></a> +<span class="sourceLineNo">689</span> if (this.regionServerMode) {<a name="line.689"></a> +<span class="sourceLineNo">690</span> monitor =<a name="line.690"></a> +<span class="sourceLineNo">691</span> new RegionServerMonitor(connection, monitorTargets, this.useRegExp,<a name="line.691"></a> +<span class="sourceLineNo">692</span> (ExtendedSink) this.sink, this.executor, this.regionServerAllRegions,<a name="line.692"></a> +<span class="sourceLineNo">693</span> this.treatFailureAsError);<a name="line.693"></a> +<span class="sourceLineNo">694</span> } else {<a name="line.694"></a> +<span class="sourceLineNo">695</span> monitor =<a name="line.695"></a> +<span class="sourceLineNo">696</span> new RegionMonitor(connection, monitorTargets, this.useRegExp, this.sink, this.executor,<a name="line.696"></a> +<span class="sourceLineNo">697</span> this.writeSniffing, this.writeTableName, this.treatFailureAsError);<a name="line.697"></a> +<span class="sourceLineNo">698</span> }<a name="line.698"></a> +<span class="sourceLineNo">699</span> return monitor;<a name="line.699"></a> +<span class="sourceLineNo">700</span> }<a name="line.700"></a> +<span class="sourceLineNo">701</span><a name="line.701"></a> +<span class="sourceLineNo">702</span> // a Monitor super-class can be extended by users<a name="line.702"></a> +<span class="sourceLineNo">703</span> public static abstract class Monitor implements Runnable, Closeable {<a name="line.703"></a> +<span class="sourceLineNo">704</span><a name="line.704"></a> +<span class="sourceLineNo">705</span> protected Connection connection;<a name="line.705"></a> +<span class="sourceLineNo">706</span> protected Admin admin;<a name="line.706"></a> +<span class="sourceLineNo">707</span> protected String[] targets;<a name="line.707"></a> +<span class="sourceLineNo">708</span> protected boolean useRegExp;<a name="line.708"></a> +<span class="sourceLineNo">709</span> protected boolean treatFailureAsError;<a name="line.709"></a> +<span class="sourceLineNo">710</span> protected boolean initialized = false;<a name="line.710"></a> +<span class="sourceLineNo">711</span><a name="line.711"></a> +<span class="sourceLineNo">712</span> protected boolean done = false;<a name="line.712"></a> +<span class="sourceLineNo">713</span> protected int errorCode = 0;<a name="line.713"></a> +<span class="sourceLineNo">714</span> protected Sink sink;<a name="line.714"></a> +<span class="sourceLineNo">715</span> protected ExecutorService executor;<a name="line.715"></a> +<span class="sourceLineNo">716</span><a name="line.716"></a> +<span class="sourceLineNo">717</span> public boolean isDone() {<a name="line.717"></a> +<span class="sourceLineNo">718</span> return done;<a name="line.718"></a> +<span class="sourceLineNo">719</span> }<a name="line.719"></a> +<span class="sourceLineNo">720</span><a name="line.720"></a> +<span class="sourceLineNo">721</span> public boolean hasError() {<a name="line.721"></a> +<span class="sourceLineNo">722</span> return errorCode != 0;<a name="line.722"></a> +<span class="sourceLineNo">723</span> }<a name="line.723"></a> +<span class="sourceLineNo">724</span><a name="line.724"></a> +<span class="sourceLineNo">725</span> public boolean finalCheckForErrors() {<a name="line.725"></a> +<span class="sourceLineNo">726</span> if (errorCode != 0) {<a name="line.726"></a> +<span class="sourceLineNo">727</span> return true;<a name="line.727"></a> +<span class="sourceLineNo">728</span> }<a name="line.728"></a> +<span class="sourceLineNo">729</span> return treatFailureAsError &&<a name="line.729"></a> +<span class="sourceLineNo">730</span> (sink.getReadFailureCount() > 0 || sink.getWriteFailureCount() > 0);<a name="line.730"></a> +<span class="sourceLineNo">731</span> }<a name="line.731"></a> +<span class="sourceLineNo">732</span><a name="line.732"></a> +<span class="sourceLineNo">733</span> @Override<a name="line.733"></a> +<span class="sourceLineNo">734</span> public void close() throws IOException {<a name="line.734"></a> +<span class="sourceLineNo">735</span> if (this.admin != null) this.admin.close();<a name="line.735"></a> +<span class="sourceLineNo">736</span> }<a name="line.736"></a> +<span class="sourceLineNo">737</span><a name="line.737"></a> +<span class="sourceLineNo">738</span> protected Monitor(Connection connection, String[] monitorTargets, boolean useRegExp, Sink sink,<a name="line.738"></a> +<span class="sourceLineNo">739</span> ExecutorService executor, boolean treatFailureAsError) {<a name="line.739"></a> +<span class="sourceLineNo">740</span> if (null == connection) throw new IllegalArgumentException("connection shall not be null");<a name="line.740"></a> +<span class="sourceLineNo">741</span><a name="line.741"></a> +<span class="sourceLineNo">742</span> this.connection = connection;<a name="line.742"></a> +<span class="sourceLineNo">743</span> this.targets = monitorTargets;<a name="line.743"></a> +<span class="sourceLineNo">744</span> this.useRegExp = useRegExp;<a name="line.744"></a> +<span class="sourceLineNo">745</span> this.treatFailureAsError = treatFailureAsError;<a name="line.745"></a> +<span class="sourceLineNo">746</span> this.sink = sink;<a name="line.746"></a> +<span class="sourceLineNo">747</span> this.executor = executor;<a name="line.747"></a> +<span class="sourceLineNo">748</span> }<a name="line.748"></a> +<span class="sourceLineNo">749</span><a name="line.749"></a> +<span class="sourceLineNo">750</span> @Override<a name="line.750"></a> +<span class="sourceLineNo">751</span> public abstract void run();<a name="line.751"></a> +<span class="sourceLineNo">752</span><a name="line.752"></a> +<span class="sourceLineNo">753</span> protected boolean initAdmin() {<a name="line.753"></a> +<span class="sourceLineNo">754</span> if (null == this.admin) {<a name="line.754"></a> +<span class="sourceLineNo">755</span> try {<a name="line.755"></a> +<span class="sourceLineNo">756</span> this.admin = this.connection.getAdmin();<a name="line.756"></a> +<span class="sourceLineNo">757</span> } catch (Exception e) {<a name="line.757"></a> +<span class="sourceLineNo">758</span> LOG.error("Initial HBaseAdmin failed...", e);<a name="line.758"></a> +<span class="sourceLineNo">759</span> this.errorCode = INIT_ERROR_EXIT_CODE;<a name="line.759"></a> +<span class="sourceLineNo">760</span> }<a name="line.760"></a> +<span class="sourceLineNo">761</span> } else if (admin.isAborted()) {<a name="line.761"></a> +<span class="sourceLineNo">762</span> LOG.error("HBaseAdmin aborted");<a name="line.762"></a> +<span class="sourceLineNo">763</span> this.errorCode = INIT_ERROR_EXIT_CODE;<a name="line.763"></a> +<span class="sourceLineNo">764</span> }<a name="line.764"></a> +<span class="sourceLineNo">765</span> return !this.hasError();<a name="line.765"></a> +<span class="sourceLineNo">766</span> }<a name="line.766"></a> +<span class="sourceLineNo">767</span> }<a name="line.767"></a> +<span class="sourceLineNo">768</span><a name="line.768"></a> +<span class="sourceLineNo">769</span> // a monitor for region mode<a name="line.769"></a> +<span class="sourceLineNo">770</span> private static class RegionMonitor extends Monitor {<a name="line.770"></a> +<span class="sourceLineNo">771</span> // 10 minutes<a name="line.771"></a> +<span class="sourceLineNo">772</span> private static final int DEFAULT_WRITE_TABLE_CHECK_PERIOD = 10 * 60 * 1000;<a name="line.772"></a> +<span class="sourceLineNo">773</span> // 1 days<a name="line.773"></a> +<span class="sourceLineNo">774</span> private static final int DEFAULT_WRITE_DATA_TTL = 24 * 60 * 60;<a name="line.774"></a> +<span class="sourceLineNo">775</span><a name="line.775"></a> +<span class="sourceLineNo">776</span> private long lastCheckTime = -1;<a name="line.776"></a> +<span class="sourceLineNo">777</span> private boolean writeSniffing;<a name="line.777"></a> +<span class="sourceLineNo">778</span> private TableName writeTableName;<a name="line.778"></a> +<span class="sourceLineNo">779</span> private int writeDataTTL;<a name="line.779"></a> +<span class="sourceLineNo">780</span> private float regionsLowerLimit;<a name="line.780"></a> +<span class="sourceLineNo">781</span> private float regionsUpperLimit;<a name="line.781"></a> +<span class="sourceLineNo">782</span> private int checkPeriod;<a name="line.782"></a> +<span class="sourceLineNo">783</span><a name="line.783"></a> +<span class="sourceLineNo">784</span> public RegionMonitor(Connection connection, String[] monitorTargets, boolean useRegExp,<a name="line.784"></a> +<span class="sourceLineNo">785</span> Sink sink, ExecutorService executor, boolean writeSniffing, TableName writeTableName,<a name="line.785"></a> +<span class="sourceLineNo">786</span> boolean treatFailureAsError) {<a name="line.786"></a> +<span class="sourceLineNo">787</span> super(connection, monitorTargets, useRegExp, sink, executor, treatFailureAsError);<a name="line.787"></a> +<span class="sourceLineNo">788</span> Configuration conf = connection.getConfiguration();<a name="line.788"></a> +<span class="sourceLineNo">789</span> this.writeSniffing = writeSniffing;<a name="line.789"></a> +<span class="sourceLineNo">790</span> this.writeTableName = writeTableName;<a name="line.790"></a> +<span class="sourceLineNo">791</span> this.writeDataTTL =<a name="line.791"></a> +<span class="sourceLineNo">792</span> conf.getInt(HConstants.HBASE_CANARY_WRITE_DATA_TTL_KEY, DEFAULT_WRITE_DATA_TTL);<a name="line.792"></a> +<span class="sourceLineNo">793</span> this.regionsLowerLimit =<a name="line.793"></a> +<span class="sourceLineNo">794</span> conf.getFloat(HConstants.HBASE_CANARY_WRITE_PERSERVER_REGIONS_LOWERLIMIT_KEY, 1.0f);<a name="line.794"></a> +<span class="sourceLineNo">795</span> this.regionsUpperLimit =<a name="line.795"></a> +<span class="sourceLineNo">796</span> conf.getFloat(HConstants.HBASE_CANARY_WRITE_PERSERVER_REGIONS_UPPERLIMIT_KEY, 1.5f);<a name="line.796"></a> +<span class="sourceLineNo">797</span> this.checkPeriod =<a name="line.797"></a> +<span class="sourceLineNo">798</span> conf.getInt(HConstants.HBASE_CANARY_WRITE_TABLE_CHECK_PERIOD_KEY,<a name="line.798"></a> +<span class="sourceLineNo">799</span> DEFAULT_WRITE_TABLE_CHECK_PERIOD);<a name="line.799"></a> +<span class="sourceLineNo">800</span> }<a name="line.800"></a> +<span class="sourceLineNo">801</span><a name="line.801"></a> +<span class="sourceLineNo">802</span> @Override<a name="line.802"></a> +<span class="sourceLineNo">803</span> public void run() {<a name="line.803"></a> +<span class="sourceLineNo">804</span> if (this.initAdmin()) {<a name="line.804"></a> +<span class="sourceLineNo">805</span> try {<a name="line.805"></a> +<span class="sourceLineNo">806</span> List<Future<Void>> taskFutures = new LinkedList<Future<Void>>();<a name="line.806"></a> +<span class="sourceLineNo">807</span> if (this.targets != null && this.targets.length > 0) {<a name="line.807"></a> +<span class="sourceLineNo">808</span> String[] tables = generateMonitorTables(this.targets);<a name="line.808"></a> +<span class="sourceLineNo">809</span> this.initialized = true;<a name="line.809"></a> +<span class="sourceLineNo">810</span> for (String table : tables) {<a name="line.810"></a> +<span class="sourceLineNo">811</span> taskFutures.addAll(Canary.sniff(admin, sink, table, executor, TaskType.READ));<a name="line.811"></a> +<span class="sourceLineNo">812</span> }<a name="line.812"></a> +<span class="sourceLineNo">813</span> } else {<a name="line.813"></a> +<span class="sourceLineNo">814</span> taskFutures.addAll(sniff(TaskType.READ));<a name="line.814"></a> +<span class="sourceLineNo">815</span> }<a name="line.815"></a> +<span class="sourceLineNo">816</span><a name="line.816"></a> +<span class="sourceLineNo">817</span> if (writeSniffing) {<a name="line.817"></a> +<span class="sourceLineNo">818</span> if (EnvironmentEdgeManager.currentTime() - lastCheckTime > checkPeriod) {<a name="line.818"></a> +<span class="sourceLineNo">819</span> try {<a name="line.819"></a> +<span class="sourceLineNo">820</span> checkWriteTableDistribution();<a name="line.820"></a> +<span class="sourceLineNo">821</span> } catch (IOException e) {<a name="line.821"></a> +<span class="sourceLineNo">822</span> LOG.error("Check canary table distribution failed!", e);<a name="line.822"></a> +<span class="sourceLineNo">823</span> }<a name="line.823"></a> +<span class="sourceLineNo">824</span> lastCheckTime = EnvironmentEdgeManager.currentTime();<a name="line.824"></a> +<span class="sourceLineNo">825</span> }<a name="line.825"></a> +<span class="sourceLineNo">826</span> // sniff canary table with write operation<a name="line.826"></a> +<span class="sourceLineNo">827</span> taskFutures.addAll(Canary.sniff(admin, sink,<a name="line.827"></a> +<span class="sourceLineNo">828</span> admin.getTableDescriptor(writeTableName), executor, TaskType.WRITE));<a name="line.828"></a> +<span class="sourceLineNo">829</span> }<a name="line.829"></a> +<span class="sourceLineNo">830</span><a name="line.830"></a> +<span class="sourceLineNo">831</span> for (Future<Void> future : taskFutures) {<a name="line.831"></a> +<span class="sourceLineNo">832</span> try {<a name="line.832"></a> +<span class="sourceLineNo">833</span> future.get();<a name="line.833"></a> +<span class="sourceLineNo">834</span> } catch (ExecutionException e) {<a name="line.834"></a> +<span class="sourceLineNo">835</span> LOG.error("Sniff region failed!", e);<a name="line.835"></a> +<span class="sourceLineNo">836</span> }<a name="line.836"></a> +<span class="sourceLineNo">837</span> }<a name="line.837"></a> +<span class="sourceLineNo">838</span> } catch (Exception e) {<a name="line.838"></a> +<span class="sourceLineNo">839</span> LOG.error("Run regionMonitor failed", e);<a name="line.839"></a> +<span class="sourceLineNo">840</span> this.errorCode = ERROR_EXIT_CODE;<a name="line.840"></a> +<span class="sourceLineNo">841</span> }<a name="line.841"></a> +<span class="sourceLineNo">842</span> }<a name="line.842"></a> +<span class="sourceLineNo">843</span> this.done = true;<a name="line.843"></a> +<span class="sourceLineNo">844</span> }<a name="line.844"></a> +<span class="sourceLineNo">845</span><a name="line.845"></a> +<span class="sourceLineNo">846</span> private String[] generateMonitorTables(String[] monitorTargets) throws IOException {<a name="line.846"></a> +<span class="sourceLineNo">847</span> String[] returnTables = null;<a name="line.847"></a> +<span class="sourceLineNo">848</span><a name="line.848"></a> +<span class="sourceLineNo">849</span> if (this.useRegExp) {<a name="line.849"></a> +<span class="sourceLineNo">850</span> Pattern pattern = null;<a name="line.850"></a> +<span class="sourceLineNo">851</span> HTableDescriptor[] tds = null;<a name="line.851"></a> +<span class="sourceLineNo">852</span> Set<String> tmpTables = new TreeSet<String>();<a name="line.852"></a> +<span class="sourceLineNo">853</span> try {<a name="line.853"></a> +<span class="sourceLineNo">854</span> if (LOG.isDebugEnabled()) {<a name="line.854"></a> +<span class="sourceLineNo">855</span> LOG.debug(String.format("reading list of tables"));<a name="line.855"></a> +<span class="sourceLineNo">856</span> }<a name="line.856"></a> +<span class="sourceLineNo">857</span> tds = this.admin.listTables(pattern);<a name="line.857"></a> +<span class="sourceLineNo">858</span> if (tds == null) {<a name="line.858"></a> +<span class="sourceLineNo">859</span> tds = new HTableDescriptor[0];<a name="line.859"></a> +<span class="sourceLineNo">860</span> }<a name="line.860"></a> +<span class="sourceLineNo">861</span> for (String monitorTarget : monitorTargets) {<a name="line.861"></a> +<span class="sourceLineNo">862</span> pattern = Pattern.compile(monitorTarget);<a name="line.862"></a> +<span class="sourceLineNo">863</span> for (HTableDescriptor td : tds) {<a name="line.863"></a> +<span class="sourceLineNo">864</span> if (pattern.matcher(td.getNameAsString()).matches()) {<a name="line.864"></a> +<span class="sourceLineNo">865</span> tmpTables.add(td.getNameAsString());<a name="line.865"></a> +<span class="sourceLineNo">866</span> }<a name="line.866"></a> +<span class="sourceLineNo">867</span> }<a name="line.867"></a> +<span class="sourceLineNo">868</span> }<a name="line.868"></a> +<span class="sourceLineNo">869</span> } catch (IOException e) {<a name="line.869"></a> +<span class="sourceLineNo">870</span> LOG.error("Communicate with admin failed", e);<a name="line.870"></a> +<span class="sourceLineNo">871</span> throw e;<a name="line.871"></a> +<span class="sourceLineNo">872</span> }<a name="line.872"></a> +<span class="sourceLineNo">873</span><a name="line.873"></a> +<span class="sourceLineNo">874</span> if (tmpTables.size() > 0) {<a name="line.874"></a> +<span class="sourceLineNo">875</span> returnTables = tmpTables.toArray(new String[tmpTables.size()]);<a name="line.875"></a> +<span class="sourceLineNo">876</span> } else {<a name="line.876"></a> +<span class="sourceLineNo">877</span> String msg = "No HTable found, tablePattern:" + Arrays.toString(monitorTargets);<a name="line.877"></a> +<span class="sourceLineNo">878</span> LOG.error(msg);<a name="line.878"></a> +<span class="sourceLineNo">879</span> this.errorCode = INIT_ERROR_EXIT_CODE;<a name="line.879"></a> +<span class="sourceLineNo">880</span> throw new TableNotFoundException(msg);<a name="line.880"></a> +<span class="sourceLineNo">881</span> }<a name="line.881"></a> +<span class="sourceLineNo">882</span> } else {<a name="line.882"></a> +<span class="s
<TRUNCATED>