1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.master;
21
22 import java.lang.Thread.UncaughtExceptionHandler;
23 import java.util.concurrent.Executors;
24
25 import org.apache.hadoop.hbase.Server;
26
27 import com.google.common.util.concurrent.ThreadFactoryBuilder;
28
29
30
31
32
33
34
35 public abstract class BulkAssigner {
36 final Server server;
37
38
39
40
41 public BulkAssigner(final Server server) {
42 this.server = server;
43 }
44
45 protected String getThreadNamePrefix() {
46 return this.server.getServerName() + "-BulkAssigner";
47 }
48
49 protected UncaughtExceptionHandler getUncaughtExceptionHandler() {
50 return new UncaughtExceptionHandler() {
51 @Override
52 public void uncaughtException(Thread t, Throwable e) {
53
54 server.abort("Uncaught exception in " + t.getName(), e);
55 }
56 };
57 }
58
59 protected int getThreadCount() {
60 return this.server.getConfiguration().
61 getInt("hbase.bulk.assignment.threadpool.size", 20);
62 }
63
64 protected long getTimeoutOnRIT() {
65 return this.server.getConfiguration().
66 getLong("hbase.bulk.assignment.waiton.empty.rit", 10 * 60 * 1000);
67 }
68
69 protected abstract void populatePool(final java.util.concurrent.ExecutorService pool);
70
71
72
73
74
75
76 public boolean bulkAssign() throws InterruptedException {
77 boolean result = false;
78 ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
79 builder.setDaemon(true);
80 builder.setNameFormat(getThreadNamePrefix() + "-%1$d");
81 builder.setUncaughtExceptionHandler(getUncaughtExceptionHandler());
82 int threadCount = getThreadCount();
83 java.util.concurrent.ExecutorService pool =
84 Executors.newFixedThreadPool(threadCount, builder.build());
85 try {
86 populatePool(pool);
87
88
89 result = waitUntilDone(getTimeoutOnRIT());
90 } finally {
91
92 pool.shutdown();
93 }
94 return result;
95 }
96
97
98
99
100
101
102
103 protected abstract boolean waitUntilDone(final long timeout)
104 throws InterruptedException;
105 }