18
18
package org .apache .hadoop .hbase .wal ;
19
19
20
20
import java .io .IOException ;
21
+ import java .io .UnsupportedEncodingException ;
22
+ import java .net .URLDecoder ;
21
23
import java .util .ArrayList ;
22
24
import java .util .Collections ;
23
25
import java .util .Comparator ;
24
26
import java .util .List ;
25
27
import java .util .concurrent .atomic .AtomicBoolean ;
26
28
import java .util .concurrent .locks .ReadWriteLock ;
27
29
import java .util .concurrent .locks .ReentrantReadWriteLock ;
30
+ import java .util .regex .Matcher ;
28
31
import java .util .regex .Pattern ;
29
32
import org .apache .hadoop .conf .Configuration ;
30
33
import org .apache .hadoop .fs .FileSystem ;
36
39
import org .apache .hadoop .hbase .client .RegionInfo ;
37
40
import org .apache .hadoop .hbase .regionserver .wal .AbstractFSWAL ;
38
41
import org .apache .hadoop .hbase .regionserver .wal .WALActionsListener ;
42
+ import org .apache .hadoop .hbase .util .Addressing ;
39
43
import org .apache .hadoop .hbase .util .CancelableProgressable ;
40
44
import org .apache .hadoop .hbase .util .CommonFSUtils ;
41
45
import org .apache .hadoop .hbase .util .RecoverLeaseFSUtils ;
45
49
import org .slf4j .LoggerFactory ;
46
50
47
51
import org .apache .hbase .thirdparty .com .google .common .collect .Lists ;
52
+ import static org .apache .hadoop .hbase .ServerName .SERVERNAME_SEPARATOR ;
48
53
49
54
/**
50
55
* Base class of a WAL Provider that returns a single thread safe WAL that writes to Hadoop FS. By
@@ -243,6 +248,17 @@ static void requestLogRoll(final WAL wal) {
243
248
244
249
// should be package private; more visible for use in AbstractFSWAL
245
250
public static final String WAL_FILE_NAME_DELIMITER = "." ;
251
+
252
+ /**
253
+ * Pattern used to parse server name from WAL file name
254
+ * see {@link #getServerNameFromWALFileName(Path)}
255
+ */
256
+ public static final Pattern SERVERNAME_IN_FILE_PATTERN =
257
+ Pattern .compile ("[^" + SERVERNAME_SEPARATOR + "]+" +
258
+ SERVERNAME_SEPARATOR + Addressing .VALID_PORT_REGEX +
259
+ SERVERNAME_SEPARATOR + Addressing .VALID_PORT_REGEX +
260
+ "[^" + WAL_FILE_NAME_DELIMITER + "]+" );
261
+
246
262
/** The hbase:meta region's WAL filename extension */
247
263
public static final String META_WAL_PROVIDER_ID = ".meta" ;
248
264
static final String DEFAULT_PROVIDER_ID = "default" ;
@@ -404,6 +420,39 @@ public static ServerName getServerNameFromWALDirectoryName(Path logFile) {
404
420
return serverName ;
405
421
}
406
422
423
+ /**
424
+ * This function returns region server name from a log file name which is in one of the following
425
+ * formats:
426
+ * <ul>
427
+ * <li>hdfs://<name node>/hbase/oldWALs/hostname%2C22101%2C1487767381290.providerId.regiongroup-0.1487785392316</li>
428
+ * <li>hdfs://<name node>/hbase/oldWALs/hostname%2C22101%2C1487767381290.1487785392316</li>
429
+ * </ul>
430
+ * @return null if the passed in logFile isn't a valid WAL file path
431
+ */
432
+ public static ServerName getServerNameFromWALFileName (Path logFile ) {
433
+ String fileName = logFile .getName ();
434
+ // We were passed the directory and not a file in it.
435
+ ServerName serverName = null ;
436
+ try {
437
+ fileName = URLDecoder .decode (fileName , HConstants .UTF8_ENCODING );
438
+ Matcher matcher = SERVERNAME_IN_FILE_PATTERN .matcher (fileName );
439
+ if (!matcher .find ()) {
440
+ throw new IllegalArgumentException ("Cannot parse a server name form filename=" + fileName );
441
+ }
442
+ String strServerName = matcher .group (0 );
443
+ serverName = ServerName .parseServerName (strServerName );
444
+ } catch (IllegalArgumentException | IllegalStateException | UnsupportedEncodingException ex ) {
445
+ serverName = null ;
446
+ LOG .warn ("Cannot parse a server name from path={}" , logFile , ex );
447
+ }
448
+ if (serverName != null && serverName .getStartCode () < 0 ) {
449
+ LOG .warn ("Invalid log file path={}, start code {} is less than 0" , logFile ,
450
+ serverName .getStartCode ());
451
+ serverName = null ;
452
+ }
453
+ return serverName ;
454
+ }
455
+
407
456
public static boolean isMetaFile (Path p ) {
408
457
return isMetaFile (p .getName ());
409
458
}
@@ -448,10 +497,6 @@ public static boolean isArchivedLogFile(Path p) {
448
497
* @throws IOException exception
449
498
*/
450
499
public static Path findArchivedLog (Path path , Configuration conf ) throws IOException {
451
- // If the path contains oldWALs keyword then exit early.
452
- if (path .toString ().contains (HConstants .HREGION_OLDLOGDIR_NAME )) {
453
- return null ;
454
- }
455
500
Path walRootDir = CommonFSUtils .getWALRootDir (conf );
456
501
FileSystem fs = path .getFileSystem (conf );
457
502
// Try finding the log in old dir
@@ -463,6 +508,12 @@ public static Path findArchivedLog(Path path, Configuration conf) throws IOExcep
463
508
}
464
509
465
510
ServerName serverName = getServerNameFromWALDirectoryName (path );
511
+ if (serverName == null ) {
512
+ // try to parse server name from wal file name.
513
+ LOG .warn ("Parse server name from wal directory failed,"
514
+ + " try to parse from wal filename" );
515
+ serverName = getServerNameFromWALFileName (path );
516
+ }
466
517
if (serverName == null ) {
467
518
LOG .warn ("Can not extract server name from path {}, "
468
519
+ "give up searching the separated old log dir" , path );
0 commit comments