Revision 6424
Added by rnahf about 13 years ago
test/edu/ucsb/nceas/metacat/dataone/HzObjectPathMapTest.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.util.Map; |
4 | 4 |
|
5 |
import org.dataone.service.types.v1.Identifier; |
|
6 |
import org.junit.Before; |
|
5 | 7 |
import org.junit.Test; |
6 | 8 |
|
7 | 9 |
import com.hazelcast.core.Hazelcast; |
... | ... | |
13 | 15 |
public class HzObjectPathMapTest extends MCTestCase { |
14 | 16 |
|
15 | 17 |
|
18 |
@Before |
|
19 |
public void setUp() { |
|
20 |
|
|
21 |
} |
|
22 |
|
|
16 | 23 |
@Test |
17 |
public void testTwoMemberMapSizes() { |
|
24 |
public void testInstantiation() { |
|
25 |
// start a member |
|
26 |
HazelcastInstance h1 = Hazelcast.newHazelcastInstance(null); |
|
27 |
Map map1 = h1.getMap("hzObjectPath"); |
|
28 |
assertEquals(99, map1.size()); |
|
29 |
} |
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
@Test public void testProducerConsumer() { |
|
35 |
// start a member |
|
18 | 36 |
|
19 | 37 |
System.getProperties().setProperty("hazelcast.config", |
20 |
"/Users/rnahf/projects/nceas/metacat/" +
|
|
38 |
"/Users/rnahf/software/workspace/nceas/metacat/" +
|
|
21 | 39 |
"test/edu/ucsb/nceas/metacat/dataone/hazelcast.test.properties.xml"); |
22 |
// start the first member |
|
23 |
HazelcastInstance h1 = Hazelcast.newHazelcastInstance(null); |
|
24 |
// get the map and put 1000 entries |
|
25 |
Map map1 = h1.getMap("hzObjectPath"); |
|
26 |
|
|
27 |
for (int i = 0; i < 1000; i++) { |
|
28 |
map1.put(i, "value" + i); |
|
29 |
} |
|
30 |
// check the map size |
|
31 |
assertEquals(1000, map1.size()); |
|
32 |
// start the second member |
|
33 |
HazelcastInstance h2 = Hazelcast.newHazelcastInstance(null); |
|
34 |
// get the same map from the second member |
|
35 |
Map map2 = h2.getMap("hzObjectPath"); |
|
36 |
// check the size of map2 |
|
37 |
assertEquals(1000, map2.size()); |
|
38 |
// check the size of map1 again |
|
39 |
assertEquals(1000, map1.size()); |
|
40 |
} |
|
40 |
HazelcastInstance h1 = Hazelcast.newHazelcastInstance(null); |
|
41 |
Map serverMap = h1.getMap("hzObjectPath"); |
|
42 |
|
|
43 |
|
|
44 |
System.getProperties().setProperty("hazelcast.hzObjectPathRole","consumer"); |
|
45 |
|
|
46 |
HazelcastInstance h2 = Hazelcast.newHazelcastInstance(null); |
|
47 |
Map clientMap = h2.getMap("hzObjectPath"); |
|
48 |
|
|
49 |
|
|
50 |
System.out.println(serverMap.get(createIdentifier("testID.35"))); |
|
51 |
System.out.println(serverMap.get(createIdentifier("testID.23434"))); |
|
52 |
|
|
53 |
String pathValue = (String) clientMap.get(createIdentifier("testID.35")); |
|
54 |
|
|
55 |
System.out.println("pathValue: " + pathValue); |
|
56 |
assertEquals("/path/testID.35", pathValue); |
|
57 |
} |
|
41 | 58 |
|
42 | 59 |
|
60 |
|
|
61 |
private Identifier createIdentifier(String idValue) { |
|
62 |
Identifier id = new Identifier(); |
|
63 |
id.setValue(idValue); |
|
64 |
return id; |
|
65 |
} |
|
66 |
|
|
43 | 67 |
} |
src/edu/ucsb/nceas/metacat/dataone/hazelcast/ObjectPathMap.java | ||
---|---|---|
38 | 38 |
private static IdentifierManager im; |
39 | 39 |
private static String dataPath; |
40 | 40 |
private static String metadataPath; |
41 |
|
|
42 |
private static boolean hasDataStore; |
|
41 | 43 |
|
42 | 44 |
|
43 | 45 |
|
44 | 46 |
/** |
45 |
* creates an ObjectPathMapLoader
|
|
47 |
* creates an ObjectPathMap |
|
46 | 48 |
*/ |
47 | 49 |
public ObjectPathMap() { |
48 | 50 |
// try { |
... | ... | |
59 | 61 |
dataPath = "/data/"; |
60 | 62 |
metadataPath = "/metadata/"; |
61 | 63 |
im = IdentifierManager.getInstance(); |
64 |
String role = System.getProperties().getProperty("hazelcast.hzObjectPathRole"); |
|
65 |
if (role != null && role.equals("consumer")) { |
|
66 |
hasDataStore = false; |
|
67 |
System.out.println("new instance of ObjectPathMap: role = " + role); |
|
68 |
} else { |
|
69 |
hasDataStore = true; |
|
70 |
System.out.println("new instance of ObjectPathMap: role = " + role); |
|
71 |
} |
|
62 | 72 |
} |
63 | 73 |
|
64 | 74 |
|
... | ... | |
69 | 79 |
*/ |
70 | 80 |
private String pathToDocid(String localid) throws McdbDocNotFoundException { |
71 | 81 |
|
72 |
Hashtable<String, Object> ht = im.getDocumentInfo(localid); |
|
73 |
if (ht.get("doctype").equals("BIN")) { |
|
74 |
return dataPath + FileUtil.getFS() + localid; |
|
75 |
} else { |
|
76 |
return metadataPath + FileUtil.getFS() + localid; |
|
77 |
} |
|
82 |
return "/some/path/" + localid; |
|
83 |
// Hashtable<String, Object> ht = im.getDocumentInfo(localid); |
|
84 |
// if (ht.get("doctype").equals("BIN")) { |
|
85 |
// return dataPath + FileUtil.getFS() + localid; |
|
86 |
// } else { |
|
87 |
// return metadataPath + FileUtil.getFS() + localid; |
|
88 |
// } |
|
78 | 89 |
} |
79 | 90 |
|
80 | 91 |
|
... | ... | |
87 | 98 |
@Override |
88 | 99 |
public String load(Identifier key) |
89 | 100 |
{ |
90 |
String docid = null; |
|
91 |
String path = null; |
|
92 |
try { |
|
93 |
docid = im.getLocalId(key.getValue()); |
|
94 |
path = pathToDocid(docid); |
|
95 |
} catch (McdbDocNotFoundException e) { |
|
96 |
// TODO Auto-generated catch block |
|
97 |
e.printStackTrace(); |
|
101 |
if (hasDataStore) { |
|
102 |
String docid = null; |
|
103 |
String path = null; |
|
104 |
// try { |
|
105 |
// docid = im.getLocalId(key.getValue()); |
|
106 |
// path = pathToDocid(docid); |
|
107 |
// } catch (McdbDocNotFoundException e) { |
|
108 |
// // TODO Auto-generated catch block |
|
109 |
// e.printStackTrace(); |
|
110 |
// return null; |
|
111 |
// } |
|
112 |
path = "/path/" + key.getValue(); |
|
113 |
return path; |
|
114 |
} else { |
|
98 | 115 |
return null; |
99 | 116 |
} |
100 |
return path; |
|
101 | 117 |
} |
102 | 118 |
|
103 | 119 |
|
... | ... | |
109 | 125 |
@Override |
110 | 126 |
public Map<Identifier, String> loadAll(Collection<Identifier> identifiers) { |
111 | 127 |
|
112 |
Hashtable<Identifier,String> map = new Hashtable<Identifier,String>(); |
|
113 |
for (Identifier id : identifiers) { |
|
114 |
try { |
|
115 |
String docid = im.getLocalId(id.getValue()); |
|
116 |
map.put(id, pathToDocid(docid)); |
|
117 |
|
|
118 |
} catch (McdbDocNotFoundException e) { |
|
119 |
// TODO should the map load an empty path instead of |
|
120 |
// leaving out the entire entry? |
|
121 |
e.printStackTrace(); |
|
128 |
if (hasDataStore) { |
|
129 |
Hashtable<Identifier,String> map = new Hashtable<Identifier,String>(); |
|
130 |
for (Identifier id : identifiers) { |
|
131 |
map.put(id, "/path/" + id.getValue()); |
|
132 |
// try { |
|
133 |
// String docid = im.getLocalId(id.getValue()); |
|
134 |
// map.put(id, pathToDocid(docid)); |
|
135 |
// |
|
136 |
// } catch (McdbDocNotFoundException e) { |
|
137 |
// // TODO should the map load an empty path instead of |
|
138 |
// // leaving out the entire entry? |
|
139 |
// e.printStackTrace(); |
|
140 |
// } |
|
122 | 141 |
} |
142 |
return map; |
|
143 |
} else { |
|
144 |
return null; |
|
123 | 145 |
} |
124 |
return map; |
|
125 | 146 |
} |
126 | 147 |
|
127 | 148 |
|
... | ... | |
134 | 155 |
*/ |
135 | 156 |
@Override |
136 | 157 |
public Set<Identifier> loadAllKeys() { |
137 |
|
|
138 |
List<String> guids = im.getAllGUIDs(); |
|
139 |
|
|
140 |
Set<Identifier> set = Collections.synchronizedSet(new HashSet<Identifier>()); |
|
141 |
for (String guid : guids) { |
|
142 |
Identifier id = new Identifier(); |
|
143 |
id.setValue(guid); |
|
144 |
set.add(id); |
|
158 |
|
|
159 |
if (hasDataStore) { |
|
160 |
// List<String> guids = im.getAllGUIDs(); |
|
161 |
// |
|
162 |
Set<Identifier> set = Collections.synchronizedSet(new HashSet<Identifier>()); |
|
163 |
// for (String guid : guids) { |
|
164 |
// Identifier id = new Identifier(); |
|
165 |
// id.setValue(guid); |
|
166 |
// set.add(id); |
|
167 |
// } |
|
168 |
|
|
169 |
for (int i=1; i< 100; i++) { |
|
170 |
Identifier id = new Identifier(); |
|
171 |
id.setValue("testID." + i); |
|
172 |
set.add(id); |
|
173 |
} |
|
174 |
return set; |
|
175 |
} else { |
|
176 |
return null; |
|
145 | 177 |
} |
146 |
return set; |
|
147 | 178 |
} |
148 | 179 |
} |
src/edu/ucsb/nceas/metacat/dataone/hazelcast/ObjectPathMapCustomer.java | ||
---|---|---|
1 |
package edu.ucsb.nceas.metacat.dataone.hazelcast; |
|
2 |
|
|
3 |
import java.util.Collection; |
|
4 |
import java.util.Collections; |
|
5 |
import java.util.HashSet; |
|
6 |
import java.util.Hashtable; |
|
7 |
import java.util.List; |
|
8 |
import java.util.Map; |
|
9 |
import java.util.Set; |
|
10 |
|
|
11 |
import org.dataone.service.types.v1.Identifier; |
|
12 |
|
|
13 |
import com.hazelcast.core.MapLoader; |
|
14 |
|
|
15 |
import edu.ucsb.nceas.metacat.IdentifierManager; |
|
16 |
import edu.ucsb.nceas.metacat.McdbDocNotFoundException; |
|
17 |
import edu.ucsb.nceas.metacat.properties.PropertyService; |
|
18 |
import edu.ucsb.nceas.metacat.shared.ServiceException; |
|
19 |
import edu.ucsb.nceas.utilities.FileUtil; |
|
20 |
import edu.ucsb.nceas.utilities.PropertyNotFoundException; |
|
21 |
|
|
22 |
|
|
23 |
/** |
|
24 |
* MapLoader implementation for a hazelcast hzObjectPath. This class is called |
|
25 |
* when the ObjectPathMap needs to refresh against the persistent data-store. |
|
26 |
* The use case for this class is to communicate the filepath between JVMs on |
|
27 |
* the same machine, specifically between the metacat instance and the d1_indexer. |
|
28 |
* |
|
29 |
* d1_indexer will get Identifiers from elsewhere, but use this class to get |
|
30 |
* the paths to their associated files. The getAllKeys() method can (and should) |
|
31 |
* return null in a live setting. For unit testing, it may be useful to use |
|
32 |
* it to get some |
|
33 |
* |
|
34 |
* @author rnahf |
|
35 |
* |
|
36 |
*/ |
|
37 |
public class ObjectPathMapCustomer implements MapLoader<Identifier, String> { |
|
38 |
private static IdentifierManager im; |
|
39 |
private static String dataPath; |
|
40 |
private static String metadataPath; |
|
41 |
|
|
42 |
private static boolean hasDataStore; |
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
/** |
|
47 |
* creates an ObjectPathMapCustomer |
|
48 |
*/ |
|
49 |
public ObjectPathMapCustomer() { |
|
50 |
// try { |
|
51 |
// PropertyService ps = PropertyService.getInstance(); |
|
52 |
// dataPath = PropertyService.getProperty("application.datafilepath"); |
|
53 |
// metadataPath = PropertyService.getProperty("application.documentfilepath"); |
|
54 |
// } catch (PropertyNotFoundException e) { |
|
55 |
// // TODO Auto-generated catch block |
|
56 |
// e.printStackTrace(); |
|
57 |
// } catch (ServiceException e) { |
|
58 |
// // TODO Auto-generated catch block |
|
59 |
// e.printStackTrace(); |
|
60 |
// } |
|
61 |
dataPath = "/data/"; |
|
62 |
metadataPath = "/metadata/"; |
|
63 |
im = IdentifierManager.getInstance(); |
|
64 |
} |
|
65 |
|
|
66 |
|
|
67 |
/* |
|
68 |
* Metadata is stored in a different place on the filesystem than |
|
69 |
* the data. The doctype value for metadata can vary, but for data |
|
70 |
* is always 'BIN', so using a simple if-then-else to separate |
|
71 |
*/ |
|
72 |
private String pathToDocid(String localid) throws McdbDocNotFoundException { |
|
73 |
|
|
74 |
return "/some/path/" + localid; |
|
75 |
// Hashtable<String, Object> ht = im.getDocumentInfo(localid); |
|
76 |
// if (ht.get("doctype").equals("BIN")) { |
|
77 |
// return dataPath + FileUtil.getFS() + localid; |
|
78 |
// } else { |
|
79 |
// return metadataPath + FileUtil.getFS() + localid; |
|
80 |
// } |
|
81 |
} |
|
82 |
|
|
83 |
|
|
84 |
/** |
|
85 |
* Implementation of hazelcast MapLoader interface method. |
|
86 |
* For the provided Identifier (as key), returns the path to the |
|
87 |
* document on the local filesystem. Returns null if it can't |
|
88 |
* create the path. |
|
89 |
*/ |
|
90 |
@Override |
|
91 |
public String load(Identifier key) |
|
92 |
{ |
|
93 |
|
|
94 |
String docid = null; |
|
95 |
String path = null; |
|
96 |
try { |
|
97 |
docid = im.getLocalId(key.getValue()); |
|
98 |
path = pathToDocid(docid); |
|
99 |
} catch (McdbDocNotFoundException e) { |
|
100 |
// TODO Auto-generated catch block |
|
101 |
e.printStackTrace(); |
|
102 |
return null; |
|
103 |
} |
|
104 |
return path; |
|
105 |
} |
|
106 |
|
|
107 |
|
|
108 |
/** |
|
109 |
* Implementation of hazelcast MapLoader interface method. This method loads |
|
110 |
* mappings for all Identifiers in the parameters. Any Identifier not found |
|
111 |
* is not included in the resulting map. |
|
112 |
*/ |
|
113 |
@Override |
|
114 |
public Map<Identifier, String> loadAll(Collection<Identifier> identifiers) { |
|
115 |
|
|
116 |
Hashtable<Identifier,String> map = new Hashtable<Identifier,String>(); |
|
117 |
for (Identifier id : identifiers) { |
|
118 |
try { |
|
119 |
String docid = "x"; // im.getLocalId(id.getValue()); |
|
120 |
map.put(id, pathToDocid(docid)); |
|
121 |
|
|
122 |
} catch (McdbDocNotFoundException e) { |
|
123 |
// TODO should the map load an empty path instead of |
|
124 |
// leaving out the entire entry? |
|
125 |
e.printStackTrace(); |
|
126 |
} |
|
127 |
} |
|
128 |
return map; |
|
129 |
} |
|
130 |
|
|
131 |
|
|
132 |
/** |
|
133 |
* Return the full set of guids in the local metacat repository as |
|
134 |
* dataone Identifiers. |
|
135 |
* |
|
136 |
* (Hazelcast allows avoiding pre-loading by returning NULL, so consider |
|
137 |
* re-implementing if that serves the purpose of this class better) |
|
138 |
*/ |
|
139 |
@Override |
|
140 |
public Set<Identifier> loadAllKeys() { |
|
141 |
|
|
142 |
// List<String> guids = im.getAllGUIDs(); |
|
143 |
// |
|
144 |
Set<Identifier> set = Collections.synchronizedSet(new HashSet<Identifier>()); |
|
145 |
// for (String guid : guids) { |
|
146 |
// Identifier id = new Identifier(); |
|
147 |
// id.setValue(guid); |
|
148 |
// set.add(id); |
|
149 |
// } |
|
150 |
|
|
151 |
for (int i=1; i< 100; i++) { |
|
152 |
Identifier id = new Identifier(); |
|
153 |
id.setValue("testID." + i); |
|
154 |
set.add(id); |
|
155 |
} |
|
156 |
return set; |
|
157 |
} |
|
158 |
} |
Also available in: Unified diff
configuring hazelcast tests