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
|
public boolean upgrade() throws AdminException {
|
52
|
|
53
|
// do this in a thread too so that we don't have to hang the UI (web)
|
54
|
ExecutorService executor = Executors.newSingleThreadExecutor();
|
55
|
Runnable command = new Runnable() {
|
56
|
@Override
|
57
|
public void run() {
|
58
|
// just run it
|
59
|
try {
|
60
|
boolean success = multiThreadUpgrade();
|
61
|
} catch (AdminException e) {
|
62
|
throw new RuntimeException(e);
|
63
|
}
|
64
|
}
|
65
|
};
|
66
|
executor.execute(command);
|
67
|
executor.shutdown();
|
68
|
|
69
|
// wait for it to finish before returning?
|
70
|
boolean wait = false;
|
71
|
if (wait) {
|
72
|
log.debug("Waiting for upgrade to complete");
|
73
|
try {
|
74
|
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.HOURS);
|
75
|
} catch (InterruptedException e) {
|
76
|
AdminException ae = new AdminException(e.getMessage());
|
77
|
ae.initCause(e);
|
78
|
throw ae;
|
79
|
}
|
80
|
log.debug("Done waiting for upgrade thread");
|
81
|
}
|
82
|
|
83
|
return true;
|
84
|
//return singleThreadUpgrade();
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* Use multiple threads to process parts of the complete ID list concurrently
|
89
|
* @return
|
90
|
* @throws AdminException
|
91
|
*/
|
92
|
public boolean multiThreadUpgrade() throws AdminException {
|
93
|
|
94
|
boolean success = true;
|
95
|
|
96
|
// do not include ORE or data, but can generate SystemMetadata for ALL records
|
97
|
final boolean includeOre = false;
|
98
|
final boolean downloadData = false;
|
99
|
int serverLocation = -1;
|
100
|
|
101
|
try {
|
102
|
|
103
|
// the ids for which to generate system metadata
|
104
|
List<String> idList = null;
|
105
|
idList = IdentifierManager.getInstance().getLocalIdsWithNoSystemMetadata(true);
|
106
|
|
107
|
// for testing, subset to a limited random number
|
108
|
boolean test = false;
|
109
|
if (test) {
|
110
|
//idList = DBUtil.getAllDocidsByType(null, true, serverLocation);
|
111
|
int limit = Math.min(idList.size(), 100);
|
112
|
Collections.shuffle(idList);
|
113
|
idList = idList.subList(0, limit);
|
114
|
log.debug("limiting random list to: " + limit);
|
115
|
}
|
116
|
|
117
|
// make sure the list is sorted so we can break them into sublists for the threads
|
118
|
Collections.sort(idList);
|
119
|
|
120
|
// executor
|
121
|
int availableProcessors = Runtime.getRuntime().availableProcessors();
|
122
|
int nThreads = availableProcessors * 1;
|
123
|
//availableProcessors++;
|
124
|
log.debug("Using nThreads: " + nThreads);
|
125
|
|
126
|
ExecutorService executor = Executors.newFixedThreadPool(nThreads);
|
127
|
int taskCount = 0;
|
128
|
|
129
|
// init HZ
|
130
|
log.debug("Making sure Hazelcast is up");
|
131
|
HazelcastService.getInstance();
|
132
|
|
133
|
// chunk into groups
|
134
|
int fromIndex = 0;
|
135
|
int toIndex = 0;
|
136
|
String prefix = null;
|
137
|
for (String docid: idList) {
|
138
|
|
139
|
// increment the next entry, exclusive
|
140
|
toIndex++;
|
141
|
|
142
|
// use scope.docid (without revision) to determine groups
|
143
|
if (prefix == null || !docid.startsWith(prefix)) {
|
144
|
|
145
|
// construct a sublist for this previous group of docids
|
146
|
final List<String> subList = idList.subList(fromIndex, toIndex);
|
147
|
log.debug("Grouping docid prefix: " + prefix);
|
148
|
log.debug("subList.size: " + subList.size());
|
149
|
|
150
|
// add the task for this sublist
|
151
|
Runnable command = new Runnable() {
|
152
|
@Override
|
153
|
public void run() {
|
154
|
// generate based on this list
|
155
|
try {
|
156
|
log.debug("Processing subList.size: " + subList.size());
|
157
|
SystemMetadataFactory.generateSystemMetadata(subList, includeOre, downloadData);
|
158
|
log.debug("Done processing subList.size: " + subList.size());
|
159
|
|
160
|
} catch (Exception e) {
|
161
|
throw new RuntimeException(e);
|
162
|
}
|
163
|
}
|
164
|
};
|
165
|
|
166
|
// execute the task
|
167
|
executor.execute(command);
|
168
|
taskCount++;
|
169
|
|
170
|
// start at the end of this sublist
|
171
|
fromIndex = toIndex;
|
172
|
|
173
|
}
|
174
|
|
175
|
log.debug("docid: " + docid);
|
176
|
|
177
|
// get the previous docid prefix
|
178
|
String previousId = docid;
|
179
|
prefix = previousId.substring(0, previousId.lastIndexOf("."));
|
180
|
|
181
|
}
|
182
|
|
183
|
log.info("done launching threaded tasks, count: " + taskCount);
|
184
|
|
185
|
// wait for executor to finish
|
186
|
executor.shutdown();
|
187
|
|
188
|
// wait a long time
|
189
|
log.debug("Waiting for all threads to complete");
|
190
|
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.HOURS);
|
191
|
log.debug("Done waiting for all threads to complete");
|
192
|
// now we are ready to be a data one node
|
193
|
PropertyService.setProperty("dataone.systemmetadata.generated", Boolean.TRUE.toString());
|
194
|
|
195
|
} catch (Exception e) {
|
196
|
String msg = "Problem generating missing system metadata: " + e.getMessage();
|
197
|
log.error(msg, e);
|
198
|
success = false;
|
199
|
throw new AdminException(msg);
|
200
|
}
|
201
|
return success;
|
202
|
}
|
203
|
|
204
|
public static void main(String [] ags){
|
205
|
|
206
|
try {
|
207
|
// set up the properties based on the test/deployed configuration of the workspace
|
208
|
SortedProperties testProperties =
|
209
|
new SortedProperties("test/test.properties");
|
210
|
testProperties.load();
|
211
|
String metacatContextDir = testProperties.getProperty("metacat.contextDir");
|
212
|
PropertyService.getInstance(metacatContextDir + "/WEB-INF");
|
213
|
// now run it
|
214
|
GenerateSystemMetadata upgrader = new GenerateSystemMetadata();
|
215
|
upgrader.upgrade();
|
216
|
|
217
|
} catch (Exception ex) {
|
218
|
System.out.println("Exception:" + ex.getMessage());
|
219
|
ex.printStackTrace();
|
220
|
}
|
221
|
}
|
222
|
}
|