2015-08-25 18:03:55 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.vremanagement.executor.persistence;
|
|
|
|
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import org.codehaus.jackson.JsonNode;
|
|
|
|
import org.codehaus.jackson.map.ObjectMapper;
|
2015-08-26 16:34:11 +02:00
|
|
|
import org.codehaus.jackson.node.ArrayNode;
|
2015-08-25 18:03:55 +02:00
|
|
|
import org.codehaus.jackson.node.ObjectNode;
|
|
|
|
import org.ektorp.CouchDbConnector;
|
|
|
|
import org.ektorp.CouchDbInstance;
|
|
|
|
import org.ektorp.ViewQuery;
|
|
|
|
import org.ektorp.ViewResult;
|
|
|
|
import org.ektorp.http.HttpClient;
|
|
|
|
import org.ektorp.http.StdHttpClient;
|
|
|
|
import org.ektorp.http.StdHttpClient.Builder;
|
|
|
|
import org.ektorp.impl.StdCouchDbConnector;
|
|
|
|
import org.ektorp.impl.StdCouchDbInstance;
|
2015-08-26 18:01:58 +02:00
|
|
|
import org.gcube.vremanagement.executor.exception.PluginStateNotRetrievedException;
|
2015-08-25 18:03:55 +02:00
|
|
|
import org.gcube.vremanagement.executor.plugin.PluginState;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
|
|
|
*
|
|
|
|
*/
|
2015-08-26 14:29:00 +02:00
|
|
|
public class CouchDBPersistenceConnector extends SmartExecutorPersistenceConnector {
|
2015-08-25 18:03:55 +02:00
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(CouchDBPersistenceConnector.class);
|
|
|
|
|
|
|
|
protected CouchDbInstance couchDbInstance;
|
|
|
|
protected CouchDbConnector couchDbConnector;
|
|
|
|
|
|
|
|
protected static final String DB_NAME = "dbName";
|
|
|
|
|
|
|
|
public CouchDBPersistenceConnector(SmartExecutorPersistenceConfiguration configuration) throws Exception {
|
2015-08-26 14:29:00 +02:00
|
|
|
super();
|
2015-08-25 18:03:55 +02:00
|
|
|
prepareConnection(configuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected HttpClient initHttpClient(URL url, String username, String password){
|
|
|
|
Builder builder = new StdHttpClient.Builder().url(url);
|
|
|
|
builder.username(username).password(password);
|
|
|
|
HttpClient httpClient = builder.build();
|
|
|
|
return httpClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void prepareConnection(SmartExecutorPersistenceConfiguration configuration) throws Exception {
|
|
|
|
logger.debug("Preparing Connection for {}", this.getClass().getSimpleName());
|
|
|
|
HttpClient httpClient = initHttpClient(configuration.getUri().toURL(), configuration.getUsername(), configuration.getPassword());
|
|
|
|
couchDbInstance = new StdCouchDbInstance(httpClient);
|
|
|
|
couchDbConnector = new StdCouchDbConnector(configuration.getProperty(DB_NAME), couchDbInstance);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected ViewResult query(ViewQuery query){
|
|
|
|
ViewResult result = couchDbConnector.queryView(query);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() throws Exception {
|
|
|
|
couchDbConnector.getConnection().shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void createItem(JsonNode node, String id) throws Exception {
|
|
|
|
if(id!=null && id.compareTo("")!=0){
|
|
|
|
couchDbConnector.create(id, node);
|
|
|
|
}else{
|
|
|
|
couchDbConnector.create(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public final static String UUID_FIELD = "uuid";
|
|
|
|
public final static String ITERATION_FIELD = "iteration";
|
|
|
|
public final static String PLUGIN_NAME_FIELD = "pluginName";
|
|
|
|
public final static String TIMESTAMP_FIELD = "timestamp";
|
|
|
|
public final static String STATE_FIELD = "state";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void pluginStateEvolution(UUID uuid, int iteration, long timestamp,
|
|
|
|
String pluginName, PluginState pluginState) throws Exception {
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
ObjectNode objectNode = objectMapper.createObjectNode();
|
|
|
|
objectNode.put(UUID_FIELD, uuid.toString());
|
|
|
|
objectNode.put(ITERATION_FIELD, iteration);
|
|
|
|
objectNode.put(TIMESTAMP_FIELD, timestamp);
|
|
|
|
objectNode.put(PLUGIN_NAME_FIELD, pluginName);
|
|
|
|
objectNode.put(STATE_FIELD, pluginState.toString());
|
|
|
|
createItem(objectNode, null);
|
|
|
|
}
|
2015-08-26 16:34:11 +02:00
|
|
|
|
2015-08-25 18:03:55 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
2015-08-26 16:34:11 +02:00
|
|
|
@Deprecated
|
2015-08-25 18:03:55 +02:00
|
|
|
public PluginState getPluginInstanceState(UUID uuid, int iterationNumber)
|
|
|
|
throws Exception {
|
2015-08-26 16:34:11 +02:00
|
|
|
return reallyQuery(null, uuid, iterationNumber);
|
2015-08-25 18:03:55 +02:00
|
|
|
}
|
2015-08-26 16:34:11 +02:00
|
|
|
|
2015-08-25 18:03:55 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
2015-08-26 16:34:11 +02:00
|
|
|
@Deprecated
|
2015-08-25 18:03:55 +02:00
|
|
|
public PluginState getLastPluginInstanceState(UUID uuid) throws Exception {
|
2015-08-26 16:34:11 +02:00
|
|
|
return reallyQuery(null, uuid, LAST);
|
2015-08-25 18:03:55 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 16:34:11 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public PluginState getPluginInstanceState(String pluginName, UUID uuid, int iterationNumber)
|
|
|
|
throws Exception {
|
|
|
|
return reallyQuery(pluginName, uuid, iterationNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected final static int LAST = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public PluginState getLastPluginInstanceState(String pluginName, UUID uuid) throws Exception {
|
|
|
|
return reallyQuery(pluginName, uuid, LAST);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static final String MAP_REDUCE__DESIGN = "_design/";
|
|
|
|
protected static final String PLUGIN_STATE = "pluginState";
|
|
|
|
protected static final String PLUGIN_STATE_DEPRECATED = "pluginStateDeprecated";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param uuid
|
|
|
|
* @param iterationNumber -1 means LAST
|
|
|
|
* @return
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
protected PluginState reallyQuery(String pluginName, UUID uuid, int iterationNumber)
|
|
|
|
throws Exception {
|
|
|
|
|
|
|
|
ViewQuery query = new ViewQuery().designDocId(String.format("%s%s", MAP_REDUCE__DESIGN, PLUGIN_STATE));
|
|
|
|
|
|
|
|
|
|
|
|
ArrayNode startKey = new ObjectMapper().createArrayNode();
|
|
|
|
ArrayNode endKey = new ObjectMapper().createArrayNode();
|
|
|
|
|
|
|
|
if(pluginName!=null && pluginName.compareTo("")!=0){
|
|
|
|
startKey.add(pluginName);
|
|
|
|
endKey.add(pluginName);
|
|
|
|
query = query.viewName(PLUGIN_STATE);
|
|
|
|
}else{
|
|
|
|
query = query.viewName(PLUGIN_STATE_DEPRECATED);
|
|
|
|
}
|
|
|
|
|
|
|
|
startKey.add(uuid.toString());
|
|
|
|
endKey.add(uuid.toString());
|
|
|
|
|
|
|
|
if(iterationNumber==LAST){
|
|
|
|
startKey.add(1);
|
|
|
|
endKey.add("{}");
|
|
|
|
}else{
|
|
|
|
startKey.add(iterationNumber);
|
|
|
|
endKey.add(iterationNumber);
|
|
|
|
|
|
|
|
startKey.add(1);
|
|
|
|
endKey.add("{}");
|
|
|
|
}
|
|
|
|
|
|
|
|
query.startKey(startKey);
|
|
|
|
query.endKey(endKey);
|
|
|
|
|
|
|
|
|
|
|
|
PluginState pluginState = null;
|
|
|
|
ViewResult viewResult = query(query);
|
|
|
|
for (ViewResult.Row row : viewResult) {
|
|
|
|
//JsonNode key = row.getKeyAsNode();
|
|
|
|
JsonNode value = row.getValueAsNode();
|
2015-08-26 18:01:58 +02:00
|
|
|
|
|
|
|
pluginState = PluginState.valueOf(value.findValue("state").getTextValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pluginState==null){
|
|
|
|
throw new PluginStateNotRetrievedException();
|
2015-08-26 16:34:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return pluginState;
|
|
|
|
}
|
2015-08-25 18:03:55 +02:00
|
|
|
}
|