Revision 28297
Added by Sandro La Bruzzo almost 10 years ago
modules/cnr-mongo-mdstore/branches/transaction-mdstore/src/test/java/eu/dnetlib/data/mdstore/modular/mongodb/ConfigurationTestConfig.java | ||
---|---|---|
1 |
package eu.dnetlib.data.mdstore.modular.mongodb; |
|
2 |
|
|
3 |
import java.net.UnknownHostException; |
|
4 |
|
|
5 |
import org.springframework.context.annotation.Bean; |
|
6 |
import org.springframework.context.annotation.Configuration; |
|
7 |
|
|
8 |
import com.mongodb.DB; |
|
9 |
import com.mongodb.Mongo; |
|
10 |
|
|
11 |
import eu.dnetlib.data.mdstore.modular.RecordParser; |
|
12 |
import eu.dnetlib.data.mdstore.modular.RecordParserFactory; |
|
13 |
import eu.dnetlib.data.mdstore.modular.StreamingRecordParser; |
|
14 |
import eu.dnetlib.data.mdstore.modular.connector.MDStoreDao; |
|
15 |
import eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager; |
|
16 |
|
|
17 |
@Configuration |
|
18 |
public class ConfigurationTestConfig { |
|
19 |
|
|
20 |
@Bean |
|
21 |
public DB db() throws UnknownHostException { |
|
22 |
Mongo mongo = new Mongo("localhost", 27017); |
|
23 |
return mongo.getDB("mdstore_test"); |
|
24 |
} |
|
25 |
|
|
26 |
@Bean |
|
27 |
public MDStoreTransactionManager manager() throws UnknownHostException { |
|
28 |
MDStoreTransactionManagerImpl manager = new MDStoreTransactionManagerImpl(); |
|
29 |
manager.setDb(db()); |
|
30 |
return manager; |
|
31 |
} |
|
32 |
|
|
33 |
@Bean |
|
34 |
public RecordParser recordParser() { |
|
35 |
RecordParserFactory rpfactory = new RecordParserFactory(); |
|
36 |
rpfactory.setParserType(StreamingRecordParser.class); |
|
37 |
return rpfactory.newInstance(); |
|
38 |
} |
|
39 |
|
|
40 |
@Bean |
|
41 |
public MDStoreDao mdstoreDao() throws UnknownHostException { |
|
42 |
MDStoreDaoImpl dao = new MDStoreDaoImpl(); |
|
43 |
dao.setDb(db()); |
|
44 |
dao.setRecordParser(recordParser()); |
|
45 |
dao.setUpsert(true); |
|
46 |
return dao; |
|
47 |
} |
|
48 |
|
|
49 |
} |
modules/cnr-mongo-mdstore/branches/transaction-mdstore/src/test/java/eu/dnetlib/data/mdstore/modular/mongodb/MDStoreTransactionManagerTest.java | ||
---|---|---|
1 |
package eu.dnetlib.data.mdstore.modular.mongodb; |
|
2 |
|
|
3 |
import java.net.UnknownHostException; |
|
4 |
import java.util.ArrayList; |
|
5 |
import java.util.List; |
|
6 |
import java.util.UUID; |
|
7 |
|
|
8 |
import org.junit.Assert; |
|
9 |
import org.junit.Ignore; |
|
10 |
import org.junit.runner.RunWith; |
|
11 |
import org.springframework.beans.factory.annotation.Autowired; |
|
12 |
import org.springframework.test.context.ContextConfiguration; |
|
13 |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
|
14 |
|
|
15 |
import com.mongodb.DB; |
|
16 |
import com.mongodb.DBCollection; |
|
17 |
import com.mongodb.DBCursor; |
|
18 |
import com.mongodb.Mongo; |
|
19 |
|
|
20 |
import eu.dnetlib.data.mdstore.MDStoreServiceException; |
|
21 |
import eu.dnetlib.data.mdstore.modular.connector.MDStore; |
|
22 |
import eu.dnetlib.data.mdstore.modular.connector.MDStoreDao; |
|
23 |
import eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager; |
|
24 |
|
|
25 |
@RunWith(SpringJUnit4ClassRunner.class) |
|
26 |
@ContextConfiguration(classes = ConfigurationTestConfig.class) |
|
27 |
public class MDStoreTransactionManagerTest { |
|
28 |
|
|
29 |
@Autowired |
|
30 |
private DB db; |
|
31 |
|
|
32 |
@Autowired |
|
33 |
private MDStoreTransactionManager manager; |
|
34 |
|
|
35 |
@Autowired |
|
36 |
private MDStoreDao dao; |
|
37 |
|
|
38 |
@Ignore |
|
39 |
public void testCreateandRetrieve() throws MDStoreServiceException { |
|
40 |
UUID idCreation = UUID.randomUUID(); |
|
41 |
db.getCollection("metadataManager").drop(); |
|
42 |
((MDStoreTransactionManagerImpl) manager).setManagerTable(null); |
|
43 |
|
|
44 |
String mdId = idCreation.toString() + "_TURTdG9yZURTUmVzb3VyY2VzL01EU3RvcmVEU1Jlc291cmNlVHlwZQ=="; |
|
45 |
manager.createMDStore(mdId); |
|
46 |
|
|
47 |
Assert.assertNotNull(manager.startTransaction(mdId, true)); |
|
48 |
Assert.assertNotNull(manager.startTransaction(mdId, true)); |
|
49 |
|
|
50 |
String s = manager.getMDStoreCollection(mdId); |
|
51 |
Assert.assertNotNull(s); |
|
52 |
this.manager.dropMDStore(mdId); |
|
53 |
s = null; |
|
54 |
try { |
|
55 |
s = manager.getMDStoreCollection(mdId); |
|
56 |
} catch (Exception e) { |
|
57 |
|
|
58 |
} |
|
59 |
Assert.assertNull(s); |
|
60 |
db.getCollection("metadataManager").drop(); |
|
61 |
((MDStoreTransactionManagerImpl) manager).setManagerTable(null); |
|
62 |
} |
|
63 |
|
|
64 |
@Ignore |
|
65 |
public void testReadMdStore() throws MDStoreServiceException { |
|
66 |
UUID idCreation = UUID.randomUUID(); |
|
67 |
String mdId = idCreation.toString() + "_TURTdG9yZURTUmVzb3VyY2VzL01EU3RvcmVEU1Jlc291cmNlVHlwZQ=="; |
|
68 |
manager.createMDStore(mdId); |
|
69 |
Assert.assertNotNull(manager.readMdStore(mdId)); |
|
70 |
Assert.assertNotNull(manager.startTransaction(mdId, true)); |
|
71 |
Assert.assertNotNull(manager.readMdStore(mdId)); |
|
72 |
db.getCollection("metadataManager").drop(); |
|
73 |
((MDStoreTransactionManagerImpl) manager).setManagerTable(null); |
|
74 |
db.getCollection("metadataManager").drop(); |
|
75 |
((MDStoreTransactionManagerImpl) manager).setManagerTable(null); |
|
76 |
|
|
77 |
} |
|
78 |
|
|
79 |
@Ignore |
|
80 |
public void testCommit() throws MDStoreServiceException { |
|
81 |
UUID idCreation = UUID.randomUUID(); |
|
82 |
String mdId = idCreation.toString() + "_TURTdG9yZURTUmVzb3VyY2VzL01EU3RvcmVEU1Jlc291cmNlVHlwZQ=="; |
|
83 |
manager.createMDStore(mdId); |
|
84 |
String idCurrent = manager.readMdStore(mdId); |
|
85 |
String transaction = manager.startTransaction(mdId, true); |
|
86 |
Assert.assertTrue(manager.commit(transaction, mdId)); |
|
87 |
Assert.assertNotSame(idCurrent, manager.readMdStore(mdId)); |
|
88 |
|
|
89 |
} |
|
90 |
|
|
91 |
@Ignore |
|
92 |
public void testDateTime() throws MDStoreServiceException, UnknownHostException { |
|
93 |
Mongo mongo = new Mongo("localhost", 27017); |
|
94 |
DB dbinput = mongo.getDB("mdstore"); |
|
95 |
DBCollection inputCollection = dbinput.getCollection("70e07e9f-b3bf-4423-8777-b159819e0c6a"); |
|
96 |
|
|
97 |
Assert.assertNotNull(inputCollection.findOne().get("body")); |
|
98 |
UUID idCreation = UUID.randomUUID(); |
|
99 |
|
|
100 |
String mdId = idCreation.toString() + "_TURTdG9yZURTUmVzb3VyY2VzL01EU3RvcmVEU1Jlc291cmNlVHlwZQ=="; |
|
101 |
|
|
102 |
manager.createMDStore(mdId); |
|
103 |
dao.createMDStore(mdId, "a", "a", "a"); |
|
104 |
|
|
105 |
String transId = manager.startTransaction(mdId, true); |
|
106 |
|
|
107 |
ArrayList<String> data = new ArrayList<String>(); |
|
108 |
DBCursor cursor = inputCollection.find(); |
|
109 |
|
|
110 |
for (int i = 0; i < 1000; i++) { |
|
111 |
data.add((String) cursor.next().get("body")); |
|
112 |
} |
|
113 |
dao.getMDStore(transId).feed(data, true); |
|
114 |
manager.commit(transId, mdId); |
|
115 |
|
|
116 |
cursor = inputCollection.find(); |
|
117 |
transId = manager.startTransaction(mdId, false); |
|
118 |
|
|
119 |
data.clear(); |
|
120 |
for (int i = 0; i < 10; i++) { |
|
121 |
data.add(cursor.next().get("body").toString().replace("oai:pumaoai.isti.cnr.it:", "SUUUCAAA")); |
|
122 |
} |
|
123 |
|
|
124 |
String currentId = manager.readMdStore(mdId); |
|
125 |
|
|
126 |
final MDStore newMdstore = dao.getMDStore(currentId); |
|
127 |
|
|
128 |
new Thread(new Runnable() { |
|
129 |
|
|
130 |
@Override |
|
131 |
public void run() { |
|
132 |
List<String> dataInput = newMdstore.deliver("", "", null).getResult(0, 10); |
|
133 |
for (int i = 0; i < 10; i++) { |
|
134 |
try { |
|
135 |
Thread.sleep(1000); |
|
136 |
} catch (InterruptedException e) { |
|
137 |
// TODO Auto-generated catch block |
|
138 |
e.printStackTrace(); |
|
139 |
} |
|
140 |
System.out.println(dataInput.get(i)); |
|
141 |
} |
|
142 |
|
|
143 |
} |
|
144 |
}).start(); |
|
145 |
|
|
146 |
dao.getMDStore(transId).feed(data, true); |
|
147 |
manager.commit(transId, mdId); |
|
148 |
|
|
149 |
} |
|
150 |
} |
modules/cnr-mongo-mdstore/branches/transaction-mdstore/src/main/java/eu/dnetlib/data/mdstore/modular/mongodb/MDStoreTransactionManagerImpl.java | ||
---|---|---|
413 | 413 |
@Override |
414 | 414 |
public void garbage() throws MDStoreServiceException { |
415 | 415 |
verifyConsistency(); |
416 |
log.info("Start garbage collection"); |
|
416 |
log.info("Start garbage collection of MdStore");
|
|
417 | 417 |
DBCursor cursor = this.managerTable.find(); |
418 | 418 |
while (cursor.hasNext()) { |
419 | 419 |
DBObject currentObject = cursor.next(); |
... | ... | |
437 | 437 |
} |
438 | 438 |
} |
439 | 439 |
} |
440 |
log.info("Complete garbage collection of MdStore"); |
|
440 | 441 |
} |
441 | 442 |
|
442 | 443 |
/** |
... | ... | |
469 | 470 |
public void setMdstoreDao(final MDStoreDao mdstoreDao) { |
470 | 471 |
this.mdstoreDao = mdstoreDao; |
471 | 472 |
} |
473 |
|
|
474 |
@Override |
|
475 |
public Boolean dropTransaction(final String mdId, final String idToDrop) throws MDStoreServiceException { |
|
476 |
verifyConsistency(); |
|
477 |
BasicDBObject query = new BasicDBObject(); |
|
478 |
query.put("mdId", mdId); |
|
479 |
DBCursor cursor = this.getManagerTable().find(query); |
|
480 |
if (cursor.hasNext() == false) throw new DocumentNotFoundException("Error, unable to find Mdstore with Id " + mdId); |
|
481 |
DBObject object = cursor.next(); |
|
482 |
BasicDBList values = (BasicDBList) object.get("transactions"); |
|
483 |
for (int i = 0; i < values.size(); i++) { |
|
484 |
DBObject value = (DBObject) values.get(i); |
|
485 |
String currentUsedId = (String) value.get("id"); |
|
486 |
if (currentUsedId.equals(idToDrop)) { |
|
487 |
db.getCollection(idToDrop).drop(); |
|
488 |
values.remove(value); |
|
489 |
this.managerTable.save(object); |
|
490 |
return true; |
|
491 |
} |
|
492 |
} |
|
493 |
throw new DocumentNotFoundException("Error, unable to drop old collection " + idToDrop); |
|
494 |
} |
|
472 | 495 |
} |
modules/cnr-mongo-mdstore/branches/transaction-mdstore/src/main/java/eu/dnetlib/data/mdstore/modular/inspector/MDStoreInspector.java | ||
---|---|---|
250 | 250 |
transactionManager.dropUsed(mdId, id); |
251 | 251 |
return "redirect:mdstores.do?id=" + mdId; |
252 | 252 |
} |
253 |
|
|
254 |
@RequestMapping(value = "/inspector/invalidTransactionCollection.do") |
|
255 |
public String invalidTransactionCollection(final Model model, @RequestParam("mdId") final String mdId, @RequestParam("id") final String id) |
|
256 |
throws MDStoreServiceException { |
|
257 |
transactionManager.dropTransaction(mdId, id); |
|
258 |
return "redirect:mdstores.do?id=" + mdId; |
|
259 |
} |
|
253 | 260 |
} |
modules/cnr-mongo-mdstore/branches/transaction-mdstore/src/main/resources/eu/dnetlib/enabling/views/inspector/infoTransaction.st | ||
---|---|---|
32 | 32 |
<tr> |
33 | 33 |
<th>Id</th> |
34 | 34 |
<th>Date</th> |
35 |
<th>Size</th> |
|
35 |
<th>Size</th> |
|
36 |
<th>drop</th> |
|
36 | 37 |
</tr> |
37 | 38 |
$info.transactions:{ |
38 |
<tr><td>$it.id$</td> <td>$it.date$</td><td>$it.size$</td> </tr> |
|
39 |
<tr><td>$it.id$</td> <td>$it.date$</td><td>$it.size$</td> <td><a href="invalidTransactionCollection.do?mdId=$info.mdId$&id=$it.id$">drop </a></td> </tr>
|
|
39 | 40 |
}$ |
40 | 41 |
</table> |
41 | 42 |
|
modules/cnr-mongo-mdstore/branches/transaction-mdstore/src/main/resources/eu/dnetlib/data/mdstore/modular/mongodb/applicationContext-mongodb-mdstore.xml | ||
---|---|---|
1 | 1 |
<?xml version="1.0" encoding="UTF-8"?> |
2 | 2 |
<beans xmlns="http://www.springframework.org/schema/beans" |
3 | 3 |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" |
4 |
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> |
|
4 |
xmlns:t="http://dnetlib.eu/springbeans/t" |
|
5 |
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> |
|
5 | 6 |
|
6 | 7 |
<bean id="mongodbMDStoreDao" class="eu.dnetlib.data.mdstore.modular.mongodb.MDStoreDaoImpl" |
7 | 8 |
p:db-ref="mdstoreMongoDB" p:recordParser-ref="mdstoreRecordParser" |
... | ... | |
26 | 27 |
<bean id= "mongoMdStoreTransaction" |
27 | 28 |
class="eu.dnetlib.data.mdstore.modular.mongodb.MDStoreTransactionManagerImpl" |
28 | 29 |
p:db-ref="mdstoreMongoDB" |
29 |
/> |
|
30 |
|
|
30 |
/> |
|
31 | 31 |
|
32 | 32 |
<bean id="mdstoreMongoDB" factory-bean="mdstoreMongoServer" |
33 | 33 |
factory-method="getDB"> |
34 | 34 |
<constructor-arg index="0" |
35 | 35 |
value="${services.mdstore.mongodb.db}" /> |
36 | 36 |
</bean> |
37 |
|
|
38 |
<bean t:id="transctionManagerGarabageCollectionJobSchedulerAccessor" |
|
39 |
class="org.springframework.scheduling.quartz.SchedulerAccessorBean" |
|
40 |
p:scheduler-ref="jobScheduler"> |
|
41 |
<property name="triggers"> |
|
42 |
<list> |
|
43 |
<bean class="org.springframework.scheduling.quartz.CronTriggerBean"> |
|
44 |
<property name="jobDetail"> |
|
45 |
<bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" |
|
46 |
p:targetObject-ref="mongoMdStoreTransaction" p:targetMethod="garbage" /> |
|
47 |
</property> |
|
48 |
<property name="cronExpression" value="0 0 23 1/1 * ? *" /> |
|
49 |
</bean> |
|
50 |
</list> |
|
51 |
</property> |
|
52 |
</bean> |
|
37 | 53 |
|
38 | 54 |
</beans> |
modules/cnr-mongo-mdstore/branches/transaction-mdstore/pom.xml | ||
---|---|---|
10 | 10 |
<groupId>eu.dnetlib</groupId> |
11 | 11 |
<artifactId>cnr-mongo-mdstore</artifactId> |
12 | 12 |
<packaging>jar</packaging> |
13 |
<version>1.1.0-SNAPSHOT</version>
|
|
13 |
<version>2.0.0-SNAPSHOT</version>
|
|
14 | 14 |
<dependencies> |
15 | 15 |
<dependency> |
16 | 16 |
<groupId>junit</groupId> |
Also available in: Unified diff
Updated version, Implemented method for dropping a transaction,
added log, created quartz bean that delete the old collection not still used