Revision 6964
Added by ben leinfelder over 12 years ago
GenerateSystemMetadata.java | ||
---|---|---|
28 | 28 |
|
29 | 29 |
import java.util.Collections; |
30 | 30 |
import java.util.List; |
31 |
import java.util.concurrent.ExecutorService; |
|
32 |
import java.util.concurrent.Executors; |
|
31 | 33 |
|
32 | 34 |
import org.apache.commons.logging.Log; |
33 | 35 |
import org.apache.commons.logging.LogFactory; |
... | ... | |
43 | 45 |
private static Log log = LogFactory.getLog(GenerateSystemMetadata.class); |
44 | 46 |
|
45 | 47 |
public boolean upgrade() throws AdminException { |
48 |
return multiThreadUpgrade(); |
|
49 |
//return singleThreadUpgrade(); |
|
50 |
} |
|
51 |
|
|
52 |
/** |
|
53 |
* Use this current single thread to process each ID in the list |
|
54 |
* @return |
|
55 |
* @throws AdminException |
|
56 |
* @deprecated use the multithreaded version of this upgrade method |
|
57 |
*/ |
|
58 |
public boolean singleThreadUpgrade() throws AdminException { |
|
46 | 59 |
boolean success = true; |
47 | 60 |
|
48 | 61 |
// do not include ORE or data, but can generate SystemMetadata for ALL records |
... | ... | |
66 | 79 |
return success; |
67 | 80 |
} |
68 | 81 |
|
82 |
/** |
|
83 |
* Use multiple threads to process parts of the complete ID list concurrently |
|
84 |
* @return |
|
85 |
* @throws AdminException |
|
86 |
*/ |
|
87 |
public boolean multiThreadUpgrade() throws AdminException { |
|
88 |
|
|
89 |
boolean success = true; |
|
90 |
|
|
91 |
// do not include ORE or data, but can generate SystemMetadata for ALL records |
|
92 |
final boolean includeOre = false; |
|
93 |
final boolean downloadData = false; |
|
94 |
int serverLocation = -1; |
|
95 |
|
|
96 |
try { |
|
97 |
|
|
98 |
// get list of ALL docids at ALL server locations |
|
99 |
List<String> idList = DBUtil.getAllDocidsByType(null, true, serverLocation); |
|
100 |
Collections.sort(idList); |
|
101 |
|
|
102 |
// executor |
|
103 |
int availableProcessors = Runtime.getRuntime().availableProcessors(); |
|
104 |
int nThreads = availableProcessors * 1; |
|
105 |
log.debug("Using nThreads: " + nThreads); |
|
106 |
|
|
107 |
ExecutorService executor = Executors.newFixedThreadPool(nThreads); |
|
108 |
|
|
109 |
// chunk into groups |
|
110 |
int fromIndex = 0; |
|
111 |
int toIndex = 0; |
|
112 |
String prefix = null; |
|
113 |
for (String docid: idList) { |
|
114 |
|
|
115 |
// increment the next entry, exclusive |
|
116 |
toIndex++; |
|
117 |
|
|
118 |
// use scope.docid (without revision) to determine groups |
|
119 |
if (prefix == null || !docid.startsWith(prefix)) { |
|
120 |
|
|
121 |
// construct a sublist for this previous group of docids |
|
122 |
final List<String> subList = idList.subList(fromIndex, toIndex); |
|
123 |
log.debug("Grouping docid prefix: " + prefix); |
|
124 |
log.debug("subList.size: " + subList.size()); |
|
125 |
|
|
126 |
// add the task for this sublist |
|
127 |
Runnable command = new Runnable() { |
|
128 |
@Override |
|
129 |
public void run() { |
|
130 |
// generate based on this list |
|
131 |
try { |
|
132 |
log.debug("Processing subList.size: " + subList.size()); |
|
133 |
SystemMetadataFactory.generateSystemMetadata(subList, includeOre, downloadData); |
|
134 |
log.debug("Done processing subList.size: " + subList.size()); |
|
135 |
|
|
136 |
} catch (Exception e) { |
|
137 |
throw new RuntimeException(e); |
|
138 |
} |
|
139 |
} |
|
140 |
}; |
|
141 |
|
|
142 |
// execute the task |
|
143 |
executor.execute(command); |
|
144 |
|
|
145 |
// start at the end of this sublist |
|
146 |
fromIndex = toIndex; |
|
147 |
|
|
148 |
} |
|
149 |
|
|
150 |
log.debug("docid: " + docid); |
|
151 |
|
|
152 |
// get the previous docid prefix |
|
153 |
String previousId = docid; |
|
154 |
prefix = previousId.substring(0, previousId.lastIndexOf(".")); |
|
155 |
|
|
156 |
} |
|
157 |
|
|
158 |
// wait for executor to finish |
|
159 |
executor.shutdown(); |
|
160 |
|
|
161 |
} catch (Exception e) { |
|
162 |
String msg = "Problem generating missing system metadata: " + e.getMessage(); |
|
163 |
log.error(msg, e); |
|
164 |
success = false; |
|
165 |
throw new AdminException(msg); |
|
166 |
} |
|
167 |
return success; |
|
168 |
} |
|
169 |
|
|
69 | 170 |
public static void main(String [] ags){ |
70 | 171 |
|
71 | 172 |
try { |
Also available in: Unified diff
multithreaded implementation for processing docids for system metadata generation.
need to investigate ant/junit running that deadlocks hazelcast (config?)