Project

General

Profile

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
                idList = DBUtil.getAllDocids("knb-lter-%"); // use a scope
112
	            int limit = Math.min(idList.size(), 100);
113
	            Collections.shuffle(idList);
114
	            idList = idList.subList(0, limit);
115
	        	log.debug("limiting random list to: " + limit);
116
            }
117

    
118
            // make sure the list is sorted so we can break them into sublists for the threads
119
            Collections.sort(idList);
120

    
121
            // executor
122
            int availableProcessors = Runtime.getRuntime().availableProcessors();
123
            int nThreads = availableProcessors * 1;
124
            //availableProcessors++;
125
        	log.debug("Using nThreads: " + nThreads);
126

    
127
            ExecutorService executor = Executors.newFixedThreadPool(nThreads);
128
            int taskCount = 0;
129

    
130
            // init HZ
131
            log.debug("Making sure Hazelcast is up");
132
            HazelcastService.getInstance();
133
            
134
            // chunk into groups
135
			int fromIndex = 0;
136
            int toIndex = 0;
137
            String prefix = null;
138
            for (String docid: idList) {
139

    
140
            	// increment the next entry, exclusive
141
            	toIndex++;
142
            	
143
            	// use scope.docid (without revision) to determine groups
144
            	if (prefix == null || !docid.startsWith(prefix)) {
145
            		
146
            		// construct a sublist for this previous group of docids
147
					final List<String> subList = idList.subList(fromIndex, toIndex);
148
	            	log.debug("Grouping docid prefix: " + prefix);
149
					log.debug("subList.size: " + subList.size());
150
					
151
					// add the task for this sublist
152
					Runnable command = new Runnable() {
153
						@Override
154
						public void run() {
155
							// generate based on this list
156
				            try {
157
				            	log.debug("Processing subList.size: " + subList.size());
158
								SystemMetadataFactory.generateSystemMetadata(subList, includeOre, downloadData);
159
								log.debug("Done processing subList.size: " + subList.size());
160
								
161
							} catch (Exception e) {
162
								throw new RuntimeException(e);
163
							}
164
						}
165
					};
166
					
167
					// execute the task 
168
					executor.execute(command);
169
					taskCount++;
170
					
171
					// start at the end of this sublist
172
					fromIndex = toIndex;
173

    
174
            	}
175

    
176
            	log.debug("docid: " + docid);
177

    
178
            	// get the previous docid prefix
179
            	String previousId = docid;
180
            	prefix = previousId.substring(0, previousId.lastIndexOf("."));
181
				
182
            }
183

    
184
            log.info("done launching threaded tasks, count: " + taskCount);
185

    
186
            // wait for executor to finish
187
            executor.shutdown();
188
            
189
			// wait a long time
190
            log.debug("Waiting for all threads to complete");
191
            executor.awaitTermination(Long.MAX_VALUE, TimeUnit.HOURS);
192
            log.debug("Done waiting for all threads to complete");
193
            // now we are ready to be a data one node
194
            PropertyService.setProperty("dataone.systemmetadata.generated", Boolean.TRUE.toString());
195
            
196
		} catch (Exception e) {
197
			String msg = "Problem generating missing system metadata: " + e.getMessage();
198
			log.error(msg, e);
199
			success = false;
200
			throw new AdminException(msg);
201
		}
202
    	return success;
203
    }
204
    
205
    public static void main(String [] ags){
206

    
207
        try {
208
        	// set up the properties based on the test/deployed configuration of the workspace
209
        	SortedProperties testProperties = 
210
				new SortedProperties("test/test.properties");
211
			testProperties.load();
212
			String metacatContextDir = testProperties.getProperty("metacat.contextDir");
213
			PropertyService.getInstance(metacatContextDir + "/WEB-INF");
214
			// now run it
215
            GenerateSystemMetadata upgrader = new GenerateSystemMetadata();
216
	        upgrader.upgrade();
217
	        
218
        } catch (Exception ex) {
219
            System.out.println("Exception:" + ex.getMessage());
220
            ex.printStackTrace();
221
        }
222
    }
223
}
(2-2/2)