65
65
@ InterfaceAudience .LimitedPrivate ({"HDFS" , "MapReduce" })
66
66
@ InterfaceStability .Unstable
67
67
public class LocalDirAllocator {
68
-
69
- static final String E_NO_SPACE_AVAILABLE =
70
- "No space available in any of the local directories" ;
71
-
68
+
72
69
//A Map from the config item names like "mapred.local.dir"
73
70
//to the instance of the AllocatorPerContext. This
74
71
//is a static object to make sure there exists exactly one instance per JVM
@@ -387,24 +384,6 @@ int getCurrentDirectoryIndex() {
387
384
return currentContext .get ().dirNumLastAccessed .get ();
388
385
}
389
386
390
- /**
391
- * Format a string, log at debug and append it to the history as a new line.
392
- *
393
- * @param history history to fill in
394
- * @param fmt format string
395
- * @param args varags
396
- */
397
- private void note (StringBuilder history , String fmt , Object ... args ) {
398
- try {
399
- final String s = String .format (fmt , args );
400
- history .append (s ).append ("\n " );
401
- LOG .debug (s );
402
- } catch (Exception e ) {
403
- // some resilience in case the format string is wrong
404
- LOG .debug (fmt , e );
405
- }
406
- }
407
-
408
387
/** Get a path from the local FS. If size is known, we go
409
388
* round-robin over the set of disks (via the configured dirs) and return
410
389
* the first complete path which has enough space.
@@ -414,12 +393,6 @@ private void note(StringBuilder history, String fmt, Object... args) {
414
393
*/
415
394
public Path getLocalPathForWrite (String pathStr , long size ,
416
395
Configuration conf , boolean checkWrite ) throws IOException {
417
-
418
- // history is built up and logged at error if the alloc
419
- StringBuilder history = new StringBuilder ();
420
-
421
- note (history , "Searchng for a directory for file at %s, size = %,d; checkWrite=%s" ,
422
- pathStr , size , checkWrite );
423
396
Context ctx = confChanged (conf );
424
397
int numDirs = ctx .localDirs .length ;
425
398
int numDirsSearched = 0 ;
@@ -433,56 +406,27 @@ public Path getLocalPathForWrite(String pathStr, long size,
433
406
pathStr = pathStr .substring (1 );
434
407
}
435
408
Path returnPath = null ;
436
-
437
- final int dirCount = ctx .dirDF .length ;
438
- long [] availableOnDisk = new long [dirCount ];
439
- long totalAvailable = 0 ;
440
-
441
- StringBuilder pathNames = new StringBuilder ();
442
-
443
- //build the "roulette wheel"
444
- for (int i =0 ; i < dirCount ; ++i ) {
445
- final DF target = ctx .dirDF [i ];
446
- // attempt to recreate the dir so that getAvailable() is valid
447
- // if it fails, getAvailable() will return 0, so the dir will
448
- // be declared unavailable.
449
- // return value is logged at debug to keep spotbugs quiet.
450
- final String name = target .getDirPath ();
451
- pathNames .append (" " ).append (name );
452
- final File dirPath = new File (name );
453
-
454
- // existence probe
455
- if (!dirPath .exists ()) {
456
- LOG .debug ("creating buffer dir {}" , name );
457
- if (dirPath .mkdirs ()) {
458
- note (history , "Created buffer dir %s" , name );
459
- } else {
460
- note (history , "Failed to create buffer dir %s" , name );
461
- }
462
- }
463
-
464
- // path already existed or the mkdir call had an outcome
465
- // make sure the path is present and a dir, and if so add its availability
466
- if (dirPath .isDirectory ()) {
467
- final long available = target .getAvailable ();
468
- availableOnDisk [i ] = available ;
469
- note (history , "%s available under path %s" , pathStr , available );
409
+
410
+ if (size == SIZE_UNKNOWN ) { //do roulette selection: pick dir with probability
411
+ //proportional to available size
412
+ long [] availableOnDisk = new long [ctx .dirDF .length ];
413
+ long totalAvailable = 0 ;
414
+
415
+ //build the "roulette wheel"
416
+ for (int i =0 ; i < ctx .dirDF .length ; ++i ) {
417
+ final DF target = ctx .dirDF [i ];
418
+ // attempt to recreate the dir so that getAvailable() is valid
419
+ // if it fails, getAvailable() will return 0, so the dir will
420
+ // be declared unavailable.
421
+ // return value is logged at debug to keep spotbugs quiet.
422
+ final boolean b = new File (target .getDirPath ()).mkdirs ();
423
+ LOG .debug ("mkdirs of {}={}" , target , b );
424
+ availableOnDisk [i ] = target .getAvailable ();
470
425
totalAvailable += availableOnDisk [i ];
471
- } else {
472
- note (history , "%s does not exist/is not a directory" , pathStr );
473
426
}
474
- }
475
-
476
- note (history , "Directory count is %d; total available capacity is %,d{}" ,
477
- dirCount , totalAvailable );
478
427
479
- if (size == SIZE_UNKNOWN ) {
480
- //do roulette selection: pick dir with probability
481
- // proportional to available size
482
- note (history , "Size not specified, so picking at random." );
483
-
484
- if (totalAvailable == 0 ) {
485
- throw new DiskErrorException (E_NO_SPACE_AVAILABLE + pathNames + "; history=" + history );
428
+ if (totalAvailable == 0 ){
429
+ throw new DiskErrorException ("No space available in any of the local directories." );
486
430
}
487
431
488
432
// Keep rolling the wheel till we get a valid path
@@ -495,19 +439,14 @@ public Path getLocalPathForWrite(String pathStr, long size,
495
439
dir ++;
496
440
}
497
441
ctx .dirNumLastAccessed .set (dir );
498
- final Path localDir = ctx .localDirs [dir ];
499
- returnPath = createPath (localDir , pathStr , checkWrite );
442
+ returnPath = createPath (ctx .localDirs [dir ], pathStr , checkWrite );
500
443
if (returnPath == null ) {
501
444
totalAvailable -= availableOnDisk [dir ];
502
445
availableOnDisk [dir ] = 0 ; // skip this disk
503
446
numDirsSearched ++;
504
- note (history , "No capacity in %s" , localDir );
505
- } else {
506
- note (history , "Allocated file %s in %s" , returnPath , localDir );
507
447
}
508
448
}
509
449
} else {
510
- note (history , "Size is %,d; searching" , size );
511
450
// Start linear search with random increment if possible
512
451
int randomInc = 1 ;
513
452
if (numDirs > 2 ) {
@@ -520,22 +459,17 @@ public Path getLocalPathForWrite(String pathStr, long size,
520
459
maxCapacity = capacity ;
521
460
}
522
461
if (capacity > size ) {
523
- final Path localDir = ctx .localDirs [dirNum ];
524
462
try {
525
- returnPath = createPath (localDir , pathStr , checkWrite );
463
+ returnPath = createPath (ctx .localDirs [dirNum ], pathStr ,
464
+ checkWrite );
526
465
} catch (IOException e ) {
527
466
errorText = e .getMessage ();
528
467
diskException = e ;
529
- note (history , "Exception while creating path %s: %s" , localDir , errorText );
530
- LOG .debug ("DiskException caught for dir {}" , localDir , e );
468
+ LOG .debug ("DiskException caught for dir {}" , ctx .localDirs [dirNum ], e );
531
469
}
532
470
if (returnPath != null ) {
533
- // success
534
471
ctx .getAndIncrDirNumLastAccessed (numDirsSearched );
535
- note (history , "Allocated file %s in %s" , returnPath , localDir );
536
472
break ;
537
- } else {
538
- note (history , "No capacity in %s" , localDir );
539
473
}
540
474
}
541
475
dirNum ++;
@@ -550,14 +484,10 @@ public Path getLocalPathForWrite(String pathStr, long size,
550
484
//no path found
551
485
String newErrorText = "Could not find any valid local directory for " +
552
486
pathStr + " with requested size " + size +
553
- " as the max capacity in any directory"
554
- + " (" + pathNames + " )"
555
- + " is " + maxCapacity ;
487
+ " as the max capacity in any directory is " + maxCapacity ;
556
488
if (errorText != null ) {
557
489
newErrorText = newErrorText + " due to " + errorText ;
558
490
}
559
- LOG .error (newErrorText );
560
- LOG .error (history .toString ());
561
491
throw new DiskErrorException (newErrorText , diskException );
562
492
}
563
493
0 commit comments