28
28
import org .apache .mnemonic .collections .DurableSinglyLinkedList ;
29
29
import org .apache .mnemonic .collections .DurableSinglyLinkedListFactory ;
30
30
31
+ import java .text .NumberFormat ;
31
32
import java .util .HashMap ;
32
33
import java .util .Iterator ;
33
34
import java .util .Map ;
34
35
35
36
import org .apache .commons .lang3 .tuple .Pair ;
36
37
import org .apache .hadoop .conf .Configuration ;
37
38
import org .apache .hadoop .fs .Path ;
39
+ import org .apache .hadoop .mapreduce .JobContext ;
38
40
import org .apache .hadoop .mapreduce .TaskAttemptContext ;
41
+ import org .apache .hadoop .mapreduce .TaskID ;
39
42
import org .apache .hadoop .mapreduce .lib .output .FileOutputFormat ;
40
43
41
44
public class MneDurableOutputSession <V >
42
45
implements MneOutputSession <V >, MneDurableComputable <NonVolatileMemAllocator > {
43
46
44
47
private long poolSize ;
45
48
private TaskAttemptContext taskAttemptContext ;
49
+ private Configuration configuration ;
46
50
private String serviceName ;
47
51
private DurableType [] durableTypes ;
48
52
private EntityFactoryProxy [] entityFactoryProxies ;
@@ -61,6 +65,11 @@ public class MneDurableOutputSession<V>
61
65
public MneDurableOutputSession (TaskAttemptContext taskAttemptContext ) {
62
66
setTaskAttemptContext (taskAttemptContext );
63
67
m_recordmap = new HashMap <V , DurableSinglyLinkedList <V >>();
68
+ setConfiguration (taskAttemptContext .getConfiguration ());
69
+ }
70
+
71
+ public MneDurableOutputSession (Configuration configuration ) {
72
+ setConfiguration (configuration );
64
73
}
65
74
66
75
public void validateConfig () {
@@ -79,7 +88,7 @@ public void readConfig(String prefix) {
79
88
if (getTaskAttemptContext () == null ) {
80
89
throw new ConfigurationException ("taskAttemptContext has not yet been set" );
81
90
}
82
- Configuration conf = getTaskAttemptContext (). getConfiguration ();
91
+ Configuration conf = getConfiguration ();
83
92
setServiceName (MneConfigHelper .getMemServiceName (conf , MneConfigHelper .DEFAULT_OUTPUT_CONFIG_PREFIX ));
84
93
setDurableTypes (MneConfigHelper .getDurableTypes (conf , MneConfigHelper .DEFAULT_OUTPUT_CONFIG_PREFIX ));
85
94
setEntityFactoryProxies (Utils .instantiateEntityFactoryProxies (
@@ -93,10 +102,40 @@ public void readConfig(String prefix) {
93
102
94
103
protected Path genNextPoolPath () {
95
104
Path ret = new Path (FileOutputFormat .getOutputPath (getTaskAttemptContext ()),
96
- FileOutputFormat . getUniqueFile ( getTaskAttemptContext (),
97
- String . format ( "%s-%05d" , getBaseOutputName (), ++ m_poolidx ), MneConfigHelper .DEFAULT_FILE_EXTENSION ));
105
+ getUniqueName ( String . format ( "%s-%05d" , getBaseOutputName (), ++ m_poolidx ),
106
+ MneConfigHelper .DEFAULT_FILE_EXTENSION ));
98
107
return ret ;
99
108
}
109
+
110
+ protected String getUniqueName (String name , String extension ) {
111
+ int partition ;
112
+
113
+ NumberFormat numberFormat = NumberFormat .getInstance ();
114
+ numberFormat .setMinimumIntegerDigits (5 );
115
+ numberFormat .setGroupingUsed (false );
116
+
117
+ if (null != getTaskAttemptContext ()) {
118
+ TaskID taskId = getTaskAttemptContext ().getTaskAttemptID ().getTaskID ();
119
+ partition = taskId .getId ();
120
+ } else {
121
+ partition = getConfiguration ().getInt (JobContext .TASK_PARTITION , -1 );
122
+ }
123
+ if (partition == -1 ) {
124
+ throw new IllegalArgumentException ("This method can only be called from an application" );
125
+ }
126
+
127
+ String taskType = getConfiguration ().getBoolean (JobContext .TASK_ISMAP , JobContext .DEFAULT_TASK_ISMAP ) ? "m" : "r" ;
128
+
129
+ StringBuilder result = new StringBuilder ();
130
+ result .append (name );
131
+ result .append ('-' );
132
+ result .append (taskType );
133
+ result .append ('-' );
134
+ result .append (numberFormat .format (partition ));
135
+ result .append (extension );
136
+ return result .toString ();
137
+
138
+ }
100
139
101
140
@ Override
102
141
public void initNextPool () {
@@ -326,4 +365,12 @@ public void setBaseOutputName(String baseOutputName) {
326
365
this .baseOutputName = baseOutputName ;
327
366
}
328
367
368
+ public Configuration getConfiguration () {
369
+ return configuration ;
370
+ }
371
+
372
+ public void setConfiguration (Configuration configuration ) {
373
+ this .configuration = configuration ;
374
+ }
375
+
329
376
}
0 commit comments