1
|
package edu.ucsb.nceas.metacat.admin.upgrade.dataone;
|
2
|
/**
|
3
|
* '$RCSfile$'
|
4
|
* Purpose: A Class for upgrading the database to version 1.5
|
5
|
* Copyright: 2000 Regents of the University of California and the
|
6
|
* National Center for Ecological Analysis and Synthesis
|
7
|
* Authors: Saurabh Garg
|
8
|
*
|
9
|
* '$Author: leinfelder $'
|
10
|
* '$Date: 2011-03-29 18:23:38 +0000 (Tue, 29 Mar 2011) $'
|
11
|
* '$Revision: 6025 $'
|
12
|
*
|
13
|
* This program is free software; you can redistribute it and/or modify
|
14
|
* it under the terms of the GNU General Public License as published by
|
15
|
* the Free Software Foundation; either version 2 of the License, or
|
16
|
* (at your option) any later version.
|
17
|
*
|
18
|
* This program is distributed in the hope that it will be useful,
|
19
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21
|
* GNU General Public License for more details.
|
22
|
*
|
23
|
* You should have received a copy of the GNU General Public License
|
24
|
* along with this program; if not, write to the Free Software
|
25
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
26
|
*/
|
27
|
|
28
|
|
29
|
import java.util.Collections;
|
30
|
import java.util.List;
|
31
|
import java.util.concurrent.ExecutorService;
|
32
|
import java.util.concurrent.Executors;
|
33
|
import java.util.concurrent.TimeUnit;
|
34
|
|
35
|
import org.apache.commons.logging.Log;
|
36
|
import org.apache.commons.logging.LogFactory;
|
37
|
|
38
|
import edu.ucsb.nceas.metacat.DBUtil;
|
39
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
40
|
import edu.ucsb.nceas.metacat.admin.AdminException;
|
41
|
import edu.ucsb.nceas.metacat.admin.upgrade.UpgradeUtilityInterface;
|
42
|
import edu.ucsb.nceas.metacat.dataone.SystemMetadataFactory;
|
43
|
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService;
|
44
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
45
|
import edu.ucsb.nceas.utilities.SortedProperties;
|
46
|
|
47
|
public class GenerateSystemMetadata implements UpgradeUtilityInterface {
|
48
|
|
49
|
private static Log log = LogFactory.getLog(GenerateSystemMetadata.class);
|
50
|
|
51
|
private int serverLocation = 1;
|
52
|
|
53
|
public boolean upgrade() throws AdminException {
|
54
|
|
55
|
// do this in a thread too so that we don't have to hang the UI (web)
|
56
|
ExecutorService executor = Executors.newSingleThreadExecutor();
|
57
|
Runnable command = new Runnable() {
|
58
|
@Override
|
59
|
public void run() {
|
60
|
// just run it
|
61
|
try {
|
62
|
boolean success = multiThreadUpgrade();
|
63
|
} catch (AdminException e) {
|
64
|
throw new RuntimeException(e);
|
65
|
}
|
66
|
}
|
67
|
};
|
68
|
executor.execute(command);
|
69
|
executor.shutdown();
|
70
|
|
71
|
// wait for it to finish before returning?
|
72
|
boolean wait = false;
|
73
|
if (wait) {
|
74
|
log.debug("Waiting for upgrade to complete");
|
75
|
try {
|
76
|
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.HOURS);
|
77
|
} catch (InterruptedException e) {
|
78
|
AdminException ae = new AdminException(e.getMessage());
|
79
|
ae.initCause(e);
|
80
|
throw ae;
|
81
|
}
|
82
|
log.debug("Done waiting for upgrade thread");
|
83
|
}
|
84
|
|
85
|
return true;
|
86
|
//return singleThreadUpgrade();
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* Use multiple threads to process parts of the complete ID list concurrently
|
91
|
* @return
|
92
|
* @throws AdminException
|
93
|
*/
|
94
|
public boolean multiThreadUpgrade() throws AdminException {
|
95
|
|
96
|
boolean success = true;
|
97
|
|
98
|
// do not include ORE or data, but can generate SystemMetadata for ALL records
|
99
|
final boolean includeOre = false;
|
100
|
final boolean downloadData = false;
|
101
|
|
102
|
try {
|
103
|
|
104
|
// the ids for which to generate system metadata
|
105
|
List<String> idList = null;
|
106
|
// only get local objects
|
107
|
idList = IdentifierManager.getInstance().getLocalIdsWithNoSystemMetadata(true, serverLocation);
|
108
|
|
109
|
// for testing, subset to a limited random number
|
110
|
boolean test = false;
|
111
|
if (test) {
|
112
|
//idList = DBUtil.getAllDocidsByType("eml://ecoinformatics.org/eml-2.1.0", true, serverLocation);
|
113
|
idList = DBUtil.getAllDocids("knb-lter-gce"); // use a scope
|
114
|
Collections.sort(idList);
|
115
|
//Collections.shuffle(idList);
|
116
|
int start = 0;
|
117
|
int count = 100;
|
118
|
int limit = Math.min(idList.size(), start + count);
|
119
|
idList = idList.subList(start, limit);
|
120
|
log.debug("limiting test list to: " + start + "-" + limit);
|
121
|
for (String docid: idList) {
|
122
|
log.debug("GENERATING SM TEST: " + docid);
|
123
|
}
|
124
|
}
|
125
|
|
126
|
// make sure the list is sorted so we can break them into sublists for the threads
|
127
|
Collections.sort(idList);
|
128
|
|
129
|
// executor
|
130
|
int availableProcessors = Runtime.getRuntime().availableProcessors();
|
131
|
int nThreads = availableProcessors * 1;
|
132
|
//availableProcessors++;
|
133
|
log.debug("Using nThreads: " + nThreads);
|
134
|
|
135
|
ExecutorService executor = Executors.newFixedThreadPool(nThreads);
|
136
|
int taskCount = 0;
|
137
|
|
138
|
// init HZ
|
139
|
log.debug("Making sure Hazelcast is up");
|
140
|
HazelcastService.getInstance();
|
141
|
|
142
|
// chunk into groups
|
143
|
int fromIndex = 0;
|
144
|
int toIndex = 0;
|
145
|
String prefix = null;
|
146
|
for (String docid: idList) {
|
147
|
|
148
|
// increment the next entry, exclusive
|
149
|
toIndex++;
|
150
|
|
151
|
// use scope.docid (without revision) to determine groups
|
152
|
// handle first document on its own, and a clause for the last document
|
153
|
if (prefix == null || !docid.startsWith(prefix) || toIndex == idList.size()) {
|
154
|
|
155
|
// construct a sublist for this previous group of docids
|
156
|
final List<String> subList = idList.subList(fromIndex, toIndex);
|
157
|
log.debug("Grouping docid prefix: " + prefix);
|
158
|
log.debug("subList.size: " + subList.size());
|
159
|
|
160
|
// add the task for this sublist
|
161
|
Runnable command = new Runnable() {
|
162
|
@Override
|
163
|
public void run() {
|
164
|
// generate based on this list
|
165
|
try {
|
166
|
log.debug("Processing subList.size: " + subList.size());
|
167
|
SystemMetadataFactory.generateSystemMetadata(subList, includeOre, downloadData);
|
168
|
log.debug("Done processing subList.size: " + subList.size());
|
169
|
|
170
|
} catch (Exception e) {
|
171
|
throw new RuntimeException(e);
|
172
|
}
|
173
|
}
|
174
|
};
|
175
|
|
176
|
// execute the task
|
177
|
executor.execute(command);
|
178
|
taskCount++;
|
179
|
|
180
|
// start at the end of this sublist
|
181
|
fromIndex = toIndex;
|
182
|
|
183
|
}
|
184
|
|
185
|
log.debug("docid: " + docid);
|
186
|
|
187
|
// get the previous docid prefix
|
188
|
String previousId = docid;
|
189
|
prefix = previousId.substring(0, previousId.lastIndexOf("."));
|
190
|
|
191
|
}
|
192
|
|
193
|
log.info("done launching threaded tasks, count: " + taskCount);
|
194
|
|
195
|
// wait for executor to finish
|
196
|
executor.shutdown();
|
197
|
|
198
|
// wait a long time
|
199
|
log.debug("Waiting for all threads to complete");
|
200
|
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.HOURS);
|
201
|
log.debug("Done waiting for all threads to complete");
|
202
|
// now we are ready to be a data one node
|
203
|
PropertyService.setProperty("dataone.systemmetadata.generated", Boolean.TRUE.toString());
|
204
|
|
205
|
} catch (Exception e) {
|
206
|
String msg = "Problem generating missing system metadata: " + e.getMessage();
|
207
|
log.error(msg, e);
|
208
|
success = false;
|
209
|
throw new AdminException(msg);
|
210
|
}
|
211
|
return success;
|
212
|
}
|
213
|
|
214
|
public int getServerLocation() {
|
215
|
return serverLocation;
|
216
|
}
|
217
|
|
218
|
public void setServerLocation(int serverLocation) {
|
219
|
this.serverLocation = serverLocation;
|
220
|
}
|
221
|
|
222
|
public static void main(String [] args){
|
223
|
|
224
|
try {
|
225
|
// set up the properties based on the test/deployed configuration of the workspace
|
226
|
SortedProperties testProperties =
|
227
|
new SortedProperties("test/test.properties");
|
228
|
testProperties.load();
|
229
|
String metacatContextDir = testProperties.getProperty("metacat.contextDir");
|
230
|
PropertyService.getInstance(metacatContextDir + "/WEB-INF");
|
231
|
|
232
|
// make an upgrader instance
|
233
|
GenerateSystemMetadata upgrader = new GenerateSystemMetadata();
|
234
|
|
235
|
// set any command line params, like the home server to run this for
|
236
|
if (args.length > 0) {
|
237
|
String serverLocation = args[0];
|
238
|
upgrader.setServerLocation(Integer.parseInt(serverLocation));
|
239
|
}
|
240
|
|
241
|
// now run it
|
242
|
upgrader.upgrade();
|
243
|
|
244
|
} catch (Exception ex) {
|
245
|
System.out.println("Exception:" + ex.getMessage());
|
246
|
ex.printStackTrace();
|
247
|
}
|
248
|
}
|
249
|
}
|