diff --git a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/CassandraClientBase.java b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/CassandraClientBase.java index 924193638..dcb56a518 100644 --- a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/CassandraClientBase.java +++ b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/CassandraClientBase.java @@ -646,7 +646,6 @@ else if (getCqlVersion().equalsIgnoreCase(CassandraConstants.CQL_VERSION_3_0) return getCqlVersion().equalsIgnoreCase(CassandraConstants.CQL_VERSION_3_0); } - /** * Returns true in case of, composite Id and if cql3 opted and not a * embedded entity. @@ -657,9 +656,9 @@ else if (getCqlVersion().equalsIgnoreCase(CassandraConstants.CQL_VERSION_3_0) */ public boolean isCql3Enabled() { - return isCql3Enabled(null); + return isCql3Enabled(null); } - + /** * Find. * @@ -1410,7 +1409,10 @@ public int executeBatch() finally { clear(); - releaseConnection(pooledConnection); + if (pooledConnection != null) + { + releaseConnection(pooledConnection); + } } return recordsExecuted; @@ -1683,6 +1685,7 @@ protected CqlResult executeCQLQuery(String cqlQuery) throws InvalidRequestExcept return conn.execute_cql3_query(ByteBufferUtil.bytes(cqlQuery), org.apache.cassandra.thrift.Compression.NONE, consistencyLevel); } +// conn.set_cql_version(getCqlVersion()); return conn.execute_cql_query(ByteBufferUtil.bytes(cqlQuery), org.apache.cassandra.thrift.Compression.NONE); } finally diff --git a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/config/CassandraPropertyReader.java b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/config/CassandraPropertyReader.java index 94b1ab906..4032d99b6 100644 --- a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/config/CassandraPropertyReader.java +++ b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/config/CassandraPropertyReader.java @@ -229,21 +229,25 @@ public Table getColumnFamily(String schemaName, String cfName) public Properties getConnectionProperties() { DataStore ds = getDataStore(); + Properties properties = new Properties(); if (ds != null && ds.getConnection() != null) { - return ds.getConnection().getProperties(); + properties = ds.getConnection().getProperties(); + return properties; } - return new Properties(); + return properties; } - + public List getConnectionServers() { DataStore ds = getDataStore(); + List servers = new ArrayList(); if (ds != null && ds.getConnection() != null) { - return ds.getConnection().getServers(); + servers = ds.getConnection().getServers(); + return servers; } - return new ArrayList(); + return servers; } } } diff --git a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/datahandler/CassandraDataHandler.java b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/datahandler/CassandraDataHandler.java index da006b834..2eaaddce5 100644 --- a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/datahandler/CassandraDataHandler.java +++ b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/datahandler/CassandraDataHandler.java @@ -69,7 +69,7 @@ public interface CassandraDataHandler * the exception */ List fromThriftRow(Class clazz, EntityMetadata m, List relationNames, boolean isWrapReq, - ConsistencyLevel consistencyLevel, boolean isCql3Enabled, Object... rowIds) throws Exception; + ConsistencyLevel consistencyLevel, boolean isCql3Enabled,Object conn, Object... rowIds) throws Exception; /** * From thrift row. @@ -91,7 +91,7 @@ List fromThriftRow(Class clazz, EntityMetadata m, List relati * the exception */ Object fromThriftRow(Class clazz, EntityMetadata m, Object rowKey, List relationNames, - boolean isWrapReq, ConsistencyLevel consistencyLevel, boolean isCql3Enabled) throws Exception; + boolean isWrapReq, ConsistencyLevel consistencyLevel, boolean isCql3Enabled, Object conn) throws Exception; /** * Populate entity. diff --git a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/datahandler/CassandraDataHandlerBase.java b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/datahandler/CassandraDataHandlerBase.java index eafde5dfb..05336baf2 100644 --- a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/datahandler/CassandraDataHandlerBase.java +++ b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/datahandler/CassandraDataHandlerBase.java @@ -61,7 +61,6 @@ import com.impetus.kundera.property.PropertyAccessor; import com.impetus.kundera.property.PropertyAccessorFactory; import com.impetus.kundera.property.PropertyAccessorHelper; -import com.impetus.kundera.property.accessor.BigDecimalAccessor; import com.impetus.kundera.property.accessor.DoubleAccessor; import com.impetus.kundera.property.accessor.IntegerAccessor; import com.impetus.kundera.property.accessor.LongAccessor; @@ -204,14 +203,14 @@ public E fromThriftRow(Class clazz, EntityMetadata m, DataRow fromThriftRow(Class clazz, EntityMetadata m, List relationNames, boolean isWrapReq, - ConsistencyLevel consistencyLevel, boolean isCql3Enabled, Object... rowIds) throws Exception + ConsistencyLevel consistencyLevel, boolean isCql3Enabled,Object conn, Object... rowIds) throws Exception { List entities = new ArrayList(); if (rowIds != null) { for (Object rowKey : rowIds) { - Object e = fromThriftRow(clazz, m, rowKey, relationNames, isWrapReq, consistencyLevel, isCql3Enabled); + Object e = fromThriftRow(clazz, m, rowKey, relationNames, isWrapReq, consistencyLevel, isCql3Enabled, conn); if (e != null) { entities.add(e); @@ -241,7 +240,7 @@ public List fromThriftRow(Class clazz, EntityMetadata m, List * the exception */ public abstract Object fromThriftRow(Class clazz, EntityMetadata m, Object rowKey, List relationNames, - boolean isWrapReq, ConsistencyLevel consistencyLevel, boolean isCqlEnabled) throws Exception; + boolean isWrapReq, ConsistencyLevel consistencyLevel, boolean isCqlEnabled, Object conn) throws Exception; /** * Populate embedded object. diff --git a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/index/InvertedIndexHandler.java b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/index/InvertedIndexHandler.java index 7e326eced..29edcd412 100644 --- a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/index/InvertedIndexHandler.java +++ b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/index/InvertedIndexHandler.java @@ -44,7 +44,7 @@ public interface InvertedIndexHandler * @param cdHandler */ void write(Node node, EntityMetadata entityMetadata, String persistenceUnit, ConsistencyLevel consistencyLevel, - CassandraDataHandler cdHandler); + CassandraDataHandler cdHandler, Object conn); /** * Searches records from Inverted index table. @@ -56,7 +56,7 @@ void write(Node node, EntityMetadata entityMetadata, String persistenceUnit, Con * @return */ List search(EntityMetadata m, String persistenceUnit, ConsistencyLevel consistencyLevel, - Map> indexClauseMap); + Map> indexClauseMap, Object conn); /** * Deletes a record from inverted index table. @@ -65,6 +65,6 @@ List search(EntityMetadata m, String persistenceUnit, ConsistencyL * @param metadata * @param consistencyLevel */ - void delete(Object entity, EntityMetadata metadata, ConsistencyLevel consistencyLevel); + void delete(Object entity, EntityMetadata metadata, ConsistencyLevel consistencyLevel, Object conn); } diff --git a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/index/InvertedIndexHandlerBase.java b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/index/InvertedIndexHandlerBase.java index 9dcef51d5..bc636b0c4 100644 --- a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/index/InvertedIndexHandlerBase.java +++ b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/index/InvertedIndexHandlerBase.java @@ -56,7 +56,7 @@ public abstract class InvertedIndexHandlerBase private static Log log = LogFactory.getLog(InvertedIndexHandlerBase.class); public List search(EntityMetadata m, String persistenceUnit, ConsistencyLevel consistencyLevel, - Map> indexClauseMap) + Map> indexClauseMap, Object conn) { String columnFamilyName = m.getTableName() + Constants.INDEX_TABLE_SUFFIX; @@ -69,7 +69,7 @@ public List search(EntityMetadata m, String persistenceUnit, Consi for (IndexExpression expression : o.getExpressions()) { searchAndAddToResults(m, persistenceUnit, consistencyLevel, columnFamilyName, searchResults, - expression, isRowKeyQuery); + expression, isRowKeyQuery, conn); } } @@ -81,7 +81,8 @@ public List search(EntityMetadata m, String persistenceUnit, Consi * search result to searchResults */ private void searchAndAddToResults(EntityMetadata m, String persistenceUnit, ConsistencyLevel consistencyLevel, - String columnFamilyName, List searchResults, IndexExpression expression, boolean isRowKeyQuery) + String columnFamilyName, List searchResults, IndexExpression expression, + boolean isRowKeyQuery, Object conn) { SearchResult searchResult = new SearchResult(); @@ -130,7 +131,7 @@ private void searchAndAddToResults(EntityMetadata m, String persistenceUnit, Con // EQUAL Operator case EQ: SuperColumn thriftSuperColumn = getSuperColumnForRow(consistencyLevel, columnFamilyName, rowKey, - superColumnName, persistenceUnit); + superColumnName, persistenceUnit, conn); if (thriftSuperColumn != null) thriftSuperColumns.add(thriftSuperColumn); @@ -146,22 +147,22 @@ private void searchAndAddToResults(EntityMetadata m, String persistenceUnit, Con // Greater than operator case GT: searchSuperColumnsInRange(columnFamilyName, consistencyLevel, persistenceUnit, rowKey, superColumnName, - thriftSuperColumns, superColumnName, new byte[0]); + thriftSuperColumns, superColumnName, new byte[0], conn); break; // Less than Operator case LT: searchSuperColumnsInRange(columnFamilyName, consistencyLevel, persistenceUnit, rowKey, superColumnName, - thriftSuperColumns, new byte[0], superColumnName); + thriftSuperColumns, new byte[0], superColumnName, conn); break; // Greater than-equals to operator case GTE: searchSuperColumnsInRange(columnFamilyName, consistencyLevel, persistenceUnit, rowKey, superColumnName, - thriftSuperColumns, superColumnName, new byte[0]); + thriftSuperColumns, superColumnName, new byte[0], conn); break; // Less than equal to operator case LTE: searchSuperColumnsInRange(columnFamilyName, consistencyLevel, persistenceUnit, rowKey, superColumnName, - thriftSuperColumns, new byte[0], superColumnName); + thriftSuperColumns, new byte[0], superColumnName, conn); break; default: @@ -212,7 +213,7 @@ private void searchAndAddToResults(EntityMetadata m, String persistenceUnit, Con } } - public void delete(Object entity, EntityMetadata metadata, ConsistencyLevel consistencyLevel) + public void delete(Object entity, EntityMetadata metadata, ConsistencyLevel consistencyLevel, Object conn) { MetamodelImpl metaModel = (MetamodelImpl) KunderaMetadata.INSTANCE.getApplicationMetadata().getMetamodel( metadata.getPersistenceUnit()); @@ -254,7 +255,7 @@ public void delete(Object entity, EntityMetadata metadata, ConsistencyLevel cons if (superColumnName != null) { deleteColumn(indexColumnFamily, rowKey, superColumnName, - metadata.getPersistenceUnit(), consistencyLevel, columnName); + metadata.getPersistenceUnit(), consistencyLevel, columnName, conn); } } } @@ -273,7 +274,7 @@ public void delete(Object entity, EntityMetadata metadata, ConsistencyLevel cons if (superColumnName != null) { deleteColumn(indexColumnFamily, rowKey, superColumnName, metadata.getPersistenceUnit(), - consistencyLevel, columnName); + consistencyLevel, columnName, conn); } } @@ -291,7 +292,7 @@ public void delete(Object entity, EntityMetadata metadata, ConsistencyLevel cons * TODO */ protected abstract void deleteColumn(String indexColumnFamily, String rowKey, byte[] superColumnName, - String persistenceUnit, ConsistencyLevel consistencyLevel, byte[] columnName); + String persistenceUnit, ConsistencyLevel consistencyLevel, byte[] columnName, Object conn); /** * @param consistencyLevel @@ -301,10 +302,10 @@ protected abstract void deleteColumn(String indexColumnFamily, String rowKey, by * @return */ protected abstract SuperColumn getSuperColumnForRow(ConsistencyLevel consistencyLevel, String columnFamilyName, - String rowKey, byte[] superColumnName, String persistenceUnit); + String rowKey, byte[] superColumnName, String persistenceUnit, Object conn); protected abstract void searchSuperColumnsInRange(String columnFamilyName, ConsistencyLevel consistencyLevel, String persistenceUnit, String rowKey, byte[] searchSuperColumnName, List thriftSuperColumns, - byte[] start, byte[] finish); + byte[] start, byte[] finish, Object conn); } diff --git a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsClient.java b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsClient.java index 14c7fa1af..a663b05a3 100644 --- a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsClient.java +++ b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsClient.java @@ -38,7 +38,6 @@ import org.apache.cassandra.thrift.InvalidRequestException; import org.apache.cassandra.thrift.KeyRange; import org.apache.cassandra.thrift.KeySlice; -import org.apache.cassandra.thrift.Mutation; import org.apache.cassandra.thrift.SchemaDisagreementException; import org.apache.cassandra.thrift.SlicePredicate; import org.apache.cassandra.thrift.SuperColumn; @@ -94,8 +93,8 @@ public class PelopsClient extends CassandraClientBase implements Client mutations; - private List mutations; + private PelopsClientFactory clientFactory; /** * default constructor. @@ -121,8 +122,8 @@ public class PelopsClient extends CassandraClientBase implements Client externalProperties) + public PelopsClient(IndexManager indexManager, EntityReader reader, PelopsClientFactory clientFactory, + String persistenceUnit, Map externalProperties) { super(persistenceUnit, externalProperties); this.persistenceUnit = persistenceUnit; @@ -130,6 +131,7 @@ public PelopsClient(IndexManager indexManager, EntityReader reader, String persi this.dataHandler = new PelopsDataHandler(externalProperties); this.invertedIndexHandler = new PelopsInvertedIndexHandler(externalProperties); this.reader = reader; + this.clientFactory = clientFactory; } @Override @@ -169,15 +171,20 @@ public final List find(Class entityClass, List relationNames, boolean is } List entities = null; + Object conn = getPooledConection(getPersistenceUnit()); try { entities = dataHandler.fromThriftRow(entityClass, metadata, relationNames, isWrapReq, - getConsistencyLevel(), isCql3Enabled(metadata), rowIds); + getConsistencyLevel(), isCql3Enabled(metadata), conn, rowIds); } catch (Exception e) { throw new KunderaException(e); } + finally + { + releaseConnection(conn); + } return entities; } @@ -219,8 +226,18 @@ public void delete(Object entity, Object pKey) getIndexManager().remove(metadata, entity, pKey.toString()); // Delete from Inverted Index if applicable - invertedIndexHandler.delete(entity, metadata, getConsistencyLevel()); - + Object conn = getPooledConection(getPersistenceUnit()); + try + { + invertedIndexHandler.delete(entity, metadata, getConsistencyLevel(), conn); + } + finally + { + if (conn != null) + { + releaseConnection(conn); + } + } } @Override @@ -229,7 +246,7 @@ public final void close() this.indexManager.flush(); this.dataHandler = null; this.invertedIndexHandler = null; - closed = true; + // closed = true; } /** @@ -474,7 +491,7 @@ protected void onPersist(EntityMetadata metadata, Object entity, Object id, List { String columnFamily = metadata.getTableName(); tf = dataHandler.toThriftRow(entity, id, metadata, columnFamily); - timestamp = System.currentTimeMillis(); + // timestamp = System.currentTimeMillis(); } catch (Exception e) { @@ -545,7 +562,17 @@ protected void indexNode(Node node, EntityMetadata entityMetadata) super.indexNode(node, entityMetadata); // Write to inverted index table if applicable - invertedIndexHandler.write(node, entityMetadata, getPersistenceUnit(), getConsistencyLevel(), dataHandler); + // Delete from Inverted Index if applicable + Object conn = getPooledConection(getPersistenceUnit()); + try + { + invertedIndexHandler.write(node, entityMetadata, getPersistenceUnit(), getConsistencyLevel(), dataHandler, + conn); + } + finally + { + releaseConnection(conn); + } } /** @@ -777,8 +804,16 @@ public List findByRange(byte[] minVal, byte[] maxVal, EntityMetadata m, boolean public List searchInInvertedIndex(String columnFamilyName, EntityMetadata m, Map> indexClauseMap) { - - return invertedIndexHandler.search(m, getPersistenceUnit(), getConsistencyLevel(), indexClauseMap); + // Delete from Inverted Index if applicable + Object conn = getPooledConection(getPersistenceUnit()); + try + { + return invertedIndexHandler.search(m, getPersistenceUnit(), getConsistencyLevel(), indexClauseMap, conn); + } + finally + { + releaseConnection(conn); + } } /* @@ -794,13 +829,16 @@ protected CassandraDataHandler getDataHandler() protected IPooledConnection getPooledConection(String persistenceUnit) { - return PelopsUtils.getCassandraConnection(persistenceUnit, getExternalProperties()); + // return PelopsUtils.getCassandraConnection(persistenceUnit, + // getExternalProperties()); + return clientFactory.getConnection(); } protected void releaseConnection(Object conn) { - PelopsUtils.releaseConnection((IPooledConnection) conn); + // PelopsUtils.releaseConnection((IPooledConnection) conn); + clientFactory.releaseConnection((IPooledConnection) conn); } @Override diff --git a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsClientFactory.java b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsClientFactory.java index bfea3648f..470b700fe 100644 --- a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsClientFactory.java +++ b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsClientFactory.java @@ -15,86 +15,119 @@ ******************************************************************************/ package com.impetus.client.cassandra.pelops; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Properties; +import net.dataforte.cassandra.pool.ConnectionPool; +import net.dataforte.cassandra.pool.PoolConfiguration; +import net.dataforte.cassandra.pool.PoolProperties; + +import org.apache.thrift.TException; import org.scale7.cassandra.pelops.Cluster; import org.scale7.cassandra.pelops.IConnection; import org.scale7.cassandra.pelops.Pelops; +import org.scale7.cassandra.pelops.exceptions.TransportException; +import org.scale7.cassandra.pelops.pool.CommonsBackedPool; +import org.scale7.cassandra.pelops.pool.IThriftPool; import org.scale7.cassandra.pelops.pool.CommonsBackedPool.Policy; +import org.scale7.cassandra.pelops.pool.IThriftPool.IPooledConnection; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.collect.Lists; +import com.impetus.client.cassandra.common.CassandraClientFactory; import com.impetus.client.cassandra.config.CassandraPropertyReader; import com.impetus.client.cassandra.query.CassandraEntityReader; import com.impetus.client.cassandra.schemamanager.CassandraSchemaManager; +import com.impetus.client.cassandra.service.CassandraHost; +import com.impetus.client.cassandra.service.CassandraHostConfiguration; +import com.impetus.client.cassandra.service.CassandraRetryService; import com.impetus.kundera.PersistenceProperties; import com.impetus.kundera.client.Client; import com.impetus.kundera.configure.schema.api.SchemaManager; import com.impetus.kundera.loader.GenericClientFactory; +import com.impetus.kundera.metadata.KunderaMetadataManager; import com.impetus.kundera.metadata.model.KunderaMetadata; import com.impetus.kundera.metadata.model.PersistenceUnitMetadata; +import com.impetus.kundera.service.Host; +import com.impetus.kundera.service.HostConfiguration; +import com.impetus.kundera.service.policy.LoadBalancingPolicy; +import com.impetus.kundera.service.policy.RoundRobinBalancingPolicy; /** * A factory for creating PelopsCliobjects. */ -public class PelopsClientFactory extends GenericClientFactory +public class PelopsClientFactory extends GenericClientFactory implements CassandraClientFactory { /** The logger. */ private static Logger logger = LoggerFactory.getLogger(PelopsClientFactory.class); + protected HostConfiguration configuration; + @Override public void initialize(Map externalProperty) { reader = new CassandraEntityReader(); initializePropertyReader(); setExternalProperties(externalProperty); + configuration = new CassandraHostConfiguration(externalProperties, CassandraPropertyReader.csmd, + getPersistenceUnit()); + hostRetryService = new CassandraRetryService(configuration, this); } @Override protected Object createPoolOrConnection() { + logger.info("Creating pool"); PersistenceUnitMetadata persistenceUnitMetadata = KunderaMetadata.INSTANCE.getApplicationMetadata() .getPersistenceUnitMetadata(getPersistenceUnit()); Properties props = persistenceUnitMetadata.getProperties(); - String contactNodes = null; - String defaultPort = null; String keyspace = null; if (externalProperties != null) { - contactNodes = (String) externalProperties.get(PersistenceProperties.KUNDERA_NODES); - defaultPort = (String) externalProperties.get(PersistenceProperties.KUNDERA_PORT); keyspace = (String) externalProperties.get(PersistenceProperties.KUNDERA_KEYSPACE); } - - if (contactNodes == null) - { - contactNodes = (String) props.get(PersistenceProperties.KUNDERA_NODES); - } - if (defaultPort == null) - { - defaultPort = (String) props.get(PersistenceProperties.KUNDERA_PORT); - } if (keyspace == null) { keyspace = (String) props.get(PersistenceProperties.KUNDERA_KEYSPACE); } - String poolName = PelopsUtils.generatePoolName(getPersistenceUnit(), externalProperties); - if (Pelops.getDbConnPool(poolName) == null) + for (Host host : ((CassandraHostConfiguration) configuration).getCassandraHosts()) { - onValidation(contactNodes, defaultPort); + CassandraHost cassandraHost = (CassandraHost) host; + String poolName = PelopsUtils.generatePoolName(getPersistenceUnit(), externalProperties); - Cluster cluster = new Cluster(contactNodes, new IConnection.Config(Integer.parseInt(defaultPort), true, -1, - PelopsUtils.getAuthenticationRequest(props)), false); + if (Pelops.getDbConnPool(poolName) == null) + { + try + { + Cluster cluster = new Cluster(cassandraHost.getHost(), new IConnection.Config( + cassandraHost.getPort(), true, -1, PelopsUtils.getAuthenticationRequest(props)), false); - Policy policy = PelopsUtils.getPoolConfigPolicy(persistenceUnitMetadata, externalProperties); - - // Add pool with specified policy. null means default operand - // policy. - Pelops.addPool(poolName, cluster, keyspace, policy, null); + Policy policy = PelopsUtils.getPoolConfigPolicy(persistenceUnitMetadata, externalProperties); + // Add pool with specified policy. null means default + // operand + // policy. + Pelops.addPool(poolName, cluster, keyspace, policy, null); + hostPools.put(cassandraHost, Pelops.getDbConnPool(poolName)); + } + catch (TransportException e) + { + logger.warn("Node " + host.getHost() + " are down"); + if (host.isRetryHost()) + { + logger.info("Scheduling node for future retry"); + ((CassandraRetryService) hostRetryService).add((CassandraHost) host); + } + } + } } // TODO return a thrift pool return null; @@ -103,7 +136,8 @@ protected Object createPoolOrConnection() @Override protected Client instantiateClient(String persistenceUnit) { - return new PelopsClient(indexManager, reader, persistenceUnit, externalProperties); + ConnectionPool pool = getPoolUsingPolicy(); + return new PelopsClient(indexManager, reader, this, persistenceUnit, externalProperties); } @Override @@ -158,7 +192,118 @@ private void initializePropertyReader() @Override protected void initializeLoadBalancer(String loadBalancingPolicyName) { - throw new UnsupportedOperationException("Load balancing feature is not supported in " - + this.getClass().getSimpleName()); + if (loadBalancingPolicyName != null) + { + switch (LoadBalancer.getValue(loadBalancingPolicyName)) + { + case ROUNDROBIN: + loadBalancingPolicy = new RoundRobinBalancingPolicy(); + break; + case LEASTACTIVE: + loadBalancingPolicy = new PelopsLeastActiveBalancingPolcy(); + break; + default: + loadBalancingPolicy = new RoundRobinBalancingPolicy(); + break; + } + } + loadBalancingPolicy = new RoundRobinBalancingPolicy(); + } + + /** + * + * @param host + * @param port + * @return + */ + private ConnectionPool getNewPool(String host, int port) + { + CassandraHost cassandraHost = ((CassandraHostConfiguration) configuration).getCassandraHost(host, port); + hostPools.remove(cassandraHost); + return getPoolUsingPolicy(); + } + + /** + * + * @return pool an the basis of LoadBalancing policy. + */ + private ConnectionPool getPoolUsingPolicy() + { + ConnectionPool pool = null; + if (!hostPools.isEmpty()) + { + pool = (ConnectionPool) loadBalancingPolicy.getPool(hostPools.values()); + } + return pool; + } + + IPooledConnection getConnection() + { + return Pelops.getDbConnPool(PelopsUtils.generatePoolName(getPersistenceUnit(), externalProperties)) + .getConnection(); + } + + void releaseConnection(IPooledConnection conn) + { + if (conn != null) + { + conn.release(); + } + } + + @Override + public boolean addCassandraHost(CassandraHost cassandraHost) + { + String keysapce = KunderaMetadataManager.getPersistenceUnitMetadata(getPersistenceUnit()).getProperties() + .getProperty(PersistenceProperties.KUNDERA_KEYSPACE); + PoolConfiguration prop = new PoolProperties(); + prop.setHost(cassandraHost.getHost()); + prop.setPort(cassandraHost.getPort()); + prop.setKeySpace(keysapce); + + PelopsUtils.setPoolConfigPolicy(cassandraHost, prop); + try + { + ConnectionPool pool = new ConnectionPool(prop); + hostPools.put(cassandraHost, pool); + return true; + } + catch (TException e) + { + logger.warn("Node " + cassandraHost.getHost() + " are still down"); + return false; + } + } + + private class PelopsLeastActiveBalancingPolcy implements LoadBalancingPolicy + { + + /** + * + * @return pool object for host which has least active connections + * determined by maxActive connection. + * + */ + @Override + public Object getPool(Collection pools) + { + List vals = Lists.newArrayList(pools); + Collections.shuffle(vals); + Collections.sort(vals, new ShufflingCompare()); + Iterator iterator = vals.iterator(); + Object concurrentConnectionPool = iterator.next(); + return concurrentConnectionPool; + } + + private final class ShufflingCompare implements Comparator + { + public int compare(Object o1, Object o2) + { + Policy policy1 = ((CommonsBackedPool) ((IThriftPool) o1)).getPolicy(); + Policy policy2 = ((CommonsBackedPool) ((IThriftPool) o2)).getPolicy(); + + return policy1.getMaxActivePerNode() - policy2.getMaxActivePerNode(); + } + } } } \ No newline at end of file diff --git a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsDataHandler.java b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsDataHandler.java index 4cf790328..4d6a2fc37 100644 --- a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsDataHandler.java +++ b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsDataHandler.java @@ -65,7 +65,7 @@ public PelopsDataHandler(Map externalProperties) @Override public Object fromThriftRow(Class clazz, EntityMetadata m, Object rowKey, List relationNames, - boolean isWrapReq, ConsistencyLevel consistencyLevel, boolean isCql3Enabled) throws Exception + boolean isWrapReq, ConsistencyLevel consistencyLevel, boolean isCql3Enabled, Object conn) throws Exception { Selector selector = Pelops .createSelector(PelopsUtils.generatePoolName(m.getPersistenceUnit(), externalProperty)); @@ -96,9 +96,17 @@ public Object fromThriftRow(Class clazz, EntityMetadata m, Object rowKey, Lis @Override public List fromThriftRow(Class clazz, EntityMetadata m, List relationNames, boolean isWrapReq, - ConsistencyLevel consistencyLevel, boolean isCql3Enabled, Object... rowIds) throws Exception + ConsistencyLevel consistencyLevel, boolean isCql3Enabled, Object conn, Object... rowIds) throws Exception { - return super.fromThriftRow(clazz, m, relationNames, isWrapReq, consistencyLevel,isCql3Enabled, rowIds); + try + { + return super.fromThriftRow(clazz, m, relationNames, isWrapReq, consistencyLevel, isCql3Enabled, conn, + rowIds); + } + finally + { + + } } @Override diff --git a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsInvertedIndexHandler.java b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsInvertedIndexHandler.java index f9b370d43..d855362a1 100644 --- a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsInvertedIndexHandler.java +++ b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsInvertedIndexHandler.java @@ -65,7 +65,7 @@ public PelopsInvertedIndexHandler(Map externalProperties) @Override public void write(Node node, EntityMetadata entityMetadata, String persistenceUnit, - ConsistencyLevel consistencyLevel, CassandraDataHandler cdHandler) + ConsistencyLevel consistencyLevel, CassandraDataHandler cdHandler, Object conn) { // Index in Inverted Index table if applicable boolean invertedIndexingApplicable = CassandraIndexHelper.isInvertedIndexingApplicable(entityMetadata); @@ -116,9 +116,9 @@ public void write(Node node, EntityMetadata entityMetadata, String persistenceUn */ @Override public List search(EntityMetadata m, String persistenceUnit, ConsistencyLevel consistencyLevel, - Map> indexClauseMap) + Map> indexClauseMap, Object conn) { - return super.search(m, persistenceUnit, consistencyLevel, indexClauseMap); + return super.search(m, persistenceUnit, consistencyLevel, indexClauseMap, conn); } /** @@ -137,7 +137,7 @@ public List search(EntityMetadata m, String persistenceUnit, Consi @Override public void searchSuperColumnsInRange(String columnFamilyName, ConsistencyLevel consistencyLevel, String persistenceUnit, String rowKey, byte[] searchSuperColumnName, List thriftSuperColumns, - byte[] start, byte[] finish) + byte[] start, byte[] finish, Object conn) { SlicePredicate colPredicate = new SlicePredicate(); SliceRange sliceRange = new SliceRange(); @@ -168,14 +168,14 @@ public void searchSuperColumnsInRange(String columnFamilyName, ConsistencyLevel */ @Override - public void delete(Object entity, EntityMetadata metadata, ConsistencyLevel consistencyLevel) + public void delete(Object entity, EntityMetadata metadata, ConsistencyLevel consistencyLevel, Object conn) { - super.delete(entity, metadata, consistencyLevel); + super.delete(entity, metadata, consistencyLevel, conn); } @Override public SuperColumn getSuperColumnForRow(ConsistencyLevel consistencyLevel, String columnFamilyName, String rowKey, - byte[] superColumnName, String persistenceUnit) + byte[] superColumnName, String persistenceUnit, Object conn) { Selector selector = Pelops.createSelector(PelopsUtils.generatePoolName(persistenceUnit, externalProperty)); SuperColumn thriftSuperColumn = null; @@ -205,7 +205,7 @@ public SuperColumn getSuperColumnForRow(ConsistencyLevel consistencyLevel, Strin * @param mutator */ public void deleteColumn(String indexColumnFamily, String rowKey, byte[] superColumnName, String persistenceUnit, - ConsistencyLevel consistencyLevel, byte[] columnName) + ConsistencyLevel consistencyLevel, byte[] columnName, Object conn) { Mutator mutator = Pelops.createMutator(PelopsUtils.generatePoolName(persistenceUnit, externalProperty)); mutator.deleteColumn(indexColumnFamily, rowKey, Bytes.fromByteArray(superColumnName)); diff --git a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsUtils.java b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsUtils.java index a5d25247d..9854d2b8b 100644 --- a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsUtils.java +++ b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/pelops/PelopsUtils.java @@ -18,19 +18,12 @@ import java.util.Map; import java.util.Properties; -import javax.persistence.PersistenceException; - -import net.dataforte.cassandra.pool.ConnectionPool; import net.dataforte.cassandra.pool.HostFailoverPolicy; import net.dataforte.cassandra.pool.PoolConfiguration; -import org.apache.cassandra.thrift.Cassandra; import org.apache.commons.lang.StringUtils; -import org.apache.thrift.TException; -import org.scale7.cassandra.pelops.Pelops; import org.scale7.cassandra.pelops.SimpleConnectionAuthenticator; import org.scale7.cassandra.pelops.pool.CommonsBackedPool.Policy; -import org.scale7.cassandra.pelops.pool.IThriftPool.IPooledConnection; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -82,7 +75,20 @@ public static String generatePoolName(String persistenceUnit, Map 0) { prop.setInitialSize(maxActivePerNode); + prop.setMaxActive(maxActivePerNode); } if (maxIdlePerNode > 0) { @@ -226,49 +233,4 @@ public static SimpleConnectionAuthenticator getAuthenticationRequest(Properties } return authenticator; } - - /** - * Returns instance of {@link IPooledConnection} for a given persistence - * unit - * - * @param persistenceUnit - * @param puProperties - * @return - */ - public static IPooledConnection getCassandraConnection(String persistenceUnit, Map puProperties) - { - return Pelops.getDbConnPool(generatePoolName(persistenceUnit, puProperties)).getConnection(); - } - - public static void releaseConnection(IPooledConnection conn) - { - if (conn != null) - { - conn.release(); - } - } - - public static Cassandra.Client getCassandraConnection(ConnectionPool pool) - { - try - { - if (pool != null) - { - return pool.getConnection(); - } - } - catch (TException te) - { - throw new PersistenceException(te); - } - return null; - } - - public static void releaseConnection(ConnectionPool pool, Cassandra.Client conn) - { - if (pool != null && conn != null) - { - pool.release(conn); - } - } } diff --git a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/schemamanager/CassandraSchemaManager.java b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/schemamanager/CassandraSchemaManager.java index 5ccf8b41e..4e48f43ac 100644 --- a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/schemamanager/CassandraSchemaManager.java +++ b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/schemamanager/CassandraSchemaManager.java @@ -385,14 +385,14 @@ protected void validate(List tableInfos) */ protected boolean initiateClient() { - if (host == null || !StringUtils.isNumeric(port) || port.isEmpty()) - { - log.error("Host or port should not be null, Port should be numeric."); - throw new IllegalArgumentException("Host or port should not be null, Port should be numeric."); - } - - if (cassandra_client == null) + String message = null; + for (String host : hosts) { + if (host == null || !StringUtils.isNumeric(port) || port.isEmpty()) + { + log.error("Host or port should not be null, Port should be numeric."); + throw new IllegalArgumentException("Host or port should not be null, Port should be numeric."); + } TSocket socket = new TSocket(host, Integer.parseInt(port)); TTransport transport = new TFramedTransport(socket); TProtocol protocol = new TBinaryProtocol(transport); @@ -403,19 +403,15 @@ protected boolean initiateClient() { socket.open(); } + return true; } catch (TTransportException e) { - log.error("Error while opening socket, Caused by: ", e); - throw new SchemaGenerationException(e, "Cassandra"); - } - catch (NumberFormatException e) - { - log.error("Error during creating schema in cassandra, Caused by: ", e); - throw new SchemaGenerationException(e, "Cassandra"); + message = e.getMessage(); + log.error("Error while opening socket for host { }, Caused by: ", host, e); } } - return cassandra_client != null ? true : false; + throw new SchemaGenerationException("Error while opening socket, Caused by: " + message); } /** @@ -589,10 +585,11 @@ private void onCompoundKey(TableInfo tableInfo) catch (InvalidRequestException ire) { StringBuilder builder = new StringBuilder("Cannot add already existing column family "); -// translator.ensureCase(builder, databaseName); -// builder.append(" to keyspace "); -// translator.ensureCase(builder, tableInfo.getTableName()); - if (ire.getWhy() != null && ire.getWhy().contains(builder.toString()) && operation.equalsIgnoreCase(ScheamOperationType.update.name())) + // translator.ensureCase(builder, databaseName); + // builder.append(" to keyspace "); + // translator.ensureCase(builder, tableInfo.getTableName()); + if (ire.getWhy() != null && ire.getWhy().contains(builder.toString()) + && operation.equalsIgnoreCase(ScheamOperationType.update.name())) { for (ColumnInfo column : tableInfo.getColumnMetadatas()) { @@ -626,8 +623,8 @@ else if (ire.getWhy() != null && ire.getWhy().equals(builder.toString())) throw new SchemaGenerationException("Error occurred while creating table " + tableInfo.getTableName(), e, "Cassandra", databaseName); } - - // After successful schema operation, perform index creation. + + // After successful schema operation, perform index creation. createIndex(tableInfo); } @@ -640,18 +637,20 @@ private void createIndex(TableInfo tableInfo) { StringBuilder indexQueryBuilder = new StringBuilder("create index $COLUMN_NAME on \""); indexQueryBuilder.append(tableInfo.getTableName()); - indexQueryBuilder.append( "\"(\"$COLUMN_NAME\")"); + indexQueryBuilder.append("\"(\"$COLUMN_NAME\")"); for (IndexInfo indexInfo : tableInfo.getColumnsToBeIndexed()) { - String replacedWithindexName = StringUtils.replace(indexQueryBuilder.toString(), "$COLUMN_NAME", indexInfo.getColumnName()); + String replacedWithindexName = StringUtils.replace(indexQueryBuilder.toString(), "$COLUMN_NAME", + indexInfo.getColumnName()); try { - cassandra_client.execute_cql3_query(ByteBuffer.wrap(replacedWithindexName.getBytes()), Compression.NONE, - ConsistencyLevel.ONE); + cassandra_client.execute_cql3_query(ByteBuffer.wrap(replacedWithindexName.getBytes()), + Compression.NONE, ConsistencyLevel.ONE); } catch (InvalidRequestException ire) { - if (ire.getWhy() != null && !ire.getWhy().equals("Index already exists") && operation.equalsIgnoreCase(ScheamOperationType.update.name())) + if (ire.getWhy() != null && !ire.getWhy().equals("Index already exists") + && operation.equalsIgnoreCase(ScheamOperationType.update.name())) { onLogException(tableInfo, indexInfo, ire); } @@ -677,9 +676,10 @@ private void createIndex(TableInfo tableInfo) private void onLogException(TableInfo tableInfo, IndexInfo indexInfo, Exception ire) { - log.error("Error occurred while creating indexes on column "+ indexInfo.getColumnName() + " of table " + tableInfo.getTableName() + ", Caused by: ", ire); - throw new SchemaGenerationException("Error occurred while creating indexes on column "+ indexInfo.getColumnName() + " of table " + tableInfo.getTableName(), ire, - "Cassandra", databaseName); + log.error("Error occurred while creating indexes on column " + indexInfo.getColumnName() + " of table " + + tableInfo.getTableName() + ", Caused by: ", ire); + throw new SchemaGenerationException("Error occurred while creating indexes on column " + + indexInfo.getColumnName() + " of table " + tableInfo.getTableName(), ire, "Cassandra", databaseName); } /** @@ -1577,11 +1577,6 @@ private boolean validateEmbeddedColumns(EntityMetadata metadata, Collection downedHost; - - public CassandraHostConfigurator(Map externalProperties, CassandraSchemaMetadata csmd, String persistenceUnit) - { - super(externalProperties, csmd != null ? csmd.getConnectionServers() : new ArrayList(), persistenceUnit); - String property = csmd.getConnectionProperties().getProperty(Constants.RETRY_DELAY); - if (StringUtils.isNumeric(property)) - { - retryDelay = Integer.parseInt(property); - } - downedHost = new HashSet(); - } - - protected CassandraHost[] buildHosts(List servers) - { - CassandraHost[] cassandraHosts = new CassandraHost[servers.size()]; - for (int i = 0; i < servers.size(); i++) - { - Server server = servers.get(i); - String host = server.getHost(); - String portAsString = server.getPort(); - onValidation(host, portAsString); - Properties serverProperties = server.getProperties(); - CassandraHost cassandraHost = new CassandraHost(host, Integer.parseInt(portAsString), serverProperties); - setConfig(cassandraHost, serverProperties, null); - cassandraHosts[i] = cassandraHost; - } - return cassandraHosts; - } - - protected CassandraHost[] buildHosts(String hosts, String portAsString) - { - String[] hostVals = hosts.split(","); - CassandraHost[] cassandraHosts = new CassandraHost[hostVals.length]; - for (int x = 0; x < hostVals.length; x++) - { - String host = hostVals[x].trim(); - onValidation(host, portAsString); - int port = Integer.parseInt(portAsString); - CassandraHost cassandraHost = port == CassandraHost.DEFAULT_PORT ? new CassandraHost(host) - : new CassandraHost(host, port); - setConfig(cassandraHost, persistenceUnitMetadata.getProperties(), externalProperties); - cassandraHosts[x] = cassandraHost; - } - return cassandraHosts; - } - - @Override - protected void setConfig(Host host, Properties props, Map puProperties) - { - CassandraHost cassandraHost = (CassandraHost) host; - String maxActivePerNode = null; - String maxIdlePerNode = null; - String minIdlePerNode = null; - String maxTotal = null; - String testOnBorrow = null; - String testWhileIdle = null; - String testOnConnect = null; - String testOnReturn = null; - String socketTimeOut = null; - if (puProperties != null) - { - maxActivePerNode = (String) puProperties.get(PersistenceProperties.KUNDERA_POOL_SIZE_MAX_ACTIVE); - maxIdlePerNode = (String) puProperties.get(PersistenceProperties.KUNDERA_POOL_SIZE_MAX_IDLE); - minIdlePerNode = (String) puProperties.get(PersistenceProperties.KUNDERA_POOL_SIZE_MIN_IDLE); - maxTotal = (String) puProperties.get(PersistenceProperties.KUNDERA_POOL_SIZE_MAX_TOTAL); - testOnBorrow = (String) puProperties.get(CassandraConstants.TEST_ON_BORROW); - testOnConnect = (String) puProperties.get(CassandraConstants.TEST_ON_CONNECT); - testOnReturn = (String) puProperties.get(CassandraConstants.TEST_ON_RETURN); - testWhileIdle = (String) puProperties.get(CassandraConstants.TEST_WHILE_IDLE); - socketTimeOut = (String) puProperties.get(CassandraConstants.SOCKET_TIMEOUT); - } - - if (maxActivePerNode == null) - { - maxActivePerNode = props.getProperty(PersistenceProperties.KUNDERA_POOL_SIZE_MAX_ACTIVE); - } - if (maxIdlePerNode == null) - { - maxIdlePerNode = props.getProperty(PersistenceProperties.KUNDERA_POOL_SIZE_MAX_IDLE); - } - if (minIdlePerNode == null) - { - minIdlePerNode = props.getProperty(PersistenceProperties.KUNDERA_POOL_SIZE_MIN_IDLE); - } - if (maxTotal == null) - { - maxTotal = props.getProperty(PersistenceProperties.KUNDERA_POOL_SIZE_MAX_TOTAL); - } - try - { - if (!StringUtils.isEmpty(maxActivePerNode)) - { - cassandraHost.setInitialSize(Integer.parseInt(maxActivePerNode)); - } - - if (!StringUtils.isEmpty(maxIdlePerNode)) - { - cassandraHost.setMaxIdle(Integer.parseInt(maxIdlePerNode)); - } - - if (!StringUtils.isEmpty(minIdlePerNode)) - { - cassandraHost.setMinIdle(Integer.parseInt(minIdlePerNode)); - } - - if (!StringUtils.isEmpty(maxTotal)) - { - cassandraHost.setMaxActive(Integer.parseInt(maxTotal)); - } - - if (testOnBorrow == null) - { - testOnBorrow = props.getProperty(CassandraConstants.TEST_ON_BORROW); - } - if (testOnConnect == null) - { - testOnConnect = props.getProperty(CassandraConstants.TEST_ON_CONNECT); - } - if (testOnReturn == null) - { - testOnReturn = props.getProperty(CassandraConstants.TEST_ON_RETURN); - } - if (testWhileIdle == null) - { - testWhileIdle = props.getProperty(CassandraConstants.TEST_WHILE_IDLE); - } - if (socketTimeOut == null) - { - socketTimeOut = props.getProperty(CassandraConstants.SOCKET_TIMEOUT); - } - - cassandraHost.setTestOnBorrow(Boolean.parseBoolean(testOnBorrow)); - cassandraHost.setTestOnConnect(Boolean.parseBoolean(testOnConnect)); - cassandraHost.setTestOnReturn(Boolean.parseBoolean(testOnReturn)); - cassandraHost.setTestWhileIdle(Boolean.parseBoolean(testWhileIdle)); - cassandraHost.setHostFailoverPolicy(getFailoverPolicy(props.getProperty(Constants.FAILOVER_POLICY))); - cassandraHost.setRetryHost(Boolean.parseBoolean(props.getProperty(Constants.RETRY))); - - if (!StringUtils.isEmpty(socketTimeOut)) - { - cassandraHost.setSocketTimeout(Integer.parseInt(socketTimeOut)); - } - else - { - cassandraHost.setSocketTimeout(CassandraHost.DEFAULT_SHOCKET_TIMEOUT); - } - - } - catch (NumberFormatException e) - { - logger.warn("Some Connection pool related property couldn't be parsed. Default pool policy would be used"); - cassandraHost = null; - } - } - - public CassandraHost[] getCassandraHosts() - { - return (CassandraHost[]) hosts; - } - - private HostFailoverPolicy getFailoverPolicy(String failoverOption) - { - if (failoverOption != null) - { - if (Constants.FAIL_FAST.equals(failoverOption)) - { - return HostFailoverPolicy.FAIL_FAST; - } - else if (Constants.ON_FAIL_TRY_ALL_AVAILABLE.equals(failoverOption)) - { - return HostFailoverPolicy.ON_FAIL_TRY_ALL_AVAILABLE; - } - else if (Constants.ON_FAIL_TRY_ONE_NEXT_AVAILABLE.equals(failoverOption)) - { - return HostFailoverPolicy.ON_FAIL_TRY_ONE_NEXT_AVAILABLE; - } - else - { - logger.warn("Not provided valid failover policy, using default"); - return HostFailoverPolicy.ON_FAIL_TRY_ALL_AVAILABLE; - } - } - return HostFailoverPolicy.ON_FAIL_TRY_ALL_AVAILABLE; - } - - public Set getDownedHost() - { - return downedHost; - } - - public void addHostToDownedHost(CassandraHost cassandraHost) - { - downedHost.add(cassandraHost); - } - - public int getRetryDelay() - { - return retryDelay; - } - - public CassandraHost getCassandraHost(String host, int port) - { - for (Host cassandraHost : hosts) - { - if (((CassandraHost) cassandraHost).equals(new CassandraHost(host, port))) - { - return (CassandraHost) cassandraHost; - } - } - return null; - } -} diff --git a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/service/ThriftLeastActiveBalancingPolcy.java b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/service/ThriftLeastActiveBalancingPolcy.java deleted file mode 100644 index 69b06b1e1..000000000 --- a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/service/ThriftLeastActiveBalancingPolcy.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.impetus.client.cassandra.service; - -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - -import net.dataforte.cassandra.pool.ConnectionPool; -import net.dataforte.cassandra.pool.PoolConfiguration; - -import com.google.common.collect.Lists; -import com.impetus.kundera.service.policy.LeastActiveBalancingPolicy; - -public class ThriftLeastActiveBalancingPolcy extends LeastActiveBalancingPolicy -{ - - @Override - public Object getPool(Collection pools, Set excludeHosts) - { - List vals = Lists.newArrayList(pools); - Collections.shuffle(vals); - Collections.sort(vals, new ShufflingCompare()); - Iterator iterator = vals.iterator(); - Object concurrentConnectionPool = iterator.next(); - return concurrentConnectionPool; - } - - private final class ShufflingCompare implements Comparator - { - public int compare(Object o1, Object o2) - { - PoolConfiguration props1 = ((ConnectionPool) o1).getPoolProperties(); - PoolConfiguration props2 = ((ConnectionPool) o2).getPoolProperties(); - return props1.getMaxActive() | -props2.getMaxActive(); - } - } -} diff --git a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/thrift/ThriftClient.java b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/thrift/ThriftClient.java index dc3a1c887..db4cf6dd1 100644 --- a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/thrift/ThriftClient.java +++ b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/thrift/ThriftClient.java @@ -105,10 +105,10 @@ public class ThriftClient extends CassandraClientBase implements Client externalProperties) { @@ -119,6 +119,7 @@ public ThriftClient(ThriftClientFactory clientFactory, IndexManager indexManager this.dataHandler = new ThriftDataHandler(pool); this.invertedIndexHandler = new ThriftInvertedIndexHandler(pool); this.reader = reader; + this.clientFactory = clientFactory; this.pool = pool; } @@ -143,8 +144,6 @@ protected void onPersist(EntityMetadata entityMetadata, Object entity, Object id // if entity is embeddable...call cql translator to get cql string! // use thrift client to execute cql query. - MetamodelImpl metaModel = (MetamodelImpl) KunderaMetadata.INSTANCE.getApplicationMetadata().getMetamodel( - entityMetadata.getPersistenceUnit()); if (isCql3Enabled(entityMetadata)) { @@ -155,7 +154,6 @@ protected void onPersist(EntityMetadata entityMetadata, Object entity, Object id Map>> mutationMap = new HashMap>>(); prepareMutation(entityMetadata, entity, id, rlHolders, mutationMap); // Write Mutation map to database - // conn.set_cql_version("3.0.0"); conn.batch_mutate(mutationMap, getConsistencyLevel()); mutationMap.clear(); @@ -195,7 +193,8 @@ protected void onPersist(EntityMetadata entityMetadata, Object entity, Object id } finally { - PelopsUtils.releaseConnection(pool, conn); + // PelopsUtils.releaseConnection(pool, conn); + releaseConnection(conn); } } @@ -285,7 +284,8 @@ public void persistJoinTable(JoinTableData joinTableData) } finally { - PelopsUtils.releaseConnection(pool, conn); + // PelopsUtils.releaseConnection(pool, conn); + releaseConnection(conn); } } @@ -299,7 +299,17 @@ protected void indexNode(Node node, EntityMetadata entityMetadata) // Write to inverted index table if applicable // setCassandraClient(); - invertedIndexHandler.write(node, entityMetadata, getPersistenceUnit(), getConsistencyLevel(), dataHandler); + + Cassandra.Client conn = getPooledConection(getPersistenceUnit()); + try + { + invertedIndexHandler.write(node, entityMetadata, getPersistenceUnit(), getConsistencyLevel(), dataHandler, + conn); + } + finally + { + releaseConnection(conn); + } } /** @@ -333,16 +343,21 @@ public final List find(Class entityClass, List relationNames, boolean is } List entities = null; + Cassandra.Client conn = getPooledConection(getPersistenceUnit()); try { entities = dataHandler.fromThriftRow(entityClass, metadata, relationNames, isWrapReq, - getConsistencyLevel(), isCql3Enabled(metadata), rowIds); + getConsistencyLevel(), isCql3Enabled(metadata), conn, rowIds); } catch (Exception e) { throw new KunderaException(e); } + finally + { + releaseConnection(conn); + } return entities; } @@ -382,7 +397,6 @@ protected final List loadSuperColumns(String keyspace, String colum try { conn = /* PelopsUtils.getCassandraConnection(pool) */getPooledConection(persistenceUnit); - coscList = conn.get_slice(ByteBuffer.wrap(rowKey), parent, predicate, getConsistencyLevel()); } @@ -408,7 +422,8 @@ protected final List loadSuperColumns(String keyspace, String colum } finally { - PelopsUtils.releaseConnection(pool, conn); + // PelopsUtils.releaseConnection(pool, conn); + releaseConnection(conn); } List superColumns = ThriftDataResultHelper.transformThriftResult(coscList, @@ -461,7 +476,8 @@ public List getColumnsById(String schemaName, String tableName, String pK } finally { - PelopsUtils.releaseConnection(pool, conn); + // PelopsUtils.releaseConnection(pool, conn); + releaseConnection(conn); } List columns = ThriftDataResultHelper.transformThriftResult(results, ColumnFamilyType.COLUMN, null); @@ -528,7 +544,8 @@ public Object[] findIdsByColumn(String schemaName, String tableName, String pKey } finally { - PelopsUtils.releaseConnection(pool, conn); + // PelopsUtils.releaseConnection(pool, conn); + releaseConnection(conn); } if (rowKeys != null && !rowKeys.isEmpty()) { @@ -599,7 +616,8 @@ public List findByRelation(String colName, Object colValue, Class entity } finally { - PelopsUtils.releaseConnection(pool, conn); + // PelopsUtils.releaseConnection(pool, conn); + releaseConnection(conn); } if (keySlices != null) @@ -624,7 +642,6 @@ public void delete(Object entity, Object pKey) try { conn = /* PelopsUtils.getCassandraConnection(pool) */getPooledConection(persistenceUnit); - MetamodelImpl metaModel = (MetamodelImpl) KunderaMetadata.INSTANCE.getApplicationMetadata().getMetamodel( metadata.getPersistenceUnit()); @@ -648,6 +665,12 @@ public void delete(Object entity, Object pKey) getConsistencyLevel()); } } + + // Delete from Lucene if applicable + getIndexManager().remove(metadata, entity, pKey.toString()); + + // Delete from Inverted Index if applicable + invertedIndexHandler.delete(entity, metadata, getConsistencyLevel(), conn); } catch (InvalidRequestException e) { @@ -671,14 +694,9 @@ public void delete(Object entity, Object pKey) } finally { - PelopsUtils.releaseConnection(pool, conn); + // PelopsUtils.releaseConnection(pool, conn); + releaseConnection(conn); } - - // Delete from Lucene if applicable - getIndexManager().remove(metadata, entity, pKey.toString()); - - // Delete from Inverted Index if applicable - invertedIndexHandler.delete(entity, metadata, getConsistencyLevel()); } @Override @@ -698,7 +716,6 @@ public void deleteByColumn(String schemaName, String tableName, String columnNam try { conn = /* PelopsUtils.getCassandraConnection(pool) */getPooledConection(persistenceUnit); - ColumnPath path = new ColumnPath(tableName); // cassandra_client.remove(ByteBuffer.wrap(columnValue.toString().getBytes()), // path, @@ -729,7 +746,8 @@ public void deleteByColumn(String schemaName, String tableName, String columnNam } finally { - PelopsUtils.releaseConnection(pool, conn); + // PelopsUtils.releaseConnection(pool, conn); + releaseConnection(conn); } } @@ -923,7 +941,8 @@ public List find(List ixClause, EntityMetadata m, boolean isRelatio } finally { - PelopsUtils.releaseConnection(pool, conn); + // PelopsUtils.releaseConnection(pool, conn); + releaseConnection(conn); } return entities; } @@ -976,7 +995,8 @@ public List findByRange(byte[] minVal, byte[] maxVal, EntityMetadata m, boolean List keys = conn.get_range_slices(cp, slicePredicate, keyRange, getConsistencyLevel()); - PelopsUtils.releaseConnection(pool, conn); + // PelopsUtils.releaseConnection(pool, conn); + releaseConnection(conn); List results = null; if (keys != null) @@ -990,7 +1010,15 @@ public List findByRange(byte[] minVal, byte[] maxVal, EntityMetadata m, boolean public List searchInInvertedIndex(String columnFamilyName, EntityMetadata m, Map> indexClauseMap) { - return invertedIndexHandler.search(m, getPersistenceUnit(), getConsistencyLevel(), indexClauseMap); + Cassandra.Client conn = getPooledConection(getPersistenceUnit()); + try + { + return invertedIndexHandler.search(m, getPersistenceUnit(), getConsistencyLevel(), indexClauseMap, conn); + } + finally + { + releaseConnection(conn); + } } /* @@ -1006,33 +1034,13 @@ protected CassandraDataHandler getDataHandler() protected Cassandra.Client getPooledConection(String persistenceUnit) { - boolean success = false; - TException message = null; - while (!success) - { - try - { - if (pool != null) - { - success = true; - return pool.getConnection(); - } - } - catch (TException te) - { - success = false; - message = te; - log.error("Host is appeared to be down "); - pool = clientFactory.getOtherPoolIfAvailable(pool.getPoolProperties().getHost(), pool - .getPoolProperties().getPort()); - } - } - throw new PersistenceException("All hosts are down caused by : ", message); + Cassandra.Client client = clientFactory.getConnection(pool); + return client; } protected void releaseConnection(Object conn) { - PelopsUtils.releaseConnection(this.pool, (Cassandra.Client) conn); + clientFactory.releaseConnection(this.pool, (Cassandra.Client) conn); } @Override diff --git a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/thrift/ThriftClientFactory.java b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/thrift/ThriftClientFactory.java index de987603a..9321e81ab 100644 --- a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/thrift/ThriftClientFactory.java +++ b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/thrift/ThriftClientFactory.java @@ -15,46 +15,45 @@ */ package com.impetus.client.cassandra.thrift; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Properties; -import java.util.Set; -import java.util.concurrent.TimeUnit; import net.dataforte.cassandra.pool.ConnectionPool; import net.dataforte.cassandra.pool.PoolConfiguration; import net.dataforte.cassandra.pool.PoolProperties; import org.apache.cassandra.thrift.Cassandra; -import org.apache.cassandra.thrift.TBinaryProtocol; import org.apache.thrift.TException; -import org.apache.thrift.protocol.TProtocol; -import org.apache.thrift.transport.TFramedTransport; -import org.apache.thrift.transport.TSocket; -import org.apache.thrift.transport.TTransport; -import org.apache.thrift.transport.TTransportException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.collect.Lists; +import com.impetus.client.cassandra.common.CassandraClientFactory; import com.impetus.client.cassandra.config.CassandraPropertyReader; import com.impetus.client.cassandra.pelops.PelopsClientFactory; import com.impetus.client.cassandra.pelops.PelopsUtils; import com.impetus.client.cassandra.query.CassandraEntityReader; import com.impetus.client.cassandra.schemamanager.CassandraSchemaManager; import com.impetus.client.cassandra.service.CassandraHost; -import com.impetus.client.cassandra.service.CassandraHostConfigurator; -import com.impetus.client.cassandra.service.ThriftLeastActiveBalancingPolcy; +import com.impetus.client.cassandra.service.CassandraHostConfiguration; +import com.impetus.client.cassandra.service.CassandraRetryService; import com.impetus.kundera.Constants; import com.impetus.kundera.KunderaException; import com.impetus.kundera.PersistenceProperties; import com.impetus.kundera.client.Client; import com.impetus.kundera.configure.schema.api.SchemaManager; import com.impetus.kundera.loader.GenericClientFactory; +import com.impetus.kundera.metadata.KunderaMetadataManager; import com.impetus.kundera.metadata.model.KunderaMetadata; import com.impetus.kundera.metadata.model.PersistenceUnitMetadata; import com.impetus.kundera.service.Host; import com.impetus.kundera.service.HostConfiguration; -import com.impetus.kundera.service.policy.HostRetryService; +import com.impetus.kundera.service.policy.LeastActiveBalancingPolicy; import com.impetus.kundera.service.policy.RoundRobinBalancingPolicy; /** @@ -64,7 +63,7 @@ * * @author amresh.singh */ -public class ThriftClientFactory extends GenericClientFactory +public class ThriftClientFactory extends GenericClientFactory implements CassandraClientFactory { /** The logger. */ private static Logger logger = LoggerFactory.getLogger(ThriftClientFactory.class); @@ -108,7 +107,7 @@ public void destroy() } schemaManager = null; externalProperties = null; - ((CassandraHostRetryService) hostRetryService).shutdown(); + ((CassandraRetryService) hostRetryService).shutdown(); } @Override @@ -120,9 +119,9 @@ public void initialize(Map externalProperty) String loadBalancingPolicyName = CassandraPropertyReader.csmd != null ? CassandraPropertyReader.csmd .getConnectionProperties().getProperty(Constants.LOADBALANCING_POLICY) : null; initializeLoadBalancer(loadBalancingPolicyName); - configuration = new CassandraHostConfigurator(externalProperties, CassandraPropertyReader.csmd, + configuration = new CassandraHostConfiguration(externalProperties, CassandraPropertyReader.csmd, getPersistenceUnit()); - hostRetryService = new CassandraHostRetryService(); + hostRetryService = new CassandraRetryService(configuration, this); } @Override @@ -143,57 +142,32 @@ protected Object createPoolOrConnection() keyspace = (String) props.get(PersistenceProperties.KUNDERA_KEYSPACE); } - for (CassandraHost cassandraHost : ((CassandraHostConfigurator) configuration).getCassandraHosts()) + for (Host host : ((CassandraHostConfiguration) configuration).getCassandraHosts()) { PoolConfiguration prop = new PoolProperties(); - prop.setHost(cassandraHost.getHost()); - prop.setPort(cassandraHost.getPort()); + prop.setHost(host.getHost()); + prop.setPort(host.getPort()); prop.setKeySpace(keyspace); - PelopsUtils.setPoolConfigPolicy(cassandraHost, prop); + PelopsUtils.setPoolConfigPolicy((CassandraHost) host, prop); try { ConnectionPool pool = new ConnectionPool(prop); - hostPools.put(cassandraHost, pool); + hostPools.put(host, pool); } catch (TException e) { - logger.warn("Node " + cassandraHost.getHost() + " are down"); - if (cassandraHost.isRetryHost()) + logger.warn("Node " + host.getHost() + " are down"); + if (host.isRetryHost()) { - addHostToDownedHostSet(cassandraHost); + logger.info("Scheduling node for future retry"); + ((CassandraRetryService) hostRetryService).add((CassandraHost) host); } } } return null; } - private void addHostToDownedHostSet(CassandraHost cassandraHost) - { - logger.info("Scheduling node for future retry"); - ((CassandraHostConfigurator) configuration).addHostToDownedHost(cassandraHost); - } - - private boolean addCassandraHost(CassandraHost cassandraHost) - { - PoolConfiguration prop = new PoolProperties(); - prop.setHost(cassandraHost.getHost()); - prop.setPort(cassandraHost.getPort()); - - PelopsUtils.setPoolConfigPolicy(cassandraHost, prop); - try - { - ConnectionPool pool = new ConnectionPool(prop); - hostPools.put(cassandraHost, pool); - return true; - } - catch (TException e) - { - logger.warn("Node " + cassandraHost.getHost() + " are still down"); - return false; - } - } - @Override protected Client instantiateClient(String persistenceUnit) { @@ -205,13 +179,13 @@ protected Client instantiateClient(String persistenceUnit) * * @return pool an the basis of LoadBalancing policy. */ - protected ConnectionPool getPoolUsingPolicy() + private ConnectionPool getPoolUsingPolicy() { - if (hostPools.isEmpty()) + ConnectionPool pool = null; + if (!hostPools.isEmpty()) { - throw new KunderaException("All hosts are down. please check serevers manully."); + pool = (ConnectionPool) loadBalancingPolicy.getPool(hostPools.values()); } - ConnectionPool pool = (ConnectionPool) loadBalancingPolicy.getPool(hostPools.values(), null); return pool; } @@ -221,133 +195,129 @@ public boolean isThreadSafe() return false; } - class CassandraHostRetryService extends HostRetryService + /** + * + * @param host + * @param port + * @return + */ + private ConnectionPool getNewPool(String host, int port) { - private Set downedHost; - - public CassandraHostRetryService() - { - super(((CassandraHostConfigurator) configuration).getRetryDelay()); - sf = executor.scheduleWithFixedDelay(new RetryRunner(), this.retryDelayInSeconds, this.retryDelayInSeconds, - TimeUnit.SECONDS); - } + CassandraHost cassandraHost = ((CassandraHostConfiguration) configuration).getCassandraHost(host, port); + hostPools.remove(cassandraHost); + return getPoolUsingPolicy(); + } - @Override - protected boolean verifyConnection(Host host) + @Override + protected void initializeLoadBalancer(String loadBalancingPolicyName) + { + if (loadBalancingPolicyName != null) { - try - { - TSocket socket = new TSocket(host.getHost(), host.getPort()); - TTransport transport = new TFramedTransport(socket); - TProtocol protocol = new TBinaryProtocol(transport); - Cassandra.Client client = new Cassandra.Client(protocol); - socket.open(); - return client.describe_cluster_name() != null; - } - catch (TTransportException e) - { - logger.warn("Node " + host.getHost() + " are still down"); - return false; - } - catch (TException e) + switch (LoadBalancer.getValue(loadBalancingPolicyName)) { - logger.warn("Node " + host.getHost() + " are still down"); - return false; + case ROUNDROBIN: + loadBalancingPolicy = new RoundRobinBalancingPolicy(); + break; + case LEASTACTIVE: + loadBalancingPolicy = new ThriftLeastActiveBalancingPolcy(); + break; + default: + loadBalancingPolicy = new RoundRobinBalancingPolicy(); + break; } } + loadBalancingPolicy = new RoundRobinBalancingPolicy(); + } - /* - * public void add(final CassandraHost cassandraHost) { - * downedHost.add(cassandraHost); - * - * // schedule a check of this host immediately, executor.submit(new - * Runnable() { - * - * @Override public void run() { if (downedHost.contains(cassandraHost) - * && verifyConnection(cassandraHost)) { if - * (addCassandraHost(cassandraHost)) { downedHost.remove(cassandraHost); - * } return; } } }); } - */ - - class RetryRunner implements Runnable + Cassandra.Client getConnection(ConnectionPool pool) + { + boolean success = false; + while (pool != null && !success) { - - @Override - public void run() + try { - if (!downedHost.isEmpty()) + success = true; + if (pool != null) { - try - { - retryDownedHosts(); - } - catch (Throwable t) - { - logger.error("Error while retrying downed hosts caused by : ", t); - } + return pool.getConnection(); } } - - private void retryDownedHosts() + catch (TException te) { - - Iterator iter = downedHost.iterator(); - while (iter.hasNext()) - { - CassandraHost host = iter.next(); - - if (host == null) - { - continue; - } - - boolean reconnected = verifyConnection(host); - if (reconnected) - { - addCassandraHost(host); - } - } + success = false; + logger.warn("{} :{} host appears to be down, trying for next ", pool.getPoolProperties().getHost(), + pool.getPoolProperties().getPort()); + pool = getNewPool(pool.getPoolProperties().getHost(), pool.getPoolProperties().getPort()); } } + throw new KunderaException("All hosts are down. please check servers manully."); + } - @Override - protected void shutdown() + void releaseConnection(ConnectionPool pool, Cassandra.Client conn) + { + if (pool != null && conn != null) { - - if (sf != null) - { - sf.cancel(true); - } - if (executor != null) - { - executor.shutdownNow(); - } + pool.release(conn); } } - public ConnectionPool getOtherPoolIfAvailable(String host, int port) + /** + * Adds a pool in hostPools map for given host. + * + * @param cassandraHost + * @return true id added successfully. + */ + public boolean addCassandraHost(CassandraHost cassandraHost) { - CassandraHost cassandraHost = ((CassandraHostConfigurator) configuration).getCassandraHost(host, port); - addHostToDownedHostSet(cassandraHost); - hostPools.get(cassandraHost); - hostPools.remove(cassandraHost); - return getPoolUsingPolicy(); + String keysapce = KunderaMetadataManager.getPersistenceUnitMetadata(getPersistenceUnit()).getProperties() + .getProperty(PersistenceProperties.KUNDERA_KEYSPACE); + PoolConfiguration prop = new PoolProperties(); + prop.setHost(cassandraHost.getHost()); + prop.setPort(cassandraHost.getPort()); + prop.setKeySpace(keysapce); + + PelopsUtils.setPoolConfigPolicy(cassandraHost, prop); + try + { + ConnectionPool pool = new ConnectionPool(prop); + hostPools.put(cassandraHost, pool); + return true; + } + catch (TException e) + { + logger.warn("Node " + cassandraHost.getHost() + " are still down"); + return false; + } } - @Override - protected void initializeLoadBalancer(String loadBalancingPolicyName) + private class ThriftLeastActiveBalancingPolcy extends LeastActiveBalancingPolicy { - if (loadBalancingPolicyName != null) + + /** + * + * @return pool object for host which has least active connections + * determined by maxActive connection. + * + */ + @Override + public Object getPool(Collection pools) { - switch (LoadBalancer.getValue(loadBalancingPolicyName)) + List vals = Lists.newArrayList(pools); + Collections.shuffle(vals); + Collections.sort(vals, new ShufflingCompare()); + Iterator iterator = vals.iterator(); + Object concurrentConnectionPool = iterator.next(); + return concurrentConnectionPool; + } + + private final class ShufflingCompare implements Comparator + { + public int compare(Object o1, Object o2) { - case ROUNDROBIN: - loadBalancingPolicy = new RoundRobinBalancingPolicy(); - break; - case LEASTACTIVE: - loadBalancingPolicy = new ThriftLeastActiveBalancingPolcy(); - break; + PoolConfiguration props1 = ((ConnectionPool) o1).getPoolProperties(); + PoolConfiguration props2 = ((ConnectionPool) o2).getPoolProperties(); + return props1.getMaxActive() - props2.getMaxActive(); } } } -} +} \ No newline at end of file diff --git a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/thrift/ThriftDataHandler.java b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/thrift/ThriftDataHandler.java index f83dc4346..8313b586d 100644 --- a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/thrift/ThriftDataHandler.java +++ b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/thrift/ThriftDataHandler.java @@ -50,12 +50,11 @@ */ public final class ThriftDataHandler extends CassandraDataHandlerBase implements CassandraDataHandler { - - private ConnectionPool pool; + // private ConnectionPool pool; public ThriftDataHandler(ConnectionPool pool) { - this.pool = pool; + // this.pool = pool; } /* @@ -68,25 +67,21 @@ public ThriftDataHandler(ConnectionPool pool) */ @Override public Object fromThriftRow(Class clazz, EntityMetadata m, Object rowKey, List relationNames, - boolean isWrapReq, ConsistencyLevel consistencyLevel, boolean isCql3Enabled) throws Exception + boolean isWrapReq, ConsistencyLevel consistencyLevel, boolean isCql3Enabled, Object conn) throws Exception { // List superColumnNames = m.getEmbeddedColumnFieldNames(); Object e = null; - - Cassandra.Client conn = PelopsUtils.getCassandraConnection(pool); - SlicePredicate predicate = new SlicePredicate(); predicate.setSlice_range(new SliceRange(Bytes.EMPTY.getBytes(), Bytes.EMPTY.getBytes(), true, 10000)); ByteBuffer key = ByteBuffer.wrap(PropertyAccessorHelper.toBytes(rowKey, m.getIdAttribute().getJavaType())); - List columnOrSuperColumns = conn.get_slice(key, new ColumnParent(m.getTableName()), + List columnOrSuperColumns = ((Cassandra.Client)conn).get_slice(key, new ColumnParent(m.getTableName()), predicate, consistencyLevel); Map> thriftColumnOrSuperColumns = new HashMap>(); thriftColumnOrSuperColumns.put(key, columnOrSuperColumns); e = populateEntityFromSlice(m, relationNames, isWrapReq, e, thriftColumnOrSuperColumns, isCql3Enabled); - PelopsUtils.releaseConnection(pool, conn); return e; } @@ -106,7 +101,8 @@ public E fromThriftRow(Class clazz, EntityMetadata m, DataRow relationNames, boolean isWrapReq, Object e, - Map> columnOrSuperColumnsFromRow,boolean isCql3Enabled) throws CharacterCodingException + Map> columnOrSuperColumnsFromRow, boolean isCql3Enabled) + throws CharacterCodingException { ThriftDataResultHelper dataGenerator = new ThriftDataResultHelper(); for (ByteBuffer key : columnOrSuperColumnsFromRow.keySet()) @@ -116,7 +112,7 @@ private Object populateEntityFromSlice(EntityMetadata m, List relationNa tr.setId(PropertyAccessorHelper.getObject(m.getIdAttribute().getJavaType(), key.array())); tr = dataGenerator.translateToThriftRow(columnOrSuperColumnsFromRow, m.isCounterColumnType(), m.getType(), tr); - e = populateEntity(tr, m, relationNames, isWrapReq,isCql3Enabled); + e = populateEntity(tr, m, relationNames, isWrapReq, isCql3Enabled); } return e; } diff --git a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/thrift/ThriftInvertedIndexHandler.java b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/thrift/ThriftInvertedIndexHandler.java index 29fd35a22..ce8951268 100644 --- a/kundera-cassandra/src/main/java/com/impetus/client/cassandra/thrift/ThriftInvertedIndexHandler.java +++ b/kundera-cassandra/src/main/java/com/impetus/client/cassandra/thrift/ThriftInvertedIndexHandler.java @@ -74,7 +74,7 @@ public ThriftInvertedIndexHandler(ConnectionPool pool) @Override public void write(Node node, EntityMetadata entityMetadata, String persistenceUnit, - ConsistencyLevel consistencyLevel, CassandraDataHandler cdHandler) + ConsistencyLevel consistencyLevel, CassandraDataHandler cdHandler, Object conn) { // Write to Inverted Index table if applicable boolean invertedIndexingApplicable = CassandraIndexHelper.isInvertedIndexingApplicable(entityMetadata); @@ -87,13 +87,10 @@ public void write(Node node, EntityMetadata entityMetadata, String persistenceUn List indexThriftyRows = thriftDataHandler.toIndexThriftRow(node.getData(), entityMetadata, indexColumnFamily); - Cassandra.Client conn = null; try { String keyspace = CassandraUtilities.getKeyspace(persistenceUnit); - conn = PelopsUtils.getCassandraConnection(pool); - for (ThriftRow thriftRow : indexThriftyRows) { byte[] rowKey = PropertyAccessorHelper.toBytes(thriftRow.getId(), thriftRow.getId().getClass()); @@ -130,7 +127,7 @@ public void write(Node node, EntityMetadata entityMetadata, String persistenceUn mulationMap.put(ByteBuffer.wrap(rowKey), columnFamilyValues); // Write Mutation map to database - conn.batch_mutate(mulationMap, consistencyLevel); + ((Cassandra.Client) conn).batch_mutate(mulationMap, consistencyLevel); } } catch (IllegalStateException e) @@ -158,26 +155,21 @@ public void write(Node node, EntityMetadata entityMetadata, String persistenceUn log.error("Unable to insert records into inverted index, Caused by: ", e); throw new IndexingException("Unable to insert records into inverted index", e); } - finally - { - PelopsUtils.releaseConnection(pool, conn); - } } - } @Override public List search(EntityMetadata m, String persistenceUnit, ConsistencyLevel consistencyLevel, - Map> indexClauseMap) + Map> indexClauseMap, Object conn) { - return super.search(m, persistenceUnit, consistencyLevel, indexClauseMap); + return super.search(m, persistenceUnit, consistencyLevel, indexClauseMap, conn); } @Override protected void searchSuperColumnsInRange(String columnFamilyName, ConsistencyLevel consistencyLevel, String persistenceUnit, String rowKey, byte[] searchSuperColumnName, List thriftSuperColumns, - byte[] start, byte[] finish) + byte[] start, byte[] finish, Object conn) { SlicePredicate colPredicate = new SlicePredicate(); SliceRange sliceRange = new SliceRange(); @@ -186,13 +178,11 @@ protected void searchSuperColumnsInRange(String columnFamilyName, ConsistencyLev colPredicate.setSlice_range(sliceRange); List coscList = null; - Cassandra.Client conn = null; try { String keyspace = CassandraUtilities.getKeyspace(persistenceUnit); - conn = PelopsUtils.getCassandraConnection(pool); - coscList = conn.get_slice(ByteBuffer.wrap(rowKey.getBytes()), new ColumnParent(columnFamilyName), - colPredicate, consistencyLevel); + coscList = ((Cassandra.Client) conn).get_slice(ByteBuffer.wrap(rowKey.getBytes()), new ColumnParent( + columnFamilyName), colPredicate, consistencyLevel); } catch (InvalidRequestException e) { @@ -214,10 +204,6 @@ protected void searchSuperColumnsInRange(String columnFamilyName, ConsistencyLev log.error("Unable to search from inverted index, Caused by: ", e); throw new IndexingException("Unable to search from inverted index", e); } - finally - { - PelopsUtils.releaseConnection(pool, conn); - } List allThriftSuperColumns = ThriftDataResultHelper.transformThriftResult(coscList, ColumnFamilyType.SUPER_COLUMN, null); @@ -236,17 +222,15 @@ protected void searchSuperColumnsInRange(String columnFamilyName, ConsistencyLev @Override protected SuperColumn getSuperColumnForRow(ConsistencyLevel consistencyLevel, String columnFamilyName, - String rowKey, byte[] superColumnName, String persistenceUnit) + String rowKey, byte[] superColumnName, String persistenceUnit, Object conn) { ColumnPath cp = new ColumnPath(columnFamilyName); cp.setSuper_column(superColumnName); ColumnOrSuperColumn cosc = null; - Cassandra.Client conn = null; try { - conn = PelopsUtils.getCassandraConnection(pool); - cosc = conn.get(ByteBuffer.wrap(rowKey.getBytes()), cp, consistencyLevel); + cosc = ((Cassandra.Client) conn).get(ByteBuffer.wrap(rowKey.getBytes()), cp, consistencyLevel); } catch (InvalidRequestException e) { @@ -272,10 +256,6 @@ protected SuperColumn getSuperColumnForRow(ConsistencyLevel consistencyLevel, St log.error("Unable to search from inverted index, Caused by: ", e); throw new IndexingException("Unable to search from inverted index", e); } - finally - { - PelopsUtils.releaseConnection(pool, conn); - } SuperColumn thriftSuperColumn = ThriftDataResultHelper.transformThriftResult(cosc, ColumnFamilyType.SUPER_COLUMN, null); @@ -283,31 +263,26 @@ protected SuperColumn getSuperColumnForRow(ConsistencyLevel consistencyLevel, St } @Override - public void delete(Object entity, EntityMetadata metadata, ConsistencyLevel consistencyLevel) + public void delete(Object entity, EntityMetadata metadata, ConsistencyLevel consistencyLevel, Object conn) { - super.delete(entity, metadata, consistencyLevel); + super.delete(entity, metadata, consistencyLevel, conn); } @Override protected void deleteColumn(String indexColumnFamily, String rowKey, byte[] superColumnName, - String persistenceUnit, ConsistencyLevel consistencyLevel, byte[] columnName) + String persistenceUnit, ConsistencyLevel consistencyLevel, byte[] columnName, Object conn) { ColumnPath cp = new ColumnPath(indexColumnFamily); cp.setSuper_column(superColumnName); - Cassandra.Client conn = null; try { - String keyspace = CassandraUtilities.getKeyspace(persistenceUnit); - - conn = PelopsUtils.getCassandraConnection(pool); - ColumnOrSuperColumn cosc; try { - cosc = conn.get(ByteBuffer.wrap(rowKey.getBytes()), cp, consistencyLevel); + cosc = ((Cassandra.Client) conn).get(ByteBuffer.wrap(rowKey.getBytes()), cp, consistencyLevel); } catch (NotFoundException e) { @@ -322,7 +297,8 @@ protected void deleteColumn(String indexColumnFamily, String rowKey, byte[] supe cp.setColumn(columnName); } - conn.remove(ByteBuffer.wrap(rowKey.getBytes()), cp, System.currentTimeMillis(), consistencyLevel); + ((Cassandra.Client) conn).remove(ByteBuffer.wrap(rowKey.getBytes()), cp, System.currentTimeMillis(), + consistencyLevel); } catch (InvalidRequestException e) { @@ -344,11 +320,5 @@ protected void deleteColumn(String indexColumnFamily, String rowKey, byte[] supe log.error("Unable to delete data from inverted index, Caused by: ", e); throw new IndexingException("Unable to delete data from inverted index", e); } - - finally - { - PelopsUtils.releaseConnection(pool, conn); - } } - } diff --git a/kundera-cassandra/src/test/java/com/impetus/client/cassandra/config/CassandraSchemaGenerationUsingXmlTest.java b/kundera-cassandra/src/test/java/com/impetus/client/cassandra/config/CassandraSchemaGenerationUsingXmlTest.java index e46939a2c..3e2894eb6 100644 --- a/kundera-cassandra/src/test/java/com/impetus/client/cassandra/config/CassandraSchemaGenerationUsingXmlTest.java +++ b/kundera-cassandra/src/test/java/com/impetus/client/cassandra/config/CassandraSchemaGenerationUsingXmlTest.java @@ -3,6 +3,9 @@ */ package com.impetus.client.cassandra.config; +import java.util.HashMap; +import java.util.Map; + import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; @@ -22,6 +25,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.impetus.client.cassandra.common.CassandraConstants; import com.impetus.client.persistence.CassandraCli; /** @@ -44,7 +48,9 @@ public void setUp() throws Exception { CassandraCli.cassandraSetUp(); CassandraCli.createKeySpace(keyspaceName); - emf = Persistence.createEntityManagerFactory("CassandraXmlPropertyTest"); + Map propertyMap = new HashMap(); + propertyMap.put(CassandraConstants.CQL_VERSION, CassandraConstants.CQL_VERSION_3_0); + emf = Persistence.createEntityManagerFactory("CassandraXmlPropertyTest", propertyMap); } /** diff --git a/kundera-cassandra/src/test/java/com/impetus/client/cassandra/thrift/ThriftClientTest.java b/kundera-cassandra/src/test/java/com/impetus/client/cassandra/thrift/ThriftClientTest.java index 88fe22019..38435b6b8 100644 --- a/kundera-cassandra/src/test/java/com/impetus/client/cassandra/thrift/ThriftClientTest.java +++ b/kundera-cassandra/src/test/java/com/impetus/client/cassandra/thrift/ThriftClientTest.java @@ -52,8 +52,14 @@ public void setUp() throws Exception @After public void tearDown() throws Exception { - em.close(); - emf.close(); + if (em != null) + { + em.close(); + } + if (emf != null) + { + emf.close(); + } CassandraCli.dropKeySpace("KunderaExamples"); } @@ -65,7 +71,7 @@ public void executeTests() findIdsByColumn(); } - public void persistJoinTable() + private void persistJoinTable() { JoinTableData joinTableData = new JoinTableData(OPERATION.INSERT, "KunderaExamples", "PERSON_ADDRESS", "PERSON_ID", "ADDRESS_ID", PersonMToM.class); @@ -84,7 +90,7 @@ public void persistJoinTable() } - public void getColumnsById() + private void getColumnsById() { List columns1 = client.getColumnsById("KunderaExamples", "PERSON_ADDRESS", "PERSON_ID", "ADDRESS_ID", "1", String.class); @@ -104,7 +110,7 @@ public void getColumnsById() } - public void findIdsByColumn() + private void findIdsByColumn() { Object[] ids1 = client.findIdsByColumn("KunderaExamples", "PERSON_ADDRESS", "PERSON_ID", "ADDRESS_ID", "A", PersonMToM.class); diff --git a/kundera-cassandra/src/test/java/com/impetus/client/crud/compositeType/CassandraCompositeTypeTest.java b/kundera-cassandra/src/test/java/com/impetus/client/crud/compositeType/CassandraCompositeTypeTest.java index 64d7567ec..c3b6a4268 100644 --- a/kundera-cassandra/src/test/java/com/impetus/client/crud/compositeType/CassandraCompositeTypeTest.java +++ b/kundera-cassandra/src/test/java/com/impetus/client/crud/compositeType/CassandraCompositeTypeTest.java @@ -34,14 +34,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.thrift.TException; -import org.databene.contiperf.PerfTest; -import org.databene.contiperf.junit.ContiPerfRule; -import org.databene.contiperf.report.CSVSummaryReportModule; -import org.databene.contiperf.report.HtmlReportModule; -import org.databene.contiperf.report.ReportModule; import org.junit.After; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; import com.impetus.client.cassandra.CassandraClientBase; diff --git a/kundera-cassandra/src/test/java/com/impetus/client/crud/datatypes/StudentCassandraBase.java b/kundera-cassandra/src/test/java/com/impetus/client/crud/datatypes/StudentCassandraBase.java index b833f9b11..ed3e4597d 100644 --- a/kundera-cassandra/src/test/java/com/impetus/client/crud/datatypes/StudentCassandraBase.java +++ b/kundera-cassandra/src/test/java/com/impetus/client/crud/datatypes/StudentCassandraBase.java @@ -262,7 +262,7 @@ protected E prepareData(long studentId, long uniqueId, String studentName, boole o.setSqlDate(newSqlDate); o.setSqlTime(sqlTime); o.setSqlTimestamp(sqlTimestamp); - // o.setBigDecimal(bigDecimal); + o.setBigDecimal(bigDecimal); o.setBigInteger(bigInteger); o.setCalendar(calendar); return (E) o; diff --git a/kundera-cassandra/src/test/java/com/impetus/client/persistence/CassandraCli.java b/kundera-cassandra/src/test/java/com/impetus/client/persistence/CassandraCli.java index 598dbf453..75c1a9b14 100644 --- a/kundera-cassandra/src/test/java/com/impetus/client/persistence/CassandraCli.java +++ b/kundera-cassandra/src/test/java/com/impetus/client/persistence/CassandraCli.java @@ -194,7 +194,7 @@ public static void dropKeySpace(String keyspaceName) { try { - if(client != null) + if (client != null) { client.system_drop_keyspace(keyspaceName); } @@ -314,7 +314,6 @@ public static void initClient() throws TTransportException TProtocol protocol = new TBinaryProtocol(transport); client = new Cassandra.Client(protocol); socket.open(); - } /** @@ -335,33 +334,27 @@ public static void executeCqlQuery(String cqlQuery) } catch (InvalidRequestException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.error(e.getMessage()); } catch (UnavailableException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.error(e.getMessage()); } catch (TimedOutException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.error(e.getMessage()); } catch (SchemaDisagreementException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.error(e.getMessage()); } catch (TException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.error(e.getMessage()); } catch (UnsupportedEncodingException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.error(e.getMessage()); } } } diff --git a/kundera-cassandra/src/test/resources/META-INF/persistence.xml b/kundera-cassandra/src/test/resources/META-INF/persistence.xml index 8e2a8e136..acbec4b42 100644 --- a/kundera-cassandra/src/test/resources/META-INF/persistence.xml +++ b/kundera-cassandra/src/test/resources/META-INF/persistence.xml @@ -312,7 +312,7 @@ - + \ No newline at end of file diff --git a/kundera-core/src/main/java/com/impetus/kundera/PersistenceProperties.java b/kundera-core/src/main/java/com/impetus/kundera/PersistenceProperties.java index e104bf7ca..abb77ac14 100644 --- a/kundera-core/src/main/java/com/impetus/kundera/PersistenceProperties.java +++ b/kundera-core/src/main/java/com/impetus/kundera/PersistenceProperties.java @@ -27,6 +27,9 @@ public interface PersistenceProperties /** The Constant KUNDERA_NODES. */ public static final String KUNDERA_NODES = "kundera.nodes"; + /** The Constant KUNDERA_NODES. */ + public static final String KUNDERA_NODE_PORT = "kundera.node.port"; + /** The Constant KUNDERA_PORT. */ public static final String KUNDERA_PORT = "kundera.port"; diff --git a/kundera-core/src/main/java/com/impetus/kundera/client/ClientResolver.java b/kundera-core/src/main/java/com/impetus/kundera/client/ClientResolver.java index 38716c68b..e5278c00e 100644 --- a/kundera-core/src/main/java/com/impetus/kundera/client/ClientResolver.java +++ b/kundera-core/src/main/java/com/impetus/kundera/client/ClientResolver.java @@ -38,8 +38,8 @@ public final class ClientResolver { - /** The client factories. */ - static Map clientFactories = new ConcurrentHashMap(); + // /** The client factories. */ + static Map clientFactories = new ConcurrentHashMap(); /** logger instance. */ private static final Logger logger = LoggerFactory.getLogger(ClientResolver.class); @@ -54,11 +54,15 @@ public final class ClientResolver public static Client discoverClient(String persistenceUnit) { logger.info("Returning client instance for:" + persistenceUnit); - ClientFactory clientFactory = clientFactories.get(persistenceUnit); - if (clientFactory != null) - { - return clientFactory.getClientInstance(); - } + // ClientMetadata clientMetadata = + // KunderaMetadata.INSTANCE.getClientMetadata(persistenceUnit); + // ClientFactory clientFactory = + // /*clientFactories.get(persistenceUnit)*/ clientMetadata != null ? + // clientMetadata.getClientFactory() : null ; + // if (clientFactory != null) + // { + // return clientFactory.getClientInstance(); + // } throw new ClientResolverException(" No client configured for:" + persistenceUnit); } @@ -71,11 +75,42 @@ public static Client discoverClient(String persistenceUnit) */ public static ClientFactory getClientFactory(String persistenceUnit, Map puProperties) { - ClientFactory clientFactory = clientFactories.get(persistenceUnit); - - if (clientFactory != null) - return clientFactory; + ClientFactory clientFactory /* = clientFactories.get(persistenceUnit) */; + // if (clientFactory != null) + // return clientFactory; + // ClientMetadata clientMetadata = + // KunderaMetadata.INSTANCE.getClientMetadata(persistenceUnit); + // if (clientMetadata != null) + // { + // clientFactory = clientMetadata.getClientFactory(); + // if (clientFactory != null) + // { + // return clientFactory; + // } + // } + // else + // { + // clientMetadata = new ClientMetadata(); + // } + clientFactory = instantiateClientFactory(persistenceUnit, puProperties); + // clientMetadata.setClientFactory(clientFactory); + // KunderaMetadata.INSTANCE.addClientMetadata(persistenceUnit, + // clientMetadata); + clientFactories.put(persistenceUnit, clientFactory); + return clientFactory; + } + /** + * Creates new instance of client factory for given persistence unit. + * + * @param persistenceUnit + * @param puProperties + * @param clientFactory + * @return new instance of clientFactory + */ + private static ClientFactory instantiateClientFactory(String persistenceUnit, Map puProperties) + { + ClientFactory clientFactory = null; logger.info("Initializing client factory for: " + persistenceUnit); PersistenceUnitMetadata persistenceUnitMetadata = KunderaMetadata.INSTANCE.getApplicationMetadata() .getPersistenceUnitMetadata(persistenceUnit); @@ -140,7 +175,7 @@ public static ClientFactory getClientFactory(String persistenceUnit, Map clazz, String persistenceU { GeneratedValueProcessor processer = new GeneratedValueProcessor(); String pu = getPersistenceUnitOfEntity(clazz); - if (pu != null && pu.equals(persistenceUnit)) + String clientFactoryName = KunderaMetadataManager.getPersistenceUnitMetadata(m.getPersistenceUnit()) + .getClient(); + if (pu != null && pu.equals(persistenceUnit) || clientFactoryName.equalsIgnoreCase("com.impetus.client.rdbms.RDBMSClientFactory")) { Field f = (Field) m.getIdAttribute().getJavaMember(); diff --git a/kundera-core/src/main/java/com/impetus/kundera/configure/SchemaConfiguration.java b/kundera-core/src/main/java/com/impetus/kundera/configure/SchemaConfiguration.java index 5454502f7..f8b131ccf 100644 --- a/kundera-core/src/main/java/com/impetus/kundera/configure/SchemaConfiguration.java +++ b/kundera-core/src/main/java/com/impetus/kundera/configure/SchemaConfiguration.java @@ -16,7 +16,6 @@ package com.impetus.kundera.configure; import java.util.ArrayList; -import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -69,7 +68,6 @@ */ public class SchemaConfiguration implements Configuration { - /** The log. */ private static Logger log = LoggerFactory.getLogger(SchemaConfiguration.class); @@ -105,6 +103,8 @@ public void configure() { ApplicationMetadata appMetadata = KunderaMetadata.INSTANCE.getApplicationMetadata(); + EntityValidator validator = new EntityValidatorImpl(externalPropertyMap); + puToSchemaMetadata = appMetadata.getSchemaMetadata().getPuToSchemaMetadata(); // TODO, FIXME: Refactoring is required. @@ -157,27 +157,43 @@ public void configure() // Add table for GeneratedValue if opted TableStrategy addTableGenerator(appMetadata, persistenceUnit, tableInfos, entityMetadata, idClassName, idName, isCompositeId); + + // Validating entity against counter column family. + validator.validateEntity(entityMetadata.getEntityClazz()); } puToSchemaMetadata.put(persistenceUnit, tableInfos); } + for (String persistenceUnit : persistenceUnits) { - Map externalProperties = KunderaCoreUtils.getExternalProperties(persistenceUnit, - externalPropertyMap, persistenceUnits); - if (getSchemaProperty(persistenceUnit, externalProperties) != null - && !getSchemaProperty(persistenceUnit, externalProperties).isEmpty()) + SchemaManager schemaManager = getSchemaManagerForPu(persistenceUnit); + if (schemaManager != null) { - ClientFactory clientFactory = ClientResolver.getClientFactory(persistenceUnit); - SchemaManager schemaManager = clientFactory != null ? clientFactory - .getSchemaManager(externalProperties) : null; - if (schemaManager != null) - { - schemaManager.exportSchema(); - } + schemaManager.exportSchema(); } } } + /** + * Return schema manager for pu. + * + * @param persistenceUnit + * @return + */ + private SchemaManager getSchemaManagerForPu(final String persistenceUnit) + { + SchemaManager schemaManager = null; + Map externalProperties = KunderaCoreUtils.getExternalProperties(persistenceUnit, + externalPropertyMap, persistenceUnits); + if (getSchemaProperty(persistenceUnit, externalProperties) != null + && !getSchemaProperty(persistenceUnit, externalProperties).isEmpty()) + { + ClientFactory clientFactory = ClientResolver.getClientFactory(persistenceUnit); + schemaManager = clientFactory != null ? clientFactory.getSchemaManager(externalProperties) : null; + } + return schemaManager; + } + /** * Add tableGenerator to table info. * diff --git a/kundera-core/src/main/java/com/impetus/kundera/configure/schema/ColumnInfo.java b/kundera-core/src/main/java/com/impetus/kundera/configure/schema/ColumnInfo.java index dfc0b0476..164660a18 100644 --- a/kundera-core/src/main/java/com/impetus/kundera/configure/schema/ColumnInfo.java +++ b/kundera-core/src/main/java/com/impetus/kundera/configure/schema/ColumnInfo.java @@ -35,17 +35,6 @@ public class ColumnInfo /** The type variable. */ private Class type; - /** Maximum allowed value for this column */ - private Integer maxValue; - - /** Minimum allowed value for this column */ - private Integer minValue; - - /** - * The index type; - */ - private String indexType; - /** * Instantiates a new column info. */ @@ -159,56 +148,4 @@ public void setType(Class type) { this.type = type; } - - /** - * @return the indexType - */ - public String getIndexType() - { - return indexType; - } - - /** - * @param indexType - * the indexType to set - */ - public void setIndexType(String indexType) - { - this.indexType = indexType; - } - - /** - * @return the maxValue - */ - public Integer getMaxValue() - { - return maxValue; - } - - /** - * @param maxValue - * the maxValue to set - */ - public void setMaxValue(Integer maxValue) - { - this.maxValue = maxValue; - } - - /** - * @return the minValue - */ - public Integer getMinValue() - { - return minValue; - } - - /** - * @param minValue - * the minValue to set - */ - public void setMinValue(Integer minValue) - { - this.minValue = minValue; - } - } diff --git a/kundera-core/src/main/java/com/impetus/kundera/configure/schema/api/AbstractSchemaManager.java b/kundera-core/src/main/java/com/impetus/kundera/configure/schema/api/AbstractSchemaManager.java index 20f469a35..0ba1731a4 100644 --- a/kundera-core/src/main/java/com/impetus/kundera/configure/schema/api/AbstractSchemaManager.java +++ b/kundera-core/src/main/java/com/impetus/kundera/configure/schema/api/AbstractSchemaManager.java @@ -18,6 +18,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.StringTokenizer; import com.impetus.kundera.PersistenceProperties; import com.impetus.kundera.configure.ClientProperties.DataStore; @@ -46,7 +47,7 @@ public abstract class AbstractSchemaManager protected String port; /** The host variable . */ - protected String host; + protected String[] hosts; /** The kundera_client variable. */ protected String clientFactory; @@ -151,8 +152,14 @@ private void readConfigProperties(String pu) // get type of schema of operation. if (operationType == null) operationType = puMetadata.getProperty(PersistenceProperties.KUNDERA_DDL_AUTO_PREPARE); + + String[] hostArray = hostName.split(","); + hosts = new String[hostArray.length]; + for (int i = 0; i < hostArray.length; i++) + { + hosts[i] = hostArray[i].trim(); + } - this.host = hostName; this.port = portName; this.databaseName = schemaName; this.operation = operationType; diff --git a/kundera-core/src/main/java/com/impetus/kundera/graph/ObjectGraphBuilder.java b/kundera-core/src/main/java/com/impetus/kundera/graph/ObjectGraphBuilder.java index 1156dcc1f..5be6ec553 100644 --- a/kundera-core/src/main/java/com/impetus/kundera/graph/ObjectGraphBuilder.java +++ b/kundera-core/src/main/java/com/impetus/kundera/graph/ObjectGraphBuilder.java @@ -22,11 +22,9 @@ import javax.persistence.MapKeyJoinColumn; -import com.impetus.kundera.PersistenceUtilHelper; import org.apache.commons.lang.StringUtils; -//import org.hibernate.collection.spi.PersistentCollection; -//import org.hibernate.proxy.HibernateProxy; +import com.impetus.kundera.PersistenceUtilHelper; import com.impetus.kundera.graph.NodeLink.LinkProperty; import com.impetus.kundera.lifecycle.states.NodeState; import com.impetus.kundera.lifecycle.states.TransientState; @@ -101,17 +99,17 @@ private Node getNode(Object entity, ObjectGraph graph, NodeState initialNodeStat } // Generate and set Id if @GeneratedValue present. + if (initialNodeState != null && initialNodeState.getClass().equals(TransientState.class)) { - idGenerator.generateAndSetId(entity, entityMetadata, pd); - } + idGenerator.generateAndSetId(entity, entityMetadata, pd); + } if (!validator.isValidEntityObject(entity)) { throw new IllegalArgumentException( "Entity object is invalid, operation failed. Please check previous log message for details"); } - Object id = PropertyAccessorHelper.getId(entity, entityMetadata); String nodeId = ObjectGraphUtils.getNodeId(id, entity.getClass()); @@ -183,8 +181,7 @@ else if (node.isProcessed()) if (Collection.class.isAssignableFrom(childObject.getClass())) { // For each entity in the collection, construct a child - // node - // and add to graph + // node and add to graph Collection childrenObjects = (Collection) childObject; if (childrenObjects != null diff --git a/kundera-core/src/main/java/com/impetus/kundera/loader/GenericClientFactory.java b/kundera-core/src/main/java/com/impetus/kundera/loader/GenericClientFactory.java index c4b0e7bf3..97277a3e9 100644 --- a/kundera-core/src/main/java/com/impetus/kundera/loader/GenericClientFactory.java +++ b/kundera-core/src/main/java/com/impetus/kundera/loader/GenericClientFactory.java @@ -38,7 +38,7 @@ import com.impetus.kundera.metadata.model.KunderaMetadata; import com.impetus.kundera.persistence.EntityReader; import com.impetus.kundera.service.Host; -import com.impetus.kundera.service.policy.HostRetryService; +import com.impetus.kundera.service.policy.RetryService; import com.impetus.kundera.service.policy.LoadBalancingPolicy; /** @@ -81,7 +81,7 @@ public abstract class GenericClientFactory implements ClientFactory, ClientLifeC protected LoadBalancingPolicy loadBalancingPolicy; /** Holds Instance of retry service */ - protected HostRetryService hostRetryService; + protected RetryService hostRetryService; /** Holds one pool instance per host */ protected ConcurrentMap hostPools = new ConcurrentHashMap(); @@ -117,8 +117,8 @@ public void load(String persistenceUnit, Map puProperties) */ protected void loadClientMetadata(Map puProperties) { - if (KunderaMetadata.INSTANCE.getClientMetadata(persistenceUnit) == null) - { + /* if (KunderaMetadata.INSTANCE.getClientMetadata(persistenceUnit) == null) + {*/ ClientMetadata clientMetadata = new ClientMetadata(); String luceneDirectoryPath = puProperties != null ? (String) puProperties .get(PersistenceProperties.KUNDERA_INDEX_HOME_DIR) : null; @@ -173,7 +173,7 @@ else if (indexerClass != null) indexManager = new IndexManager(null); } KunderaMetadata.INSTANCE.addClientMetadata(persistenceUnit, clientMetadata); - } + /* }*/ } /** diff --git a/kundera-core/src/main/java/com/impetus/kundera/metadata/model/ApplicationMetadata.java b/kundera-core/src/main/java/com/impetus/kundera/metadata/model/ApplicationMetadata.java index 308bfed4e..a063d177b 100644 --- a/kundera-core/src/main/java/com/impetus/kundera/metadata/model/ApplicationMetadata.java +++ b/kundera-core/src/main/java/com/impetus/kundera/metadata/model/ApplicationMetadata.java @@ -49,7 +49,7 @@ public class ApplicationMetadata private Map persistenceUnitMetadataMap = new ConcurrentHashMap(); /** The Constant log. */ - private static Log logger = LogFactory.getLog(EntityManagerFactoryImpl.class); + private static Log logger = LogFactory.getLog(ApplicationMetadata.class); private SchemaMetadata schemaMetadata = new SchemaMetadata(); diff --git a/kundera-core/src/main/java/com/impetus/kundera/metadata/model/KunderaMetadata.java b/kundera-core/src/main/java/com/impetus/kundera/metadata/model/KunderaMetadata.java index 46241a684..bbbbc5bce 100644 --- a/kundera-core/src/main/java/com/impetus/kundera/metadata/model/KunderaMetadata.java +++ b/kundera-core/src/main/java/com/impetus/kundera/metadata/model/KunderaMetadata.java @@ -123,6 +123,11 @@ public void addClientMetadata(String persistenceUnit, ClientMetadata clientMetad this.clientMetadata.put(persistenceUnit, clientMetadata); } + /** + * Unloads client metadata for given pu. + * + * @param pu + */ public void unloadKunderaMetadata(final String pu) { ClientMetadata clientMetadata = getClientMetadata(pu); @@ -134,7 +139,5 @@ public void unloadKunderaMetadata(final String pu) clientMetadata.setLuceneIndexDir(null); clientMetadata = null; } - // this.clientMetadata.clear(); - this.getApplicationMetadata().unloadApplicationMatadata(pu); } } diff --git a/kundera-core/src/main/java/com/impetus/kundera/metadata/validator/EntityValidatorImpl.java b/kundera-core/src/main/java/com/impetus/kundera/metadata/validator/EntityValidatorImpl.java index 4d3d108ee..a257c3fa6 100644 --- a/kundera-core/src/main/java/com/impetus/kundera/metadata/validator/EntityValidatorImpl.java +++ b/kundera-core/src/main/java/com/impetus/kundera/metadata/validator/EntityValidatorImpl.java @@ -226,7 +226,7 @@ public void validateEntity(Class clazz) EntityMetadata metadata = KunderaMetadataManager.getEntityMetadata(clazz); if (metadata != null) { - SchemaManager schemaManager = ClientResolver.getClientFactory(metadata.getPersistenceUnit(), puProperties) + SchemaManager schemaManager = ClientResolver.getClientFactory(metadata.getPersistenceUnit()) .getSchemaManager(puProperties); if (schemaManager != null && !schemaManager.validateEntity(clazz)) { diff --git a/kundera-core/src/main/java/com/impetus/kundera/persistence/EntityManagerFactoryImpl.java b/kundera-core/src/main/java/com/impetus/kundera/persistence/EntityManagerFactoryImpl.java index e63c4fa32..2ca7752fd 100644 --- a/kundera-core/src/main/java/com/impetus/kundera/persistence/EntityManagerFactoryImpl.java +++ b/kundera-core/src/main/java/com/impetus/kundera/persistence/EntityManagerFactoryImpl.java @@ -19,6 +19,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import javax.persistence.Cache; import javax.persistence.EntityManager; @@ -41,10 +42,13 @@ import com.impetus.kundera.cache.CacheProvider; import com.impetus.kundera.cache.NonOperationalCacheProvider; import com.impetus.kundera.client.ClientResolver; +import com.impetus.kundera.client.ClientResolverException; +import com.impetus.kundera.configure.ClientMetadataBuilder; +import com.impetus.kundera.loader.ClientFactory; import com.impetus.kundera.loader.ClientLifeCycleManager; import com.impetus.kundera.metadata.KunderaMetadataManager; import com.impetus.kundera.metadata.model.KunderaMetadata; -import com.impetus.kundera.utils.InvalidConfigurationException; +import com.impetus.kundera.utils.KunderaCoreUtils; /** * Implementation class for {@link EntityManagerFactory} @@ -83,6 +87,9 @@ public class EntityManagerFactoryImpl implements EntityManagerFactory private final PersistenceUtilHelper.MetadataCache cache = new PersistenceUtilHelper.MetadataCache(); + /** ClientFactory map holds one clientfactory for one persistence unit */ + private Map clientFactories = new ConcurrentHashMap(); + /** * Use this if you want to construct this directly. * @@ -105,6 +112,9 @@ public EntityManagerFactoryImpl(String persistenceUnit, Map prop this.properties = propsMap; this.persistenceUnits = persistenceUnit.split(Constants.PERSISTENCE_UNIT_SEPARATOR); + // configure client factories + configureClientFactories(); + // Initialize L2 cache this.cacheProvider = initSecondLevelCache(); this.cacheProvider.createCache(Constants.KUNDERA_SECONDARY_CACHE_NAME); @@ -164,12 +174,13 @@ public final void close() for (String pu : persistenceUnits) { - ((ClientLifeCycleManager) ClientResolver.getClientFactory(pu)).destroy(); + ((ClientLifeCycleManager) clientFactories.get(pu)).destroy(); KunderaMetadata.INSTANCE.unloadKunderaMetadata(pu); } this.persistenceUnits = null; this.properties = null; - + clientFactories.clear(); + clientFactories = null; } else { @@ -328,6 +339,19 @@ public PersistenceUnitUtil getPersistenceUnitUtil() return this.util; } + /** + * Initialize and load clientFactory for all persistenceUnit with external + * properties. + * + * @param persistenceUnit + * @param externalProperties + */ + private void configureClientFactories() + { + ClientMetadataBuilder builder = new ClientMetadataBuilder(getProperties(), getPersistenceUnits()); + builder.buildClientFactoryMetadata(clientFactories); + } + /** * Inits the second level cache. * @@ -379,40 +403,14 @@ String[] getPersistenceUnits() return persistenceUnits; } - /** - * @param puProperty - */ - private Map getExternalProperties(String pu) - { - Map puProperty; - if (persistenceUnits.length > 1 && properties != null) - { - puProperty = (Map) properties.get(pu); - - // if property found then return it, if it is null by pass it, else - // throw invalidConfiguration. - if (puProperty != null) - { - return fetchPropertyMap(puProperty); - } - } - return properties; - } - - /** - * @param puProperty - * @return - */ - private Map fetchPropertyMap(Map puProperty) + ClientFactory getClientFactory(final String pu) { - if (puProperty.getClass().isAssignableFrom(Map.class) || puProperty.getClass().isAssignableFrom(HashMap.class)) - { - return puProperty; - } - else + ClientFactory clientFactory = clientFactories.get(pu); + if (clientFactory != null) { - throw new InvalidConfigurationException( - "For cross data store persistence, please specify as: Map {pu,Map of properties}"); + return clientFactory; } + logger.error("Client Factory Not Configured For Specified Client Type : "); + throw new ClientResolverException("Client Factory Not Configured For Specified Client Type."); } } diff --git a/kundera-core/src/main/java/com/impetus/kundera/persistence/EntityManagerImpl.java b/kundera-core/src/main/java/com/impetus/kundera/persistence/EntityManagerImpl.java index 2bb7bae33..7949c2ddd 100644 --- a/kundera-core/src/main/java/com/impetus/kundera/persistence/EntityManagerImpl.java +++ b/kundera-core/src/main/java/com/impetus/kundera/persistence/EntityManagerImpl.java @@ -44,6 +44,9 @@ import com.impetus.kundera.Constants; import com.impetus.kundera.KunderaException; +import com.impetus.kundera.client.Client; +import com.impetus.kundera.client.ClientResolverException; +import com.impetus.kundera.loader.ClientFactory; import com.impetus.kundera.metadata.model.ApplicationMetadata; import com.impetus.kundera.metadata.model.KunderaMetadata; import com.impetus.kundera.persistence.context.PersistenceCache; @@ -71,9 +74,6 @@ public class EntityManagerImpl implements EntityManager, ResourceManager /** Flush mode for this EM, default is AUTO. */ private FlushModeType flushMode = FlushModeType.AUTO; - // /** The session. */ - // private EntityManagerSession session; - /** Properties provided by user at the time of EntityManager Creation. */ private Map properties; @@ -133,7 +133,7 @@ public class EntityManagerImpl implements EntityManager, ResourceManager for (String pu : ((EntityManagerFactoryImpl) this.factory).getPersistenceUnits()) { - this.persistenceDelegator.loadClient(pu); + this.persistenceDelegator.loadClient(pu, discoverClient(pu)); } if (logger.isDebugEnabled()) @@ -900,16 +900,6 @@ private String getPersistenceUnit() return (String) getEntityManagerFactory().getProperties().get(Constants.PERSISTENCE_UNIT_NAME); } - // /** - // * Gets the session. - // * - // * @return the session - // */ - // private EntityManagerSession getSession() - // { - // return session; - // } - /** * Gets the persistence delegator. * @@ -982,4 +972,24 @@ private TypedQuery onTypedQuery(Class paramClass, Query q) throw new IllegalArgumentException("Mismatch in expected return type. Expected:" + paramClass + " But actual class is:" + ((QueryImpl) q).getKunderaQuery().getEntityClass()); } + + /** + * Gets the client. + * + * @param persistenceUnit + * the persistence unit + * @return the client + */ + private Client discoverClient(String persistenceUnit) + { + logger.info("Returning client instance for:" + persistenceUnit); + + ClientFactory clientFactory = ((EntityManagerFactoryImpl) getEntityManagerFactory()) + .getClientFactory(persistenceUnit); + if (clientFactory != null) + { + return clientFactory.getClientInstance(); + } + throw new ClientResolverException(" No client configured for: " + persistenceUnit); + } } diff --git a/kundera-core/src/main/java/com/impetus/kundera/persistence/PersistenceDelegator.java b/kundera-core/src/main/java/com/impetus/kundera/persistence/PersistenceDelegator.java index 44280d6af..bcbdda854 100644 --- a/kundera-core/src/main/java/com/impetus/kundera/persistence/PersistenceDelegator.java +++ b/kundera-core/src/main/java/com/impetus/kundera/persistence/PersistenceDelegator.java @@ -37,7 +37,6 @@ import com.impetus.kundera.PersistenceProperties; import com.impetus.kundera.client.Client; import com.impetus.kundera.client.ClientPropertiesSetter; -import com.impetus.kundera.client.ClientResolver; import com.impetus.kundera.client.ClientResolverException; import com.impetus.kundera.graph.Node; import com.impetus.kundera.graph.ObjectGraph; @@ -757,11 +756,11 @@ void populateClientProperties(Map properties) * @param client */ - void loadClient(String persistenceUnit/*, Client client*/) + void loadClient(String persistenceUnit, Client client) { if (!clientMap.containsKey(persistenceUnit)) { - clientMap.put(persistenceUnit, ClientResolver.discoverClient(persistenceUnit)/*client*/); + clientMap.put(persistenceUnit, /*ClientResolver.discoverClient(persistenceUnit)*/client); } } diff --git a/kundera-core/src/main/java/com/impetus/kundera/service/Host.java b/kundera-core/src/main/java/com/impetus/kundera/service/Host.java new file mode 100644 index 000000000..1a540b908 --- /dev/null +++ b/kundera-core/src/main/java/com/impetus/kundera/service/Host.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * * Copyright 2012 Impetus Infotech. + * * + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + ******************************************************************************/ +package com.impetus.kundera.service; + +/** + * + * @author Kuldeep.Mishra + * + */ +public interface Host +{ + /** + * + * @return host. + */ + String getHost(); + + /** + * + * @return port. + */ + int getPort(); + + /** + * @return the maxTotal + */ + int getMaxTotal(); + + /** + * @return the maxActive + */ + int getMaxActive(); + + /** + * @return the maxIdle + */ + int getMaxIdle(); + + /** + * @return the minIdle + */ + int getMinIdle(); + + /** + * + * @return true/false. + */ + public boolean isRetryHost(); +} diff --git a/kundera-core/src/main/java/com/impetus/kundera/service/HostConfiguration.java b/kundera-core/src/main/java/com/impetus/kundera/service/HostConfiguration.java new file mode 100644 index 000000000..bd801d367 --- /dev/null +++ b/kundera-core/src/main/java/com/impetus/kundera/service/HostConfiguration.java @@ -0,0 +1,129 @@ +/******************************************************************************* + * * Copyright 2012 Impetus Infotech. + * * + * * Licensed under the Apache License, Version 2.0 (the "License"); + * * you may not use this file except in compliance with the License. + * * You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, software + * * distributed under the License is distributed on an "AS IS" BASIS, + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * * See the License for the specific language governing permissions and + * * limitations under the License. + ******************************************************************************/ +package com.impetus.kundera.service; + +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.CopyOnWriteArrayList; + +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.impetus.kundera.PersistenceProperties; +import com.impetus.kundera.configure.ClientProperties.DataStore.Connection.Server; +import com.impetus.kundera.metadata.model.KunderaMetadata; +import com.impetus.kundera.metadata.model.PersistenceUnitMetadata; + +/** + * Configure host name and port and build Hosts array. + * + * @author Kuldeep.Mishra + * + */ +public abstract class HostConfiguration +{ + /** The logger. */ + private static Logger logger = LoggerFactory.getLogger(HostConfiguration.class); + + /** Delay time for host retry */ + protected int retryDelay = 100; + + /** + * Persistence unit metadata. + */ + protected PersistenceUnitMetadata persistenceUnitMetadata; + + /** + * External configuration passed at the time of emf creation. + */ + protected Map externalProperties; + + /** + * Array of hosts. + */ + protected List hostsList = new CopyOnWriteArrayList(); + + public HostConfiguration(Map externalProperties, List servers, String persistenceUnit) + { + buildHosts(externalProperties, servers, persistenceUnit); + } + + /** + * Build host array. + * + * @param externalProperties + * @param servers + * @param persistenceUnit + */ + private void buildHosts(Map externalProperties, List servers, String persistenceUnit) + { + persistenceUnitMetadata = KunderaMetadata.INSTANCE.getApplicationMetadata().getPersistenceUnitMetadata( + persistenceUnit); + this.externalProperties = externalProperties; + String hosts = externalProperties != null ? (String) externalProperties + .get(PersistenceProperties.KUNDERA_NODES) : null; + String portAsString = externalProperties != null ? (String) externalProperties + .get(PersistenceProperties.KUNDERA_PORT) : null; + + if (hosts != null) + { + buildHosts(hosts, portAsString, this.hostsList); + } + else if (servers != null && servers.size() >= 1) + { + buildHosts(servers, this.hostsList); + } + else + { + Properties props = persistenceUnitMetadata.getProperties(); + String contactNodes = (String) props.get(PersistenceProperties.KUNDERA_NODES); + String defaultPort = (String) props.get(PersistenceProperties.KUNDERA_PORT); + buildHosts(contactNodes, defaultPort, this.hostsList); + } + } + + /** + * Validate host and port. + * + * @param host + * @param port + */ + protected void onValidation(final String host, final String port) + { + if (host == null || !StringUtils.isNumeric(port) || port.isEmpty()) + { + logger.error("Host or port should not be null / port should be numeric"); + throw new IllegalArgumentException("Host or port should not be null / port should be numeric"); + } + } + + /** + * + * @return Array of hosts. + */ + public List getHosts() + { + return hostsList; + } + + protected abstract void buildHosts(List servers, List hostsList); + + protected abstract void buildHosts(String hosts, String portAsString, List hostsList); + + protected abstract void setConfig(Host host, Properties props, Map puProperties); +} diff --git a/kundera-core/src/main/java/com/impetus/kundera/service/policy/FailoverPolicy.java b/kundera-core/src/main/java/com/impetus/kundera/service/policy/FailoverPolicy.java new file mode 100644 index 000000000..75a407958 --- /dev/null +++ b/kundera-core/src/main/java/com/impetus/kundera/service/policy/FailoverPolicy.java @@ -0,0 +1,6 @@ +package com.impetus.kundera.service.policy; + +public class FailoverPolicy +{ + +} diff --git a/kundera-core/src/main/java/com/impetus/kundera/service/policy/LeastActiveBalancingPolicy.java b/kundera-core/src/main/java/com/impetus/kundera/service/policy/LeastActiveBalancingPolicy.java new file mode 100644 index 000000000..db7934806 --- /dev/null +++ b/kundera-core/src/main/java/com/impetus/kundera/service/policy/LeastActiveBalancingPolicy.java @@ -0,0 +1,33 @@ +/** + * Copyright 2012 Impetus Infotech. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.impetus.kundera.service.policy; + +import java.util.Collection; + +/** + * @author Kuldeep.Mishra + * + */ +public abstract class LeastActiveBalancingPolicy implements LoadBalancingPolicy +{ + + /** + *@return pool object for host which has least active connections. + * + */ + @Override + public abstract Object getPool(Collection pools); +} diff --git a/kundera-core/src/main/java/com/impetus/kundera/service/policy/LoadBalancingPolicy.java b/kundera-core/src/main/java/com/impetus/kundera/service/policy/LoadBalancingPolicy.java new file mode 100644 index 000000000..2bd047f18 --- /dev/null +++ b/kundera-core/src/main/java/com/impetus/kundera/service/policy/LoadBalancingPolicy.java @@ -0,0 +1,37 @@ +/** + * Copyright 2012 Impetus Infotech. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.impetus.kundera.service.policy; + +import java.util.Collection; + +/** + * LoadBalancing policy interface for {@link RoundRobinBalancingPolicy} and {@link LeastActiveBalancingPolicy}. + * + * @author Kuldeep.mishra + * + */ +public interface LoadBalancingPolicy +{ + + /** + * Returns pool on the basis of loadbalancing policy. + * + * @param pools + * @param excludeHosts + * @return pool object. + */ + Object getPool(Collection pools); +} diff --git a/kundera-core/src/main/java/com/impetus/kundera/service/policy/RoundRobinBalancingPolicy.java b/kundera-core/src/main/java/com/impetus/kundera/service/policy/RoundRobinBalancingPolicy.java new file mode 100644 index 000000000..0f22c947c --- /dev/null +++ b/kundera-core/src/main/java/com/impetus/kundera/service/policy/RoundRobinBalancingPolicy.java @@ -0,0 +1,72 @@ +/** + * Copyright 2012 Impetus Infotech. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.impetus.kundera.service.policy; + +import java.util.Collection; + +import com.google.common.collect.Iterables; + +/** + * RoundRobinBalancingPolicy returns pool using round robin algorithm. + * + * @author Kuldeep.Mishra + * + */ +public final class RoundRobinBalancingPolicy implements LoadBalancingPolicy +{ + + private int counter; + + public RoundRobinBalancingPolicy() + { + counter = 0; + } + + /* + * (non-Javadoc) + * + * @see + * com.impetus.kundera.policy.LoadBalancingPolicy#getPool(java.util.Collection + * , java.util.Set) + */ + @Override + public Object getPool(Collection pools) + { + try + { + return Iterables.get(pools, getAndIncrement(pools.size())); + } + catch (IndexOutOfBoundsException e) + { + return pools.iterator().next(); + } + } + + private int getAndIncrement(int size) + { + int counterToReturn; + synchronized (this) + { + if (counter >= 16384) + { + counter = 0; + } + counterToReturn = counter++; + } + + return counterToReturn % size; + } +} diff --git a/kundera-core/src/main/java/com/impetus/kundera/utils/KunderaThreadFactory.java b/kundera-core/src/main/java/com/impetus/kundera/utils/KunderaThreadFactory.java index 0a10e15c7..78c79eb6e 100644 --- a/kundera-core/src/main/java/com/impetus/kundera/utils/KunderaThreadFactory.java +++ b/kundera-core/src/main/java/com/impetus/kundera/utils/KunderaThreadFactory.java @@ -7,21 +7,21 @@ public class KunderaThreadFactory implements ThreadFactory { private ConcurrentHashMap counters = new ConcurrentHashMap(); - private final String name; - public KunderaThreadFactory(Class clazz) + public KunderaThreadFactory(final String threadName) { - this.name = "Kundera." + clazz.getName(); + this.name = threadName; } - private int getNextThreadNumber() + private int getNext() { - if (!counters.containsKey(name)) + AtomicInteger threadCount = counters.get(name); + if (threadCount==null) { - counters.putIfAbsent(name, new AtomicInteger()); + counters.put(name, new AtomicInteger()); } - return counters.get(name).incrementAndGet(); + return threadCount.incrementAndGet(); } @Override @@ -29,7 +29,10 @@ public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setDaemon(true); - t.setName(name + "-" + getNextThreadNumber()); + StringBuilder tdNameBuilder = new StringBuilder(name); + tdNameBuilder.append("#"); + tdNameBuilder.append(getNext()); + t.setName(tdNameBuilder.toString()); return t; } } diff --git a/kundera-core/src/main/java/com/impetus/kundera/utils/ObjectUtils.java b/kundera-core/src/main/java/com/impetus/kundera/utils/ObjectUtils.java index 6e0582ba7..57e4c632e 100644 --- a/kundera-core/src/main/java/com/impetus/kundera/utils/ObjectUtils.java +++ b/kundera-core/src/main/java/com/impetus/kundera/utils/ObjectUtils.java @@ -44,6 +44,7 @@ import com.impetus.kundera.metadata.model.MetamodelImpl; import com.impetus.kundera.metadata.model.Relation; import com.impetus.kundera.metadata.model.attributes.AbstractAttribute; +import com.impetus.kundera.persistence.context.CacheBase; import com.impetus.kundera.property.PropertyAccessorHelper; import com.impetus.kundera.proxy.KunderaProxy; @@ -71,6 +72,8 @@ public static final Object deepCopy(Object source) /** * @param source + * @param mainCache + * TODO * @return */ private static Object deepCopyUsingMetadata(Object source, Map copiedObjectMap) @@ -216,7 +219,9 @@ else if (attrib.getPersistentAttributeType().equals(PersistentAttributeType.BASI Field relationField = relation.getProperty(); Object sourceRelationObject = PropertyAccessorHelper.getObject(source, relationField); - if (sourceRelationObject != null && !( PersistenceUtilHelper.instanceOfHibernateAbstractPersistentCollection(sourceRelationObject))) + if (sourceRelationObject != null + && !(PersistenceUtilHelper + .instanceOfHibernateAbstractPersistentCollection(sourceRelationObject))) { if (sourceRelationObject instanceof KunderaProxy) { @@ -236,8 +241,7 @@ else if (attrib.getPersistentAttributeType().equals(PersistentAttributeType.BASI for (Object obj : (Collection) sourceRelationObject) { - Object copyTargetRelObj = deepCopyUsingMetadata(obj, copiedObjectMap); - + Object copyTargetRelObj = searchInCacheThenCopy(copiedObjectMap, obj); m.invoke(targetRelationObject, copyTargetRelObj); } @@ -252,8 +256,15 @@ else if (Map.class.isAssignableFrom(relationObjectClass)) { Object valObj = ((Map) sourceRelationObject).get(keyObj); - Object copyTargetKeyObj = deepCopyUsingMetadata(keyObj, copiedObjectMap); - Object copyTargetValueObj = deepCopyUsingMetadata(valObj, copiedObjectMap); + // Object copyTargetKeyObj = + // deepCopyUsingMetadata(keyObj, copiedObjectMap, + // mainCache); + Object copyTargetKeyObj = searchInCacheThenCopy(copiedObjectMap, keyObj); + + // Object copyTargetValueObj = + // deepCopyUsingMetadata(valObj, copiedObjectMap, + // mainCache); + Object copyTargetValueObj = searchInCacheThenCopy(copiedObjectMap, valObj); m.invoke(targetRelationObject, new Object[] { copyTargetKeyObj, copyTargetValueObj }); } @@ -261,7 +272,10 @@ else if (Map.class.isAssignableFrom(relationObjectClass)) } else { - targetRelationObject = deepCopyUsingMetadata(sourceRelationObject, copiedObjectMap); + // targetRelationObject = + // deepCopyUsingMetadata(sourceRelationObject, + // copiedObjectMap, mainCache); + targetRelationObject = searchInCacheThenCopy(copiedObjectMap, sourceRelationObject); } PropertyAccessorHelper.set(target, relationField, targetRelationObject); } @@ -296,6 +310,21 @@ else if (Map.class.isAssignableFrom(relationObjectClass)) return target; } + private static Object searchInCacheThenCopy(Map copiedObjectMap, Object sourceObject) + { + Object copyTargetRelObj = null; + /* + * if(mainCache == null || mainCache.getNodeFromCache(sourceObject) == + * null) { + */ + copyTargetRelObj = deepCopyUsingMetadata(sourceObject, copiedObjectMap); + /* + * } else { copyTargetRelObj = mainCache.getNodeFromCache(sourceObject); + * } + */ + return copyTargetRelObj; + } + /** * Gets the field instance. * diff --git a/kundera-core/src/test/java/com/impetus/kundera/EntityManagerImplTest.java b/kundera-core/src/test/java/com/impetus/kundera/EntityManagerImplTest.java index 552441cda..a7c5c766c 100644 --- a/kundera-core/src/test/java/com/impetus/kundera/EntityManagerImplTest.java +++ b/kundera-core/src/test/java/com/impetus/kundera/EntityManagerImplTest.java @@ -47,7 +47,7 @@ public class EntityManagerImplTest @Before public void setUp() { - KunderaMetadata.INSTANCE.unloadKunderaMetadata("kunderatest"); + KunderaMetadata.INSTANCE.setApplicationMetadata(null); emf = Persistence.createEntityManagerFactory("kunderatest"); em = emf.createEntityManager(); diff --git a/kundera-core/src/test/java/com/impetus/kundera/configure/SchemaConfigurationTest.java b/kundera-core/src/test/java/com/impetus/kundera/configure/SchemaConfigurationTest.java index 63f2b7506..07cb9fffb 100644 --- a/kundera-core/src/test/java/com/impetus/kundera/configure/SchemaConfigurationTest.java +++ b/kundera-core/src/test/java/com/impetus/kundera/configure/SchemaConfigurationTest.java @@ -60,6 +60,8 @@ public class SchemaConfigurationTest private String[] persistenceUnits = new String[] { persistenceUnit }; + private ClientMetadataBuilder builder; + /** * Sets the up before class. * @@ -91,7 +93,8 @@ public static void tearDownAfterClass() throws Exception @Before public void setUp() throws Exception { - configuration = new SchemaConfiguration(null, persistenceUnit); + builder = new ClientMetadataBuilder(null, persistenceUnits); + // configuration = new SchemaConfiguration(null, persistenceUnit); puToSchemaMetadata = new HashMap>(); } @@ -115,7 +118,8 @@ public void testConfigure() intialize(); try { - configuration.configure(); + Map map = new HashMap(); + builder.buildClientFactoryMetadata(map); } catch (ClientResolverException cre) { @@ -136,8 +140,8 @@ private void intialize() props.put(PersistenceProperties.KUNDERA_PORT, "9160"); props.put(PersistenceProperties.KUNDERA_KEYSPACE, "KunderaCoreExmples"); props.put(PersistenceProperties.KUNDERA_DDL_AUTO_PREPARE, "create"); - // props.put(PersistenceProperties.KUNDERA_CLIENT_FACTORY, - // "com.impetus.client.cassandra.pelops.PelopsClientFactory"); + props.put(PersistenceProperties.KUNDERA_CLIENT_FACTORY, + "com.impetus.kundera.cache.ehcache.CoreTestClientFactory"); ApplicationMetadata appMetadata = KunderaMetadata.INSTANCE.getApplicationMetadata(); PersistenceUnitMetadata puMetadata = new PersistenceUnitMetadata(); puMetadata.setPersistenceUnitName(persistenceUnit); diff --git a/kundera-hbase/src/main/java/com/impetus/client/hbase/schemamanager/HBaseSchemaManager.java b/kundera-hbase/src/main/java/com/impetus/client/hbase/schemamanager/HBaseSchemaManager.java index 1ad648902..1e61fec35 100644 --- a/kundera-hbase/src/main/java/com/impetus/client/hbase/schemamanager/HBaseSchemaManager.java +++ b/kundera-hbase/src/main/java/com/impetus/client/hbase/schemamanager/HBaseSchemaManager.java @@ -50,6 +50,8 @@ */ public class HBaseSchemaManager extends AbstractSchemaManager implements SchemaManager { + private static final String DEFAULT_ZOOKEEPER_PORT = "2181"; + /** * Hbase admin variable holds the admin authorities. */ @@ -301,44 +303,62 @@ public void dropSchema() */ protected boolean initiateClient() { - if (host == null || !StringUtils.isNumeric(port) || port.isEmpty()) + String message = null; + for (String host : hosts) { - logger.error("Host or port should not be null / port should be numeric"); - throw new IllegalArgumentException("Host or port should not be null / port should be numeric"); - } + vaildateHostPort(host, port); - Configuration hadoopConf = new Configuration(); - hadoopConf.set("hbase.master", host + ":" + port); - conn = HBasePropertyReader.hsmd.getDataStore() != null ? HBasePropertyReader.hsmd.getDataStore() - .getConnection() : null; - if (conn != null && conn.getProperties() != null) - { - String zookeeperHost = conn.getProperties().getProperty("hbase.zookeeper.quorum"); - String zookeeperPort = conn.getProperties().getProperty("hbase.zookeeper.property.clientPort"); - hadoopConf.set("hbase.zookeeper.quorum", zookeeperHost != null ? zookeeperHost : host); - hadoopConf.set("hbase.zookeeper.property.clientPort", zookeeperPort != null ? zookeeperPort : "2181"); - } - else - { - hadoopConf.set("hbase.zookeeper.quorum", host); - hadoopConf.set("hbase.zookeeper.property.clientPort", "2181"); - } - HBaseConfiguration conf = new HBaseConfiguration(hadoopConf); - try - { - admin = new HBaseAdmin(conf); - } - catch (MasterNotRunningException mnre) - { - logger.error("Master not running exception, Caused by:", mnre); - throw new SchemaGenerationException(mnre, "Hbase"); + Configuration hadoopConf = new Configuration(); + hadoopConf.set("hbase.master", host + ":" + port); + conn = HBasePropertyReader.hsmd.getDataStore() != null ? HBasePropertyReader.hsmd.getDataStore() + .getConnection() : null; + if (conn != null && conn.getProperties() != null) + { + String zookeeperHost = conn.getProperties().getProperty("hbase.zookeeper.quorum"); + String zookeeperPort = conn.getProperties().getProperty("hbase.zookeeper.property.clientPort"); + vaildateHostPort(zookeeperHost, zookeeperPort); + hadoopConf.set("hbase.zookeeper.quorum", zookeeperHost != null ? zookeeperHost : host); + hadoopConf.set("hbase.zookeeper.property.clientPort", zookeeperPort != null ? zookeeperPort + : DEFAULT_ZOOKEEPER_PORT); + } + else + { + hadoopConf.set("hbase.zookeeper.quorum", host); + hadoopConf.set("hbase.zookeeper.property.clientPort", DEFAULT_ZOOKEEPER_PORT); + } + HBaseConfiguration conf = new HBaseConfiguration(hadoopConf); + try + { + admin = new HBaseAdmin(conf); + return true; + } + catch (MasterNotRunningException mnre) + { + message = mnre.getMessage(); + logger.error("Master not running exception, Caused by:", mnre); + } + catch (ZooKeeperConnectionException zkce) + { + message = zkce.getMessage(); + logger.error("Unable to connect to zookeeper, Caused by:", zkce); + } } - catch (ZooKeeperConnectionException zkce) + throw new SchemaGenerationException("Master not running exception, Caused by:" + message); + } + + /** + * Validate host and port. + * + * @param host + * @param port + */ + private void vaildateHostPort(String host, String port) + { + if (host == null || !StringUtils.isNumeric(port) || port.isEmpty()) { - logger.error("Unable to connect to zookeeper, Caused by:", zkce); - throw new SchemaGenerationException(zkce, "Hbase"); + logger.error("Host or port should not be null / port should be numeric"); + throw new IllegalArgumentException("Host or port should not be null / port should be numeric"); } - return true; } /** diff --git a/kundera-hbase/src/test/java/com/impetus/client/hbase/schemaManager/HBaseSchemaOperationTest.java b/kundera-hbase/src/test/java/com/impetus/client/hbase/schemaManager/HBaseSchemaOperationTest.java index ac1e1fb90..28c9c68d5 100644 --- a/kundera-hbase/src/test/java/com/impetus/client/hbase/schemaManager/HBaseSchemaOperationTest.java +++ b/kundera-hbase/src/test/java/com/impetus/client/hbase/schemaManager/HBaseSchemaOperationTest.java @@ -145,7 +145,8 @@ public void testCreate() throws IOException public void testCreatedrop() throws IOException { getEntityManagerFactory("create-drop"); - + schemaManager = new HBaseSchemaManager(HBaseClientFactory.class.getName(), null); + schemaManager.exportSchema(); Assert.assertTrue(admin.isTableAvailable(HBASE_ENTITY_SIMPLE)); HTableDescriptor descriptor = admin.getTableDescriptor(HBASE_ENTITY_SIMPLE.getBytes()); @@ -157,7 +158,7 @@ public void testCreatedrop() throws IOException Assert.assertNotNull(columnDescriptor.getNameAsString()); Assert.assertEquals(HBASE_ENTITY_SIMPLE, columnDescriptor.getNameAsString()); } - ((HBaseClientFactory) ClientResolver.getClientFactory(persistenceUnit)).destroy(); + schemaManager.dropSchema(); Assert.assertFalse(admin.isTableAvailable(HBASE_ENTITY_SIMPLE)); } diff --git a/kundera-mongo/src/main/java/com/impetus/client/mongodb/schemamanager/MongoDBSchemaManager.java b/kundera-mongo/src/main/java/com/impetus/client/mongodb/schemamanager/MongoDBSchemaManager.java index bd9827b2f..d7e6f836a 100644 --- a/kundera-mongo/src/main/java/com/impetus/client/mongodb/schemamanager/MongoDBSchemaManager.java +++ b/kundera-mongo/src/main/java/com/impetus/client/mongodb/schemamanager/MongoDBSchemaManager.java @@ -37,7 +37,6 @@ import com.mongodb.DBCollection; import com.mongodb.DBObject; import com.mongodb.Mongo; -import com.mongodb.MongoException; /** * The Class MongoDBSchemaManager manages auto schema operation @@ -194,29 +193,27 @@ protected void validate(List tableInfos) */ protected boolean initiateClient() { - if (host == null || !StringUtils.isNumeric(port) || port.isEmpty()) + String message = null; + for (String host : hosts) { - logger.error("Host or port should not be null / port should be numeric"); - throw new IllegalArgumentException("Host or port should not be null / port should be numeric"); - } - - int localport = Integer.parseInt(port); - try - { - mongo = new Mongo(host, localport); - db = mongo.getDB(databaseName); - } - catch (UnknownHostException e) - { - logger.error("Database host cannot be resolved, Caused by", e); - throw new SchemaGenerationException(e, "mongoDb"); - } - catch (MongoException e) - { - throw new SchemaGenerationException(e); + if (host == null || !StringUtils.isNumeric(port) || port.isEmpty()) + { + logger.error("Host or port should not be null / port should be numeric"); + throw new IllegalArgumentException("Host or port should not be null / port should be numeric"); + } + try + { + mongo = new Mongo(host, Integer.parseInt(port)); + db = mongo.getDB(databaseName); + return true; + } + catch (UnknownHostException e) + { + message = e.getMessage(); + logger.error("Database host cannot be resolved, Caused by", e); + } } - - return true; + throw new SchemaGenerationException("Database host cannot be resolved, Caused by" + message); } /** @@ -258,18 +255,18 @@ private void createIndexes(TableInfo tableInfo, DBCollection collection) { for (IndexInfo indexInfo : tableInfo.getColumnsToBeIndexed()) { - DBObject keys = new BasicDBObject(); - getIndexType(indexInfo.getIndexType(), keys, indexInfo.getColumnName()); - DBObject options = new BasicDBObject(); - if (indexInfo.getMinValue() != null) - { - options.put(MongoDBConstants.MIN, indexInfo.getMinValue()); - } - if (indexInfo.getMaxValue() != null) - { - options.put(MongoDBConstants.MAX, indexInfo.getMaxValue()); - } - collection.ensureIndex(keys, options); + DBObject keys = new BasicDBObject(); + getIndexType(indexInfo.getIndexType(), keys, indexInfo.getColumnName()); + DBObject options = new BasicDBObject(); + if (indexInfo.getMinValue() != null) + { + options.put(MongoDBConstants.MIN, indexInfo.getMinValue()); + } + if (indexInfo.getMaxValue() != null) + { + options.put(MongoDBConstants.MAX, indexInfo.getMaxValue()); + } + collection.ensureIndex(keys, options); } } diff --git a/kundera-mongo/src/test/resources/META-INF/persistence.xml b/kundera-mongo/src/test/resources/META-INF/persistence.xml index e3288cec1..dd8038628 100644 --- a/kundera-mongo/src/test/resources/META-INF/persistence.xml +++ b/kundera-mongo/src/test/resources/META-INF/persistence.xml @@ -8,8 +8,8 @@ com.impetus.kundera.KunderaPersistence - - + + @@ -28,8 +28,8 @@ com.impetus.kundera.KunderaPersistence - - + + @@ -47,8 +47,8 @@ com.impetus.kundera.KunderaPersistence - - + + @@ -65,8 +65,8 @@ com.impetus.kundera.KunderaPersistence - - + + @@ -83,8 +83,8 @@ com.impetus.kundera.KunderaPersistence - - + + @@ -102,8 +102,8 @@ com.impetus.kundera.KunderaPersistence - - + + diff --git a/kundera-neo4j/src/main/java/com/impetus/client/neo4j/Neo4JClientFactory.java b/kundera-neo4j/src/main/java/com/impetus/client/neo4j/Neo4JClientFactory.java index ba187b330..0c2fb07ff 100644 --- a/kundera-neo4j/src/main/java/com/impetus/client/neo4j/Neo4JClientFactory.java +++ b/kundera-neo4j/src/main/java/com/impetus/client/neo4j/Neo4JClientFactory.java @@ -134,7 +134,8 @@ public SchemaManager getSchemaManager(Map puProperties) @Override public void destroy() { - this.externalProperties = null; + this.externalProperties = null; + ((GraphDatabaseService) getConnectionPoolOrConnection()).shutdown(); // Not required for multithreaded clients } diff --git a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/GraphEntityMapperTest.java b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/GraphEntityMapperTest.java index dce52f082..49a8d7c61 100644 --- a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/GraphEntityMapperTest.java +++ b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/GraphEntityMapperTest.java @@ -62,7 +62,7 @@ public class GraphEntityMapperTest GraphDatabaseService graphDb; - final static String PU = "neo4jTest"; + final static String PU = "imdb"; /** * @throws java.lang.Exception diff --git a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/Neo4JClientFactoryTest.java b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/Neo4JClientFactoryTest.java index ca19b04a4..ef1656e69 100644 --- a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/Neo4JClientFactoryTest.java +++ b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/Neo4JClientFactoryTest.java @@ -15,6 +15,8 @@ */ package com.impetus.client.neo4j; +import java.io.File; +import java.io.IOException; import java.lang.reflect.Field; import javax.persistence.EntityManagerFactory; @@ -26,11 +28,15 @@ import org.junit.Before; import org.junit.Test; import org.neo4j.kernel.EmbeddedGraphDatabase; +import org.neo4j.kernel.impl.util.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.impetus.kundera.PersistenceProperties; import com.impetus.kundera.client.ClientResolver; import com.impetus.kundera.loader.ClientFactory; +import com.impetus.kundera.metadata.KunderaMetadataManager; +import com.impetus.kundera.metadata.model.PersistenceUnitMetadata; /** * Test case for {@link Neo4JClientFactory} @@ -112,11 +118,18 @@ public void testConnection() /** * Tear down. + * @throws IOException */ @After - public void tearDown() + public void tearDown() throws IOException { + PersistenceUnitMetadata puMetadata = KunderaMetadataManager.getPersistenceUnitMetadata(persistenceUnit); + String datastoreFilePath = puMetadata.getProperty(PersistenceProperties.KUNDERA_DATASTORE_FILE_PATH); + emf.close(); + + if (datastoreFilePath != null) + FileUtils.deleteRecursively(new File(datastoreFilePath)); } } diff --git a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/Neo4JClientTest.java b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/Neo4JClientTest.java index 1936360cb..2fdc93c8c 100644 --- a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/Neo4JClientTest.java +++ b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/Neo4JClientTest.java @@ -48,7 +48,7 @@ public class Neo4JClientTest GraphEntityMapper mapper; - final static String PU = "neo4jTest"; + final static String PU = "imdb"; /** * @throws java.lang.Exception @@ -74,8 +74,8 @@ public static void tearDownAfterClass() throws Exception em.close(); emf.close(); - if (datastoreFilePath != null) - FileUtils.deleteRecursively(new File(datastoreFilePath)); + /* if (datastoreFilePath != null) + FileUtils.deleteRecursively(new File(datastoreFilePath));*/ } /** diff --git a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBBatchInsertionTest.java b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBBatchInsertionTest.java index 47e345c70..b2be3157b 100644 --- a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBBatchInsertionTest.java +++ b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBBatchInsertionTest.java @@ -32,6 +32,7 @@ import com.impetus.kundera.PersistenceProperties; import com.impetus.kundera.metadata.KunderaMetadataManager; +import com.impetus.kundera.metadata.model.KunderaMetadata; import com.impetus.kundera.metadata.model.PersistenceUnitMetadata; /** @@ -71,6 +72,7 @@ public void tearDown() throws Exception String datastoreFilePath = puMetadata.getProperty(PersistenceProperties.KUNDERA_DATASTORE_FILE_PATH); em.close(); + KunderaMetadata.INSTANCE.setApplicationMetadata(null); emf.close(); if (datastoreFilePath != null) diff --git a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBCRUDTest.java b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBCRUDTest.java index a4189f168..2d1a495ec 100644 --- a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBCRUDTest.java +++ b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBCRUDTest.java @@ -43,8 +43,7 @@ public void setUp() throws Exception @After public void tearDown() throws Exception { - em.close(); - emf.close(); + clean(); } @Test diff --git a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBJPAQueriesTest.java b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBJPAQueriesTest.java index ad6f9db9e..77d2653f2 100644 --- a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBJPAQueriesTest.java +++ b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBJPAQueriesTest.java @@ -66,8 +66,7 @@ public void tearDown() throws Exception em.getTransaction().commit(); } - em.close(); - emf.close(); + clean(); } @Test diff --git a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBMapMetamodelTest.java b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBMapMetamodelTest.java index 5480b5bc5..a994fafda 100644 --- a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBMapMetamodelTest.java +++ b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBMapMetamodelTest.java @@ -40,9 +40,7 @@ public class IMDBMapMetamodelTest extends IMDBTestBase @Before public void setUp() throws Exception { - emf = Persistence.createEntityManagerFactory("imdb"); - em = emf.createEntityManager(); - + init(); } /** @@ -51,8 +49,7 @@ public void setUp() throws Exception @After public void tearDown() throws Exception { - em.close(); - emf.close(); + clean(); } @Test diff --git a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBNativeLuceneQueryTest.java b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBNativeLuceneQueryTest.java index 467ded253..a3196fd03 100644 --- a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBNativeLuceneQueryTest.java +++ b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBNativeLuceneQueryTest.java @@ -65,8 +65,7 @@ public void tearDown() throws Exception em.getTransaction().commit(); } - em.close(); - emf.close(); + clean(); } @Test diff --git a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBTransactionTest.java b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBTransactionTest.java index b4df23b6e..81e9ef6e3 100644 --- a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBTransactionTest.java +++ b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/IMDBTransactionTest.java @@ -15,12 +15,21 @@ */ package com.impetus.client.neo4j.imdb; +import java.io.File; + import javax.persistence.Persistence; import org.junit.After; +import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; +import org.neo4j.kernel.impl.util.FileUtils; + +import com.impetus.kundera.PersistenceProperties; +import com.impetus.kundera.metadata.KunderaMetadataManager; +import com.impetus.kundera.metadata.model.PersistenceUnitMetadata; /** * Test case for validating transaction handling provided by Kundera for Neo4J @@ -30,6 +39,21 @@ public class IMDBTransactionTest extends IMDBTestBase { + /* @BeforeClass + public static void setUpBeforeClass() throws Exception + { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception + { + + PersistenceUnitMetadata puMetadata = KunderaMetadataManager.getPersistenceUnitMetadata(IMDB_PU); + String datastoreFilePath = puMetadata.getProperty(PersistenceProperties.KUNDERA_DATASTORE_FILE_PATH); + FileUtils.deleteRecursively(new File(datastoreFilePath)); +// emf.close(); + }*/ + /** * @throws java.lang.Exception */ @@ -52,8 +76,8 @@ public void tearDown() throws Exception em.remove(actor2); em.getTransaction().commit(); - em.close(); - emf.close(); +// em.close(); + clean(); } @Test diff --git a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/composite/IMDBCompositeKeyTest.java b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/composite/IMDBCompositeKeyTest.java index 89f6a00a7..00ee7b399 100644 --- a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/composite/IMDBCompositeKeyTest.java +++ b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/composite/IMDBCompositeKeyTest.java @@ -15,6 +15,7 @@ */ package com.impetus.client.neo4j.imdb.composite; +import java.io.File; import java.util.Map; import javax.persistence.EntityManager; @@ -25,6 +26,7 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.neo4j.kernel.impl.util.FileUtils; import com.impetus.kundera.PersistenceProperties; import com.impetus.kundera.metadata.KunderaMetadataManager; @@ -72,6 +74,9 @@ public void tearDown() throws Exception em.close(); emf.close(); + /*if (datastoreFilePath != null) + FileUtils.deleteRecursively(new File(datastoreFilePath));*/ + } @Test diff --git a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/datatype/IMDBAllDataTypeTest.java b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/datatype/IMDBAllDataTypeTest.java index 3b5204461..250a8ab64 100644 --- a/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/datatype/IMDBAllDataTypeTest.java +++ b/kundera-neo4j/src/test/java/com/impetus/client/neo4j/imdb/datatype/IMDBAllDataTypeTest.java @@ -15,6 +15,7 @@ */ package com.impetus.client.neo4j.imdb.datatype; +import java.io.File; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Calendar; @@ -29,6 +30,7 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.neo4j.kernel.impl.util.FileUtils; import com.impetus.kundera.PersistenceProperties; import com.impetus.kundera.metadata.KunderaMetadataManager; @@ -112,6 +114,8 @@ public void tearDown() throws Exception em.close(); emf.close(); + if (datastoreFilePath != null) + FileUtils.deleteRecursively(new File(datastoreFilePath)); } @@ -123,12 +127,6 @@ public void testCRUD() delete(); } - @Test - public void testDummy() - { - - } - private void insert() { prepareData(); diff --git a/kundera-neo4j/src/test/resources/META-INF/persistence.xml b/kundera-neo4j/src/test/resources/META-INF/persistence.xml index b6813c2f5..42acdace0 100644 --- a/kundera-neo4j/src/test/resources/META-INF/persistence.xml +++ b/kundera-neo4j/src/test/resources/META-INF/persistence.xml @@ -40,20 +40,20 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + + + diff --git a/kundera-oracle-nosql/src/main/java/com/impetus/client/oraclenosql/query/OracleNoSQLQuery.java b/kundera-oracle-nosql/src/main/java/com/impetus/client/oraclenosql/query/OracleNoSQLQuery.java index bd121d06b..5c21136d3 100644 --- a/kundera-oracle-nosql/src/main/java/com/impetus/client/oraclenosql/query/OracleNoSQLQuery.java +++ b/kundera-oracle-nosql/src/main/java/com/impetus/client/oraclenosql/query/OracleNoSQLQuery.java @@ -29,13 +29,9 @@ import com.impetus.client.oraclenosql.OracleNoSQLClient; import com.impetus.client.oraclenosql.OracleNoSQLEntityReader; -import com.impetus.kundera.PersistenceProperties; import com.impetus.kundera.client.Client; import com.impetus.kundera.index.LuceneIndexer; -import com.impetus.kundera.metadata.model.ApplicationMetadata; -import com.impetus.kundera.metadata.model.ClientMetadata; import com.impetus.kundera.metadata.model.EntityMetadata; -import com.impetus.kundera.metadata.model.KunderaMetadata; import com.impetus.kundera.metadata.model.attributes.AbstractAttribute; import com.impetus.kundera.persistence.EntityReader; import com.impetus.kundera.persistence.PersistenceDelegator; @@ -69,12 +65,7 @@ protected List populateEntities(EntityMetadata m, Client client) } Set results = new HashSet(); - ApplicationMetadata appMetadata = KunderaMetadata.INSTANCE.getApplicationMetadata(); - String indexerClass = KunderaMetadata.INSTANCE.getApplicationMetadata() - .getPersistenceUnitMetadata(m.getPersistenceUnit()).getProperties() - .getProperty(PersistenceProperties.KUNDERA_INDEXER_CLASS); - ClientMetadata clientMetadata = KunderaMetadata.INSTANCE.getClientMetadata(m.getPersistenceUnit()); OracleNoSQLQueryInterpreter interpreter = translateQuery(getKunderaQuery().getFilterClauseQueue(), m); Set resultsFromIdSearch = new HashSet(); @@ -181,7 +172,7 @@ else if (clause instanceof String) return interpreter; } - private void addToResults(Set results, Set resultsToAdd, String operation) + /*private void addToResults(Set results, Set resultsToAdd, String operation) { if (resultsToAdd == null || resultsToAdd.isEmpty()) { @@ -202,6 +193,6 @@ else if (operation.equalsIgnoreCase("AND")) } resultsToAdd.clear(); - } + }*/ } diff --git a/kundera-oracle-nosql/src/test/java/com/impetus/client/oraclenosql/OracleEntityTransactionTest.java b/kundera-oracle-nosql/src/test/java/com/impetus/client/oraclenosql/OracleEntityTransactionTest.java index 752f68bab..440acdebc 100644 --- a/kundera-oracle-nosql/src/test/java/com/impetus/client/oraclenosql/OracleEntityTransactionTest.java +++ b/kundera-oracle-nosql/src/test/java/com/impetus/client/oraclenosql/OracleEntityTransactionTest.java @@ -297,7 +297,8 @@ public void tearDown() throws Exception String deleteQuery = "Delete from PersonKVStore p"; Query query = em.createQuery(deleteQuery); int updateCount = query.executeUpdate(); - + em.close(); + emf.close(); } /** diff --git a/kundera-oracle-nosql/src/test/java/com/impetus/client/oraclenosql/datatypes/tests/StudentOracleNoSQLBigDecimalTest.java b/kundera-oracle-nosql/src/test/java/com/impetus/client/oraclenosql/datatypes/tests/StudentOracleNoSQLBigDecimalTest.java index 66674cf6e..cc1625f94 100644 --- a/kundera-oracle-nosql/src/test/java/com/impetus/client/oraclenosql/datatypes/tests/StudentOracleNoSQLBigDecimalTest.java +++ b/kundera-oracle-nosql/src/test/java/com/impetus/client/oraclenosql/datatypes/tests/StudentOracleNoSQLBigDecimalTest.java @@ -19,8 +19,6 @@ public class StudentOracleNoSQLBigDecimalTest extends OracleNoSQLBase { - private static final String keyspace = "KunderaTests"; - private EntityManagerFactory emf; @Before diff --git a/kundera-rdbms/pom.xml b/kundera-rdbms/pom.xml index 6d73a4601..aed32656e 100644 --- a/kundera-rdbms/pom.xml +++ b/kundera-rdbms/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 kundera @@ -35,12 +36,17 @@ hibernate-entitymanager 4.1.7.Final - + + - hsqldb + org.hsqldb hsqldb - 1.8.0.7 + 2.2.9 test diff --git a/kundera-rdbms/src/main/java/com/impetus/client/rdbms/query/RDBMSQuery.java b/kundera-rdbms/src/main/java/com/impetus/client/rdbms/query/RDBMSQuery.java index 061ecae87..33475c10d 100644 --- a/kundera-rdbms/src/main/java/com/impetus/client/rdbms/query/RDBMSQuery.java +++ b/kundera-rdbms/src/main/java/com/impetus/client/rdbms/query/RDBMSQuery.java @@ -122,7 +122,7 @@ protected List populateEntities(EntityMetadata m, Client client) } catch (Exception e) { - log.error("Error during query execution" + e.getMessage()); + log.error("Error during query execution ", e); throw new QueryHandlerException(e); } return result; diff --git a/kundera-rdbms/src/test/java/com/impetus/client/crud/PersonRdbmsTest.java b/kundera-rdbms/src/test/java/com/impetus/client/crud/PersonRdbmsTest.java index 8db555fe1..146717bb1 100644 --- a/kundera-rdbms/src/test/java/com/impetus/client/crud/PersonRdbmsTest.java +++ b/kundera-rdbms/src/test/java/com/impetus/client/crud/PersonRdbmsTest.java @@ -140,6 +140,8 @@ public void tearDown() throws Exception { em.remove(val); } + em.close(); + emf.close(); cli.update("DELETE FROM TESTDB.PERSON"); cli.update("DROP TABLE TESTDB.PERSON"); cli.update("DROP SCHEMA TESTDB"); diff --git a/kundera-rdbms/src/test/java/com/impetus/client/crud/RDBMSCli.java b/kundera-rdbms/src/test/java/com/impetus/client/crud/RDBMSCli.java index 8fdfd2a90..001e44ca5 100644 --- a/kundera-rdbms/src/test/java/com/impetus/client/crud/RDBMSCli.java +++ b/kundera-rdbms/src/test/java/com/impetus/client/crud/RDBMSCli.java @@ -176,7 +176,7 @@ public void createSchema(final String schemaName) throws SQLException public void dropSchema(final String schemaName) throws SQLException { - String sql = "drop schema " + schemaName; + String sql = "drop schema " + schemaName ; update(sql); } diff --git a/kundera-rdbms/src/test/java/com/impetus/client/crud/datatypes/StudentBase.java b/kundera-rdbms/src/test/java/com/impetus/client/crud/datatypes/StudentBase.java index 253c0df66..8b8835f91 100644 --- a/kundera-rdbms/src/test/java/com/impetus/client/crud/datatypes/StudentBase.java +++ b/kundera-rdbms/src/test/java/com/impetus/client/crud/datatypes/StudentBase.java @@ -130,21 +130,25 @@ protected void setupInternal(String persisntenceUnit) */ protected void teardownInternal(String persistenceUnit) { - - if (RUN_IN_EMBEDDED_MODE) - { - stopServer(); - } - if (AUTO_MANAGE_SCHEMA) { deleteSchema(); } + if (em != null) + { + em.close(); + } if (emf != null) { emf.close(); } + + if (RUN_IN_EMBEDDED_MODE) + { + stopServer(); + } + } /** diff --git a/kundera-rdbms/src/test/java/com/impetus/client/crud/datatypes/StudentRdbmsTest.java b/kundera-rdbms/src/test/java/com/impetus/client/crud/datatypes/StudentRdbmsTest.java index f5bf1fc2c..8df582c32 100644 --- a/kundera-rdbms/src/test/java/com/impetus/client/crud/datatypes/StudentRdbmsTest.java +++ b/kundera-rdbms/src/test/java/com/impetus/client/crud/datatypes/StudentRdbmsTest.java @@ -49,8 +49,8 @@ public void setUp() throws Exception cli.createSchema("testdb"); // cli.update("USE testdb"); cli.update("CREATE TABLE TESTDB.STUDENT (STUDENT_ID BIGINT PRIMARY KEY,UNIQUE_ID BIGINT, STUDENT_NAME VARCHAR(256), AGE SMALLINT,IS_EXCEPTIONAL BOOLEAN,SEMESTER VARCHAR(4),DIGITAL_SIGNATURE TINYINT,CGPA SMALLINT," - + "PERCENTAGE DECIMAL(10,1),HEIGHT DECIMAL(10,8) ,ENROLMENT_DATE DATE,ENROLMENT_TIME TIME,JOINING_DATE_TIME TIMESTAMP,YEARS_SPENT INTEGER,ROLL_NUMBER BIGINT" - + ",MONTHLY_FEE DECIMAL(10,2),SQL_DATE DATE,SQL_TIMESTAMP TIMESTAMP , SQL_TIME TIME,BIG_INT BIGINT,BIG_DECIMAL DECIMAL,CALENDAR TIMESTAMP)"); + + "PERCENTAGE DECIMAL(10,8),HEIGHT DECIMAL(11,8) ,ENROLMENT_DATE DATE,ENROLMENT_TIME TIME,JOINING_DATE_TIME TIMESTAMP,YEARS_SPENT INTEGER,ROLL_NUMBER BIGINT" + + ",MONTHLY_FEE DOUBLE,SQL_DATE DATE,SQL_TIMESTAMP TIMESTAMP , SQL_TIME TIME,BIG_INT BIGINT,BIG_DECIMAL DECIMAL,CALENDAR TIMESTAMP)"); setupInternal(persistenceUnit); } diff --git a/kundera-rdbms/src/test/resources/META-INF/persistence.xml b/kundera-rdbms/src/test/resources/META-INF/persistence.xml index 15cf1c233..98e3be75c 100644 --- a/kundera-rdbms/src/test/resources/META-INF/persistence.xml +++ b/kundera-rdbms/src/test/resources/META-INF/persistence.xml @@ -26,7 +26,7 @@ - + @@ -35,6 +35,16 @@ + diff --git a/kundera-redis/src/main/java/com/impetus/client/redis/RedisClient.java b/kundera-redis/src/main/java/com/impetus/client/redis/RedisClient.java index 73f2e83b3..b6d667d9a 100644 --- a/kundera-redis/src/main/java/com/impetus/client/redis/RedisClient.java +++ b/kundera-redis/src/main/java/com/impetus/client/redis/RedisClient.java @@ -69,7 +69,6 @@ import com.impetus.kundera.property.PropertyAccessorFactory; import com.impetus.kundera.property.PropertyAccessorHelper; import com.impetus.kundera.property.accessor.ObjectAccessor; -import com.impetus.kundera.property.accessor.StringAccessor; /** * Redis client implementation for REDIS. @@ -170,8 +169,7 @@ public Object find(Class entityClass, Object key) try { result = fetch(entityClass, key, connection, null); - - } + } catch (InstantiationException e) { logger.error("Error during find by key:", e); diff --git a/kundera-redis/src/test/java/com/impetus/client/RedisClientFactoryTest.java b/kundera-redis/src/test/java/com/impetus/client/RedisClientFactoryTest.java index 9d931f40a..7e2dbfbd8 100644 --- a/kundera-redis/src/test/java/com/impetus/client/RedisClientFactoryTest.java +++ b/kundera-redis/src/test/java/com/impetus/client/RedisClientFactoryTest.java @@ -39,74 +39,87 @@ /** * The Class RedisClientFactoryTest. Junit for {@link RedisClientFactory} */ -public class RedisClientFactoryTest { - - /** The Constant REDIS_PU. */ - private static final String REDIS_PU = "redis_pu"; - - /** The emf. */ - private EntityManagerFactory emf; - - /** The logger. */ - private static Logger logger = LoggerFactory - .getLogger(RedisClientFactoryTest.class); - - /** - * Setup. - */ - @Before - public void setup() { - Map properties = new HashMap(1); - properties.put("kundera.transaction.timeout", "30000"); - properties.put("kundera.pool.size.max.active", "10"); - emf = Persistence.createEntityManagerFactory(REDIS_PU, properties); - } - - /** - * Test connection. - */ - @Test - public void testConnection() { - logger.info("On test connection"); - - ClientFactory clientFactory = ClientResolver.getClientFactory(REDIS_PU); - Assert.assertNotNull(clientFactory); - Assert.assertEquals(RedisClientFactory.class, clientFactory.getClass()); - Field connectionField; - try { - String field_name = "connectionPoolOrConnection"; - connectionField = ((RedisClientFactory) clientFactory).getClass() - .getSuperclass().getDeclaredField(field_name); - - if (!connectionField.isAccessible()) { - connectionField.setAccessible(true); - } - - Object connectionObj = connectionField.get(clientFactory); - - Assert.assertNotNull(connectionObj); - - } catch (SecurityException e) { - logger.error(e.getMessage()); - Assert.fail(e.getMessage()); - } catch (NoSuchFieldException e) { - logger.error(e.getMessage()); - Assert.fail(e.getMessage()); - } catch (IllegalArgumentException e) { - logger.error(e.getMessage()); - Assert.fail(e.getMessage()); - } catch (IllegalAccessException e) { - logger.error(e.getMessage()); - Assert.fail(e.getMessage()); - } - - } - - /** - * Tear down. - */ - @After - public void tearDown() { - emf.close(); - } +public class RedisClientFactoryTest +{ + + /** The Constant REDIS_PU. */ + private static final String REDIS_PU = "redis_pu"; + + /** The emf. */ + private EntityManagerFactory emf; + + /** The logger. */ + private static Logger logger = LoggerFactory.getLogger(RedisClientFactoryTest.class); + + /** + * Setup. + */ + @Before + public void setup() + { + Map properties = new HashMap(1); + properties.put("kundera.transaction.timeout", "30000"); + properties.put("kundera.pool.size.max.active", "10"); + emf = Persistence.createEntityManagerFactory(REDIS_PU, properties); + } + + /** + * Test connection. + */ + @Test + public void testConnection() + { + logger.info("On test connection"); + + ClientFactory clientFactory = ClientResolver.getClientFactory(REDIS_PU); + Assert.assertNotNull(clientFactory); + Assert.assertEquals(RedisClientFactory.class, clientFactory.getClass()); + Field connectionField; + try + { + String field_name = "connectionPoolOrConnection"; + connectionField = ((RedisClientFactory) clientFactory).getClass().getSuperclass() + .getDeclaredField(field_name); + + if (!connectionField.isAccessible()) + { + connectionField.setAccessible(true); + } + + Object connectionObj = connectionField.get(clientFactory); + + Assert.assertNotNull(connectionObj); + + } + catch (SecurityException e) + { + logger.error(e.getMessage()); + Assert.fail(e.getMessage()); + } + catch (NoSuchFieldException e) + { + logger.error(e.getMessage()); + Assert.fail(e.getMessage()); + } + catch (IllegalArgumentException e) + { + logger.error(e.getMessage()); + Assert.fail(e.getMessage()); + } + catch (IllegalAccessException e) + { + logger.error(e.getMessage()); + Assert.fail(e.getMessage()); + } + + } + + /** + * Tear down. + */ + @After + public void tearDown() + { + emf.close(); + } } diff --git a/kundera-redis/src/test/java/com/impetus/client/RedisClientTest.java b/kundera-redis/src/test/java/com/impetus/client/RedisClientTest.java index a7a75e95b..e673ec1d2 100644 --- a/kundera-redis/src/test/java/com/impetus/client/RedisClientTest.java +++ b/kundera-redis/src/test/java/com/impetus/client/RedisClientTest.java @@ -101,9 +101,10 @@ public void testCRUD() em.close(); } -// @Test + @Test public void testCRUDWithBatch() { + Map batchProperty = new HashMap(1); batchProperty.put(PersistenceProperties.KUNDERA_BATCH_SIZE, "5"); EntityManagerFactory emf = Persistence.createEntityManagerFactory(REDIS_PU, batchProperty); diff --git a/kundera-redis/src/test/java/com/impetus/client/generatedId/RedisGeneratedIdTest.java b/kundera-redis/src/test/java/com/impetus/client/generatedId/RedisGeneratedIdTest.java index 26fb4146a..60b5d839c 100644 --- a/kundera-redis/src/test/java/com/impetus/client/generatedId/RedisGeneratedIdTest.java +++ b/kundera-redis/src/test/java/com/impetus/client/generatedId/RedisGeneratedIdTest.java @@ -72,12 +72,6 @@ public void tearDown() throws Exception emf.close(); } - // @Test - public void testDemo() - { - - } - @Test public void testPersist() { diff --git a/kundera-redis/src/test/resources/META-INF/persistence.xml b/kundera-redis/src/test/resources/META-INF/persistence.xml index 56f675642..b71b39791 100644 --- a/kundera-redis/src/test/resources/META-INF/persistence.xml +++ b/kundera-redis/src/test/resources/META-INF/persistence.xml @@ -7,8 +7,8 @@ com.impetus.kundera.KunderaPersistence - - + + diff --git a/kundera-redis/src/test/resources/RedisTest.xml b/kundera-redis/src/test/resources/RedisTest.xml index 5434f8ca7..b09c4af7a 100644 --- a/kundera-redis/src/test/resources/RedisTest.xml +++ b/kundera-redis/src/test/resources/RedisTest.xml @@ -6,7 +6,7 @@ - + @@ -14,4 +14,4 @@ - \ No newline at end of file + diff --git a/kundera-tests/pom.xml b/kundera-tests/pom.xml index fd8c21634..54d7f1b00 100644 --- a/kundera-tests/pom.xml +++ b/kundera-tests/pom.xml @@ -1,4 +1,5 @@ - + 4.0.0 com.impetus diff --git a/kundera-tests/src/test/java/com/impetus/kundera/tests/crossdatastore/generatedId/AddressMongoGeneratedId.java b/kundera-tests/src/test/java/com/impetus/kundera/tests/crossdatastore/generatedId/AddressMongoGeneratedId.java index 86d7e4436..462d9074d 100644 --- a/kundera-tests/src/test/java/com/impetus/kundera/tests/crossdatastore/generatedId/AddressMongoGeneratedId.java +++ b/kundera-tests/src/test/java/com/impetus/kundera/tests/crossdatastore/generatedId/AddressMongoGeneratedId.java @@ -18,7 +18,6 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @@ -32,7 +31,7 @@ public class AddressMongoGeneratedId { @Id @Column(name = "ADDRESS_ID") - @GeneratedValue(strategy = GenerationType.AUTO) + @GeneratedValue() private String addressId; @Column(name = "STREET") @@ -52,5 +51,4 @@ public void setStreet(String street) { this.street = street; } - } \ No newline at end of file diff --git a/kundera-tests/src/test/java/com/impetus/kundera/tests/crossdatastore/generatedId/CrossdatastoreGeneratedIdTest.java b/kundera-tests/src/test/java/com/impetus/kundera/tests/crossdatastore/generatedId/CrossdatastoreGeneratedIdTest.java index 4b5d60b8c..532b57fc6 100644 --- a/kundera-tests/src/test/java/com/impetus/kundera/tests/crossdatastore/generatedId/CrossdatastoreGeneratedIdTest.java +++ b/kundera-tests/src/test/java/com/impetus/kundera/tests/crossdatastore/generatedId/CrossdatastoreGeneratedIdTest.java @@ -14,7 +14,8 @@ import org.junit.Before; import org.junit.Test; -import com.impetus.client.persistence.CassandraCli; +import com.impetus.client.crud.RDBMSCli; +import com.impetus.kundera.tests.cli.CassandraCli; /** * @author impadmin @@ -26,6 +27,8 @@ public class CrossdatastoreGeneratedIdTest private EntityManager em; + private RDBMSCli cli; + /** * @throws java.lang.Exception */ @@ -33,8 +36,7 @@ public class CrossdatastoreGeneratedIdTest public void setUp() throws Exception { CassandraCli.cassandraSetUp(); - emf = Persistence - .createEntityManagerFactory("secIdxAddCassandra,redis,rdbms,addMongo,oracle_kvstore,piccandra,picongo,addCassandra"); + emf = Persistence.createEntityManagerFactory("secIdxAddCassandra,addMongo"); em = emf.createEntityManager(); } @@ -71,5 +73,4 @@ public void test() Assert.assertEquals("sector 20, G Block", result.get(0).getAddress().getStreet()); Assert.assertEquals("Kuldeep", result.get(0).getPersonName()); } - } diff --git a/kundera-tests/src/test/java/com/impetus/kundera/tests/crossdatastore/useraddress/AssociationBase.java b/kundera-tests/src/test/java/com/impetus/kundera/tests/crossdatastore/useraddress/AssociationBase.java index 2bb0d88f3..d7de13194 100644 --- a/kundera-tests/src/test/java/com/impetus/kundera/tests/crossdatastore/useraddress/AssociationBase.java +++ b/kundera-tests/src/test/java/com/impetus/kundera/tests/crossdatastore/useraddress/AssociationBase.java @@ -54,7 +54,6 @@ import com.impetus.kundera.metadata.model.PersistenceUnitMetadata; import com.impetus.kundera.tests.cli.CassandraCli; import com.impetus.kundera.tests.cli.CleanupUtilities; -import com.impetus.kundera.tests.cli.HBaseCli; import com.impetus.kundera.tests.crossdatastore.useraddress.dao.UserAddressDaoImpl; import com.mongodb.DB; diff --git a/kundera-tests/src/test/resources/META-INF/persistence.xml b/kundera-tests/src/test/resources/META-INF/persistence.xml index 25cc65cd2..771b2dc3b 100644 --- a/kundera-tests/src/test/resources/META-INF/persistence.xml +++ b/kundera-tests/src/test/resources/META-INF/persistence.xml @@ -43,8 +43,8 @@ com.impetus.kundera.KunderaPersistence - - + + @@ -93,8 +93,8 @@ com.impetus.kundera.KunderaPersistence - - + + @@ -201,8 +201,8 @@ com.impetus.kundera.KunderaPersistence - - + + @@ -217,8 +217,8 @@ com.impetus.kundera.KunderaPersistence - - + +