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.io.IOException;
23 import java.lang.Thread.UncaughtExceptionHandler;
24 import java.util.concurrent.Executors;
25
26 import org.apache.hadoop.hbase.Server;
27
28 import com.google.common.util.concurrent.ThreadFactoryBuilder;
29
30
31
32
33
34
35
36
37
38 public abstract class BulkAssigner {
39 protected final Server server;
40
41
42
43
44 public BulkAssigner(final Server server) {
45 this.server = server;
46 }
47
48
49
50
51 protected String getThreadNamePrefix() {
52 return this.server.getServerName() + "-" + this.getClass().getName();
53 }
54
55 protected UncaughtExceptionHandler getUncaughtExceptionHandler() {
56 return new UncaughtExceptionHandler() {
57 @Override
58 public void uncaughtException(Thread t, Throwable e) {
59
60 server.abort("Uncaught exception in " + t.getName(), e);
61 }
62 };
63 }
64
65 protected int getThreadCount() {
66 return this.server.getConfiguration().
67 getInt("hbase.bulk.assignment.threadpool.size", 20);
68 }
69
70 protected long getTimeoutOnRIT() {
71 return this.server.getConfiguration().
72 getLong("hbase.bulk.assignment.waiton.empty.rit", 5 * 60 * 1000);
73 }
74
75 protected abstract void populatePool(
76 final java.util.concurrent.ExecutorService pool) throws IOException;
77
78 public boolean bulkAssign() throws InterruptedException, IOException {
79 return bulkAssign(true);
80 }
81
82
83
84
85
86
87
88
89
90
91 public boolean bulkAssign(boolean sync) throws InterruptedException,
92 IOException {
93 boolean result = false;
94 ThreadFactoryBuilder builder = new ThreadFactoryBuilder();
95 builder.setDaemon(true);
96 builder.setNameFormat(getThreadNamePrefix() + "-%1$d");
97 builder.setUncaughtExceptionHandler(getUncaughtExceptionHandler());
98 int threadCount = getThreadCount();
99 java.util.concurrent.ExecutorService pool =
100 Executors.newFixedThreadPool(threadCount, builder.build());
101 try {
102 populatePool(pool);
103
104
105 if (sync) result = waitUntilDone(getTimeoutOnRIT());
106 } finally {
107
108 pool.shutdown();
109 }
110 return result;
111 }
112
113
114
115
116
117
118
119 protected abstract boolean waitUntilDone(final long timeout)
120 throws InterruptedException;
121 }