From 5f75292c13fbb8c8acd722ce59c014828f1619c6 Mon Sep 17 00:00:00 2001 From: Shawn Feldman Date: Thu, 19 Mar 2015 13:57:19 -0600 Subject: [PATCH] tests passing in queryindex --- .../corepersistence/CpEntityManager.java | 23 +++---- .../CpEntityManagerFactory.java | 2 +- .../corepersistence/CpManagerCache.java | 6 +- .../corepersistence/CpRelationManager.java | 19 +++--- .../events/EntityDeletedHandler.java | 3 +- .../events/EntityVersionDeletedHandler.java | 3 +- .../results/FilteringLoader.java | 3 +- .../usergrid/persistence/EntityManager.java | 7 +- .../cassandra/EntityManagerImpl.java | 9 +-- .../StaleIndexCleanupTest.java | 17 ++--- .../persistence/index/IndexIdentifier.java | 3 + .../index/impl/EsEntityIndexImpl.java | 66 ++++++++----------- .../index/impl/EntityIndexTest.java | 17 +++-- 13 files changed, 76 insertions(+), 102 deletions(-) diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java index 87874109f8..b23111ab27 100644 --- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java +++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java @@ -36,6 +36,7 @@ import com.codahale.metrics.Meter; import org.apache.usergrid.persistence.collection.FieldSet; import org.apache.usergrid.persistence.core.future.BetterFuture; +import org.apache.usergrid.persistence.index.ApplicationEntityIndex; import org.elasticsearch.action.ListenableActionFuture; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -211,6 +212,7 @@ public class CpEntityManager implements EntityManager { private Timer entGetRepairedEntityTimer; private Timer updateEntityTimer; private Meter updateEntityMeter; + private EntityIndex ei; // /** Short-term cache to keep us from reloading same Entity during single request. */ // private LoadingCache entityCache; @@ -221,7 +223,8 @@ public CpEntityManager() { } @Override - public void init( EntityManagerFactory emf, UUID applicationId ) { + public void init( EntityManagerFactory emf, EntityIndex ei, UUID applicationId ) { + this.ei = ei; Preconditions.checkNotNull( emf, "emf must not be null" ); Preconditions.checkNotNull( applicationId, "applicationId must not be null" ); @@ -286,7 +289,6 @@ public void init( EntityManagerFactory emf, UUID applicationId ) { @Override public Health getIndexHealth() { - EntityIndex ei = managerCache.getEntityIndex( applicationScope ); return ei.getIndexHealth(); } @@ -1079,7 +1081,7 @@ public void deleteProperty( EntityRef entityRef, String propertyName ) throws Ex getCollectionScopeNameFromEntityType( entityRef.getType() ) ); EntityCollectionManager ecm = managerCache.getEntityCollectionManager( collectionScope ); - EntityIndex ei = managerCache.getEntityIndex( getApplicationScope() ); + ApplicationEntityIndex aei = managerCache.getEntityIndex( getApplicationScope() ); Id entityId = new SimpleId( entityRef.getUuid(), entityRef.getType() ); @@ -1106,7 +1108,7 @@ public void deleteProperty( EntityRef entityRef, String propertyName ) throws Ex //Adding graphite metrics Timer.Context timeESBatch = esDeletePropertyTimer.time(); - BetterFuture future = ei.createBatch().index( defaultIndexScope, cpEntity ).execute(); + BetterFuture future = aei.createBatch().index( defaultIndexScope, cpEntity ).execute(); timeESBatch.stop(); // update in all containing collections and connection indexes CpRelationManager rm = ( CpRelationManager ) getRelationManager( entityRef ); @@ -2889,24 +2891,15 @@ public void refreshIndex() { emf.refreshIndex(); // refresh this Entity Manager's application's index - EntityIndex ei = managerCache.getEntityIndex( getApplicationScope() ); ei.refresh(); } @Override public void createIndex() { - EntityIndex ei = managerCache.getEntityIndex( applicationScope ); ei.initializeIndex(); } - public ListenableActionFuture deleteIndex(){ - EntityIndex ei = managerCache.getEntityIndex( applicationScope ); - return ei.deleteIndex(); - } - - - @Override @@ -2981,8 +2974,8 @@ void indexEntityIntoCollection( org.apache.usergrid.persistence.model.entity.Ent org.apache.usergrid.persistence.model.entity.Entity memberEntity, String collName ) { - final EntityIndex ei = getManagerCache().getEntityIndex( getApplicationScope() ); - final EntityIndexBatch batch = ei.createBatch(); + final ApplicationEntityIndex aie = getManagerCache().getEntityIndex( getApplicationScope() ); + final EntityIndexBatch batch = aie.createBatch(); // index member into entity collection | type scope IndexScope collectionIndexScope = new IndexScopeImpl( collectionEntity.getId(), diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java index e70e538ccf..14b7865b0d 100644 --- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java +++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManagerFactory.java @@ -199,7 +199,7 @@ public EntityManager getEntityManager(UUID applicationId) { private EntityManager _getEntityManager( UUID applicationId ) { EntityManager em = new CpEntityManager(); - em.init( this, applicationId ); + em.init( this,entityIndex ,applicationId ); return em; } diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpManagerCache.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpManagerCache.java index aa8a139279..4e35f8cc51 100644 --- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpManagerCache.java +++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpManagerCache.java @@ -25,6 +25,7 @@ import org.apache.usergrid.persistence.core.scope.ApplicationScope; import org.apache.usergrid.persistence.graph.GraphManager; import org.apache.usergrid.persistence.graph.GraphManagerFactory; +import org.apache.usergrid.persistence.index.ApplicationEntityIndex; import org.apache.usergrid.persistence.index.EntityIndex; import org.apache.usergrid.persistence.index.EntityIndexFactory; import org.apache.usergrid.persistence.map.MapManager; @@ -62,16 +63,15 @@ public CpManagerCache( final EntityCollectionManagerFactory ecmf, final EntityIn @Override public EntityCollectionManager getEntityCollectionManager( CollectionScope scope ) { - //cache is now in the colletion manager level return ecmf.createCollectionManager( scope ); } @Override - public EntityIndex getEntityIndex( ApplicationScope appScope ) { + public ApplicationEntityIndex getEntityIndex( ApplicationScope appScope ) { - return eif.createEntityIndex( appScope ); + return eif.createApplicationEntityIndex( appScope ); } diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpRelationManager.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpRelationManager.java index 2eeee28252..df01c47b8b 100644 --- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpRelationManager.java +++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/CpRelationManager.java @@ -30,6 +30,7 @@ import java.util.UUID; import org.apache.usergrid.persistence.core.future.BetterFuture; +import org.apache.usergrid.persistence.index.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.Assert; @@ -75,10 +76,6 @@ import org.apache.usergrid.persistence.graph.impl.SimpleSearchByEdge; import org.apache.usergrid.persistence.graph.impl.SimpleSearchByEdgeType; import org.apache.usergrid.persistence.graph.impl.SimpleSearchEdgeType; -import org.apache.usergrid.persistence.index.EntityIndex; -import org.apache.usergrid.persistence.index.EntityIndexBatch; -import org.apache.usergrid.persistence.index.IndexScope; -import org.apache.usergrid.persistence.index.SearchTypes; import org.apache.usergrid.persistence.index.impl.IndexScopeImpl; import org.apache.usergrid.persistence.index.query.CandidateResult; import org.apache.usergrid.persistence.index.query.CandidateResults; @@ -397,7 +394,7 @@ public void updateContainingCollectionAndCollectionIndexes( // loop through all types of edge to target - final EntityIndex ei = managerCache.getEntityIndex( applicationScope ); + final ApplicationEntityIndex ei = managerCache.getEntityIndex(applicationScope); final EntityIndexBatch entityIndexBatch = ei.createBatch(); @@ -828,7 +825,7 @@ public void removeFromCollection( String collName, EntityRef itemRef ) throws Ex org.apache.usergrid.persistence.model.entity.Entity memberEntity = ((CpEntityManager)em).load( new CpEntityManager.EntityScope( memberScope, entityId)); - final EntityIndex ei = managerCache.getEntityIndex( applicationScope ); + final ApplicationEntityIndex ei = managerCache.getEntityIndex( applicationScope ); final EntityIndexBatch batch = ei.createBatch(); // remove item from collection index @@ -945,7 +942,7 @@ public Results searchCollection( String collName, Query query ) throws Exception cpHeadEntity.getId(), CpNamingUtils.getCollectionScopeNameFromCollectionName( collName ) ); - final EntityIndex ei = managerCache.getEntityIndex( applicationScope ); + final ApplicationEntityIndex ei = managerCache.getEntityIndex( applicationScope ); final SearchTypes types = SearchTypes.fromTypes( collection.getType() ); @@ -1060,7 +1057,7 @@ public ConnectionRef createConnection( String connectionType, EntityRef connecte GraphManager gm = managerCache.getGraphManager( applicationScope ); gm.writeEdge( edge ).toBlockingObservable().last(); - EntityIndex ei = managerCache.getEntityIndex( applicationScope ); + ApplicationEntityIndex ei = managerCache.getEntityIndex( applicationScope ); EntityIndexBatch batch = ei.createBatch(); // Index the new connection in app|source|type context @@ -1292,7 +1289,7 @@ public void deleteConnection( ConnectionRef connectionRef ) throws Exception { GraphManager gm = managerCache.getGraphManager( applicationScope ); gm.deleteEdge( edge ).toBlockingObservable().last(); - final EntityIndex ei = managerCache.getEntityIndex( applicationScope ); + final ApplicationEntityIndex ei = managerCache.getEntityIndex( applicationScope ); final EntityIndexBatch batch = ei.createBatch(); // Deindex the connection in app|source|type context @@ -1372,7 +1369,7 @@ public Results getConnectedEntities( final SearchTypes searchTypes = SearchTypes.fromNullableTypes( connectedEntityType ); - final EntityIndex ei = managerCache.getEntityIndex( applicationScope ); + final ApplicationEntityIndex ei = managerCache.getEntityIndex( applicationScope ); logger.debug("Searching connected entities from scope {}:{}", @@ -1465,7 +1462,7 @@ public Results searchConnectedEntities( Query query ) throws Exception { final SearchTypes searchTypes = SearchTypes.fromNullableTypes( query.getEntityType() ); - EntityIndex ei = managerCache.getEntityIndex( applicationScope ); + ApplicationEntityIndex ei = managerCache.getEntityIndex( applicationScope ); logger.debug( "Searching connections from the scope {}:{} with types {}", new Object[] { indexScope.getOwner().toString(), indexScope.getName(), searchTypes diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/events/EntityDeletedHandler.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/events/EntityDeletedHandler.java index 94ef5e44bb..85c59dac8a 100644 --- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/events/EntityDeletedHandler.java +++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/events/EntityDeletedHandler.java @@ -20,6 +20,7 @@ import java.util.UUID; +import org.apache.usergrid.persistence.index.ApplicationEntityIndex; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -70,7 +71,7 @@ public void deleted( CollectionScope scope, Id entityId, UUID version ) { } ); CpEntityManagerFactory cpemf = ( CpEntityManagerFactory ) emf; - final EntityIndex ei = cpemf.getManagerCache().getEntityIndex( scope ); + final ApplicationEntityIndex ei = cpemf.getManagerCache().getEntityIndex( scope ); final IndexScope indexScope = new IndexScopeImpl( new SimpleId( scope.getOwner().getUuid(), scope.getOwner().getType() ), diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/events/EntityVersionDeletedHandler.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/events/EntityVersionDeletedHandler.java index c45949b94e..ece7562109 100644 --- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/events/EntityVersionDeletedHandler.java +++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/events/EntityVersionDeletedHandler.java @@ -29,6 +29,7 @@ import org.apache.usergrid.persistence.collection.MvccLogEntry; import org.apache.usergrid.persistence.collection.event.EntityVersionDeleted; import org.apache.usergrid.persistence.collection.serialization.SerializationFig; +import org.apache.usergrid.persistence.index.ApplicationEntityIndex; import org.apache.usergrid.persistence.index.EntityIndex; import org.apache.usergrid.persistence.index.EntityIndexBatch; import org.apache.usergrid.persistence.index.IndexScope; @@ -85,7 +86,7 @@ public void versionDeleted( final CollectionScope scope, final Id entityId, CpEntityManagerFactory cpemf = (CpEntityManagerFactory)emf; - final EntityIndex ei = cpemf.getManagerCache().getEntityIndex( scope ); + final ApplicationEntityIndex ei = cpemf.getManagerCache().getEntityIndex( scope ); final IndexScope indexScope = new IndexScopeImpl( new SimpleId(scope.getOwner().getUuid(), scope.getOwner().getType()), diff --git a/stack/core/src/main/java/org/apache/usergrid/corepersistence/results/FilteringLoader.java b/stack/core/src/main/java/org/apache/usergrid/corepersistence/results/FilteringLoader.java index b571f21a2f..0a0f71afda 100644 --- a/stack/core/src/main/java/org/apache/usergrid/corepersistence/results/FilteringLoader.java +++ b/stack/core/src/main/java/org/apache/usergrid/corepersistence/results/FilteringLoader.java @@ -30,6 +30,7 @@ import javax.annotation.Nullable; +import org.apache.usergrid.persistence.index.ApplicationEntityIndex; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -82,7 +83,7 @@ protected FilteringLoader( this.applicationScope = applicationScope; this.indexScope = indexScope; - final EntityIndex index = managerCache.getEntityIndex( applicationScope ); + final ApplicationEntityIndex index = managerCache.getEntityIndex( applicationScope ); indexBatch = index.createBatch(); } diff --git a/stack/core/src/main/java/org/apache/usergrid/persistence/EntityManager.java b/stack/core/src/main/java/org/apache/usergrid/persistence/EntityManager.java index 2a951aad45..71dcd074fb 100644 --- a/stack/core/src/main/java/org/apache/usergrid/persistence/EntityManager.java +++ b/stack/core/src/main/java/org/apache/usergrid/persistence/EntityManager.java @@ -17,6 +17,7 @@ package org.apache.usergrid.persistence; +import org.apache.usergrid.persistence.index.EntityIndex; import org.apache.usergrid.persistence.index.query.Query; import java.nio.ByteBuffer; import java.util.Collection; @@ -694,12 +695,8 @@ Mutator batchUpdateProperties(Mutator batch, */ void createIndex(); - /** - * Create the index, should ONLY ever be called the first time an application is created - */ - ListenableActionFuture deleteIndex(); - public void init( EntityManagerFactory emf, UUID applicationId); + public void init( EntityManagerFactory emf, EntityIndex entityIndex, UUID applicationId); /** For testing purposes */ public void flushManagerCaches(); diff --git a/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/EntityManagerImpl.java b/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/EntityManagerImpl.java index 966a601d8e..803512b144 100644 --- a/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/EntityManagerImpl.java +++ b/stack/core/src/main/java/org/apache/usergrid/persistence/cassandra/EntityManagerImpl.java @@ -36,6 +36,7 @@ import javax.annotation.Resource; +import org.apache.usergrid.persistence.index.EntityIndex; import org.elasticsearch.action.ListenableActionFuture; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -214,7 +215,7 @@ public EntityManagerImpl() { @Override - public void init(EntityManagerFactory emf, UUID applicationId) { + public void init(EntityManagerFactory emf, EntityIndex entityIndex, UUID applicationId) { init( (EntityManagerFactoryImpl)emf, null, null, applicationId, false); } @@ -2895,12 +2896,6 @@ public void createIndex() { } - @Override - public ListenableActionFuture deleteIndex() { - //no op - return null; - } - @Override public EntityRef getGroupRoleRef( UUID ownerId, String roleName) throws Exception { diff --git a/stack/core/src/test/java/org/apache/usergrid/corepersistence/StaleIndexCleanupTest.java b/stack/core/src/test/java/org/apache/usergrid/corepersistence/StaleIndexCleanupTest.java index 1becee4b20..fdd230cfeb 100644 --- a/stack/core/src/test/java/org/apache/usergrid/corepersistence/StaleIndexCleanupTest.java +++ b/stack/core/src/test/java/org/apache/usergrid/corepersistence/StaleIndexCleanupTest.java @@ -24,6 +24,7 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; +import org.apache.usergrid.persistence.index.*; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -46,10 +47,6 @@ import org.apache.usergrid.persistence.collection.EntityCollectionManagerFactory; import org.apache.usergrid.persistence.core.scope.ApplicationScope; import org.apache.usergrid.persistence.core.scope.ApplicationScopeImpl; -import org.apache.usergrid.persistence.index.EntityIndex; -import org.apache.usergrid.persistence.index.EntityIndexFactory; -import org.apache.usergrid.persistence.index.IndexScope; -import org.apache.usergrid.persistence.index.SearchTypes; import org.apache.usergrid.persistence.index.impl.IndexScopeImpl; import org.apache.usergrid.persistence.index.query.CandidateResults; import org.apache.usergrid.persistence.index.query.Query; @@ -155,12 +152,12 @@ public void testUpdateVersionMaxFirst() throws Exception { UUID newVersion = getCpEntity( thing ).getVersion(); - assertEquals( 2, queryCollectionCp( "things", "thing", "select * order by ordinal desc" ).size() ); + assertEquals(2, queryCollectionCp("things", "thing", "select * order by ordinal desc").size()); //now run enable events and ensure we clean up - System.setProperty( EVENTS_DISABLED, "false" ); + System.setProperty(EVENTS_DISABLED, "false"); - final Results results = queryCollectionEm( "things", "select * order by ordinal desc" ); + final Results results = queryCollectionEm("things", "select * order by ordinal desc"); assertEquals( 1, results.size() ); assertEquals(1, results.getEntities().get( 0 ).getProperty( "ordinal" )); @@ -169,9 +166,9 @@ public void testUpdateVersionMaxFirst() throws Exception { //ensure it's actually gone final CandidateResults candidates = queryCollectionCp( "things", "thing", "select * order by ordinal desc" ); - assertEquals( 1, candidates.size() ); + assertEquals(1, candidates.size()); - assertEquals(newVersion, candidates.get( 0 ).getVersion()); + assertEquals(newVersion, candidates.get(0).getVersion()); } @@ -483,7 +480,7 @@ private CandidateResults queryCollectionCp( ApplicationScope as = new ApplicationScopeImpl( new SimpleId( em.getApplicationId(), TYPE_APPLICATION ) ); - EntityIndex ei = eif.createEntityIndex( as ); + ApplicationEntityIndex ei = eif.createApplicationEntityIndex(as); IndexScope is = new IndexScopeImpl( new SimpleId( em.getApplicationId(), TYPE_APPLICATION ), CpNamingUtils.getCollectionScopeNameFromCollectionName( collName ) ); diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/IndexIdentifier.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/IndexIdentifier.java index 1ac9d49dd7..c659ed62f9 100644 --- a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/IndexIdentifier.java +++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/IndexIdentifier.java @@ -20,12 +20,15 @@ package org.apache.usergrid.persistence.index; +import com.google.inject.Inject; + /** * Class is used to generate an index name and alias name */ public class IndexIdentifier{ private final IndexFig config; + @Inject public IndexIdentifier(IndexFig config) { this.config = config; } diff --git a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EsEntityIndexImpl.java b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EsEntityIndexImpl.java index d510a8d0be..e92e133045 100644 --- a/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EsEntityIndexImpl.java +++ b/stack/corepersistence/queryindex/src/main/java/org/apache/usergrid/persistence/index/impl/EsEntityIndexImpl.java @@ -55,6 +55,8 @@ import org.elasticsearch.indices.IndexAlreadyExistsException; import org.elasticsearch.indices.IndexMissingException; +import org.elasticsearch.indices.InvalidAliasNameException; +import org.elasticsearch.rest.action.admin.indices.alias.delete.AliasesMissingException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -119,9 +121,6 @@ public EsEntityIndexImpl( final IndexFig config, this.esProvider = provider; this.config = config; - - - this.alias = indexIdentifier.getAlias(); this.aliasCache = indexCache; this.addTimer = metricsFactory @@ -143,14 +142,14 @@ public void initializeIndex() { if(indexes == null || indexes.length==0) { addIndex(null, numberOfShards, numberOfReplicas, config.getWriteConsistencyLevel()); } + } @Override public void addIndex(final String indexSuffix,final int numberOfShards, final int numberOfReplicas, final String writeConsistency) { - String normalizedSuffix = StringUtils.isNotEmpty(indexSuffix) ? indexSuffix : null; try { //get index name with suffix attached - String indexName = indexIdentifier.getIndex(normalizedSuffix); + String indexName = indexIdentifier.getIndex(indexSuffix); //Create index try { @@ -186,12 +185,10 @@ public void addIndex(final String indexSuffix,final int numberOfShards, final in //We do NOT want to create an alias if the index already exists, we'll overwrite the indexes that //may have been set via other administrative endpoint - addAlias(normalizedSuffix); + addAlias(indexSuffix); testNewIndex(); - - } catch (IndexAlreadyExistsException expected) { // this is expected to happen if index already exists, it's a no-op and swallow } catch (IOException e) { @@ -202,51 +199,47 @@ public void addIndex(final String indexSuffix,final int numberOfShards, final in @Override public void addAlias(final String indexSuffix) { - - final Timer.Context timeRemoveAlias = updateAliasTimer.time(); + Timer.Context timer = updateAliasTimer.time(); try { - - + Boolean isAck; String indexName = indexIdentifier.getIndex(indexSuffix); final AdminClient adminClient = esProvider.getClient().admin(); - String[] indexNames = getIndexesFromEs(AliasType.Write); - - - final IndicesAliasesRequestBuilder aliasesRequestBuilder = adminClient.indices().prepareAliases(); + String[] indexNames = getIndexes(AliasType.Write); - //remove the write alias from it's target + int count = 0; + IndicesAliasesRequestBuilder aliasesRequestBuilder = adminClient.indices().prepareAliases(); for ( String currentIndex : indexNames ) { aliasesRequestBuilder.removeAlias( currentIndex, alias.getWriteAlias() ); - logger.info("Removing existing write Alias Name [{}] from Index [{}]", alias.getWriteAlias(), currentIndex); + count++; } + if(count>0) { + isAck = aliasesRequestBuilder.execute().actionGet().isAcknowledged(); + logger.info("Removed Index Name from Alias=[{}] ACK=[{}]", alias, isAck); + } + aliasesRequestBuilder = adminClient.indices().prepareAliases(); //Added For Graphite Metrics - // add read alias - aliasesRequestBuilder.addAlias( indexName, alias.getReadAlias()); - logger.info("Created new read Alias Name [{}] on Index [{}]", alias.getReadAlias(), indexName); - + aliasesRequestBuilder.addAlias( + indexName, alias.getReadAlias()); + //Added For Graphite Metrics //add write alias - aliasesRequestBuilder.addAlias( indexName, alias.getWriteAlias() ); - - logger.info("Created new write Alias Name [{}] on Index [{}]", alias.getWriteAlias(), indexName); + aliasesRequestBuilder.addAlias( + indexName, alias.getWriteAlias()); - final IndicesAliasesResponse result = aliasesRequestBuilder.execute().actionGet(); + isAck = aliasesRequestBuilder.execute().actionGet().isAcknowledged(); - final boolean isAcknowledged = result.isAcknowledged(); + logger.info("Created new aliases ACK=[{}]", isAck); - if(!isAcknowledged){ - throw new RuntimeException( "Unable to add aliases to the new index. Elasticsearch did not acknowledge to the alias change for index '" + indexSuffix + "'"); - } + aliasCache.invalidate(alias); + } catch (Exception e) { + logger.warn("Failed to create alias ", e); } - finally{ - //invalidate the alias - aliasCache.invalidate(alias); - //stop the timer - timeRemoveAlias.stop(); + finally { + timer.stop(); } } @@ -255,7 +248,6 @@ public String[] getIndexes(final AliasType aliasType) { return aliasCache.getIndexes(alias, aliasType); } - /** * Get our index info from ES, but clear our cache first * @param aliasType @@ -266,8 +258,6 @@ public String[] getIndexesFromEs(final AliasType aliasType){ return getIndexes( aliasType ); } - - /** * Tests writing a document to a new index to ensure it's working correctly. See this post: * http://s.apache.org/index-missing-exception diff --git a/stack/corepersistence/queryindex/src/test/java/org/apache/usergrid/persistence/index/impl/EntityIndexTest.java b/stack/corepersistence/queryindex/src/test/java/org/apache/usergrid/persistence/index/impl/EntityIndexTest.java index 483efb9918..28c548d872 100644 --- a/stack/corepersistence/queryindex/src/test/java/org/apache/usergrid/persistence/index/impl/EntityIndexTest.java +++ b/stack/corepersistence/queryindex/src/test/java/org/apache/usergrid/persistence/index/impl/EntityIndexTest.java @@ -81,22 +81,21 @@ public class EntityIndexTest extends BaseIT { @Test public void testIndex() throws IOException, InterruptedException { - Id appId = new SimpleId( "application" ); + Id appId = new SimpleId("application"); + ei.initializeIndex(); - ApplicationScope applicationScope = new ApplicationScopeImpl( appId ); + ApplicationScope applicationScope = new ApplicationScopeImpl(appId); ApplicationEntityIndex entityIndex = eif.createApplicationEntityIndex(applicationScope); - final String entityType = "thing"; - IndexScope indexScope = new IndexScopeImpl( appId, "things" ); - final SearchTypes searchTypes = SearchTypes.fromTypes( entityType ); + IndexScope indexScope = new IndexScopeImpl(appId, "things"); + final SearchTypes searchTypes = SearchTypes.fromTypes(entityType); - insertJsonBlob(entityIndex, entityType, indexScope, "/sample-large.json",101,0); - - ei.refresh(); + insertJsonBlob(entityIndex, entityType, indexScope, "/sample-large.json", 101, 0); + ei.refresh(); - testQueries( indexScope, searchTypes, entityIndex ); + testQueries(indexScope, searchTypes, entityIndex); } @Test