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.regionserver;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.hadoop.fs.Path;
25 import org.apache.hadoop.hbase.*;
26 import org.apache.hadoop.hbase.regionserver.wal.FailedLogCloseException;
27 import org.apache.hadoop.hbase.regionserver.wal.HLog;
28 import org.apache.hadoop.hbase.regionserver.wal.HLogKey;
29 import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
30 import org.apache.hadoop.hbase.regionserver.wal.WALActionsListener;
31 import org.apache.hadoop.hbase.util.Bytes;
32 import org.apache.hadoop.hbase.util.HasThread;
33
34 import java.io.IOException;
35 import java.util.concurrent.atomic.AtomicBoolean;
36 import java.util.concurrent.locks.ReentrantLock;
37
38
39
40
41
42
43
44
45 class LogRoller extends HasThread implements WALActionsListener {
46 static final Log LOG = LogFactory.getLog(LogRoller.class);
47 private final ReentrantLock rollLock = new ReentrantLock();
48 private final AtomicBoolean rollLog = new AtomicBoolean(false);
49 private final Server server;
50 protected final RegionServerServices services;
51 private volatile long lastrolltime = System.currentTimeMillis();
52
53 private final long rollperiod;
54 private final int threadWakeFrequency;
55
56
57 public LogRoller(final Server server, final RegionServerServices services) {
58 super();
59 this.server = server;
60 this.services = services;
61 this.rollperiod = this.server.getConfiguration().
62 getLong("hbase.regionserver.logroll.period", 3600000);
63 this.threadWakeFrequency = this.server.getConfiguration().
64 getInt(HConstants.THREAD_WAKE_FREQUENCY, 10 * 1000);
65 }
66
67 @Override
68 public void run() {
69 while (!server.isStopped()) {
70 long now = System.currentTimeMillis();
71 boolean periodic = false;
72 if (!rollLog.get()) {
73 periodic = (now - this.lastrolltime) > this.rollperiod;
74 if (!periodic) {
75 synchronized (rollLog) {
76 try {
77 rollLog.wait(this.threadWakeFrequency);
78 } catch (InterruptedException e) {
79
80 }
81 }
82 continue;
83 }
84
85 if (LOG.isDebugEnabled()) {
86 LOG.debug("Hlog roll period " + this.rollperiod + "ms elapsed");
87 }
88 } else if (LOG.isDebugEnabled()) {
89 LOG.debug("HLog roll requested");
90 }
91 rollLock.lock();
92 try {
93 this.lastrolltime = now;
94
95 byte [][] regionsToFlush = getWAL().rollWriter(rollLog.get());
96 if (regionsToFlush != null) {
97 for (byte [] r: regionsToFlush) scheduleFlush(r);
98 }
99 } catch (FailedLogCloseException e) {
100 server.abort("Failed log close in log roller", e);
101 } catch (java.net.ConnectException e) {
102 server.abort("Failed log close in log roller", e);
103 } catch (IOException ex) {
104
105 server.abort("IOE in log roller",
106 RemoteExceptionHandler.checkIOException(ex));
107 } catch (Exception ex) {
108 LOG.error("Log rolling failed", ex);
109 server.abort("Log rolling failed", ex);
110 } finally {
111 rollLog.set(false);
112 rollLock.unlock();
113 }
114 }
115 LOG.info("LogRoller exiting.");
116 }
117
118
119
120
121 private void scheduleFlush(final byte [] encodedRegionName) {
122 boolean scheduled = false;
123 HRegion r = this.services.getFromOnlineRegions(Bytes.toString(encodedRegionName));
124 FlushRequester requester = null;
125 if (r != null) {
126 requester = this.services.getFlushRequester();
127 if (requester != null) {
128 requester.requestFlush(r);
129 scheduled = true;
130 }
131 }
132 if (!scheduled) {
133 LOG.warn("Failed to schedule flush of " +
134 Bytes.toString(encodedRegionName) + ", region=" + r + ", requester=" +
135 requester);
136 }
137 }
138
139 public void logRollRequested() {
140 synchronized (rollLog) {
141 rollLog.set(true);
142 rollLog.notifyAll();
143 }
144 }
145
146
147
148
149
150 public void interruptIfNecessary() {
151 try {
152 rollLock.lock();
153 this.interrupt();
154 } finally {
155 rollLock.unlock();
156 }
157 }
158
159 protected HLog getWAL() throws IOException {
160 return this.services.getWAL(null);
161 }
162
163 @Override
164 public void preLogRoll(Path oldPath, Path newPath) throws IOException {
165
166 }
167
168 @Override
169 public void postLogRoll(Path oldPath, Path newPath) throws IOException {
170
171 }
172
173 @Override
174 public void preLogArchive(Path oldPath, Path newPath) throws IOException {
175
176 }
177
178 @Override
179 public void postLogArchive(Path oldPath, Path newPath) throws IOException {
180
181 }
182
183 @Override
184 public void visitLogEntryBeforeWrite(HRegionInfo info, HLogKey logKey,
185 WALEdit logEdit) {
186
187 }
188
189 @Override
190 public void visitLogEntryBeforeWrite(HTableDescriptor htd, HLogKey logKey,
191 WALEdit logEdit) {
192
193 }
194
195 @Override
196 public void logCloseRequested() {
197
198 }
199 }